Two ways to delete a user completely under Linux

  • 2020-12-05 17:33:13
  • OfStack

Linux operation

Experimental environment: Centos7 virtual machine

First create a regular user, gubeiqing.


[root@localhost ~]# useradd gubeiqing
[root@localhost ~]# passwd gubeiqing
Changing password for user gubeiqing.
New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.

This creates a normal user and then deletes the user.


[root@localhost ~]# userdel gubeiqing
[root@localhost ~]#

Using the useradd command, it is removed, but the problem arises when we create gubeiqing again:


[root@localhost ~]# useradd gubeiqing
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
Creating mailbox file: File exists

The file cannot be created because it exists. Why? Since the user's home directory, password file, user group (if not user group), and mailbox file are generated by default when the user is created, the userdel command only removes the user and the user's file is still there, so these files need to be completely deleted. I looked at 1 and there were about 4 places that needed to be dealt with.


/home
/etc/passwd
/etc/group
/var/spool/mail

Delete these files in turn.

1. Delete files in /home directory


[root@localhost ~]# cd /home
[root@localhost home]# ls
gubeiqing
[root@localhost home]# rm -rf gubeiqing
[root@localhost home]# ls
[root@localhost home]#

2. Delete users under /etc/passwd

We can take a look at this file.


[root@localhost ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
dockerroot:x:997:994:Docker User:/var/lib/docker:/sbin/nologin
gubeiqing:x:1000:1000::/home/gubeiqing:/bin/bash

Here you can see all the users in the system, and you can see that the last line is the one you just created, so use the vi editor to delete the last line.

3. Delete user group files under /etc/group

First check this file:


[root@localhost ~]# cat /etc/group
root:x:0:
bin:x:1:
daemon:x:2:
sys:x:3:
adm:x:4:
tty:x:5:
disk:x:6:
lp:x:7:
mem:x:8:
kmem:x:9:
wheel:x:10:
cdrom:x:11:
mail:x:12:postfix
man:x:15:
dialout:x:18:
floppy:x:19:
games:x:20:
tape:x:30:
video:x:39:
ftp:x:50:
lock:x:54:
audio:x:63:
nobody:x:99:
users:x:100:
utmp:x:22:
utempter:x:35:
ssh_keys:x:999:
input:x:998:
systemd-journal:x:190:
systemd-network:x:192:
dbus:x:81:
polkitd:x:997:
postdrop:x:90:
postfix:x:89:
sshd:x:74:
chrony:x:996:
cgred:x:995:
dockerroot:x:994:
gubeiqing:x:1000:

Then use the vi editor to delete this user group.

4. Delete the mailbox file under /var/spool/mail


[root@localhost ~]# cd /var/spool/mail
[root@localhost mail]# ls
gubeiqing
[root@localhost mail]# rm -rf gubeiqing
[root@localhost mail]# ls
[root@localhost mail]#

Once the deletion is complete, go ahead and create the gubeiqing user.


[root@localhost mail]# useradd gubeiqing
[root@localhost mail]# passwd gubeiqing
Changing password for user gubeiqing.
New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.

Done!

In addition to this method, there is one way to delete completely.


[root@localhost mail]# userdel -rf gubeiqing
[root@localhost mail]# useradd gubeiqing
[root@localhost mail]# passwd gubeiqing
Changing password for user gubeiqing.
New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.

Using these two methods, you can delete users completely.

conclusion


Related articles: