How to create user without useradd command in OpenWRT

There is no useradd command in OpenWRT. Instead, shadow user package is available but I found it overkill. Anyway, if you are interested in using it – check out this tutorial,
Follow these steps to create a user without using useradd command in OpenWRT.

Step 1

Traditionally, the /etc/passwd file is used to keep track of every registered user that has  access to a system.
Each line is an entry for one user, and fields on each line are separated by a colon.  The fields are:

  • User name
  • Encrypted password
  • User ID number (UID)
  • User’s group ID number (GID)
  • Full name of the user (GECOS)
  • User home directory
  • Login shell

To add a new user by hand, add a new line at the end of the file, filling in the appropriate information. The information you add needs to meet some requirements, or your new user may have problems logging in. First, make sure that both the user name and user ID is unique. Assign the user a group, either 100 (the “users” group in OpenWRT) or your default group (use its number, not its name). Give the user a valid home directory (which you’ll create later) and shell (set shell to /bin/false if you want the user to be able to use FTP but not SSH).

So, open the file for editing and add an entry of user details in /etc/passwd file.

$ vi /etc/passwd

Example entry:

testuser:x:501:501:test user:/home/user:/bin/ash

Step 2

Assign a password to the user you just created.

$ passwd testuser

 

passwd will prompt you for a password. You will see output like this

Changing password for user testuser.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

Step 3

All normal users are members of the “users” group on a typical Linux system. However, if you want to create a new group, or add the new user to additional groups, you’ll need to modify the /etc/group file. Here is a typical entry:

cvs::102:chris,logan,david,root

The fields are group name, group password, group ID, and group members, separated by commas. Creating a new group is a simple matter of adding a new line with a unique group ID, and listing all the users you want to be in the group. Any users that are in this new group and are logged in will have to log out and log back in for those changes to take effect.

Step 4

Use mkdir to create the new user’s home directory in the location you entered into the /etc/passwd file, and use chown to change the owner of the new directory to the new user.

$ mkdir /home/testuser
$ chown testuser /home/testuser

NB: Removing a user is a simple matter of deleting all of the entries that exist for that user. Remove the user’s entry from /etc/passwd and remove the login name from any groups in the /etc/group file. If you wish, delete the user’s home directory, too.

This tutorial is based on Slackware Linux Essentials book.

Leave a Reply

Your email address will not be published. Required fields are marked *