The Linux system is an application where multiple people can simultaneously link into the same system.
As the system administrator, it falls to you to manage the system’s groups and users.
This article discusses how to create new user accounts with the useradd command.
Useradd Command -Functions
This is used to create user accounts in Linux. It also has the ability to add multiple users to the same system.
When running the useradd command, it does the following tasks:
- It makes and overruns a home directory for the new user.
- It edits etc/shadow, etc/passwd, etc/group and etc/shadow files for the new user account.
- It places consent and rights to the home directory.
This is the basic syntax of the command:
useradd [options] username
The only user who can create a new user account or use the useradd command is a root user who has sudo privileges.
Useradd makes a new user account based on the choices mentioned on the command line. Generic values are set in the etc default/useradd file.
Furthermore, useradd surveys everything on the /etc/login.defs file. It features the configuration of the shadow password suite. This includes the following:
- Ranges of user IDs when creating a user and system
- Password expiration policy
Linux: How to Create a New User
To add or create a new user on Linux, all you have to do is type the useradd command along with the user’s name.
For example, in order to create a user called username, this is what you will need to run:
sudo useradd username
Username is a default login username that is used by users to login to Linux’s system.
This command is an addition to files such as etc/shadow, etc/passwd, etc/group and etc/shadow.
For you to login to your newly created user, the user password needs to be set. To do this, run the passwd command along with the username.
sudo passwd username
After applying this command, you will be tasked to input and confirm your password.
Output Changing Password for user username. New Password Retype new password passwd: all authentication token updated successfully.
Once you have done all this, a new user is created.
Linux: How to Add a New User
If you’re a new user, it’s important to have a distinct username that no one else uses.
Let’s use ‘rdj’ as an example. To add this new user on the system, the following command must be applied:
[root@rdj ~]# useradd rdj
After inputting your distinct username with the useradd command, it will be preset in ‘locked state’.
You need to apply the passwd command to set your account’s password. By doing this, your user account will be unlocked.
[root@jdr ~]# passwd rdj Changing password for user rdj. New UNIX password: Retype new UNIX password: passwd: all authentication tokens updated successfully.
After you create a user account, this username is automatically included in the ‘/etc/passwd’ file. The file’s function is to collect information about each user. This is how it should be inputted:
Linux: Add Users to Group and Make Home Directory
To add a home directory, select the -m (–create-home) option. This command makes a home directory under ‘/home/name of username’.
In this case, the default home directory for ‘rdj’ is ‘/home/rdj‘.
This command sets up the user’s home directory. This also duplicates files from the /etc/skel directory to the user’s personal home directory.
You will see the initialization files if you register the files in the /home/rdj directory:
ls -la /home/rdj/
Output drwxr-xr-x-x 2 rdj rdj 4132 Nov 28 10:43 drwxr-xr-x-x 4 root root 4132 Nov 28 10:43 -rw-r--r-- 1 rdj rdj 445 Aug 30 2020 .bash_logout -rw-r--r-- 1 rdj rdj 3887 Aug 30 2020 .bashrc -rw-r--r-- 1 rdj rdj 623 Aug 30 2020 .profile
Thanks to the home directory, users can write, delete and edit files and separate directories.
To change the user’s home directory to another location, use the d (–home) option
sudo useradd -m -d /opt/rdj rdj
Linux: How to Create a User with Separate User ID
UIDs (User Identifiers) along with other access control policies determine the different actions a user can do on system resources.
Type useradd with the -u (–uid) option.
This will help in creating a user that has a specific UID. For example, let’s give rdj a UID of 824. To do this, type this command:
sudo useradd -u 824 rdj
With the help of the ID command, you can verify the value of your user’s UID.
id -u rdj Output 824
Linux: Add User To Specific Group ID
The -g (–gid) option lets you add a new user account with a specific starting login group. Either the group ID or name must be present.
Here is an example of how to create the rdj username and set the group to “users”:
sudo useradd -g users rdj
To verify the user GID, apply the id command
id -gn username Output users
Linux: Add User Group and Assign Different Groups
The -G (–groups) option lets you identify supplementary groups.
For example, you can create the user account ‘rdj’. You can also add a primary group ‘control’ as well as secondary groups ‘bike’ and ‘full name’
sudo useradd -g control-G bike,developers rdj
To verify, type: id username
Output uid=1123 (rdj) gid 100 = (control) groups=100(full name), 15(bike), 988(full name)
Linux: How to Create a User with Specific Login Shell
The -s (–shell) option enables the user to state its login shell.
To create a username ‘rdj’ with the login shell type /usr/bin/zsh, and input the following:
sudo useradd -s /usr/bin/zsh rdj
To verify your login shell, refer to the /etc/passwd file
grep rdj /etc/passwd Output rdj:x :2002:2002::/home/rdj/usr/bin/zsh
Linux: How to Create a User with Custom Comment
The -c (–comment) option enables you to give a brief description of your user account.
In this example, we’ll use the text string “Test Account” as the comment.
sudo useradd -c "Test Account" rdj
This comment will be saved in the /etc/passwd file:
grep rdj /etc/passwd
Output rdj:x:1043:1042:Test Account:/home/rdj:/bin/sh
Linux: How to Create a User with Specific Expiry Date
You can identify each user account’s expiry date. To do this, apply the -e (–expiredate) option. This will come in handy for temporary accounts.
The date must be displayed in the YYYY-MM-DD format.
To set the expiry date to September 1, 2020, this is what you should run.
sudo useradd -e 2020-09-01 rdj
The chage command must be used to verify the user account’s date of expiration:
sudo chage -l rdj
This is what your output value should look like after applying these commands:
Last password change : December 23, 2019 Password expires : never Password inactive : never Account expires : September 5, 2020 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
Linux: How to Create a System User
The -r(–system) option is there for you to create a system user account.
To add a new system user named ‘rdj’, you should input this command: sudo useradd -r username
These are created without an expiration date. Their UIDs are randomly selected from a variety of system user IDs in the login.defs file.
Linux: How to Change Default Useradd Values
The -D, –defaults option can manipulate or view the default useradd values. To do this, type the following command:
useradd - D
This will be the output:
Output GROUP=100 HOME=/home INACTIVE=-1 EXPIRE= SHELL=/bin/sh SKEL=/etc/skel CREATE_MAIL_SPOOL=no
For changing the default login shell from /bin/sh to bin/bash, type the new shell as seen below:
sudo useradd -D -s /bin/bash Output SHELL = /bin/bash
This article discusses the different options you can use when starting your own user account.
All these syntax codes are rooted in the useradd command, which is the most important command in Linux.
To navigate your choices, input the useradd command on your Linux screen to get your new user account started.