Linux all operations are done with users. In Linux environment to create a file, start a service, open a network port we need a user with required privileges. Linux users are stored in plain test database at /etc/passwd . As old times user passwords also stored in this file but on modern time for security reasons user passwords are hold in /etc/shadow . There is also user groups to manage permission better. User groups are stored in plain text database /etc/group . In this tutorial we will look how to add new user to the Linux.
Getting Help About useradd Command
useradd command is very useful command for system administrators. Knowing its parameters and options are very crucial for daily operations. To get quick help about useradd command issue following command.
$ useradd -h
List Current Users in A Linux System
Before adding a Linux user we will list existing users to provide non colliding username.
$ cat /etc/passwd | cut -d : -f 1
Create New User
We will create new user with name test. This user will be added to the /etc/passwd file with the default shell and other related parameters.
$ useradd test
We can list newly created user with the following command.
$ cat /etc/passwd | cut -d : -f 1 | grep test
Create A User With Different Home Directory
By default useradd command will create a home directory in /home path with the same name as username. For example if the username is test the home directory of the user will be /home/test
$ useradd -d /home/mytest test5
This command will create test user with a home directory like /home/mytest
Create A User With Specific User ID
Linux operating system gives users User ID in order to separate them. User ID’s starts from 500 and increments upon new user addition. But we can change this default behavior by setting specific UID without getting default one.
-u will be provided to the useradd command to specify new users UID.
$ useradd -u 567 nick
After user creation we check new users UID with grep command by providing user name. As we can see user nick is created with UID
Create A User With Specific Group ID
While creating user in Linux operating system new group is created too. This group is dedicated to the new user and the new user is automatically assigned to this user group. The new user group ID is assigned by default and starts from 1000. This value can be changed with
$ useradd -g 0 nick
As we can see newly created user group assigned to the 0 which is root group.
Add A User To Multiple Groups
In previous example we have added new user with the specified group ID. We have just provided only single group ID. We may also need to add user to the multiple groups by providing multiple group ID’s in a single command. This can be done with
-G options like below.
$ useradd -G root,vboxusers,kvm nick
While creating new user
nick we added nick to the groups named
root,vboxusers,kvm with this example.
Add A User Without Home Directory
Linux users are great way to login and use Linux systems. Being a Linux system user means having a home directory. But this is not always true. Should it be? Should all Linux users have a home directory? No. For example services users those used for Linux daemons and services do not needs home directories. Also there may be some situations we do not need home directories. Another example is using Linux users just for authentication and providing VPN service. With
-M options new user will be created without a home directory.
$ useradd -M nick
Create A User With Account Expire Date
In a busy system creating a lot of user without any restriction and future notification will create security problems. Some added users will be permanent for the Linux system but some others will be temporary and should be removed or at least locked after some time. While adding new users we can set account expire date for the new user. Just issue
-e option to the useradd command like below. In this example we set 28 February 2017 as account expire time for user nick. The date format is
$ useradd -e 2017-02-28 nick
After adding user with a account expire date we can check this date with
chage command like below.
$ chage -l nick
Create A User With Password Expire Date
Another way to secure newly created user is specifying password expire date. Specifying password expire date will force the user to change his/her password at the specified date. This will make the user credentials more secure by changing the passwords regularly. To set password expire date use
-f option with number of days . If we provide value
-1 the password will never expire. We need to provide also a start day for this operation
$ useradd -e 2017-02-28 -f 60 nick
Add User With Custom Comments
While adding user we may need to add some extra informal information. This informal information can be the full name, phone number etc. The comment can be added with
-c option like below. We will add the phone number of the new user named nick in this example.
$ useradd -c "1212121" nick
Change User Login Shell
Linux users use shell to issue commands from command line interface. There are alternative command line interfaces. Default and most popular command line interface or shell is
bash . The newly create user will have
bash as default shell. The default shell can be changed while creating user with
-s option like below. In this example we will set
sh as default shell.
$ useradd -s /bin/sh nick
Disable Login Shell
In previous example we have set shell preferences for the newly created user. There is also an option where user will have no shell. This will prevent user to login to the Linux system. This can be used also a security step for the system. We will set the created users default shell
/bin/nologin which is not a shell.
$ useradd -s /bin/nologin nick
How To Add New User Account To Linux Infografic