adduser command is used to create new users in Linux. This article describes the usage of Linux adduser command with the help of examples.
Add a New User in Linux
# adduser -c "Kam Aggarwal, email@example.com" kaggarwal
Executing this command shall create a user ‘Kam Aggarwal’ with username ‘kaggarwal’.
-c option is used to provide the user information like user’s full name, contact information etc.
Using ‘-c’ option is optional.
Set Password for the User
# passwd kaggarwal Changing password for user kaggarwal. New UNIX password: Retype new UNIX password: passwd: all authentication tokens updated successfully.
Whenever a new user is created in Linux. An entry is added/modified to each of the following configuration files.
Here are the entries added to these configuration files for newly created user kaggarwal.
# cat /etc/passwd | grep kaggarwal kaggarwal:x:503:503:Kam Aggarwal, firstname.lastname@example.org:/home/kaggarwal:/bin/bash # cat /etc/shadow | grep kaggarwal kaggarwal:$1$5pGn7NRX$dA7DMZZWALjqhg1eGxODn.:15596:0:99999:7::: # cat /etc/group | grep kaggarwal kaggarwal:x:503:
Set User’s Home Directory
By default, Linux creates the user’s home directory as /home/<username>.
# ls -ld /home/kaggarwal drwx------ 3 kaggarwal kaggarwal 4096 Sep 13 12:28 /home/kaggarwal
This is possible to specify a different location for user’s home directory using ‘-d’ option.
# adduser -d /opt/newuser1 newuser1
In this example, we have configured the user’s home directory as /opt/newuser1
# cat /etc/passwd | grep newuser1 newuser1:x:505:505::/opt/newuser1:/bin/bash # ls -ld /opt/newuser1/ drwx------ 3 newuser1 newuser1 4096 Sep 13 13:09 /opt/newuser1/
Set User’s Primary Group
Whenever a user is created, Linux automatically creates a primary group for the user. Group name is usually same as the user name.
For example, in our case creating a user ‘newuser1′ automatically created a new group named ‘newuser1′. The group ‘newuser1′ is made the primary group for the user ‘newuser1′.
You can also specify the user’s primary group during the user creation by using ‘-g’ option. The group specified must be available in the /etc/group file.
# adduser newuser2 -g kaggarwal
In this example, we are creating a user ‘newuser2′ with ‘kaggarwal’ as its primary default group.
# groups newuser2 newuser2 : kaggarwal # ls -ld /home/newuser2/ drwx------ 3 newuser2 kaggarwal 4096 Sep 13 13:22 /home/newuser2/
Note: ‘groups’ command is used to print the user groups.
Specify Additional Groups for the User
A user can also be made member of other groups by using ‘-G’ option.
# adduser newuser3 -g kaggarwal -G newuser1,wheel,root
In this example, we are adding a new user ‘newuser3′. User’s default group is configured as ‘kaggarwal’ and the user is also made the member of groups, newuser1, wheel and root.
# groups newuser3 newuser3 : kaggarwal root wheel newuser1
Set User Expiry Date For the User
‘-e’ command can used to set the expiry for the user. By default an account never expires.
The expiry date can be passed in ‘YYYYMMDD’ format.
# adduser testuser3 -e 20121115
In this example a new user ‘testuser3′ is created and the user expiry date is configured as 15 Nov 2012.
In Linux, chage command can be used to get the user expiry information.
# chage -l testuser3 Last password change : Sep 13, 2012 Password expires : never Password inactive : never Account expires : Nov 15, 2012 Minimum number of days between password change : 0 Maximum number of days between password change : 99999 Number of days of warning before password expires : 7
Configure Account Usable Days After Password Expiry
-f option can be used to set the number of days after which the user account will be disabled after password expiry.
Value ’0′ indicates that the user account will be immediately disabled on password expiry.
Value ‘-1′ indicates that the user account will never be disabled at all.
# adduser testuser4 -e 20121101 -f 20
In this example, the password of user is configured to expire on ’01 Nov 2012′. However, on password expiry, the user shall still be able to use the account till ’20 Nov 2012′ after which the userr’s account shall be disabled.
Create a Homeless User
If you want to create a user but not it’s home directory, then this can be achieved by using ‘-M’ option.
# adduser -M testuser5
In this example, testuser5 is created but without any home directory.
# ls -l /home/testuser5 ls: /home/testuser5: No such file or directory
Configure Default User’s Login Shell
When a user is created, a default shell is configured for the user. The configured shell is the first program that executes whenever the user logs in.
# cat /etc/passwd | grep kaggarwal kaggarwal:x:503:503:Kam Aggarwal, email@example.com:/home/kaggarwal:/bin/bash
By default ‘/bin/bash’ shell is configured for new users. However, you can use ‘-s’ option to configure any other shell.
The list of all available shells are available in /etc/shells file.
# cat /etc/shells /bin/sh /bin/bash /sbin/nologin /bin/tcsh /bin/csh /bin/ksh /bin/zsh
Let’s create a new user with login shell ‘/bin/ksh’
# adduser tester7 -s /bin/ksh cat /etc/passwd | grep tester7 tester7:x:513:513::/home/tester7:/bin/ksh
User’s default login shell is configured as /bin/ksh.
Create User With Custom UID
Whenever a new user is created, Linux automatically assign the next free user id to the new user. However, this is also possible to force Linux to assign a specific UID to the new user using ‘-u’ option.
# adduser testuser8 -u 665 # cat /etc/passwd | grep testuser8 testuser8:x:665:665::/home/testuser8:/bin/bashz
Create Only User Not Group
As you have already seen earlier, whenever a new user is created, Linux automatically creates a new user group for the user with the same name as the new user’s login. This behaviour can be disabled by using ‘-n’ option while creating the user.
In such case the user’s primary group shall be configured as ‘users’.
# groups testuser9 testuser9 : users # ls -ld /home/testuser9 drwx------ 3 testuser9 users 4096 Sep 13 22:08 /home/testuser9