都知道删除用户用userdel -r USERNAME,-r代表把用户相对应的目录一并删除,那么,假如疏忽了忘记加参数,会有什么结果,今天我尝试了一下。


#首先创建用户user2,删除后再重新添加:

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

#没错,显示家目录已存在,并且,mailbox文件存在。因为删除用户时候没加-r选项,查看/home里面是否真存在该目录:
[root@localhost ~]# ll /home
total 60
drwx------. 2 ab1       ab1     4096 Aug 16 02:13 ab1
drwx------. 2 ab2       ab2     4096 Aug 16 02:13 ab2
drwx------. 2 ab3       ab3     4096 Aug 16 02:14 ab3
drwx------. 2 mageia    mageia  4096 Aug 20 21:38 linux
drwx------. 2 root      root   16384 Aug  6 07:18 lost+found
drwx------. 2 tom       tom     4096 Aug 17 01:51 tom
drwx------. 2 tom1      tom1    4096 Aug 17 01:51 tom1
drwx------. 4 user      user    4096 Aug 20 04:21 user
drwx------. 2 user2     user2   4096 Aug 21 14:10 user2

#是否删除/home/user2即可?我们尝试一下:
[root@localhost ~]# rm -rf /home/user2
[root@localhost ~]# useradd user2
useradd: user 'user2' already exists
[root@localhost ~]# userdel user2
[root@localhost ~]# useradd user2
Creating mailbox file: File exists

#这个就是一开始我们只删除home目录下对应的文件,并没有删除mailbox里的文件,该文件会存放在哪里?
我们查看一下userdel的帮助手册,其中-r命令里有这样的描述显示该命令作用区域:
home directory itself and the user′s mail spool:

[root@localhost ~]# man userdel
-r, --remove
           Files in the user′s home directory will be 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.

           The mail spool is defined by the MAIL_DIR variable in the login.defs
           file.
[root@localhost ~]# ls /var/spool/mail
ab1  ab2  ab3  mageia  mandriva  openstack  slackware  tom  tom1  user
  user2
  
[root@localhost ~]# rm /var/spool/mail/user2

删除该文件并重新创建user2,成功。


由此可知,删除用户userdel 加参数-r是多么重要。


那么问题来了,创建用户会生成什么文件?

目前得知的是/etc/passwd, /etc/shadow, 但这两个都只是添加记录,

创建文件夹的有/home/USERNAME,/var/spool/mail/USERNAME  至于其他地方有没有,等待后续深入学习吧。