How to Create and Manage New Users on Linux

Filed Under: UNIX/Linux

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 username

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

# adduser penguin

>Sample Output

create a new user in Linux

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

 cat /etc/passwd

>Sample Output

view users in a Linux system

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

useradd -option username

-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.

$ useradd -m username 

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

$ useradd -m james

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

$ ls /home

Sample Output
useradd -m option

-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:

$ useradd -u 1400 james 

This can be verified by the command

$ id james

Sample Output

Useradd - U Option

Alternatively, you can verify the UID by running

id -u james

Sample Output

Id -u Username

-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

$ useradd -g users james

To verify this, run the command below

$ id -gn james

Sample Output

Useradd -g option

-c (–comment) 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

$ useradd -c "IT departmemt" james

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

$ useradd -e 2019-06-10 username

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

sudo chage -l username

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

$ usermod -a -G sudo username

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

$ usermod -a -G sudo james 

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

groups james

Sample output

Aduser To Sudoers Group Using Usermod

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

visudo

This will open the file as shown below

Visudo

Next, append the following line

username ALL = (ALL)ALL

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.

Update System Using User

Deleting users

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

$ userdel -r username

To delete user ‘james’ run the below command.

$ userdel -r james

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages