How to Create and Manage New Users on Linux With Examples

As a systems administrator or regular Linux user, you may be required to create additional users in the system so that other users can reap the benefits of interacting with the system. In this tutorial, we will walk you through how to create and manage new users on a Linux system.

Creating New Users

There are 2 commands that can be used to create users in the system.

  1. adduser
  2. useradd

Let’s have a look at each of them in detail.

adduser command

adduser command is a user-friendly Perl script that creates a fully functional Linux user on the system.

Syntax

The syntax for adding a new user to a Linux system is as shown below

adduser command offers a high-level interface and performs the following functions

  1. Creates a new user called ‘username’
  2. Creates a new group with the new user’s username
  3. Places the new user in the newly created group bearing user’s username
  4. Creates a home directory (/home/username) and later copy the files from /etc/skel into it.
  5. Prompts for additional information about the new user such as Full Name , Room Number, Work Phone, and Home Phone

Adding a new user

In this example, we are going to add a user ‘penguin’ to the system

Sample Output

The list of users in a Linux system is stored in /etc/passwd file. Therefore, to verify that the user has been created run

Sample Output

adduser-username

useradd command

useradd command is a native binary command and is considered a low-level utility for adding users to the system. The command cannot achieve most of the functionalities that adduser accomplishes at once and therefore , requires more options in its syntax.

Syntax

The syntax for using the useradd command is

-m option

By default, useradd command does not create a home directory for the new user like adduser command. This is show in the output below where a new user ‘james’ has been created.

Sample Output

useradd-command-without-options

To create a user along with the home directory, use the -m option as shown.

For example, to create user ‘james’ alongside his home directory, execute:

Now you can verify the existence of the home directory for the user ‘james’ by running:

Sample Output

useradd-command-without-options

-u (–uid) option

The -u option creates the user with a specific UID (User ID). A User ID is a positive integer that is automatically assigned to a new user by the Linux system.

For example, to create a user ‘james’ with a UID of 1400 execute:

This can be verified by the command

Sample Output

useradd-u-option

Alternatively, you can verify the UID by running

Sample Output

-g (–gid) option

The -g or --gid option creates a user belonging to a certain group by specifying either the group name or the GID number.

For example, to create the user ‘james’ belonging to a new group called users, execute the command

To verify this, run the command below

Sample Output

useradd-u-option

The -c (–comment) option allows you to create a user with a brief description of the new user. For example, to create a new user ‘james’ with a string “IT department” execute

The comment is stored in the /etc/passwd file. You can use the cat command to reveal the details of the user a shown

Sample Output

useradd-c-option

-e (–expiredate) option

If you want to define a date upon which a new user account will expire, then use the -e option.

For instance, to create a new user called ‘james’ with an expiry date set to June 10 2019 execute

Next, use the chage command to verify the user account expiry date:

Sample Output

useradd-expiry

 

Adding users to sudoers group

Sometimes, you may need to delegate some administrative rights to regular users so that they can perform some administrative tasks. To accomplish this, you need to add them to the sudoers group.

One way of adding users to the sudoers group is by running the command below

Where username is the user that you want to add to the sudoers group. For instance, to add the user ‘james’ to sudoers group run the command

To verify that the user ‘james’ has been added to the sudo group run

Sample output

useradd-u-option

Alternatively, you can edit your sudoers file using the visudo command by running

This will open the file as shown below

Next, append the following line

Save and exit the text editor.

Now you can switch to the regular user and execute an admin command by preceding the username with
sudo as shown.

visudo

Deleting users

To completely delete a user from the Linux system, use the userdel command with -r attribute as shown

To delete user ‘james’ run the below command.

Conclusion

In this article, you learned how to create and manage new users in Linux with adduser and useradd commands. These commands run across all distributions, both Debian and Redhat distributions.

By admin

Leave a Reply