本次博文的主題是Linux使用者群組。我們先來說說作業系統中使用者群組的基本知識哈!
一、使用者群組基礎:
有誰能告訴我“使用者”和“組”分别是什麼?OS是如何識别每個使用者的?
使用者:是能夠擷取系統資源的權限的集合;它由兩部分組成:使用者名+使用者ID。我們的作業系統就是通過使用者ID來識别每個使用者的(類似于人的姓名與×××号碼)。
作業系統上有個與使用者相關的資訊庫,采用某種對應關系,将每個使用者的使用者名與使用者ID對應,例如:root使用者的使用者ID是0。
組:指派權限的容器,用于一次性完成使用者權限的指派。它由兩部分組成:組名+GID。Linux就是通過GID來識别每個組的。
那麼作業系統儲存使用者群組的資訊的方式主要有如下幾種:
1、存放在文本檔案中;
2、存放在資料庫中;
3、采用ldap(輕量級目錄通路協定)儲存;
Linux中,所有的配置都是儲存在配置檔案中的,下面具體說說Linux下跟使用者群組相關的配置檔案:
1、/etc/passwd:儲存使用者的屬性資訊的檔案

字段說明:
從左到右共7個字段分别表示為:
使用者名:密碼占位符:UID:GID:注釋資訊:家目錄:預設shell
說明:所謂密碼占位符隻是表示這是一個密碼字段,但使用者密碼卻不是存放在這裡,而是存放在/etc/shadow檔案中。之是以這樣設計,是由于在早期的UNIX作業系統中,使用者賬号的密碼資訊是存放在passwd中的,但這樣不法使用者可以很容易地擷取密碼字串并進行暴力破解,是以存在一定的安全隐患。是以後來經改進後,将密碼轉存入專門的shadow檔案中,并嚴格控制權限,這樣passwd檔案中就僅保留密碼占位符"x”
預設法則:
UID:已有最大的UID+1
GID:/etc/group中已有最大GID+1
家目錄:/home/USERNAME
shell:/bin/bash
2、/etc/shadow:使用者的影子密碼
從左到右共9個字段,分别表示如下:
使用者名:使用者密碼:最近一次修改密碼的時間:密碼最短使用期限:密碼最長使用期限:告警時間:帳号的非活動天數:賬号過期日期:保留字段
每個字段的具體說明如下:
使用者名 | 使用者密碼 | 最近一次修改密碼的時間 | 密碼最短使用期限 | 密碼最長使用期限 | 告警時間 | 帳号的非活動天數 | 賬号過期日期 | 保留字段 |
表示使用者的登入名稱 | 如果有些使用者在這個字段是"x"或"*"或"!!"表示此使用者不能登入到系統; 密碼一共由3個字段組成,用$分隔,各字段的含義分别為: 第一個字段: $1:表示加密方法 第二個字段: $8位随機數:salt 第三個字段: $字元串: 真正的密碼 | 從1970年1月1号開始至上次修改密碼時,中間所經過的天數; | 表示使用者必須經過多少天才能修改其密碼;如果設定成0,則禁用此功能 | 兩次修改密碼的最長時間間隔;在密碼到期時必須要修改密碼,99999表示不需要修改 | 提前多少天通知使用者密碼即将過期 | 密碼過期後多久禁用此使用者,也就是說系統不會再讓此使用者登入,也不會提示使用者過期,而是完全禁用此使用者賬号 | 指定的時間到達,即鎖定此賬号(從1970年1月1号開始的天數),如果這個字段的值為空,表示此賬号永久可用 | 一般為空,為Linux以後發展保留的字段 |
補充知識:加密方法
- 對稱加密:加密和解密使用同一個密鑰;最常見的為:3DES(3輪DES加密), AES(Advanced Encrption Standard);密鑰管理及分發功能很差;
- 公鑰加密: 也叫非對稱加密,私鑰/公鑰(s/p),公鑰可以從私鑰中提取得到;加密速度慢,但安全性高;通常用于密鑰分發;目前常見的公鑰加密的算法為:RSA, DSA
-
單向加密:提取資料特征碼,特性:
(1)無論輸入是多大的資料,其輸出是定長的;
(2)如果輸入的資料一樣,其結果一定一樣;反之,輸入資料微小改變,将引起結果的巨大改變,這種現象稱之為“雪崩效應”;
(3)算法:md5(message digest),SHA(Secure Hash Algorithm), SHA1
md5: 無論輸入是多長,輸出為定長128bits
sha1: 無論輸入是多長,輸出為定長160bits
sha-256: 無論輸入是多長,輸出為定長256bits
sha-512: 無論輸入是多長,輸出為定長512bits
"salt”的意義:随機加入,防止使用者設定同一個密碼後的加密算法生成的密碼相同;
舉例說明如下:[root@rhel5 home]# openssl passwd -1 -salt 12345678 redhat
$1$12345678$0ME5N6oDyoEAwUp7b5UDM/
[root@rhel5 home]# openssl passwd -1 -salt 12345678redhat
$1$12345678$0ME5N6oDyoEAwUp7b5UDM/ #可以看到,兩次輸入的salt相同,加密後生成的密碼是相同的哈
[root@rhel5 home]# openssl passwd -1 -salt 12345698 redhat
$1$12345698$jX8ffqc5lSEfqT.NXmyP2. #這裡可以看到,兩次輸入的salt有細微的差别,則生成的密碼差别甚大
3、/etc/group:組的屬性資訊
一共4個字段;從左到右各字段的含義:
組名:密碼占位符:GID:以此組為額外組的使用者清單,使用者間用逗号分隔
4、/etc/gshadow:組的密碼(小知識:windows系統中組是沒有密碼的)
一共4個字段,從左到右各字段的含義:
組名稱:組密碼(空或!表示沒有密碼):組管理者(可以為空,如果有多個管理者用逗号隔開):組成員(如果有多個成員,用逗号隔開)
Linux中組的類型有哪些呢?下面具體說說:
從使用者的角度來說,Linux中的組類型有兩種:
1、私有組:也叫“基本組”,建立使用者時如果不指明其基本組,則會預設建立與使用者同名的基本組
2、額外組:每個使用者可以同時加入多個組,使用者另外加入的組稱為該使用者的額外組(附加組)
Linux中使用者群組可以用下面的表格來總結:
使用者類型說明 | 使用者/組分類 | UID/GID範圍 | 說明 |
Linux系統中的所有使用者賬号資訊都存放在/etc/passwd和/etc/shadow檔案中,檔案的每一行就代表一個使用者。Windows系統中所有本地使用者賬号資訊都存放%systemroot%\system32\config\sam檔案和系統資料庫中;域使用者賬戶資訊則存放在活動目錄資料庫中; | 管理者root | root是Linux系統中預設的超級使用者賬号,類似于windows系統中的administrator賬戶,但需要注意的是Linux中真正的管理者隻有root使用者一個 | |
Linux系統每建立一個使用者賬号就會自動建立一個與該賬号同名的使用者組,該組為使用者的基本組。每個使用者可以同時加入多個組,使用者另外加入的組稱為該使用者的額外組(附加組);Windows系統沒有所謂基本組和附加組的概念 | 系統使用者 | 1~499 | 在安裝Linux系統及部分應用程式時,會添加一些特定的低權限使用者賬号,這些使用者一般不允許登陸到系統,而僅用于維持系統或程式的正常運作 |
普通使用者(可登陸使用者) | 500+ | 此類使用者賬号需要由root使用者或其他管理者使用者建立,擁有的權限受到一定限制,一般隻在使用者自己的主目錄中有完全權限 | |
管理者組(root) | 類似于windows系統的系統管理者組 | ||
系統使用者組 | 一般加入一些用于日常系統管理的使用者 | ||
普通使用者組 | 當建立使用者時,預設建立一個與使用者名同名的基本組,還可以指定加入其它額外組 |
二、使用者群組的管理:
建立使用者:
指令名稱:useradd(adduser)
指令所在路徑:/usr/sbin/useradd
執行權限:root
功能描述:建立使用者,在添加使用者的過程中會自動完成以下幾項任務:
(1)在“/etc/passwd”和“etc/shadow”檔案的末尾增加該使用者賬号的記錄;
(2)若未指明使用者的主目錄,則在“/home”目錄下自動建立與該使用者賬号同名的主目錄,并在該目錄中建立使用者的初始配置檔案;
(3)若未指明使用者所屬的組,則自動建立與該使用者賬号同名的基本組賬号,組賬号的記錄資訊儲存在"/etc/group”和"/etc/gshadow”檔案中;
文法:useradd [options] LOGIN
範例:
[root@rhel5 jjx]#tail -2 /etc/passwd
jjx:x:500:500:jijianxin:/home/jjx:/bin/bash
xin:x:501:501::/home/xin:/bin/bash
[root@rhel5 jjx]# useradd redhat
[root@rhel5 jjx]# tail -2 /etc/passwd
xin:x:501:501::/home/xin:/bin/bash
redhat:x:502:502::/home/redhat:/bin/bash
[root@rhel5 jjx]#
通過useradd指令的選項可以定制使用者屬性:
-u:定制UID,有效範圍0~65535
[root@rhel5 jjx]# useradd -u 1000 ubuntu
[root@rhel5 jjx]# tail-3/etc/passwd
xin:x:501:501::/home/xin:/bin/bash
redhat:x:502:502::/home/redhat:/bin/bash
ubuntu:x:1000:1000::/home/ubuntu:/bin/bash
-g:定制GID,有效範圍為已有的GID(注意:指定GID時,所表示的組必須存在)
[root@rhel5 jjx]# useradd -g 1001 debian
useradd:未知的組 1001
[root@rhel5 jjx]#
可以看到:指定的組不存在(可以事先用groupadd指令添加);
-G:建立額外組,有效範圍為已有的GID;可以有多個,用逗号分隔
[root@rhel5 ~]# groupadd testGroup
[root@rhel5 ~]# useradd -G testGroup user01
[root@rhel5 ~]#tail -1 /etc/passwd
user01:x:2003:2004::/home/user01:/bin/bash
[root@rhel5 ~]#tail -2 /etc/group
testGroup:x:2003:user01 #建立了testGroup額外組
user01:x:2004: #系統自動建立了與使用者user01同名的基本組
[root@rhel5 ~]#id -ng user01
user01 #驗證使用者user01的基本組
[root@rhel5 ~]#
-c:定制注釋資訊
[root@rhel5 jjx]# useradd -c "Linux Distribution" gentoo
[root@rhel5 jjx]# tail-4/etc/passwd
xin:x:501:501::/home/xin:/bin/bash
redhat:x:502:502::/home/redhat:/bin/bash
ubuntu:x:1000:1000::/home/ubuntu:/bin/bash
gentoo:x:1001:1001:Linux Distribution:/home/gentoo:/bin/bash #可以看到"gentoo"使用者屬性中注釋字段中添加了相應的條目
[root@rhel5 jjx]#
說明:以上選項可以組合起來使用,例如:
[root@rhel5 jjx]# useradd -c "Light Weight Distribution" -u 2000 archlinux
[root@rhel5 jjx]# tail -1 /etc/passwd
archlinux:x:2000:2000:Light Weight Distribution:/home/archlinux:/bin/bash
[root@rhel5 jjx]#
-d:定制所建立使用者的家目錄(/path/to/somewhere)
[root@rhel5 jjx]#mkdir /tmp/users
[root@rhel5 jjx]# useradd -d /tmp/users/user01 slackware
[root@rhel5 jjx]#tail-1/etc/passwd
slackware:x:2001:2001::/tmp/users/user01:/bin/bash #可以看出,使用者slackware的家目錄被指定為user01
[root@rhel5 jjx]#
補充知識1:轉換使用者
su(switch user):
[root@rhel5 ~]#su jjx
[jjx@rhel5 root]$ exit
exit
[root@rhel5 ~]#su - jjx
[jjx@rhel5 ~]$pwd
/home/jjx
[jjx@rhel5 ~]$ exit
logout
提示:中間那個 - 符号的意思完整切換到jjx使用者。所謂的完整就是連着所有變量一起切換,即互動式切換;否則屬于非互動式切換。大家不要忘記,不然你切換使用者會出現莫名其妙的問題。管理者切換到普通使用者不需要輸入密碼,反之不行!
對于“-”;切換的效果是看不出來的,在哪裡能展現到這一點?
舉例說明:先是完整切換到一般使用者,然後不用- 切換回root。再用useradd建立一個使用者試試。
[root@rhel5 ~]#su - jjx
[jjx@rhel5 ~]$su root
密碼:
[root@rhel5 jjx]# useradd user01
bash: useradd: command not found #可以看到,出現了無法找到指令的提示
[root@rhel5 jjx]#
補充知識2:如何檢視有哪些shell可用?
cat /etc/shells,舉例如下:
[root@rhel5 ~]#cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/tcsh
/bin/csh
/bin/ksh
/bin/zsh
提示:SHELL是個環境變量(儲存目前使用者的shell)
-s:定制使用者的shell
[root@rhel5 ~]# useradd -s /etc/csh suse
[root@rhel5 ~]#tail-1/etc/passwd
suse:x:2002:2002::/home/suse:/etc/csh #可以看到指定新使用者suse的shell為csh。
補充知識: 如何檢視使用者id?有兩種方法:
1、id指令:
指令名稱:id
指令所在路徑:/usr/bin/id
執行權限:所有使用者
功能描述:檢視使用者的id
文法:id [OPTION] [USERNAME]
常有選項:
-u:檢視UID
-g:檢視GID
-n:一般與-u、-g合用,顯示的是使用者名或組名
[root@rhel5 ~]#tail -1 /etc/passwd
suse:x:2002:2002::/home/suse:/etc/csh
[root@rhel5 ~]#id
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel) context=root:system_r:unconfined_t:SystemLow-SystemHigh
[root@rhel5 ~]#id -u suse
2002
[root@rhel5 ~]#id -un suse
suse #可以看到顯示的是使用者名哈
2:finger指令(如果沒有此指令,則需要用"yum install finger”導入一下)
文法:finger [user]
範例:
[root@rhel5 ~]# finger suse
Login: suse
Name: (null)
Directory: /home/suse
Shell: /etc/csh
Never logged in.
No mail.
No Plan.
删除使用者:
指令名稱:userdel
指令所在路徑:/usr/sbin/userdel
功能描述:删除使用者
文法:userdel [options] LOGIN
不帶任何選項删除使用者賬号是不會删除其家目錄的:
常用選項:
-r:删除使用者連同其家目錄一并删除
[root@rhel5 ~]#tail -2 /etc/passwd
user01:x:2003:3005::/home/user01:/bin/bash
user02:x:2004:2004::/home/user02:/bin/bash
[root@rhel5 ~]# userdel -r user02
[root@rhel5 ~]#id user02
id: user02:無此使用者
[root@rhel5 ~]#ls /home/archlinux
gentoo jjx mandriva redhat ubuntu user01 xin
[root@rhel5 ~]#ls /home/user02
ls: /home/user02: 沒有那個檔案或目錄 #user02的家目錄一并被删除了哈
[root@rhel5 ~]#
修改使用者屬性
指令名稱:useremod
指令所在路徑:/usr/sbin/usermod
功能描述:修改使用者屬性
文法:usermod [option] username
-a -G合用:修改使用者的額外組
[root@rhel5 etc]#tail -5 /etc/passwd
mandriva:x:2002:3003::/home/mandriva:/bin/bash
user01:x:2003:3005::/home/user01:/bin/bash
hadoop:x:2004:2004::/home/hadoop:/bin/bash
hive:x:2005:2005::/home/hive:/bin/bash
centos:x:2006:2006::/home/centos:/bin/bash
[root@rhel5 etc]# usermod -a -G centos user01 #user01加入額外組centos
[root@rhel5 etc]# id user01 #驗證指令結果
uid=2003(user01) gid=3005(user01)groups=3005(user01),2006(centos) context=root:system_r:unconfined_t:SystemLow-SystemHigh
[root@rhel5 etc]# tail-5/etc/group
grp02:x:5000:
magedu:x:5001:
hadoop:x:2004:
hive:x:2005:
centos:x:2006:user01 #從這裡也可以看出usere01被加入了額外組centos哈
-l:修改使用者登入名
[root@rhel5 etc]#id user01
uid=2003(user01) gid=3005(user01)groups=3005(user01),2006(centos) context=root:system_r:unconfined_t:SystemLow-SystemHigh
[root@rhel5 etc]# usermod -l user1 user01 #将user01使用者名修改為user1
[root@rhel5 etc]# id user1 #驗證指令結果
uid=2003(user1) gid=3005(user01)groups=3005(user01),2006(centos) context=root:system_r:unconfined_t:SystemLow-SystemHigh
-d -m合用:修改使用者的家目錄(-m是把原有家目錄裡的資料一并移入新的家目錄中)
[root@rhel5 home]#tail -2 /etc/passwd
centos:x:2006:2006::/home/centos:/bin/bash
user1:x:2003:3005::/home/user01:/bin/bash
[root@rhel5 home]# ls /home/user01/ #檢視使用者user1原來家目錄user01的内容
rc rc.local rc.sysinit
[root@rhel5 home]# usermod -d /home/user1 -m user1 #修改使用者user1的家目錄為/home/user1,并且将原來家目錄裡的内容一并遷移過去
[root@rhel5 home]# ls /home/ #驗證結果
archlinux centos gentoo hadoop hive jjx mandriva redhat ubuntu user1 xin
[root@rhel5 home]# ls /home/user1
rc rc.local rc.sysinit
-e:以“MM/DD/YY”為格式,指定賬号的過期時間
[root@rhel5 home]#tail -3 /etc/shadow
hive:!!:16077:0:99999:7:::
centos:!!:16077:0:99999:7:::
user1:!!:16069:0:99999:7:::
[root@rhel5 home]# usermod -e 02/18/2014 user1 #修改user1的賬号過期時間,系統會自動換算成自1970年1月1日開始的天數
[root@rhel5 home]# tail -1 /etc/shadow
user1:!!:16069:0:99999:7::16119:
-f #: 修改使用者賬号的非活動天數;
[root@rhel5 home]#tail -1 /etc/shadow
user1:!!:16069:0:99999:7::16119:
[root@rhel5 home]# usermod -f7 user1 #修改user1賬号的非活動天數為7天,7天後系統會禁用user1使用者賬号
[root@rhel5 home]#tail -1 /etc/shadow
user1:!!:16069:0:99999:7:7:16119:
修改使用者影子密碼屬性:
指令名稱:passwd
指令所在路徑:/usr/bin/passwd
執行權限:root(普通使用者隻能用passwd指令修改自己的密碼而不能修改密碼的相關屬性)
功能描述:修改密碼屬性
文法:passwd [options] username
常用選項:
-n: 密碼最短使用天數;
[redhat@rhel5 ~]$passwd -n 8
redhatOnly root can do that. #提示隻有管理者才能更改密碼的屬性哈
[root@rhel5 home]# passwd -n 8 redhat
Adjusting aging data for user redhat.
passwd: Success
[root@rhel5 home]# grep ^redhat /etc/shadow
redhat:!!:16062:8:99999:7::16216:
-x:最長使用天數;
[root@rhel5 home]# !grepgrep ^redhat /etc/shadow
redhat:!!:16062:8:99999:7::16216:
[root@rhel5 home]# passwd -x 365 redhat
Adjusting aging data for user redhat.
passwd: Success
[root@rhel5 home]# !grepgrep ^redhat /etc/shadow
redhat:!!:16062:8:365:7::16216: #将redhat使用者的密碼的最長使用天數改為365天
-w: 警告天數;
[root@rhel5 home]# !grep
grep ^redhat /etc/shadow
redhat:!!:16062:8:365:7::16216:
[root@rhel5 home]# passwd -w 10 redhat
Adjusting aging data for user redhat.
passwd: Success
[root@rhel5 home]# !grep
grep ^redhat /etc/shadow
redhat:!!:16062:8:365:10::16216: #将redhat使用者的密碼的警告天數修改為10天(原來是7天),提醒使用者10天後密碼過期哈
-i: 非活動天數;
[root@rhel5 home]# !grep
grep ^redhat /etc/shadow
redhat:!!:16062:8:365:10::16216:
[root@rhel5 home]# passwd-i7 redhat
Adjusting aging data for user redhat.
passwd: Success
[root@rhel5 home]# !grep
grep ^redhat /etc/shadow
redhat:!!:16062:8:365:10:7:16216: #修改使用者redhat的密碼非活動天數為7天,7天後系統将禁用此賬号哈
-S:顯示使用者狀态資訊
[root@rhel5 home]#passwd -S redhat
redhat LK 2013-12-238365107(Password locked.) #顯示redhat使用者的賬号資訊(LK表示鎖定此賬号)
指令名稱:chage
指令所在路徑:/usr/bin/chage
執行權限:root(普通使用者隻能用chage –l 指令檢視自己密碼的詳細資訊)
功能描述:修改影子密碼屬性(即密碼時效管理)
文法:chage [options] username
-l:列出使用者密碼的詳細時間參數
[root@rhel5 home]# chage -l
user1Last password change : Dec 30,2013Password
expires :neverPassword
inactive : neverAccount
expires : Feb 18,2014
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
-d: 修改密碼的最近一次修改時間,/etc/shadow中的第三字段,為0則表示使用者第一次登入強制修改密碼
[root@rhel5 home]#tail -1 /etc/shadow
user1:!!:16040:0:99999:7:7:16119:
[root@rhel5 home]# chage -l user1
Last password change : Dec 01,2013
Password expires : never
Password inactive : neverAccount
expires : Feb 18,2014
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires :
[root@rhel5 home]# chage -d11/01/2013 user1 #修改user1密碼的最近一次修改時間為2013年11月1日
[root@rhel5 home]# tail -1 /etc/shadow #驗證結果
user1:!!:16010:0:99999:7:7:16119:
[root@rhel5 home]# chage -l user1
Last password change : Nov 01,2013
Password expires : never
Password inactive : never
Account expires : Feb 18,2014
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
-m, –mindays MIN_DAYS:密碼最短使用期限,/etc/shadow中的第四字段;
[root@rhel5 home]#tail -1 /etc/shadow
user1:!!:16010:0:99999:7:7:16119:
[root@rhel5 home]# chage -l user1
Last password change : Nov 01,2013
Password expires : never
Password inactive : never
Account expires : Feb 18,2014
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
[root@rhel5 home]# chage -m3 user1 #将user1的密碼最短使用期限設定為3天
[root@rhel5 home]# tail-1/etc/shadow
user1:!!:16010:3:99999:7:7:16119:
[root@rhel5 home]# chage -l user1
Last password change : Nov 01,2013
Password expires : never
Password inactive : never
Account expires : Feb 18,2014
Minimum number of days between password change : 3
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
-M, --maxdays MAX_DAYS:密碼最長使用期限,/etc/shadow中的第五字段;
範例:
[root@rhel5 home]#tail -1 /etc/shadow
user1:!!:16010:3:99999:7:7:16119:
[root@rhel5 home]# chage -l user1
Last password change : Nov 01,2013
Password expires : never
Password inactive : never
Account expires : Feb 18,2014
Minimum number of days between password change : 3
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
[root@rhel5 home]# chage -M365 user1 #設定user1的密碼最長使用期限為365天
[root@rhel5 home]# tail-1/etc/shadow
user1:!!:16010:3:365:7:7:16119:
[root@rhel5 home]# chage -l user1
Last password change : Nov 01,2013
Password expires : Nov 01,2014
Password inactive : Nov 08,2014
Account expires : Feb 18,2014
Minimum number of days between password change : 3
Maximum number of days between password change : 365
Number of days of warning before password expires : 7
-W, --warndays WARN_DAYS:警告天數,/etc/shadow中的第六字段;
[root@rhel5 home]#tail -1 /etc/shadow
user1:!!:16010:3:365:7:7:16119:
[root@rhel5 home]# chage -l user1
Last password change : Nov 01,2013
Password expires : Nov 01,2014
Password inactive : Nov 08,2014
Account expires : Feb 18,2014
Minimum number of days between password change : 3
Maximum number of days between password change : 365
Number of days of warning before password expires : 7
[root@rhel5 home]# chage -W10 user1 #提醒user1使用者10天後密碼到期(原來是7天)
10[root@rhel5 home]# tail-1/etc/shadow
user1:!!:16010:3:365:10:7:16119:
[root@rhel5 home]# chage -l user1
Last password change : Nov 01,2013
Password expires : Nov 01,2014
Password inactive : Nov 08,2014
Account expires : Feb 18,2014
Minimum number of days between password change : 3
Maximum number of days between password change : 365
Number of days of warning before password expires : 10
-I, --inactive INACTIVE:非活動天數,/etc/shadow中的第七字段;
[root@rhel5 home]#tail -1 /etc/shadow
user1:!!:16010:3:365:10:7:16119:
[root@rhel5 home]# chage -l user1
Last password change : Nov 01,2013
Password expires : Nov 01,2014
Password inactive : Nov 08,2014
Account expires : Feb 18,2014
Minimum number of days between password change : 3
Maximum number of days between password change : 365
Number of days of warning before password expires : 10
[root@rhel5 home]# chage -I14 user1 #修改user1使用者賬号密碼過期後14天禁用user1使用者
[root@rhel5 home]# tail-1/etc/shadow
user1:!!:16010:3:365:10:14:16119:
[root@rhel5 home]# chage -l user1
Last password change : Nov 01,2013
Password expires : Nov 01,2014
Password inactive : Nov 15,2014
Account expires : Feb 18,2014
Minimum number of days between password change : 3
Maximum number of days between password change : 365
Number of days of warning before password expires : 10
-E, --expiredate EXPIRE_DATE:密碼過期時間,/etc/shadow中的第八字段;
[root@rhel5 home]#tail -1 /etc/shadow
user1:!!:16010:3:365:10:14:16119:
[root@rhel5 home]# chage -l user1
Last password change : Nov 01,2013
Password expires : Nov 01,2014
Password inactive : Nov 15,2014
Account expires : Feb 18,2014
Minimum number of days between password change : 3
Maximum number of days between password change : 365
Number of days of warning before password expires : 10
[root@rhel5 home]# chage -E01/01/2015 user1 #修改user1使用者賬号的密碼在2015年1月1日過期
[root@rhel5 home]# tail -1 /etc/shadow
user1:!!:16010:3:365:10:14:16436:
[root@rhel5 home]# chage -l user1
Last password change : Nov 01,2013
Password expires : Nov 01,2014
Password inactive : Nov 15,2014
Account expires : Jan 01,2015
Minimum number of days between password change : 3
Maximum number of days between password change : 365
Number of days of warning before password expires : 10
建立組
指令名稱:groupadd(addgroup)
指令所在路徑:/usr/sbin/groupadd
執行權限:root組
功能描述:建立組
文法:groupadd [options] groupname
[root@rhel5 ~]# groupadd grp01
[root@rhel5 ~]#tail -3 /etc/group
linux:x:3004:mandrivauser01:x:3005:grp01:x:3006:
[root@rhel5 ~]#
常用選項:
-g:用指定的GID建立新組
[root@rhel5 ~]# groupadd -g 5000 grp02 #建立grp02組,GID=5000
[root@rhel5 ~]#tail -3 /etc/group
user01:x:3005:
grp01:x:3006:
grp02:x:5000:
[root@rhel5 ~]#
-r:建立系統組
[root@rhel5 ~]#groupadd –r DBA
[root@rhel5 ~]#tail -3 /etc/group
grp01:x:3006:
grp02:x:5000:
DBA:x:101: #可以看到DBA組的GID小于500,屬于系統組哈
添加、删除組成員
指令名稱:gpasswd
指令所在路徑:/usr/bin/gpasswd
功能描述:本來是用于設定組賬号的密碼,但很少使用,實際上該指令更多地用來添加和删除組成員
文法:gpasswd [選項] group
-a:添加組成員
[root@rhel5 home]#tail -3 /etc/group
centos:x:2006:
DBA:x:101:
user1:x:5002:
[root@rhel5 home]# gpasswd -a user1 centos #将使用者user1加入centos組
Adding user user1 to group centos
[root@rhel5 home]# id user1
uid=2003(user1) gid=5002(user1)groups=5002(user1),2006(centos) context=root:system_r:unconfined_t:SystemLow-SystemHigh[root@rhel5 home]# tail -3 /etc/group
centos:x:2006:user1DBA:x:101:user1:x:5002:
-d:删除組成員
[root@rhel5 home]#tail -3 /etc/group
centos:x:2006:user1DBA:x:101:user1:x:5002:
[root@rhel5 home]# gpasswd -d user1 centos
Removing user user1 from group centos #從centos組中删除使用者user1
[root@rhel5 home]# tail -3 /etc/group
centos:x:2006:
DBA:x:101:
user1:x:5002:
[root@rhel5 home]# id user1
uid=2003(user1) gid=5002(user1)groups=5002(user1) context=root:system_r:unconfined_t:SystemLow-SystemHigh
臨時設定基本組
指令名稱:newgrp
指令所在路徑:/usr/bin/newgrp
功能描述:臨時設定指定組為其基本組,若被指定的組不是該使用者的額外組,則在設定時需要該指定組的密碼
文法:newgrp [-] groupname
[root@rhel5 ~]# tail -3 /etc/group
centos:x:2006:
DBA:x:101:
user1:x:5002:
[root@rhel5 ~]# gpasswd centos #修改centos組的密碼
正在修改 centos 組的密碼新密碼:
請重新輸入新密碼:
[root@rhel5 ~]# su - jjx
[jjx@rhel5 ~]$ ls
b_d bin file01.txt file02.txt include kerberos lang.sh lib sbin scripts sudo test testgrp tmp
[jjx@rhel5 ~]$ cd tmp
[jjx@rhel5 tmp]$ ll
總計 20
drwxr-xr-x 2 root root 409601-0720:51 data
-rw-rw-r-- 1 jjx jjx 001-2011:29 hellgrp
drwxr-xr-x 3 root root 409612-2316:27 users
[jjx@rhel5 tmp]$ id
uid=500(jjx) gid=500(jjx) groups=0(root),500(jjx) context=root:system_r:unconfined_t:SystemLow-SystemHigh
[jjx@rhel5 tmp]$ newgrp centos #臨時設定centos組為使用者jjx的基本組
密碼:
[jjx@rhel5 tmp]$ id #可以看到jjx的基本組被設定成了centos哈
uid=500(jjx) gid=2006(centos) groups=0(root),500(jjx),2006(centos) context=root:system_r:unconfined_t:SystemLow-SystemHigh
[jjx@rhel5 tmp]$ touch hellogrp01 [jjx@rhel5 tmp]$ ll
總計 24
drwxr-xr-x 2 root root 409601-0720:51 data
-rw-rw-r-- 1 jjx jjx 001-2011:29 hellgrp
-rw-r--r-- 1 jjx centos 001-2011:34 hellogrp01 #可以看到hellogrp01的屬組為centos哈
drwxr-xr-x 3 root root 409612-2316:27 users
[jjx@rhel5 tmp]$ exit #退出centos組
exit
[jjx@rhel5 tmp]$ id #使用者jjx的基本組又變回原來的jjx組了哈
uid=500(jjx) gid=500(jjx) groups=0(root),500(jjx) context=root:system_r:unconfined_t:SystemLow-SystemHigh
修改組屬性
指令名稱:groupmod
指令所在路徑:/usr/sbin/groupmod
功能描述:修改組屬性
文法:groupmod [選項] groupname
-n:修改組名(注意:新組名不能與已有的組名相同,否則不能改名)
[root@rhel5 home]#id user1
uid=2003(user1) gid=5002(test)groups=5002(test),2006(centos) context=root:system_r:unconfined_t:SystemLow-SystemHigh
[root@rhel5 home]# groupmod -n user1 test #将test組名改為user1
[root@rhel5 home]# id user1
uid=2003(user1) gid=5002(user1)groups=5002(user1),2006(centos) context=root:system_r:unconfined_t:SystemLow-SystemHigh
[root@rhel5 home]# groupmod -n centos user1
groupmod: centos is not a unique name #提示centos的組名已經存在不能修改哈
-g:修改指定組的GID,如果被指定的GID存在而要強制修改,則與-o選項合用
[root@rhel5 home]#tail -1 /etc/group
51CTO:x:5004:
[root@rhel5 home]# groupmod -g5002 51CTO
groupmod: 5002 is not a unique GID #提示GID=5002已經存在,不能被指定哈
[root@rhel5 home]# groupmod -g5002-o 51CTO #強制指定GID=5002
[root@rhel5 home]# tail-3/etc/group
DBA:x:101:
user1:x:5002:
51CTO:x:5002:
删除組
指令名稱:groupdel
指令所在路徑:/usr/sbin/groupdel
功能描述:删除組(若删除群組為某些使用者的基本組,則必須先删除這些使用者後,方能删除群組)
[root@rhel5 home]# groupdel 51CTO
groupdel: cannot remove user's primary group.
[root@rhel5 home]#tail-3/etc/group
DBA:x:101:
user1:x:5002:
51CTO:x:5002: #因為51CTO組的GID與user1組的GID相同,是以不讓删除哈
[root@rhel5 home]# groupmod -g5003 51CTO
[root@rhel5 home]# groupdel 51CTO
[root@rhel5 home]# tail-3/etc/group
centos:x:2006:DBA:x:101:user1:x:5002: