linux下刪除某個組下的某個用戶

linux下刪除某個組下的某個用戶(刪除用戶的附加組)

一、在LINUX下我們都知道,一個用戶可以屬於多個用戶組,讓用戶屬於多個用戶組,我們可以有多種實現方法,如下:

1、假設系統已經存在testGroup1,testGroup2兩個用戶組,現在要新增一個用戶testUser1,同時讓testUser1屬於testGroup1,testGroup2兩個用戶組

useradd -G  testGroup1,testGroup2  testUser1


2、假設系統已經存在testGroup1,testGroup2兩個用戶組和一個用戶testUser1,現在要把testUser1加到testGroup1,testGroup2兩個用戶組中

usermod -a -G  testGroup1,testGroup2  testUser1

或者

usermod -a -G  testGroup1 testUser1

usermod -a -G  testGroup2 testUser1


注意:

這裏必須要加上-a這個參數,表示追加組的意思,如果不加-a,比如執行usermod -G  testGroup1,testGroup2  testUser1,那 testUser1就只除了 testUser1組外,就只剩下 testGroup1,testGroup2兩個組,如果是個新的用戶還無所謂,那假設testUser1以前還屬於ftp和root組,執行 usermod -G  testGroup1,testGroup2  testUser1命令以後,testUser1這時已經不屬於ftp和root組了。


二、用戶屬於多個組,那怎麼修改用戶的附加組呢?或者怎麼把用戶從某個組中刪除?也有多種實現方法,如下:

假設testUser1用戶現在屬於testUser1,testGroup1,testGroup2,testGroup3,testGroup4組


1、usermod -G "" testUser1

這是清空用戶testUser1所有的追加組,執行該命令以後testUser1只屬於testUser1這個默認組,不管testUser1以前有多少個附加組。


2、gpasswd -d  testUser1 testGroup2

刪 除用戶testUser1的testGroup2所屬組,即把用戶testUser1從testGroup2 組中剔除,執行以後用戶testUser1屬於testUser1,testGroup1,testGroup3,testGroup4組,注 意,gpasswd -d只能一個組一個組操作,如果要刪除多個組則只能操作多次


3、usermod -G  testGroup2,testGroup3 testUser1

讓 用戶testUser1的附加組變成testUser1,testGroup1,testGroup4,這個命令有點類似usermod -G "" testUser1,只不過usermod -G "" testUser1是清空,而usermod -G  testGroup2,testGroup3 testUser1 則是把 testGroup2,testGroup3以外的組清空,而保留testGroup2,testGroup3。因爲gpasswd -d  一次只能刪除一個,所以用這種方法可以一步直達。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章