1. 程式人生 > >Linux使用者名稱、使用者組的相關命令

Linux使用者名稱、使用者組的相關命令

whoami  檢視當前登入使用者

id 使用者名稱  檢視使用者名稱的id 及所屬組

groups 檢視當前登入使用者的所有所屬組

groups 使用者名稱 檢視指定使用者的所有所屬組

cat /etc/passwd 檢視所有使用者

cat /etc/group 檢視所有使用者組

useradd 使用者名稱 新增使用者

usermod 修改使用者

userdel 使用者名稱 刪除使用者(必須先關閉該使用者的所有任務)

groupadd 使用者組名 新增使用者組

groupmod 修改使用者組

groupdel 使用者組名 刪除使用者組(必須先刪除使用者)

usermod -aG 使用者組 使用者名稱 將使用者新增到某個使用者組

 

 

 

 

1、Linux裡檢視所有使用者

  (1)在終端裡.其實只需要檢視 /etc/passwd檔案就行了. cat  /etc/passwd

  (2)看第三個引數:500以上的,就是後面建的使用者了.其它則為系統的使用者.

2. 檢視某個使用者的UID(使用者ID),GID(主組ID),以及所在組(主組及附屬組)

  id 使用者名稱

id test 
uid=500(test) gid=500(test) groups=500(test),501(usertest) 

3.檢視當前登陸使用者

  whoami

 

4.建立使用者

由/etc/passwd檔案可以看出,使用者的相關資訊主要有使用者名稱,使用者所在組,使用者家目錄組成

所以在建立使用者時也可以指定這些資訊 -g 指定主使用者組名,-G 指定附屬使用者組名,-d 指定使用者登入目錄

useradd 使用者名稱   例:useradd gao

  在不指定引數的情況下,建立使用者的同時會預設建立與使用者名稱相同的使用者組,作為該使用者的主組,

  同時會預設建立以使用者名稱命名的使用者的登陸目錄(使用者家目錄)/home/使用者名稱

useradd -g 使用者組名 -d 使用者家目錄 使用者名稱

useradd -g 使用者組名  使用者名稱      說明:使用者組名必須存在

useradd  -d 使用者家目錄 使用者名稱

例項

新增使用者

[[email protected] vagrant]# groupadd testgao
[[email protected] vagrant]# useradd test_gao01
[[email protected] vagrant]# useradd -g testgao test_gao02
[[email protected] vagrant]# usesradd -g gao test_gao-3
bash: usesradd: コマンドが見つかりません
[[email protected] vagrant]# useradd -d /home/gao test_gao04
[[email protected] vagrant]# cat /etc/passwd | grep test_gao0
test_gao01:x:715:716::/home/test_gao01:/bin/bash
test_gao02:x:716:715::/home/test_gao02:/bin/bash
test_gao04:x:717:717::/home/gao:/bin/bash
[[email protected] vagrant]#

  使用者組資訊

[[email protected] vagrant]# tail -5 /etc/group
gao1:x:713:
test_gao1:x:714:
testgao:x:715:
test_gao01:x:716:
test_gao04:x:717:

  使用者登陸目錄

[[email protected] vagrant]# ll /home | grep test
drwx------  2 test_gao04 test_gao04 4096 12月 27 12:11 2018 gao
drwx------  2 test_gao01 test_gao01 4096 12月 27 12:09 2018 test_gao01
drwx------  2 test_gao02 testgao    4096 12月 27 12:10 2018 test_gao02
drwx------  2 test_gao1  test_gao1  4096 12月 27 12:08 2018 test_gao1
[[email protected] vagrant]#

  

5.設定使用者密碼

passwd 使用者名稱  給已建立的使用者設定密碼,連續輸入兩次即可

[[email protected] vagrant]# passwd test_gao01
ユーザー test_gao01 のパスワードを変更。
新しいパスワード:
新しいパスワードを再入力してください:
passwd: 全ての認証トークンが正しく更新できました。
[[email protected] vagrant]#

  


6.修改使用者資訊

如:

修改test使用者的家目錄
usermod -md /home/testnew(新使用者家目錄) test(使用者名稱)
修改使用者名稱
$ usermod -l testnew(新使用者名稱稱)  test(原來使用者名稱稱) 
把test使用者加入usertest組
$ usermod -aG usertest test ##多個組之間用空格隔開 


7.刪除使用者

userdel 使用者名稱

 說明:刪除使用者並不會刪除使用者的家目錄,若想刪除使用下面的命令


8.刪除使用者家目錄

rm -rf testuser  刪除使用者testuser所在目錄

 

9.建立使用者組

 groupadd 使用者組名

10.修改使用者組

groupmod 

11.·刪除使用者組

groupdel 使用者組名

12. 檢視所有使用者組

cat /etc/group

13.檢視使用者的所有使用者組

groups 檢視當前使用者的使用者組

groups 使用者名稱  檢視指定使用者的所有使用者組

id 使用者名稱 檢視某一個使用者UID GID GROUPS

[[email protected] vagrant]# groups
root
[[email protected] vagrant]# groups w_gao
w_gao : w_gao
[[email protected] vagrant]# usermod -aG w_gao w_gao2
[[email protected] vagrant]# groups w_gao2
w_gao2 : w_gao2 w_gao
[[email protected] vagrant]# id w_gao2
uid=707(w_gao2) gid=707(w_gao2) 所屬グループ=707(w_gao2),706(w_gao)

 

14.檢視某一個使用者組的所有使用者

使用者組的使用者主要分為兩類,

一類是以該使用者組為主使用者組的使用者,此類使用者在/etc/group檔案中不顯示,但可通過GID在/etc/passwd檔案中查詢

另一類是以該使用者組為附屬使用者組的使用者,此類使用者在/etc/group檔案中直接顯示顯示

檢視某一個使用者組的所有使用者就是檢視這兩類使用者

cat /etc/group |grep 使用者組名

可以得到以該使用者組為附屬使用者組的使用者,並得到GID

然後同過GID在通過GID在/etc/passwd檔案中查詢以該使用者組為主使用者組的使用者

cat /etc/passwd | grep GID