Ubuntu – How to add user without useradd command

passwd-filepasswordusers

Is it possible to add a user in ubuntu linux without useradd command I think its possible by adding the entries in /etc/passwd and /etc/shadow but i don't know the exact steps to do it and the user should get its home directory and bash shell too.

Best Answer

It's not recommended to manually modify /etc/passwd, /etc/shadow, /etc/group or /etc/gshadow because the risk of breakage. If you're looking for an alternative command that is easier to use, take a look at adduser(8). All you have to run is:

sudo adduser user

The shell will be /bin/bash by default per /etc/adduser.conf.

Usually, if you wish to add a user with the bash shell, thereby create a home directory /home/user and a user group, you would use:

sudo useradd --create-home --shell /bin/bash --user-group user

This command is basically determining a free User ID $UID and Group ID $GID and then executing the next commands:

echo "user:x:$UID:$GID::/home/user:/bin/bash" | sudo tee -a /etc/passwd
echo "user:x:$GID:" | sudo tee -a /etc/group
echo "user:!:$DATE_OF_LAST_PASS_CHANGE:0:99999:7:::" | sudo tee -a /etc/shadow
echo "user:!::" | sudo tee -a /etc/gshadow

..and thereby possibly making a backup of the files.

The next manual pages about the file formats may be of interest to you: