linux可將用戶從組中刪除嗎

可以。刪除方法:1、使用usermod從組中刪除用戶,語法“sudo usermod -G root 用戶名”;2、使用gpasswd從指定組中刪除用戶,語法“sudo gpasswd -d 用戶名 組名”;3、編輯“/etc/group”文件,手動從組中刪除用戶。

linux可將用戶從組中刪除嗎

本教程操作環(huán)境:linux7.3系統(tǒng)、Dell G3電腦。

linux中,用戶可以是一個主要組和一個或幾個次要(輔助)組的成員。文件“ / etc / group”為Linux系統(tǒng)中的每個用戶定義了組成員身份。

在本教程中,我們將學習如何在Linux組中刪除用戶。我們將使用兩種方法,還將展示如何通過從“ / etc / group”文件中刪除來手動從組中刪除用戶。

創(chuàng)建Linux用戶

就本教程而言,我們將創(chuàng)建一個名為“ testuser”的用戶。當創(chuàng)建一個新用戶時,也會創(chuàng)建一個與該用戶同名的新主要組。

我們將通過發(fā)出以下命令來創(chuàng)建新用戶:

$ sudo useradd -m testuser

現(xiàn)在,我們可以為該用戶創(chuàng)建密碼:

$ sudo passwd testuser Changing password for user testuser. New password:  BAD PASSWORD: The password contains the user name in some form Retype new password:  passwd: all authentication tokens updated successfully.

我使用了與用戶名相同的密碼,因此我收到警告,密碼中不應包含用戶名的某種形式。

將用戶添加到組

首先,我們將使用groupadd命令創(chuàng)建兩個新組,命令如下所示:

$ sudo groupadd testgroup1$ sudo groupadd testgroup2

現(xiàn)在,我們將“ testuser”用戶添加到上面創(chuàng)建的兩個組中,并使用以下命令將“ testuser”用戶添加到“ root”組中:

$ sudo usermod -a -G root testuser $ sudo usermod -a -G testgroup1 testuser $ sudo usermod -a -G testgroup2 testuser

好的,現(xiàn)在來看一下“ / etc / group”文件,可以看到“ testuser”是這三個組的成員。

$ cat /etc/grouproot:x:0:testuser.............testuser:x:1001:testgroup1:x:1002:testusertestgroup2:x:1003:testuser

找出用戶所屬的組

我們還可以使用以下兩種替代方法來檢查用戶屬于哪個組

$ groups testuser testuser : testuser testgroup1 testgroup2 root$ id -nG testuser testuser testgroup1 testgroup2 root

可以看到輸出非常相似,并且這些命令完成的是相同的事情。

使用usermod從組中刪除用戶

我們可以使用usermod命令一次從一個或多個組中刪除一個用戶。使用usermod時,您必須指定將用戶保留在哪些輔助組中。讓我用一個示例來解釋一下。

$ groups testuser testuser : testuser testgroup1 testgroup2 root

為了從“ testgroup1”和“ testgroup2”組中刪除用戶“ testuser”,請運行以下命令(即,僅將testuser保留在“ root”組中,它是主要的“ testuser”組):

$ sudo usermod -G root testuser
Results$ groups testuser testuser : testuser root

因此,為了將用戶劃分為更多的組,您需要提及用逗號(,)分隔的組名,例如:

$ sudo usermod -G root,testgroup1 testuser
Results# groups testusertestuser : testuser root testgroup1

使用gpasswd從組中刪除用戶

完成相似結果的另一個命令是gpasswd。與usermod不同,我們使用此命令從指定的組中刪除用戶。

要從一個特定的組中刪除用戶,我們可以使用gpasswd命令:

$ sudo gpasswd -d testuser root Removing user testuser from group root  $ groups testuser testuser : testuser testgroup1 testgroup2  $ sudo gpasswd -d testuser testgroup1 Removing user testuser from group testgroup1  $ groups testuser testuser : testuser testgroup2

從組中刪除用戶(手動)

我們還可以通過手動編輯文件’/ etc / group’從組中刪除用戶。重新啟動后,此方法的效果將應用于用戶。

你可以使用最喜歡的文本編輯器來編輯“ / etc / group”文件:

$ cat /etc/group .............sssd:x:993:sshd:x:74:chrony:x:992:vagrant:x:1000:slocate:x:21:vboxsf:x:991:testuser:x:1001:testgroup1:x:1002:testusertestgroup2:x:1003:testuser$ groups testuser testuser : testuser testgroup1 testgroup2 root

現(xiàn)在,我們將手動編輯最后兩個條目來刪除testuser,并從testgroup1和testgroup2中刪除(編輯后的文件應如下所示):

$ cat /etc/group............. sssd:x:993: sshd:x:74: chrony:x:992: vagrant:x:1000: slocate:x:21: vboxsf:x:991: testuser:x:1001: testgroup1:x:1002: testgroup2:x:1003:

更改將在重新啟動后進行,現(xiàn)在該用戶已從這兩個組中刪除:

$ groups testuser testuser : testuser root

結論

在本教程中,我們學習了如何使用usermod、gpasswd以及從“ / etc / group”文件中手動刪除用戶來從組中刪除用戶。

相關推薦:《Linux視頻教程

以上就是

? 版權聲明
THE END
喜歡就支持一下吧
點贊11 分享