How to add & remove a user account on Linux Cloud Servers

2020-05-25 By Ryan 7696 Views linux user account
14 reviews

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 layerstack 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 layerstack with the command useradd.

    # useradd layerstack
    

    linuxuser1

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

    # passwd layerstack
    

    linuxuser2

  3. To add a user called layerstack with the command adduser command.

    # adduser layerstack
    

    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 layerstack /etc/passwd
    

    linuxuser4

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

    layerstack:x:1001:1001::/home/layerstack:/bin/sh
    


  1. layerstack – Account username.

  2. x – User password and it is stored in /etc/shadow file in encrypted format as below image.

      # grep layerstack /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 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 layerstack /etc/group
    

    linuxuser6

  5. The empty field. It allows adding additional information about the user such as the user’s full name, phone number, etc. The -c option can be used along with useradd command to add custom records, such as the user’s full name, phone number, etc to the /etc/passwd file. The below command will add the full name as LayerStack User for the user account layerstack.

      # useradd -c “LayerStack User” layerstack
    

    This detail will be added in /etc/passwd as below:

      layerstack:x:1001:1001: LayerStack User:/home/layerstack:/bin/sh
    
  6. /home/layerstack – It is the absolute location of the user’s home directory.

  7. /bin/sh –Shell: It is the absolute location of the user’s shell.

    Note: Replace the username layerstack 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 layerstack.

# grep layerstack /etc/passwd

linuxuser7


  1. Below is the basic syntax of the command to delete/remove a user account.

    # userdel [options] username
    
  2. Before deleting, must kill all running processes which 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 layerstack
    # ps -fp $(pgrep -u layerstack)
    # killall -9 -u layerstack
    # userdel layerstack
    # grep layerstack /etc/passwd
    

    linuxuser8

    linuxuser9

    linuxuser10

  3. The below command can be used to remove the user’s home directory and mail spool:

    # userdel -r layerstack
    

    linuxuser6

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


Related Tutorials

What do you think about this article?

Rate this article
Need assistance?

Try this guide to receive free bundled services at signup on a new free account.

Sign Up

Your Feedback Is Important

We hope you’ll give the new products and updates a try. If you have an idea for improving our products or want to vote on other user ideas so they get prioritized, please submit your feedback on our Community platform. And if you have any questions, please feel free to ask in the Community or contact our Technical Support team.