How to 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

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

  • deluser, userdel, rm -rf, userdel -f, userdel -r, userdel -Z
  • 0 משתמשים שמצאו מאמר זה מועיל
?האם התשובה שקיבלתם הייתה מועילה

מאמרים קשורים

SSH login without password

You want to use Linux and OpenSSH to automate your tasks. Therefore you need an automatic login...

How to restrict and except SSH access to specific IPs

Once your IP is public it gets attention from so many bots in the internet that do brute force...

How to lock and unlock user account in linux

With the help of two commands you can lock and unlock the user account in Linux. To Lock the...

Adding a user to a group

Synopsis This article describes how to add a Linux user (account) to a group. Environment...

Create a user with no password

This article shows you how to create a user account without a password or an empty password on...

Powered by WHMCompleteSolution