Creating users

Creating users with useradd

The command to add a new user to your Linux system is "useradd". In its simplest form you issue the useradd command followed by the name of the user your are creating: useradd user. This will create a user with the default settings as specified in your "/etc/default/useradd" file. Default files are also copied from your "/etc/skel" for your shell.

useradd command options

useradd options username

-m : Creates the home directory and copies default files form your "/etc/skel" area.
-M : Do not create the users home directory!
-c : User Info (also known commonly as GECOS)
-d : Specifies an alternative home area. If not passed, the assumption is to create "/home/user"
-e : Expiry Date specified in the format of YYYY-MM-DD
-f : The number of days after a password expires until the account is permanently disabled. A value of 0 disables the account as soon as the password has expired, and a value of -1 disables the feature.
-g : The users primary group (GID). This group must exist first, however, if you have enabled the variable "USERGROUPS_ENAB" in your "/etc/login.defs" this file group will be created automatically.
-G : Subsequent groups can be added in a comma separated list (No white space) between items.
-k : Allows you to specify a different directory instead of "/etc/skel". You could have different directories for different types of user.
-N : Do not create a group with the same name as that of the user.
-o : Allow the creation of a user with a duplicate UID (non unique)
-r : Create a system account with no ageing.
-s : The name of the users login shell. If left blank, default is set from "/etc/default/useradd"
-u : Specify a (UID), must be unique unless "-o" option is specified. If omitted, then system will select next available UID.
-U : Create a group with the same name as the user and add the user to this group.

Create a new user account

useradd -m -c "Test User Account" -s /bin/bash testuser

In the above example, we created a user and specified that their default shell should be "/bin/bash". It is always a good idea to set a password for any new accounts that you create. This can be achieved with the passwd command. Here we can see that a UID of 1001 and a GID of 1001 has been automatically assigned. This information can be seen by issuing either the "groups" command or the "id" command. You can also view the "/etc/group" file to see your new group.

[root@fedsrv01a ~]# useradd -m -c "Test User Account" -s /bin/bash testuser

[root@fedsrv01a ~]# grep testuser /etc/passwd
testuser:x:1001:1001:Test User Account:/home/testuser:/bin/bash

[root@fedsrv01a ~]# passwd testuser
Changing password for user testuser.
New password: 
Retype new password:
passwd: all authentication tokens updated successfully.

Create a new account and specify a UID

useradd -m -c "Test User Account TWO" -u 1005 testuser2

[root@fedsrv01a ~]# useradd -m -c "Test User Account TWO" -u 1005 testuser2

[root@fedsrv01a ~]# id testuser2
uid=1005(testuser2) gid=1005(testuser2) groups=1005(testuser2)

In the above example we specified the "-u" parameter. This enabled us to specify the UID to use with the new account. If you do not specify a UID, the system will automatically select the next available UID for you. A UID is a unique numerical identifier associated with an individual account. The numerical range is taken from the values that are set in a file called "/etc/login.defs". Normally accounts with a UID above 1000 are none system accounts.

Create a new account and specify group membership

useradd -m -c "Test User Three" -g testers -G staff,support testuser3

useradd -m -c "Test User Three" -g testers -G staff,support testuser3

[root@fedsrv01a ~]# id testuser3
uid=1006(testuser3) gid=1008(testers) groups=1008(testers),1006(staff),1007(support)

In the above example we specified the primary group of the new user to be "testers" by using the "-g" parameter. We then specified membership to additional groups by specifying the "-G parameter. By issuing the "id" command, we can see that the new account has a primary group of "testers" and belong also to the groups "staff and support".

Create a new user and specify home directory

useradd -m -c "Test User Four" -d /testing testuser4

[root@fedsrv01a ~]# useradd -m -c "Test User 4" -d /testing testuser4
[root@fedsrv01a /]# ls -ld /testing/ drwx------. 2 testuser4 testuser4 4096 May 16 18:32 /testing/

Whenever you create a new account on a Linux system, you can have the option of specifying whether to create the home directory and its location. If you do not specify a home directory location, then by default the system will create a directory under the "/home" area. In the example below, we are going to create a new user and specify the "home" directory location. To specify the location for the home directory we can use the parameter "-d".

Create a new user and specify a different shell

useradd -m -c "Test User Five" -s /bin/ksh testuser5

By default most Linux systems will automatically assign the "Bash" shell to any user accounts that are created. If you want to use a different shell such as the "Korn Shell", then this can be specified if it is available on your system. To specify a different shell you need to pass the parameter "-s" followed by the "shell" of your choice.

To verify what shells are available on your system you can view the contents of a file called "/etc/shells". The available shells will depend on which version of Linux you are running and what shells have been installed.

[root@fedsrv01a ~]# cat /etc/shells


Once you have verified what shells are available to your system, you can now issue you command:

[root@fedsrv01a ~]# useradd -m -c "Test User Five" -s /bin/ksh testuser5

[root@fedsrv01a ~]# grep testuser5 /etc/passwd
testuser5:x:1008:1010:Test User Five:/home/testuser5:/bin/ksh

In the above example, we chose to give the user the "Korn Shell". You can verify that the new shell has been added by using the "grep" command as above.