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.

ADVERTISEMENTS

Linux drop user account

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

Remove a Linux user
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/

Linux drop user account from the system

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.