This article will help you to understand the ‘useradd’ command in Linux/Unix based systems. As we all are aware that Linux is a multi-user and a multitasking operating system (OS). It has built with a large number of commands from basic to advance. Here, we are going to see the ‘useradd’ command, it is also called as ‘adduser’. In RHEL/CENTOS both (useradd and adduser) commands do the same functions/features, and there is no key difference between them, whereas it may not be true with other distros. The path of the ‘adduser’ is just a symbolic link to the ‘useradd’ command.

Useradd command is to create and manage users in Linux/Unix based systems. It comes with a lot of options/arguments. Being a Linux system administrator, it is one of your routine jobs to create users with a normal/default setup, or sometimes you may ask to create a specific home directory of the users, or set user account expiry date, or shell, or group, etc, etc.

This step by step guide will help you how to use various options in ‘useradd’ commands on RHEL/CentOS 7.6. We will see one by one mostly with examples. The same guide can be used to all the other versions of RHEL/CentOS/Fedora. For other distros, it may come with a small type of difference in using it. Therefore, it is highly recommended to read the guidelines before creating the user accounts.

The Global Syntax of useradd command with options:

useradd [options] USERNAME

The following cheat sheet provides an overview of the options used in useradd command in Linux.

Let us begin with executing the default form of useradd command followed by the user name. Once this command is executed, it will perform the following actions:

(a) A new entry will be added to ‘/etc/passwd, /etc/shadow, /etc/group, /etc/gshadow’ files.

(b) A new user’s home directory will be created along with the bash files, which copied from the skel directory. Also, permission and ownership of the home directory will be set by default.

Warning: To execute a useradd command in Terminal you need to be either a root user (Administrator) or a superuser or with sudo privilege. Permission denied for all the normal users to execute the useradd command.

1. How to add/create a user ?

# useradd linuxteck

Note: The above command will create a username as “linuxteck” into your local Linux Server/Desktop with default settings as mentioned above. A new entry will be updated into 4 files ie.,(/etc/passwd, /etc/shadow, /etc/group and /etc/gshadow). Let us see one by one.

(a) /etc/passwd => This file is used to store all the information of the user accounts. You can verify the information of the newly created username in the ‘/etc/passwd’ file with the following command:

# cat /etc/passwd | grep linuxteck

Output: linuxteck:x:1003:1005::/home/linuxteck:/bin/bash

Note: The above output represents a user account which contains the following “7 fields” separated with colons (:).

(b) /etc/shadow => This file is used to store all the password information of the user’s. Only root account/equivalents privileges account’s can only handle this file. By default, the root account holds the permission and ownership of this file. Use the following command to view the password related information of the new user named “linuxteck”. # cat /etc/shadow | grep linuxteck Output: linuxteck:$6$PsZ2cWaJ$yUscpxrjnDkq4LgwvRNLMBK6GqXkWavI4Czf44v6NVl4NzH.czTTxw.IHqvohU5Y1LQrTjPYe/MRd8w8ndAbe0:18333:0:99999:7::: Note: The ‘/etc/shadow’ file is not readable like ‘/etc/passwd’ file. Only root and super user can read this file. From the above output contains 9 files seperated with colons (:).

(c) /etc/group => This file is used stores a list of groups and the members belong to each group. Basically in Linux /Unix multiple users can be placed into single or multiple groups. To see the information in the ‘/etc/group’ file can be used by the following command: # cat /etc/group | grep linuxteck

(d) /etc/gshadow => This file is used stores an encrypted password for each group and group membership. Generally, it can be readable by only root or super users and the default permission of this file is set as “000”. # cat /etc/gshadow | grep linuxteck Output: linuxteck : ! : : Note: The above output contains the following fields:.

2. How to define a home directory during user creation in Linux? #useradd -b /opt linuxteck Note: The above command can override the default path of the user’s home directory when creating a new user. It usually placed under “/home”, whereas using ‘-b’ flag we can be customized the path based on our choice. In this example, the location of the “linuxteck” user’s home directory will be placed under ‘ /opt’ instead of ‘/home’. Use the following command to verify the home directory path of the newly created user. # cat /etc/passwd | grep linuxteck Output: linuxteck:x:1003:1005::/opt/linuxteck:/bin/bash # useradd -c “John Gomez” john Note: Using the’-c’ option can add a custom comment like the user’s full name, email address, etc while creating a user. The output of the above example can be verified by the following command. # cat /etc/passwd | grep john Output: john:x:1003:1004:John Gomez:/home/john:/bin/bash 4. How to create a specific home directory name when creating a new user? # useradd -d /opt/oracle jose Note: The ‘-d’ option authorize us to change the user’s default home directory name different than the username. For example, when you execute a useradd command by default the user’s home directory name will also be the same as the username. In our case, the above command creates a username as “jose” with a home directory of “/opt/oracle”. Use the following command to see the details of the user’s home directory and the username. # cat /etc/passwd | grep jose Output: jose:x:1004:1005::/opt/oracle:/bin/bash 5. How to check the user’s default configuration? # useradd -D OR # cat /etc/default/useradd Output: GROUP=100

HOME=/home

INACTIVE=-1

EXPIRE=

SHELL=/bin/bash

SKEL=/etc/skel

CREATE_MAIL_SPOOL=yes Note: You can use either one of the above commands to check the user’s default configuration file. In the above output, you can see there are 7 rows in total and each row having its default values. By editing this file from the command you can change the default shell and directory. A short explanation is here below: