How to remove user from linux

How to delete a user account on Ubuntu Linux

How to delete a user account on Ubuntu

  1. Open the terminal app
  2. Login to server using the ssh user@server-ip-here command
  3. Run sudo deluser —remove-home userNameHere command to delete a user account on Ubuntu
  4. Verify it by running id command

Let us see all commands in details to remove a user account in Ubuntu.

Ubuntu delete user account command

Say you would like to delete a user named ubuntu, run:
$ sudo deluser —remove-home ubuntu
If you want to backup files before removing user account, try:
## create a dir to store backups ##
$ sudo mkdir /oldusers-data
$ sudo chown root:root /oldusers-data
$ sudo chmod 0700 /oldusers-data
$ sudo deluser —remove-home —backup-to /oldusers-data/ ubuntu

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

How to verify that user has been deleted from Ubuntu

Use the id command or grep command as follows:
$ id ubuntu
$ grep ‘^ubuntu’ /etc/passwd

Источник

Linux Drop User – Remove a Linux user

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:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

  • -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

Читайте также:  Как сделать кнопку картинкой windows form

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/# ls /home/

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How to Delete User Accounts with Home Directory in Linux

In this tutorial, I am going to take your through steps you can use to delete a user’s account together with his/her home directory on a Linux system.

Delete User Accounts with Home Directory in Linux

To learn how to create user accounts and manage them on Linux systems, read the following articles from the links below:

As a System Administrator in Linux, you may have to remove users account at after sometime when a user account may become dormant for so long, or user may leave the organization or company or any other reasons.

When removing user accounts on a Linux system, it is also important to remove their home directory to free up space on the storage devices for new system users or other services.

Deleting/Removing a User Account with His/Her Home Directory

1. For demonstration purpose, first I will start by creating two user accounts on my system that is user tecmint and user linuxsay with their home directories /home/tecmint and /home/linusay respectively using adduser command.

Create New User Accounts in Linux

From the screenshot above, I have used the adduser command to create user accounts on Linux. You can also use useradd command, both are same and does the same job.

2. Let’s now move further to see how to delete or remove user accounts in Linux using deluser (For Debian and it’s derivatives) and userdel (For RedHat/CentOS based systems) command.

The directives inside the configuration file for deluser and userdel commands determine how this it will handle all user files and directory when you run the command.

Let us look at the configuration file for the deluser command which is /etc/deluser.conf on Debian derivatives such as Ubuntu, Kali, Mint and for RHEL/CentOS/Fedora users, you can view the /etc/login.defs files.

The values in the these configuration are default and can be changed as per your needs.

3. To delete a user with home directory, you can use the advanced way by following these steps on your Linux server machine. When users are logged on to the server, they use services and run different processes. It is important to note that user can only be deleted effectively when they are not logged on to the server.

Lock User Accounts in Linux

Start by locking the user account password so that there is no access for the user to the system. This will prevent a user from running processes on the system.

The passwd command including the –lock option can help you achieve this:

Lock User Account Password in Linux

Find and Kill All Running Processes of User

Next find out all running processes of user account and kill them by determine the PIDs (Process IDs) of processes owned by the user using:

Then you can list the processes interms of username, PIDs, PPIDs (Parent Process IDs), terminal used, process state, command path in a full formatting style with the help of following command as shown:

Find All Running Processes of User

Once you find all the running processes of user, you can use the killall command to kill those running processes as shown.

The -9 is the signal number for the SIGKILL signal or use -KILL instead of -9 and -u defines username.

Note: In recent releases of RedHat/CentOS 7.x versions and Fedora 21+, you will get error message as:

To fix such error, you need to install psmisc package as shown:

Backup User Data Before Deleting

Next you can backup users files, this can be optional but it is recommended for future use when need arises to review user account details and files.

I have used the tar utilities to create a backup of users home directory as follows:

Backup User Home Directory in Linux

Delete/Remove User Account and Files

Now you can safely remove user together with his/her home directory, to remove all user files on the system use the —remove-all-files option in the command below:

Delete User Account with Home Directory

Summary

That is all to do with removing user and their home directory from a Linux system. I believe the guide is easy enough to follow, but you can voice a concern or add more idea by leaving a comment.

Читайте также:  File needed to boot windows

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

How To: Linux Delete / Remove User Account Using userdel

H ow do I remove a user’s access from my server? How do I delete a user account under Linux operating systems include home directory and running cron jobs?

You need to use the userdel command to delete a user account and related files from user account under Linux operating system. The userdel command must be run as root user on Linux.

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Linux
Est. reading time 4 minutes

Linux delete user command syntax

The syntax is as follows to remove a user account on Linux.
userdel userName
userdel [options] userName
userdel -r userName

userdel command examples

Let us remove the user named vivek or account named vivek from the local Linux system / server / workstation, enter:
# userdel vivek
Next, delete the user’s home directory and mail spool pass the -r option to userdel for a user named ashish, enter:
# userdel -r ashish

Explains how to delete user account with home directory in Linux

A Note About /etc/login.defs File

Default values are taken from the information provided in the /etc/login.defs file for RHEL (Red Hat) based distros. Debian and Ubuntu Linux based system use /etc/deluser.conf file:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Complete example to remove user account from Linux

The following is recommend procedure to delete a user from the Linux server. First, lock user account, enter:
# passwd -l vivek
OR set the date on which the user account will be disabled (syntax is usermod —expiredate YYYY-MM-DD userNameHere ):
# usermod —expiredate 1 vivek
If user try to login, he or she will get the following message:

Next, backup files from /home/vivek to /nas/backup
# tar -zcvf /nas/backup/account/deleted/v/vivek.$uid.$now.tar.gz /home/vivek/
Please replace $uid, $now with actual UID and date/time. Tye userdel command will not allow you to remove an account if the user is currently logged in. You must kill any running processes which belong to an account that you are deleting, enter:
# pgrep -u vivek
# ps -fp $(pgrep -u vivek)
# killall -KILL -u vivek
Delete at jobs, enter
# find /var/spool/at/ -name «[^.]*» -type f -user vivek -delete
Remove cron jobs, enter:
# crontab -r -u vivek
Delete print jobs, enter:
# lprm vivek
To find all files owned by user vivek, enter:
# find / -user vivek -print
You can find file owned by a user called vivek and change its ownership as follows:
# find / -user vivek -exec chown newUserName:newGroupName <> \;
Finally, delete user account called vivek, enter:
# userdel -r vivek
Sample session:

Fig.01: Delete User Accounts with Home Directory and All Data In Linux

See also:

  • Help: Old Employees Accessing The Linux Server.
  • /etc/passwd – The basic attributes of users.
  • /etc/shadow – The basic attributes of users password.
  • /etc/group – The basic attributes of groups.
  • Man pages – ps(1)

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How To Delete/Remove A User Account In Linux?

User account deletion is one of the basic task for Linux administrator.

Whenever a new employee joins in your organization, we need to create the user account for them and when he/she is left from the company then we need to delete it immediately to avoid unnecessary security breach.

We generally use the userdel command to remove the user account in Linux.

It will delete the corresponding user’s information from the system.

You should have privileges to perform this action. It should be root or other privilege user.

Navigate to the following URLs to create a user account and setting up a password to them.

What is userdel?

userdel command is used to delete a user account and related files from the Linux system.

It modifies the system account files, deleting all entries that refer to the user name LOGIN.

userdel will not allow you to remove an account if there are running processes which belong to this account. In that case, you may have to kill those processes or lock the user’s password or account and remove the account later. The -f option can force the deletion of this account.

You should manually check all file systems to ensure that no files remain owned by this user.

When we deleting the user account from the Linux system the below files will be modified.

  • /etc/passwd: User details will be updated in this file.
  • /etc/shadow: User password info will be updated in this file.
  • /etc/group: Group details will be updated of the new user in this file.
  • /etc/gshadow: Group password info will be updated of the new user in the file.

Common Syntax for userdel command.

1) How To Delete The User Account Alone From Linux System?

Use the following format if you would like to delete only the user account from the system. It will delete only the user account from the system and keep the files.

Here we are going to delete the user1 user account from the system and see what will happen once we removed it. See the below output.

Output: I could see that the user1 user account (user and group) permission has been gone and it’s replaced by UID & GID of the user instead.

The above output is clearly showing that the user account got deleted from the system but the user home directory is still exist in the system. If you want to delete the user’s home directory as well then use the -r option.

2) How To Delete/Remove An Entire User Account From Linux System?

Use the below command to delete an entire user account. It will delete the users home directory as well.

Yes, the below output clearly showing that the given user account got completely deleted from the system.

But still crontab entries are exist in system. It should be removed manually by searching the users files.

3) How To Remove The Logged In User Account In Linux?

Add -f option with userdel command to remove the logged in user from the system. As i can see user3 is currently logged into the system, I’m going to remove him with help of force option.

The above output is clearly showing that user3 user is currently accessing the ssh session, Now I’m going to delete the user account. See the output.

The above output is showing the following warning message that userdel: user user3 is currently used by process 30371 and the same time the user account got deleted from system but the ssh session not killed automatically.

Once the user logged out from the system then the current session get disconnected and after that he can’t able to login because we have already deleted the account.

Alternatively we can kill the corresponding ssh session by navigating to the following url.

Yes, the below output clearly showing that the given user account got completely deleted from the system.

4) How To Delete/Remove SELinux User Mapping From Linux System?

Use the below command to delete SELinux user mapping for the user by adding -Z option.

5) How To Find All Files Owned By user2?

userdel command will delete the user account, home directory and user mail spool but it wont delete other file system. Use the following command to find out those file and delete it manually.

The above output is showing that the user2 is not exist in system. But user crontab entry is there, which was created by the user2. To find the details use the following format. See the output.

For details, use the following command.

Now, I’m clear when we delete the user account from system. It deletes the user account, home directory, user mail spool as well as users primary group.

But it won’t delete some of the files which was created by the user if it’s there in other filesystem. Hence, we need to remove it manually by searching them using the above find command.

If you want the particular user account content for future use then you need to take a backup using tar command before performing the delete action., you can backup the content using tar command and delete it.

Hope this article useful for you. If you like our work please share on social media to support us.

Источник

Читайте также:  Installing cmake on linux
Оцените статью