How To Create Users in Linux Using useradd / adduser Command

0

If you are a Linux system admin who administers Linux servers, you are very frequently asked to create users in Linux unless the organization uses LDAP. As a Linux administrator, you should be well versed with creating and removing users, assigning users to different groups in Linux.

This post explains to you how to create users in Linux using the useradd / adduser command.

Prerequisites

To be able to create a user account, you need to be logged in as a root user or a user with sudo privileges.

useradd command

In Linux, you can create user accounts with useradd / adduser command. The useradd command is a low-level utility which is used for creating user accounts in Linux and other Unix like operating systems.

The adduser command is just a symbolic link to useradd, so it will work similar to useradd command.

In Ubuntu / Debian, the adduser command may behave differently as useradd and adduser are two seperate commands.

Syntax:

useradd [options] user_name

When then the useradd command is issued, the useradd command creates a new user account using options specified by you in the command line and default values specified in /etc/default/useradd file.

Also, the useradd command edits /etc/passwd, /etc/shadow and /etc/group to put an entry of user details and configurations (User and Group IDs, password policies, encryption method), by reading the contents of /etc/login.defs file.

The default values in this file may differ depending on the Linux distribution. For example, useradd command in Ubuntu does not create the user’s home directory by default if you do not specify options in the command line. Whereas in RHEL, the useradd command creates the home directory for a new user.

Create a New User in Linux

To create a new user called user_name using the useradd command, run.

useradd user_name

The username must be unique. Otherwise, the system would go through an error that the user name already exists.

Set a password for the newly created account with passwd command to log in to the system with that user.

passwd user_name

You will now be prompted to enter and confirm the password.

Output:

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

Create a User with Home Directory

As I said earlier, the home directory for a new user may or may not be created when we use the useradd command with no options.

Use the -m option to ensure the home directory for the user is created while creating a new user.

useradd -m user_name

The above command creates the home directory /home/user_name for user user_name and copies files from /etc/skel directory to the user’s home directory.

ls -al /home/user_name

Output:

total 12
drwx------. 2 user_name user_name 62 Apr 15 23:21 .
drwxr-xr-x. 4 root root 37 Apr 15 23:21 ..
-rw-r--r--. 1 user_name user_name 18 Apr 10 2018 .bash_logout
-rw-r--r--. 1 user_name user_name 193 Apr 10 2018 .bash_profile
-rw-r--r--. 1 user_name user_name 231 Apr 10 2018 .bashrc

Create a User with Specific Home Directory

When only -m is specified as an option for useradd command, it creates the user’s home directory under /home. If you want to create the user’s home in a different location, use the -d <path_to_homedirectory> along with -m option.

To create a home directory under /opt for user user_name, use.

useradd -m -d /opt/user_name user_name

Create a User with Specific User ID

In Linux, users are identified by UID (Unique Identification Number). By default, whenever the user is created in Linux, the system assigns the next available UID from the range of user IDs between UID_MIN and UID_MAX in the /etc/login.defs file.

Use the -u option to create a user with a specific UID. To create a user called user_name with UID of 9999, you need to use.

useradd -u 9999 user_name

Verify that the user’s UID is 9999 using the id command.

id user_name

Output:

uid=9999(user_name) gid=9999(user_name) groups=9999(user_name)

Create a User with Specific Group

Users are often grouped together with the Linux group. The Linux groups help us to define privileges like read, write or execute to all users within that group for a particular resource. Linux group has its own identification number GID.

When a useradd command is executed without any options, it creates a group with the same as the username and same GID as UID.

Use the -g option to create a user with a specific GID. You can either specify the group name or the GID.

GID

useradd -g 100 user_name

Group Name

useradd -g users user_name
The group name or GID must already exist.

Verify that the user’s GID is 500 using the id command.

id user_name

Output:

uid=1001(user_name) gid=100(users) groups=100(users)

Create a User and Add to Multiple Groups

In Linux, the user can be part of one primary group and multiple secondary groups (optional). The -G option is used to create a user and add to additional groups.

A comma must separate group names.

Syntax:

useradd -G group_name1 group_name2,group_name3 user_name
The group name or GID must already exist.

GID

useradd -G 100,74 user_name

Group Name

useradd -G users,sshd user_name

Verify the groups the user belongs to using the id command.

id user_name

Output:

uid=1001(user_name) gid=1002(user_name) groups=1002(user_name),100(users),74(sshd)

Create a User with an Expiry Date

This option is useful when you want to create a temporary user who will have access to the system only for a period of time. By default, when a useradd command issued without any options, the user will be created with account never expire value.

The -e option allows setting the expiry date for the user. The date must be specified in YYYY-MM-DD format.

To create a new user user_name with an expiry date set to May 1st, 2019, you can use.

useradd -e 2019-05-01 user_name

Verify the user’s expiration date using the chage command.

chage -l user_name

Output:

Last password change                                    : Apr 16, 2019
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : May 01, 2019
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

Create a User with Specific Login Shell

Login shell defines which shell to be invoked when the user logs in. There are lots of login shells are available, such as /bin/bash, /bin/sh, /bin/chsh etc.

By default, when a useradd command issued without any options, the user will be created with /bin/bash (RHEL) or /bin/sh (Debian / Ubuntu) shell.

The -s option allows specifying login shell for the user.

useradd -s /bin/bash user_name

Verify the user’s login shell by reading the /etc/passwd file.

grep -i user_name /etc/passwd

Output:

user_name:x:1001:1002::/home/user_name:/bin/bash

Create a User and Add Comment To User

The -c option allows to add comment in /etc/passwrd for informational purpose.

useradd -c "Demo User" user_name

Check the comment we added for this user.

grep -i user_name /etc/passwd

Output:

user_name:x:1001:1002:Demo User:/home/user_name:/bin/bash

Conclusion

I hope you have learned how to create users in Linux using useradd command. The post explained every single option you can use with the useradd command; however, you can combine options to get the desired results.

Please share your feedback in the comments section.

You might also like