1. 程式人生 > >linux基礎命令--userdel 刪除用戶帳戶和相關文件

linux基礎命令--userdel 刪除用戶帳戶和相關文件

l命令 共享 ota lin round com efs oot tps

描述

  • userdel命令用於刪除用戶帳戶和相關文件
  • userdel命令修改系統賬戶文件,刪除所有涉及用戶的信息,指定的用戶(LOGIN)必須存在。

語法

  • userdel [options] LOGIN

選項列表

選項(常用的已加粗) 說明
-f, --force

強制刪除用戶賬戶,即使用戶處於登錄狀態。它強制刪除用戶家目錄和郵件池(一般在/var/spool/mail/目錄下),即使其他用戶家目錄和指定的用戶家目錄同名或指定的用戶沒有郵件池

如果在/etc/login.defs中將USERGROUPS_ENAB定義為yes,並且存在與刪除的用戶同名的組,那麽這個組也會被刪除,即使它是另一個用戶的主組。

註意:該選項很危險,可能會導致系統處於不一致的情況,謹慎使用。

-r, --remove

刪除用戶的家目錄和郵件池(文件),包括它們自身。必須搜索和手動刪除其他文件系統下的相關文件

-R, --root CHROOT_DIR 指定在CHROOT_DIR(改變後的根目錄)下更改生效以及使用CHROOT_DIR目錄下的配置文件
-Z, --selinux-user 刪除用戶登錄的任何SELinux用戶映射

說明

  • 應該先手動檢查所有的文件系統,確保沒有任何文件仍然屬於該用戶。
  • 不能刪除NIS客戶端上的任何NIS屬性,這必須在NIS服務器上執行。
  • 如果正在運行屬於該賬戶的進程,userdel將不會允許刪除該賬戶;此時,你需要先kill該進程或鎖定用戶的口令(或賬戶),然後再刪除賬戶;當前也可以使用-f選項強制刪除。
  • 如果/etc/login.defs的USERGROUPS_ENAB設置為yes, userdel將刪除與用戶同名的組。為了避免口令和組數據庫中的不一致,userdel會檢查該組不會作為另一個用戶的主組,否則只會警告而不會刪除組。-f選項可以強制刪除該組

實例

  • 分別使用-f和-r選項測試刪除共享的主組。

[[email protected] ~]# ls -la /home/test/ /var/mail/test /var/spool/mail/test 
-rw-rw---- 1 test mail  0 Apr 10 13:05 /var/mail/test
-rw-rw---- 1 test mail  0 Apr 10 13:05 /var/spool/mail/test

/home/test/:
total 16
drwx------  2 test test  83 Apr 10 18:53 .
drwxr-xr-x. 5 root root  41 Apr 10 13:05 ..
-rw-------  1 test test  41 Apr 10 18:53 .bash_history
-rw-r--r--  1 test test  18 Oct 31 01:07 .bash_logout
-rw-r--r--  1 test test 193 Oct 31 01:07 .bash_profile
-rw-r--r--  1 test test 231 Oct 31 01:07 .bashrc
[[email protected] ~]# useradd -g test test_userdel                                    # 添加一個新用戶,設置它的初始主組為用戶test的主組
[[email protected] ~]# id test && id test_userdel                       # 共享一個用戶組:test
uid=1002(test) gid=1002(test) groups=1002(test)
uid=1003(test_userdel) gid=1002(test) groups=1002(test)
[[email protected] ~]# userdel -r test                              # 刪除用戶test,提示不能刪除組test,因為該組也是另一個用戶的主組,但除用戶組之外的信息都已經刪除了。
userdel: group test is the primary group of another user and is not removed.
[[email protected] ~]# tail -2 /etc/passwd && tail -2 /etc/group               # 可以發現test組還存在,用戶test已刪除。
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
test_userdel:x:1003:1002::/home/test_userdel:/bin/bash
tss:x:59:
test:x:1002:
[[email protected] ~]# useradd -g test test                           # 恢復用戶test,進行下一步測試。
[[email protected] ~]# id test && id test_userdel                               # 對比兩用戶信息。
uid=1004(test) gid=1002(test) groups=1002(test)
uid=1003(test_userdel) gid=1002(test) groups=1002(test)
[[email protected] ~]# userdel -f test                                        # 可以看到跟使用-r選項不同的是使用-f選項沒有報任何異常,強制性刪除。
[[email protected] ~]# tail -2 /etc/passwd && tail -2 /etc/group              # 所有有關用戶test的信息已刪除,包括其主組,即使該組是另一個用戶的主組。
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
test_userdel:x:1003:1002::/home/test_userdel:/bin/bash
stapdev:x:158:
tss:x:59:
[[email protected] ~]# id test_userdel                              # 可以發現/etc/group文件裏已經沒有組test文件的信息,用戶test_userdel的gid是不存在的,無效的。
uid=1003(test_userdel) gid=1002 groups=1002
[[email protected] ~]# su - test_userdel
/usr/bin/id: cannot find name for group ID 1002                                 # 切換到該用戶下提示沒有找到用戶的gid對應的組名
[[email protected] ~]$ touch a.txt                                             # 但是依舊能創建一個文件,不過文件的屬組是不存在的。
[[email protected] ~]$ ls -l a.txt 
-rw-r--r-- 1 test_userdel 1002 0 Apr 15 15:36 a.txt

總結:刪除一個用戶賬戶,應該確保沒有任何文件仍然屬於該賬戶,而且應謹慎使用-f選項,手動檢查用戶組不被其他用戶當作主組來使用,否則會導致組信息異常。

linux基礎命令--userdel 刪除用戶帳戶和相關文件