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:
- Open the Terminal app. Log in to your server using ssh command:
ssh user@server-name-here - Run the userdel command to remove the old user named tom:
sudo userdel tom - 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/