How to add and remove a user account on Linux

Estimated reading: 3 minutes 574 views

In Linux and other Unix-like operating systems, the useradd command can be used to add/create new user accounts including their usernames, passwords, and other data.

Below is the basic syntax of the command to add a user account.

useradd [options] username

This guide will describe the steps to add and remove a user account in the Linux server and cloudvps will be used as a sample username.

Add a user account

To add a new user, both useradd and adduser commands can be used because adduser is just a symbolic link to useradd. The new username should be different from other usernames that are already created in the system.

  1. To add a user called cloudvps with the command useradd.
    # useradd cloudvps
    

    linuxuser1

  2. Below is the command to set a password for the user account.
    # passwd cloudvps
    

    linuxuser2

  3. To add a user called cloudvps with the command adduser command.
    # adduser cloudvps1
    

    linuxuser3

  4. In Linux, user account details are stored in the following file /etc/passwd. The below command is used to verify and check the details of the newly created user account.
    # grep cloudvps /etc/passwd
    

    linuxuser4

    *It contains a set of seven colon-separated fields as mentioned below.

    cloudvps:x:1000:1000::/home/cloudvps:/bin/sh
    cloudvps1:x:1001:1001:,,,:/home/cloudvps1:/bin/bash
    
  1. cloudvps – Account username.
  2. x – User password and it is stored in /etc/shadow file in encrypted format as below image.
      # grep cloudvps /etc/shadow
    

    linuxuser5

  3. 1001 – User ID (UID): Every user must have a UID (User identification number) and the root user is always reserved with 0 and the 1-99 range is reserved for other predefined accounts.
  4. 1001 – Group ID (GID) group identification number and it is stored in /etc/group file.
      # grep cloudvps /etc/group
    

    linuxuser6

  5. /home/cloudvps – It is the absolute location of the user’s home directory.
  6. /bin/sh –Shell: It is the absolute location of the user’s shell.

    Note: Replace the username cloudvps with the original user account name.

Remove a user account

In Linux, user account details are stored in the file /etc/passwd. The below command is used to verify and check the details of the newly created test user account cloudvps.

# grep cloudvps /etc/passwd

linuxuser4

  1. Below is the basic syntax of the command to delete/remove a user account.
    # userdel [options] username
    
  2. Before deleting, you must kill all running processes that belong to that user account should be killed/terminated. The below commands are used to find and kill the running process, then remove that user and verify it from the /etc/passwd file.
    # pgrep -u cloudvps
    # killall -9 -u cloudvps
    # userdel cloudvps
    
  3. To remove the home directory and mail spool too, enter:
# userdel -r {user-name}
# userdel -r cloudvps

linuxuser6

Note: Replace the username cloudvps1 with the original useraccount name.

 

Leave a Comment