【Linux命令】id,usermod用戶管理命令(包括/etc/passwd、shadow、group、gshadow文件)


一、id命令

可以用來查看用戶的UID、GID和附加組信息

id會顯示用戶以及所屬群組的實際與有效ID。若兩個ID相同,則僅顯示實際ID。若僅指定用戶名稱,則顯示目前用戶的ID。

1.格式

id [OPTION]... [USER]
id [-gGnru][--help][--version][用戶名稱]

2.參數:

  -a        忽略,為了與其他版本的兼容性
  -Z, --context  只打印當前用戶的安全上下文

  -u, --user    只打印用戶有效的ID
  -g, --group     只打印有效的組ID
  -G, --groups  打印所有的組【附加組】ID
  -n, --name     打印名稱而不是數字。適用於 -ugG
  -r, --real      打印真實的ID替代有效的ID。 對-ugG 而言
  -z, --zero delimit entries with NUL characters, not whitespace;
not permitted in default format
--help display this help and exit
--version output version information and exit

4.使用

[root@VM_0_10_centos shellScript]# id root uid=0(root) gid=0(root) groups=0(root)

PS:能看到uid(用戶ID)、gid(初始組ID), groups是用戶所在組,這里既可以看到初始組,如果有附加組,則也能看到附加組

5.案例

[root@VM_0_10_centos shellScript]# id -nug root id: cannot print "only" of more than one choice [root@VM_0_10_centos shellScript]# id -ng root root [root@VM_0_10_centos shellScript]# id -nu root root [root@VM_0_10_centos shellScript]# id -n root id: cannot print only names or real IDs in default format [root@VM_0_10_centos shellScript]# id -r id: cannot print only names or real IDs in default format [root@VM_0_10_centos shellScript]# id -ru 0 [root@VM_0_10_centos shellScript]# id -ru root 0 [root@VM_0_10_centos shellScript]# id -rg root 0 [root@VM_0_10_centos shellScript]# id -rG root 0 [root@VM_0_10_centos shellScript]# id -ruG root id: cannot print "only" of more than one choice

PS: -nr 不能和ugG連用,只能使用-nu或-nG、-ng。-r同理

6.查看命令所在位置和類型

[root@VM_0_10_centos shellScript]# whereis id id: /usr/bin/id /usr/share/man/man1/id.1.gz [root@VM_0_10_centos shellScript]# type id id is hashed (/usr/bin/id)

二、usermod命令

  修改系統帳戶文件來反映通過命令行指定的變化(修改用戶賬戶的各項設置)

  /etc/passwd文件中的每個用戶都有一個對應的記錄行,記錄着這個用戶的基本屬性。該文件對所有用戶可讀。

  /etc/shadow文件是passwd文件的一個影子,/etc/shadow文件中的記錄行與/etc/passwd中的一一對應,它由pwconv命令根據/etc/passwd中的數據自動產生。但是/etc/shadow文件只有系統管理員才能夠進行修改和查看。

1.格式

usermod [-LU][-c <備注>][-d <登入目錄>][-e <有效期限>][-f <緩沖天數>][-g <群組>][-G <群組>][-l <帳號名稱>][-s <shell>][-u <uid>][用戶帳號]

2.參數

-a | --append      ##把用戶追加到某些組中,僅與-G選項一起使用 -c | --comment     ##修改/etc/passwd文件第五段comment -d | --home        ##修改用戶的家目錄通常和-m選項一起使用 -e | --expiredate  ##指定用戶帳號禁用的日期,格式YY-MM-DD -f | --inactive    ##用戶密碼過期多少天后采用就禁用該帳號,0表示密碼已過期就禁用帳號,-1表示禁用此功能,默認值是-1
-u | --uid         ##修改用戶的uid,該uid必須唯一
-g | --gid ##修改用戶的gid,改組一定存在 -G | --groups      ##把用戶追加到某些組中,僅與-a選項一起使用 -l | --login ##修改用戶的登錄名稱 -L | --lock ##鎖定用戶的密碼 -m | --move-home   ##修改用戶的家目錄通常和-d選項一起使用 -s | --shell ##修改用戶的shell-U | --unlock      ##解鎖用戶的密碼

3.案例

1)新建test1賬戶,密碼自己設置,添加用戶組gtest1

[root@VM_0_10_centos shellScript]# useradd test1 [root@VM_0_10_centos shellScript]# echo "密碼" | passwd --stdin test1
[root@VM_0_10_centos shellScript]# groupadd gtest1

2)將test1用戶加入到用戶組gtest1中(多個組之間用空格隔開)

[root@VM_0_10_centos shellScript]# usermod -aG gtest1 test1 [root@VM_0_10_centos shellScript]# id test1 uid=1006(test1) gid=1008(test1) groups=1008(test1),1009(gtest1)

3)修改用戶的家目錄為usertest

[root@VM_0_10_centos shellScript]# usermod -md /home/usertest1 test1 [root@VM_0_10_centos shellScript]# ll /home/ drwx------ 2 test1    test1    4096 Oct 14 09:57 usertest1

4)修改用戶名稱

[root@VM_0_10_centos shellScript]# usermod -l usertest1 test1 [root@VM_0_10_centos shellScript]# id usertest1 uid=1006(usertest1) gid=1008(test1) groups=1008(test1),1009(gtest1)

5)鎖定usertest1的密碼

# 查看usertest1用戶的密碼
# $p:匹配$最后一行  /$/p:匹配帶$的所有行 [root@VM_0_10_centos shellScript]# sed
-n '$p' /etc/shadow usertest1:$1$iXyz/MbY$7gYSO3kumZXgiH.jYVkst/:18183:0:99999:7::: [root@VM_0_10_centos shellScript]# usermod -L usertest1 [root@VM_0_10_centos shellScript]# sed -n '$p' /etc/shadow usertest1:!$1$iXyz/MbY$7gYSO3kumZXgiH.jYVkst/:18183:0:99999:7::: # 查看用戶密碼鎖定狀態 [root@VM_0_10_centos shellScript]# passwd -S usertest1 usertest1 LK 2019-10-14 0 99999 7 -1 (Password locked.)

或使用passwd鎖定密碼

[root@rhel7 ~]# passwd -l usertest1    --- -l 鎖定 Locking password for user usertest1. passwd: Success [root@rhel7 ~]# passwd -S usertest1   --- 查看狀態 usertest1LK 2016-06-20 0 99999 7 -1 (Password locked.) [root@rhel7 ~]# passwd -u usertest1   --- 解鎖 Unlocking password for user usertest1. passwd: Success [root@rhel7 ~]# passwd -S usertest1 usertest1 PS 2016-06-20 0 99999 7 -1 (Password set, SHA512 crypt.)

6)解鎖usertest1的密碼

[root@VM_0_10_centos usertest1]# usermod -U usertest1 [root@VM_0_10_centos usertest1]# sed -n '$p' /etc/shadow usertest1:$1$iXyz/MbY$7gYSO3kumZXgiH.jYVkst/:18183:0:99999:7:::

7)修改用戶的shell

# $!d:最后一行不刪除($表示最后一行) d: 刪除pattern中的所有行,並讀入下一新行到pattern中 [root@VM_0_10_centos usertest1]# sed -n '$p' /etc/passwd usertest1:x:1006:1008::/home/usertest1:/bin/bash [root@VM_0_10_centos usertest1]# sed '$!d' /etc/passwd usertest1:x:1006:1008::/home/usertest1:/bin/bash [root@VM_0_10_centos usertest1]# usermod -s /bin/sh usertest1 [root@VM_0_10_centos usertest1]# sed '$!d' /etc/passwd usertest1:x:1006:1008::/home/usertest1:/bin/sh 或 手動編輯 vi /etc/passwd 找到usertest1編輯保存即可

8)修改用戶的UID、GID

[root@VM_0_10_centos usertest1]# usermod -u 222 usertest1 [root@VM_0_10_centos usertest1]# usermod -g 1115 test1

9)指定賬戶過期日期

[root@VM_0_10_centos usertest1]# sed -n '$p' /etc/shadow  
usertest1:$1$iXyz/MbY$7gYSO3kumZXgiH.jYVkst/:18183:0:99999:7::: [root@VM_0_10_centos usertest1]# usermod -e 2020-10-15 usertest1 [root@VM_0_10_centos usertest1]# sed -n '$p' /etc/shadow usertest1:$1$iXyz/MbY$7gYSO3kumZXgiH.jYVkst/:18183:0:99999:7::18550:

10)指定用戶帳號密碼過期多少天后,禁用該帳號

[root@VM_0_10_centos usertest1]# usermod -f 0 usertest1 [root@VM_0_10_centos usertest1]# sed -n '$p' /etc/shadow usertest1:$1$iXyz/MbY$7gYSO3kumZXgiH.jYVkst/:18183:0:99999:7:0:18550:

PS:  usermod不允許你改變正在線上的使用者帳號名稱。當usermod用來改變userID,必須確認這名user沒在電腦上執行任何程序

三、用戶文件詳解

1./etc/passwd、/etc/shadow

1)/etc/passwd中一行記錄對應着一個用戶,每行記錄又被冒號(:)分隔為7個字段

其格式和具體含義如下: 

用戶名:口令:用戶標識號:組標識號:注釋性描述:主目錄:登錄Shell

字段解釋:

  用戶名(login_name):代表用戶賬號的字符串。通常長度不超過8個字符,並且由大小寫字母和/或數字組成。登錄名中不能有冒號(:),因為冒號在這里是分隔符。為了兼容起見,登錄名中最好不要包含點字符(.),並且不使用連字符(-)和加號(+)打頭。
  口令(passwd):存放着加密后的用戶口令字。雖然這個字段存放的只是用戶口令的加密串,不是明文,但是由於/etc/passwd文件對所有用戶都可讀,所以這仍是一個安全隱患。因此,現在許多Linux系統(如SVR4)都使用了shadow技術,把真正的加密后的用戶口令字存放到/etc/shadow文件中,而在/etc/passwd文件的口令字段中只存放一個特殊的字符,例如“x”或者“*”。
  用戶標識號(UID):是一個整數,系統內部用它來標識用戶。一般情況下它與用戶名是一一對應的。如果幾個用戶名對應的用戶標識號是一樣的,系統內部將把它們視為同一個用戶,但是它們可以有不同的口令、不同的主目錄以及不同的登錄Shell等。取值范圍是0-65535。0是超級用戶root的標識號,1-99由系統保留,作為管理賬號,普通用戶的標識號從100開始。在Linux系統中,這個界限是500。
  組標識號(GID):用戶所屬的用戶組。它對應着/etc/group文件中的一條記錄。
  注釋性描述(users):字段記錄着用戶的一些個人情況,例如用戶的真實姓名、電話、地址等,這個字段並沒有什么實際的用途。在不同的Linux系統中,這個字段的格式並沒有統一。在許多Linux系統中,這個字段存放的是一段任意的注釋性描述文字,用做finger命令的輸出。
  主目錄(home_directory):用戶的起始工作目錄(家目錄),它是用戶在登錄到系統之后所處的目錄。在大多數系統中,各用戶的主目錄都被組織在同一個特定的目錄下,而用戶主目錄的名稱就是該用戶的登錄名。各用戶對自己的主目錄有讀、寫、執行(搜索)權限,其他用戶對此目錄的訪問權限則根據具體情況設置。
  登錄Shell(Shell):用戶登錄后,要啟動一個進程,負責將用戶的操作傳給內核,這個進程是用戶登錄到系統后運行的命令解釋器或某個特定的程序,即Shell。Shell是用戶與Linux系統之間的接口。Linux的Shell有許多種,每種都有不同的特點。常用的有sh(BourneShell),csh(CShell),ksh(KornShell),tcsh(TENEX/TOPS-20typeCShell),bash(BourneAgainShell)等。系統管理員可以根據系統情況和用戶習慣為用戶指定某個Shell。如果不指定Shell,那么系統使用sh為默認的登錄Shell,即這個字段的值為/bin/sh。

2)/etc/shadow文件格式與/etc/passwd文件格式類似,同樣由若干個字段組成,字段之間用“:”隔開。

字段主要含義為:

登錄名:加密口令:最后一次修改時間:最小時間間隔:最大時間間隔:警告時間:不活動時間:失效時間:標志

字段解釋:

  登錄名:是與/etc/passwd文件中的登錄名相一致的用戶賬號
  口令:存放的是加密后的用戶口令:

    如果為空,則對應用戶沒有口令,登錄時不需要口令;
    星號代表帳號被鎖定;
    雙嘆號表示這個密碼已經過期了;
    $6$開頭的,表明是用SHA-512加密;
    $1$表明是用MD5加密;
    $2$ 是用Blowfish加密;
    $5$ 是用 SHA-256加密;
  最后一次修改時間:表示的是從某個時刻起,到用戶最后一次修改口令時的天數。時間起點對不同的系統可能不一樣。例如在SCOLinux中,這個時間起點是1970年1月1日

  最小時間間隔:指的是兩次修改口令之間所需的最小天數。
  最大時間間隔:指的是口令保持有效的最大天數。
  警告時間:表示的是從系統開始警告用戶到用戶密碼正式失效之間的天數。
  不活動時間:表示的是用戶沒有登錄活動但賬號仍能保持有效的最大天數。
  失效時間:給出的是一個絕對的天數,如果使用了這個字段,那么就給出相應賬號的生存期。期滿后,該賬號就不再是一個合法的賬號,也就不能再用來登錄了。

2./etc/group、/etc/gshadow文件

1)/etc/group

  包括用戶組(Group):用戶組口令:GID及該用戶組所包含的用戶(User):每個用戶組一條記錄

格式如下:

group_name:passwd:GID:user_list

 

2)/etc/gshadow

  /gshadow和/etc/group是互補的兩個文件;對於大型服務器,針對很多用戶和組,定制一些關系結構比較復雜的權限模型,設置用戶組密碼是極有必的。比如我們不想讓一些非用戶組成員永久擁有用戶組的權限和特性,這時我們可以通過密碼驗證的方式來讓某些用戶臨時擁有一些用戶組特性,這時就要用到用戶組密碼。

格式如下,每個用戶組獨占一行;

groupname:password:admin,admin,…:member,member,…

 

字段解釋:

  第一字段:用戶組
  第二字段:用戶組密碼,這個段可以是空的或!,如果是空的或有!,表示沒有密碼;
  第三字段:用戶組管理者,這個字段也可為空,如果有多個用戶組管理者,用,號分割;
  第四字段:組成員,如果有多個成員,用,號分割;

 

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM