Blog

03/06/2019

Linux Drop User – Remove a Linux user



How do I drop or remove old users account from my Linux server? I can login using the user’s SSH access, and run ‘sudo -s’ command to become the root user. How can I remove old account, user’s file and directory ownership on Linux?

You can remove a Linux user using the userdel command. The userdel delete/remove a user account and related files from the Linux server. Use this command to modify the system account files. It delete all entries that refer to the given user name. Usually, user info removed from the /etc/passwd, [/etc/shadow[/file], and /etc/group files.

Linux drop user account

kbd { display: inline-block; margin: 0 .1em; padding: .1em .6em; font-family: Arial,”Helvetica Neue”,Helvetica,sans-serif; font-size: 11px; line-height: 1.4; color: #242729; text-shadow: 0 1px 0 #FFF; background-color: #e1e3e5; border: 1px solid #adb3b9; border-radius: 3px; box-shadow: 0 1px 0 rgba(12,13,14,0.2), 0 0 0 2px #FFF inset; white-space: nowrap;}

Procedure to remove the user account is as follows:

  1. Open the Terminal app. Log in to your server using ssh command:
    ssh user@server-name-here
  2. Run the userdel command to remove the old user named tom:
    sudo userdel tom
  3. To delete all files in the user’s home directory along with the home directory itself and the user’s mail spool:
    sudo userdel -r tom

Let us see all examples and command in details.

How to remove a Linux user

The syntax is as follows to remove a user account and related files:
userdel userNameHere
userdel -f userNameHere
userdel -r userNameHere
userdel -Z user-name-here

Where options are as follows for the command:

  • -f – This option forces the removal of the user account, even if the user is still logged in. It also forces userdel to remove the user’s home directory and mail spool, even if another user uses the same home directory or if the specified user does not own the mail spool.
  • -r – Files in the user’s home directory removed along with the home directory itself and the user’s mail spool. Files located in other file systems will have to be searched for and deleted manually.
  • -Z – Remove any SELinux user mapping for the user’s login. Useful on Fedora, RHEL, CentOS and other distro where SELinux used.

Warning: Be careful with the userdel command. It delete a user’s home directory/files. Always keep backups.

Examples

Delete a Linux user named jerry:
# userdel Jerry
Verify that user has been deleted with the id command or grep command
# id Jerry
# grep ^Jerry /etc/passwd

You have to delete home directory and other files manually using rm command:
# ls /home/
# rm -rf /home/Jerry


To avoid deleting files manually pass the -r to the userdel command. In this example, delete a user named tom:
# userdel -r tom
# id tom
# grep ^tom /etc/{passwd,shadow}
# ls /home/

(adsbygoogle = window.adsbygoogle || []).push({});

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....
12/08/2019

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...