15 Useful Useradd Commands with Examples in Linux – Tecmint

We all know the most popular command called ‘useradd‘ or ‘adduser‘ in Linux. There are times when a Linux system administrator is asked to create user accounts on Linux with some specific properties, limitations, or comments.

[ You may also be interested in: How to create a shared directory for all users

on Linux ] On Linux, a ‘

useradd‘ command is a low-level utility used to add/create user accounts on Linux and other Unix-like operating systems. The ‘adduser‘ is very similar to the useradd command because it is just a symbolic link to it.

add users in linux
examples of useradd commands

In some other Linux distributions, the useradd command may come with a slightly different version. I suggest you read their documentation, before using our instructions for creating new user accounts on Linux.

When we run the ‘useradd‘ command in the Linux terminal, it does the following main things:

Edit the /etc/passwd, /etc/shadow, /etc/group, and /

  • etc/gshadow files for newly created user accounts
  • . Creates and populates a home directory for the new user.

  • Sets permissions and properties for the home directory.

Useradd command syntax

The basic syntax of the useradd command is:

# useradd [options] username

In this article, we will show you the 15 most commonly used useradd commands with their practical examples on Linux. We have divided the section into two parts, from basic use to advanced use of the command.

Part I: Basic Useradd Commands with

    10 Examples

  • Part II: Advanced Useradd Commands with 5 Examples

1. How

to add a new user in Linux

To add/create a new user, you need to follow the ‘useradd‘ or ‘adduser‘ command with ‘username‘. The ‘username’ is a user login name, which is used by a user to log into the system.

Only one user can be added and that username must be unique (different from other usernames that already exist in the system).

For example, to add a new user named ‘tecmint‘, use the following command.

[[email protected] ~]# useradd tecmint When we add a new user in Linux with the command ‘

useradd‘ it is created in a locked state and to unlock that user account, we need to set a password for that account with the command ‘passwd’. [[email protected] ~]# passwd

tecmint Password change for the user tecmint. New UNIX password: Retype the new UNIX password: passwd: All authentication tokens were successfully updated. Create a user

on Linux

Once a new user is created, their entry is automatically added to the ‘/etc/passwd‘ file. The file is used to store the user’s information and the input must be.

tecmint:x:1000:1000:tecmint:/home/tecmint:/bin/bash <img src="https://www.tecmint.com/wp-content/uploads/2014/03/Create-User-in-Linux.png" alt="

View user information on Linux” />
View user information on Linux

The above entry contains a set of seven fields separated by colons, each field has its own meaning. Let’s see what these fields are

:

  • Username: Login name of the user used to log in to the system. It must be between 1 and 32 characters.
  • Password: User password (or x character) stored in an /etc/shadow file in an encrypted format.
  • User ID (UID

  • ): Each user must have a User ID User Identification Number (UID). By default, UID 0 is reserved for the root user and UIDs ranging from 1 to 99 are reserved for other predefined accounts. Other UIDs ranging from 100 to 999 are reserved for system accounts and groups.
  • Group ID (GID

  • ): The primary group ID (GID) group identification number stored in the /etc/group file.
  • User Information: This field is optional and allows you to define additional information about the user. For example, the full name of the user. This field is filled with the command ‘finger’.
  • Home Directory: The absolute location of the user’s home directory.
  • Shell: The absolute location of a user’s shell, i.e. /bin/bash.

Arabic numeral. Create

a user with a different home directory

By default, the ‘useradd‘ command creates a user’s home directory in /home directory with a user name. So, for example, we have seen above that the default home directory for the user ‘tecmint’ is ‘/home/tecmint‘.

However, this action can be changed using the ‘-d‘ option along with the location of the new home directory (i.e. /data/projects). For example, the following command will create a user ‘anusha‘ with a home directory ‘/data/projects‘.

[[email protected] ~]# useradd -d /data/projects anusha [[email protected] ~]# passwd anusha

You can view the user’s home directory and other user-related information such as user ID, group ID, shell, and comments.

[[email protected] ~]# cat /etc/passwd | grep anusha anusha:x:1001:1001::/data/projects:/bin/bash

Create a user with home directory in Linux

3. Create a user with a

specific user ID

On Linux, each user has their own UID (Unique Identification Number). By default, every time we create a new user account in Linux, it assigns the user ID 500, 501, 502, etc. But, we can create users with custom userrid with the ‘-u’ option.

For example, the following command will create a user ‘navin‘ with the custom user ID ‘1002‘.

[[email protected] ~]# useradd -u 1002 navin

Now, let’s verify that the user created with a defined user ID (1002) using the following command.

[[email protected] ~]# cat /etc/passwd | grep navin navin:x:1002:1002::/home/navin:/bin/bash <img src="https://www.tecmint.com/wp-content/uploads/2014/03/Create-User-with-Home-Directory-in-Linux.png" alt="Create user with user ID on Linux

” />
Create

a user with user ID on Linux

NOTE: Ensure that the value of a user ID must be unique to any other user already created on the system

.

4. Create a user with a specific group ID

Similarly, each user has their own GID (group identifier). We can also create users with specific group IDs with the -g option.

Here in this example, we will add a user ‘tarunika‘ with a specific UID and GID simultaneously with the help of the ‘-u’ and ‘-g options.

[[email protected] ~]# useradd -u 1005 -g tecmint tarunika

Now, see the assigned user ID and group ID in the ‘/etc/passwd’ file. [[email protected] ~]# cat /etc/passwd

| grep tarunika tarunika:x:1005:1000::/home/tarunika:/bin/bash

To verify the user’s GID, use the command id:

[[email protected] ~]# id -gn tarunika <img src="https://www.tecmint.com/wp-content/uploads/2014/03/Create-User-with-User-ID-in-Linux.png" alt="Create user with group ID on Linux

” />
Create a user with group ID on Linux

5. Add

a user to multiple groups

The ‘-G‘ option is used to add a user to additional groups. Each group name is separated by a comma, with no spaces in between.

Here in

this example, we are adding a ‘tecmint’ user in various groups such as administrators, web administrators, and developers. [[email protected]:~]# groupadd admins [[email protected]:~]# groupadd webadmin [[email protected]:~]# groupadd developers [[email protected]:~]# usermod -a -G admins,webadmin,developers tecmint [[email protected]:~]# useradd -G admins,webadmin,developers

paddy Next, verify that multiple groups are assigned to the user with the id command.

[[email protected] ~]# id tecmint uid=1000(tecmint) gid=1000(tecmint) groups=1000(tecmint), 1007(admins),1008(webadmin),1009(developers) context=root:system_r:unconfined_t:SystemLow-SystemHigh <img src="https://www.tecmint.com/wp-content/uploads/2014/03/Create-User-with-Group-ID-in-Linux.png" alt="Add User to Group in Linux

” />
Add User to Group in

Linux

[ You may also be interested in: How to add or remove a user from a group in Linux ]

6. Add

a user without home directory

In some situations, where we don’t want to map home directories for a user, due to security reasons. In such a situation, when a user logs on to a system that has just restarted, their home directory will be root. When that user uses the su command, their login directory will be the home directory of the previous user.

To create users without their home directories, ‘-M‘ is used. For example, the following command will create a user ‘shilpi‘ without a home directory.

[[email protected] ~]# useradd -M shilpi Now,

let’s verify that the user is created without a home directory, using the ls command.

[[email protected] ~]# ls -l /home/shilpi ls: Unable to access /home/shilpi: No such file or directory <img src="https://www.tecmint.com/wp-content/uploads/2014/03/Add-User-to-Group-in-Linux.png" alt="Create user without home directory on Linux

” />
Create user without home directory in Linux

7. Create a

user with an account expiration date

By default, when we add users with the ‘useradd‘ command, the user account never expires, i.e. its expiration date is set to 0 (meaning it never expired).

However, we can set the expiration date using the ‘-e’ option, which sets the date in YYYY-MM-DD format. This is useful for creating temporary accounts for a specific period of time.

[ You may also be interested in: How to manage

user password expiration and aging on Linux ] Here in this example, we create an ‘aparna’ user with an account expiration date, i.e. August 27, 2021

in YYYY-MM-DD format. [[EMAIL PROTECTED] ~]# USERADD -E 2021-08-27

aparna

Next, check the account age and password with the ‘chage’ command for the ‘aparna’ user after setting the account expiration date. [[email protected] ~]# chage -l aparna

Last password change: Jun 25, 2021 Password expires : never Inactive password: never Account expires : Aug 27, 2021 Minimum number of days between password change: 0 Maximum number of days between password change: 99999 Number of warning days before password expires: 7 <img src="https://www.tecmint.com/wp-content/uploads/2014/03/Create-User-Without-Home-Directory-in-Linux.png" alt="Create user with account expiration date

” />
Create user with account

expiration date 8. Create a user with password expiration date

The ‘-f‘ argument is used to define the number of days after a password expires. A value of 0 inactivates the user account as soon as the password has expired. By default, the password expiration value set to -1 means that it never expires.

Here, in this example, we will set an account password expiration date, i.e. 45 days on a ‘mansi’ user using the ‘-e’ and ‘-f’ options. [[email protected] ~]# useradd -e 2014-04-27

-f 45 mansi

<img src="https://www.tecmint.com/wp-content/uploads/2014/03/Create-User-With-Account-Expiry-Date.png" alt="Create user with password expiration date

” />
Create user with password expiration date

9. Add a user with

custom comments

The ‘-c‘ option allows you to add custom comments such as the user’s full name, phone number, etc. to the /etc/passwd file. The comment can be added as a single line without spaces.

For example, the following command will add a ‘mansi‘ user and insert that user’s full name, Manis Khurana, in the comment field.

[[email protected] ~]# useradd -c “Manis Khurana” mansi You can see your comments in the file ‘/etc/passwd

‘ in the comments section. [[email protected] ~]# tail -1 /etc/passwd

mansi:x:1010:1013:Manis Khurana:/home/mansi:/bin/sh <img src="https://www.tecmint.com/wp-content/uploads/2014/03/Create-User-With-Password-Expiry-Date.png" alt="Create full name user

” />
Create full name user

10. Create a

user login shell on Linux Sometimes

we add users who have nothing to do with the login shell or sometimes we need to assign different shells to our users. We can assign different login shells to each user with the ‘-s‘ option.

Here in this example you will add a user ‘tecmint‘ without a login shell, i.e. a shell ‘/sbin/nologin‘.

[[email protected] ~]# useradd -s /sbin/nologin tecmint

You can check the shell assigned to the user in the file ‘/etc/passwd’. [[email protected] ~]# tail -1 /etc/passwd

tecmint:x:1011:1014::/home/tecmint:/sbin/nologin <img src="https://www.tecmint.com/wp-content/uploads/2014/03/Create-User-with-Name.png" alt="Create user with login

shell” />
Create a user with login shell

11. Add

a user with a specific home directory, a default shell, and a custom comment The following command will create a user ‘ravi’ with the home directory ‘/var/www/tecmint’, default shell /bin/bash and add additional information about the user. [[email protected] ~]# useradd -m -d /var/www/ravi -s /

bin/bash

-c “TecMint Owner” -U ravi Create a user with the home directory and login shell In the above command, the ‘-m -d’ option creates a

user with a specified home directory and

the ‘

-s‘ option sets the user’s default shell, that is, /bin/bash. The ‘-c‘ option adds additional information about the user and the ‘-U‘ argument creates/adds a group with the same name as the user.

12. Add a user with home directory

, custom shell, custom comment and UID/GID

The command is very similar to the previous one, but here we define shell as ‘/bin/zsh‘ and custom UID and GID for a ‘tarunika‘ user. Where ‘-u‘ defines the UID of the new user (i.e. 100) and while ‘-g‘ defines GID (i.e. 1000).

[[email protected] ~]# useradd -m -d /var/www/tarunika -s /bin/zsh -c “TecMint Technical Writer” -u 1000 -g 100 tarunika <img src="https://www.tecmint.com/wp-content/uploads/2014/03/Create-User-with-Home-Directory-and-Shell.png" alt="Create user with UID and

GID” />
Create a user with UID and GID

13. Add

a user with home directory, no shell, custom comment, and user ID

The following command is very similar to the previous two commands, the only difference is here, that we disable the login shell for a user named ‘avishek‘ with a custom user ID (i.e. 1019).

Here the ‘

-s‘ option adds the default /bin/bash shell , But in this case, we set a login to ‘/usr/sbin/nologin‘. That means the ‘avishek‘ user will not be able to log into the system.

[[email protected] ~]# useradd -m -d /var/www/avishek -s /usr/sbin/nologin -c “TecMint Mr. Technical Writer” -u 1019 avishek <img src="https://www.tecmint.com/wp-content/uploads/2014/03/Create-User-with-UID-andGID.png" alt="Create user with UID and

Nologin” />
Create a user with UID and Nologin

14. Add a user with home directory, shell, custom skell/comment, and user ID

The only change in this command is that we use the ‘-k‘ option to set the custom skeleton directory, i.e. /etc/custom.skell, not the default /etc/skel. We also use the ‘-s‘ option to define different shells, i.e. /bin/tcsh for the ‘navin‘ user.

[[email protected] ~]# useradd -m -d /var/www/navin -k /etc/custom.skell -s /bin/tcsh -c “No Active Member of TecMint” -u 1027 navin <img src="https://www.tecmint.com/wp-content/uploads/2014/03/Create-User-with-UID-and-Nologin.png" alt="Create User with Shell and UID

” />
Create a User with Shell and UID

15. Add a user with no home directory, no shell, no group, and custom comment

The following command is very different from the other commands explained above. Here we use the ‘-M‘ option to create a user without the user’s home directory and use the ‘-N‘ argument that tells the system to only create a username (no group). The ‘-r‘ argument is to create a system user.

[[email protected] ~]# useradd -M -N -r -s /bin/false -c “TecMint Member Disabled” clayton <img src="https://www.tecmint.com/wp-content/uploads/2014/03/Create-User-with-Shell-and-UID.png" alt="Create user

with NoLogin and Group” />
Create a user with NoLogin and Group

For more information and options about useradd, run the ‘useradd’ command in the terminal to see the available options. # useradd

[ You may also be interested: 15 useful examples of Usermod commands on Linux ]