How to add a new user Ubuntu, Red Hat, Linux
I know that you can read the man page by typing
However I like jotting down a few notes via this blog to ensure that I am not trying to unravel the cryptic official documentation when I need to add another user in the future.
-b or --base-dir
This is specifying the home directory of the SYSTEM eg “/home” not the USER eg “/home/theNewUser”
When only specifying -b or –base-dir
useradd theNewUser -b "/home"
The system will not create a home directory for the user such as “/home/theNewUser” if we want the system to create a home directory we need to add the -m argument at the end of the useradd command like this
useradd theNewUser -b "/home" -m
This will take the “/home” we provided and append “theNewUser” to it to create “/home/theNewUser” behind the scenes.
-d or –home
This is specifying the complete home directory of the USER eg “/home/theNewUser” not the SYSTEM home eg “/home”
If you give a path that is bogus like this
useradd theNewUser -d "blahblah/donkey" -m
You get an error like this
useradd: invalid home directory
When specifying -d or –home, the directory for the not yet created user must exist, If the home directory for the not yet created user does not exist it will NOT be created.
If the directory does not yet exist and we want the system to create a home directory behind the scenes we need to add the -m argument at the end of the useradd command like this
useradd theNewUser -d "/home/theNewUser" -m
I set the password after creating the new user using the passwd command like this
sudo passwd theNewUser
I believe that the -p argument is not a good idea given what I understand from the man page
-p, –password PASSWORD
The encrypted password, as returned by crypt(3). The default is to disable the password.
Note: This option is not recommended because the password (or encrypted password) will be visible by users listing the processes.
-s or –shell like this
sudo useradd aaaa -s "/bin/bash" -d "/home/aaaa" -m
sudo useradd aagg -g aaag -s "/bin/bash" -d "/home/aagg" -m
Note when using lowercase g (-g) you are setting the primary group of the user, whatever you add after lowecase g will overwrite the one and only primary group assigned to that user. when using the upper case G, you are able to add additional groups to which the user can belong. This is handy when doing usermod -G secondGroup theNewUser