User Management in Linux is pretty simple and easy to remember. There are only two commands through which you can add new users.
‘useradd’ and ‘adduser’
Both these command do the same task. That is adding a new user. But there’s a little difference between them.
‘adduser’ is the new command introduced by RedHat and Debian Operating systems to make ‘useradd’ more friendly.
We will get into this later. Let us first understand how to add a new user and change them according to your convenience.
Here in all the examples below we will be using ‘useradd’. You can try ‘adduser’, it won’t make much of a difference.
So to add user run
useradd -m Ram
The ‘-m’ switchis used to specify the kernel to make its home directory.
- useradd -M Ram
Here ‘-M’ switch will tell the kernel to create a user BUT NOT ITS HOME DIRECTORY.
useradd -d /home/Ram_home Ram
The ‘-d’ switch is used to manually give location of the home folder to the new user.
The kernel won’t create its own Home directory inside ‘/home‘ filesystem. Instead it will make ‘/home/Ram_home‘ folder as its home directory. in this case, no folder with the name ‘/home/Ram‘ will be created.
useradd -G test user1
If you don’t want to use the ‘usermod
‘ command, then you can use ‘-G’.
This switchcan be used to directly add a new user into an existing group.
useradd -c 'This is test user' user1
If you want to tell something to someone, you can use the ‘-c’ switch. It stands for comment.
You can add more info about the user like which group does he belongs too? or why are we creating him?
useradd -e 2013/12/31 Ram
This sets an end date for the new user. The new user (Ram in this case) will be deleted on 31/12/2013.
The format to specify the Date isYYYY/MM/DD
useradd -s /sbin/nologin
If we use ‘-s /sbin/nologin’ with the useradd command, the Linux kernell will not give shell access to that new user.
It tells the kernel user which is to be created should not access shell.
You can see in the below image, any normal user can useshell but not the newly created user.
These were the basic useradd commands with switches. But what if you want to change a group of user after its creation?
We saw how to create a new user and add him to a current group. So to change a user’s group we user ‘usermod’ command.
usermod -G ABCD xyz
i.e
usermod -G
[ Group_name ] [ User_name ]
Difference between ‘useradd
‘ and ‘adduser
‘
‘Useradd
‘ is a native binary command which is used with the system. It carries out all the hard work by itself.
‘Adduser
‘ interacts with user in Debian Operating Systems. When you try to add a new user using ‘adduser’ command it will ask you many questions.
When you run ‘adduser’ in Debian based OS, it will show all the process like :
Adding user ‘xyz’…
Adding new group ‘xyz’ (502).
Adding new user ‘xyz’ (502) with group ‘xyz’.
Creating home directory ‘/home/xyz’.
Copying files from ‘/etc/skel’
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for xyz
Enter the new value, or press ENTER for the default
Full Name []: XYZ
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [y/N] y
In RedHat and other RedHat based operating systems, ‘adduser’ command is linked with ‘useradd’.
That is ‘adduser’ works as symbolic link pointing to ‘useradd’.