userdel: user vivek is currently used by process 749 error and fix on Linux

Posted on in Categories , , , , last updated September 22, 2017

I am trying to delete a user named vivek using the userdel command as follows

userdel -r vivek
userdel: user vivek is currently used by process 749

How do I fix this problem and delete username vivek on a Debian Linux 9.x server?

One can use the userdel command to delete a user account on a Debian or any other Linux distro. It is a low level utility for removing users.
On Debian, sysadmin should usually use deluser command instead.

Understanding problem

It seems that PID # 749 used by a user named vivek. It can be simple open ssh session or something running in the background. Use the ps command to find out about a PID # 749:
$ ps aux | grep 749
Sample outputs:

vivek      749  0.0  0.1  92716  4028 ?        S    17:03   0:00 sshd: [email protected]/0
root     10038  0.0  0.0  12784   944 pts/1    S+   17:13   0:00 grep 749

The ‘sshd: [email protected]/0’ indicates that active ssh session. From the man page:

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.

So, all you have to do is kill this PID and run the userdel command again using either the kill command/killall command:
# kill -15 749
# killall -TERM -u vivek
Verify it:
$ ps aux | grep 749
Now delete the user account name vivek:
# userdel -r vivek
Verify that user account deleted from /etc/passwd and /etc/shadow file with the id command or grep command:
$ id vivek
$ grep ^vivek /etc/passwd
$ grep vivek /etc/shadow

Another option is to pass the -f option to the userdel command. 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 mail spool is not owned by the specified user. You must be very careful while using the -f option as it may leave your system in an inconsistent state:
# userdel -f userNameHere
See man pages for more info:
$ man userdel
$ man deluser
$ man 5 passwd
$ man 5 shadow

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Leave a Comment