Delete a User from an Ubuntu Server in Just 3 Steps

For instance, if a user no longer needs access to a server, or he has violated some usage policy, or his account has been hacked, the system administrator can decide to delete that user account from the server then. In this article, I will show you how to delete a user from an Ubuntu server.

Process flow chart to delete a user from an Ubuntu server:

flow chart to delete a user from an Ubuntu server

Distro Used Throughout the Tutorial: Ubuntu 22.04.1 LTS

3 Steps to Delete User in Ubuntu Server

To demonstrate how to delete users in a Ubuntu server, I have used a server and a client computer. I will access the server from the client using an IP address and delete a user named “myuser”.

Step 01: Log into a Server

In the beginning, I will log into the server using the ssh command. Here, the IP address of my server is 172.17.177.87. Now, follow the steps to log into the server:

    1. At first, launch a Ubuntu Terminal on your client computer.
    2. Now insert the following command to connect to the server:
      ssh [email protected]
      EXPLANATION
      • ssh: Uses SSH protocol to connect two computers.
      • walid: Name of the User.
      • 172.17.177.87: IP address of the Server.
    3. Provide the password associated with the user.
    4. Finally, press the ENTER button from the keyboard.Log into the serverIt is showing “Welcome to Ubuntu”. Moreover, the terminal has changed from the previous “walid@Ubuntu” to the new “walid@Server”. So, you are successfully logged into the server.

Step 02: Delete a User from the Server in Ubuntu

In this step, I will delete the user “myuser” from the server. To do so, check the steps:

  1. (Optional) Now, list current users, copy the following command:
    cat /etc/passwd | cut -d: -f1
    EXPLANATION
    • cat: Displays the contents of a file.
    • /etc/passwd: Path of the passwd file that contains user information.
    • Pipe(|): Redirects the output of one command into another.
    • cut: Extracts parts of lines from a file or piped data.
    • -d:: Specifies colon (“:”) as a delimiter.
    • -f1: Specifies a field. Here number 1 means the first field.
    Command to view current users

    List of current userYou will see a list of users as shown in the image above. At the bottom, there is a user by the name of “myuser” and I will delete it.

  2. Now use the following command to delete a user:
    sudo deluser --remove-home myuser
    EXPLANATION
    • sudo: Grants root privileges.
    • deluser: Deletes user.
    • –remove-home: Deletes the home directory of a user.
    • myuser: Name of the user that will be deleted.
  3. Provide the password and hit ENTER.Delete a user from Ubuntu serverYou will see something like “Removing user ‘myuser’” and finally “Done”.


Step 03: Verify If the User has been Deleted from Ubuntu Server

Finally, I will verify if the deleting process was successful. This step is optional, so you can skip it if you want. Now, follow the steps to verify whether the user has been deleted from the Ubuntu server:

  1. Insert the command below on the Ubuntu Terminal:
    cat /etc/passwd | cut -d: -f1
    EXPLANATION
    • cat: Displays the contents of a file.
    • /etc/passwd: Path of the passwd file that contains user information.
    • Pipe(|): Redirects the output of one command into another.
    • cut: Extracts parts of lines from a file or piped data.
    • -d:: Specifies colon (“:”) as a delimiter.
    • -f1: Specifies a field. Here number 1 means the first field.

    Showing the user is deletedAs you can see, there is no “myuser” in the “/etc/passwd” file. So, the user myuser has been successfully deleted.
  2. Now to exit from the server, execute the following command:
    exit
    EXPLANATION
    • exit: Quits a shell.
    Exiting from the server

Conclusion

In this article, I have shown how to delete a user from a Ubuntu server. Hopefully, it was helpful to you.

People Also Ask

How to delete a user from Ubuntu server?

To delete a user named “myuser” from the Ubuntu server, first log into the server and then run the following command:

sudo deluser --remove-home myuser

How to delete in Ubuntu server command line?

To delete something in the Ubuntu server, you need to use the rm command. For instance, I have a file and a directory by the name of “myfile” and “myfolder” respectively. Now, use rm myfile command to remove myfile. To remove the directory myfolder, run rm -r myfolder command.

How do I delete my administrator account?

To delete an administrator account, you need to follow the process of deleting a regular account. Let’s say a system has an administrator account named “admin”. Then you need to follow the command below to delete that account:

sudo deluser admin

How do I add a user to Ubuntu server?

To add a user to the Ubuntu server, first log into the server, then run the following command:

sudo adduser myuser

After running this command, a new user named “myuser” will be added to the Ubuntu server.


Related Articles

5/5 - (10 votes)
Walid Al Asad

Hello Everyone! I am Walid Al Asad. Currently, I am working at a tech company named Softeko as a Linux Content Developer Executive. I live in Dhaka, Bangladesh. I have completed my BSc. in Mechanical Engineering from Bangladesh University of Engineering and Technology (BUET). You can find me on LinkedIn, and ResearchGate. Read Full Bio

Leave a Comment