天天看點

使用者管理,權限管理

/etc/passwd檔案總共分為7部分

使用者名:使用者密碼:UID:GID:使用者的描述:使用者的家目錄:登入shell

/etc/shadow檔案總共分為9部分

使用者名:密碼位:密碼上一次修改時間:密碼最小存活時間:密碼最大存活時間:密碼過期之前多少天提醒使用者:密碼過期之後多少天使用者過期:賬戶過期時間(與密碼上一次修改時間無關):保留位

id ----------------------------------檢視使用者的資訊(使用者名,UID,GID,附加組等資訊)

useradd&adduser ------>建立使用者指令

-u:指定使用者的UID

[root@centos7 ~]# useradd -u 2000 user1 [root@centos7 ~]# id user1 uid=2000(user1) gid=2000(user1) groups=2000(user1)

-g:指定使用者的主組

[root@centos7 ~]# useradd -g user1 user2 [root@centos7 ~]# id user2 uid=2001(user2) gid=2000(user1) groups=2000(user1)

-G:指定使用者的附加組資訊

[root@centos7 ~]# useradd -G user1 user3 [root@centos7 ~]# id user3 uid=2002(user3) gid=2002(user3) groups=2002(user3),2000(user1)

-c:使用者的描述資訊

[root@centos7 ~]# useradd -c "ceshixinxi" user4 [root@centos7 ~]# cat /etc/passwd | grep user4 user4:x:2003:2003:ceshixinxi:/home/user4:/bin/bash

-s:指定使用者的登入shell

[root@centos7 ~]# useradd -s /sbin/nologin user5 [root@centos7 ~]# grep user5 /etc/passwd user5:x:2004:2004::/home/user5:/sbin/nologin

-d:指定使用者的家目錄

[root@centos7 ~]# useradd -d /user6 user6

-o:不檢查UID的唯一性,相當于建立有兩個登入名的使用者

[root@centos7 user6]# useradd -o -u 2000 user7 [root@centos7 user6]# id user1 [root@centos7 user6]# id user7

-N:不建立私有組,指定users組做為主組

[root@centos7 user6]# grep user8 /etc/passwd user8:x:2006:100::/home/user8:/bin/bash

-r:建立系統使用者(CentOS 6之前的 <500,Centos 7 <1000)

[root@centos7 user6]# useradd -r user9 [root@centos7 user6]# grep user9 /etc/passwd user9:x:991:986::/home/user9:/bin/bash

usermod -------------------->更改使用者的資訊

-u newUID user:修改使用者的uid

[root@centos7 user6]# usermod -u 3000 user1 uid=3000(user1) gid=2000(user1) groups=2000(user1)

-g newGID user:修改使用者名的主組

[root@centos7 user6]# usermod -g 2002 user1 uid=3000(user1) gid=2002(user3) groups=2002(user3)

-G newGID user:修改使用者的附加組

[root@centos7 user6]# usermod -G 2006 user1 uid=3000(user1) gid=2002(user3) groups=2002(user3),2006(user7)

-a 與G配合使用,在添加附加組的時候,不會影響之前的附加組

-s shell名 user:設定使用者的登入shell

-c "描述資訊" user:修改使用者的描述資訊

-d /newname 使用者名 :修改使用者的家目錄,新家目錄不會自動建立;若要建立新家目錄并移動原家資料,同時使用-m選項

l newname oldname:修改使用者名

-L user:鎖定使用者,相當于在/etc/shadow檔案中的密碼位前添加!

[root@centos7 user6]# usermod -L user1 [root@centos7 user6]# grep user1 /etc/shadow user1:!$6$PsV1.zv4$dGgmKQwIA1Hed/PkSqFdWApsysJ.3t3a4kndhC48fEftgmAauS9cFE2dyhpJ.fhfFAe/ki5LqZogFOHGAAq8b/:17551:0:99999:7:::

-U user:解鎖使用者

[root@centos7 user6]# usermod -U user1 user1:$6$PsV1.zv4$dGgmKQwIA1Hed/PkSqFdWApsysJ.3t3a4kndhC48fEftgmAauS9cFE2dyhpJ.fhfFAe/ki5LqZogFOHGAAq8b/:17551:0:99999:7:::

-e YYYY-MM-DD:指明使用者賬戶過期日期

[root@centos7 user6]# usermod -e 2019-02-20 user1 user1:$6$PsV1.zv4$dGgmKQwIA1Hed/PkSqFdWApsysJ.3t3a4kndhC48fEftgmAauS9cFE2dyhpJ.fhfFAe/ki5LqZogFOHGAAq8b/:17551:0:99999:7::17947:

userdel -------------------->删除使用者

-r 使用者名:删除使用者及其和使用者有關的所有檔案

passwd -------------------->設定密碼

passwd username :指定要修改密碼的使用者名(僅root可以使用)

-l :鎖定使用者

-u:解鎖使用者

-e:強制使用者下次登入更改密碼

-n:指定密碼最短使用期限

-x:指定密碼最大使用期限

-w:提前多少天開始警告

--stdin 從标準輸入接收使用者面

舉例:echo 123456 |passwd --stdin root

chage --------------------->用來修改密碼政策

-m:密碼最小使用天數,如果為零将不受限制,随意更改。

-M:密碼最大使用天數。

-w:密碼過期前的多少天提醒使用者。

-E:賬戶過期時間,如果超過該天數,賬戶将不可使用。

-d:上一次更改的日期。

-i:停滞時期。如果一個密碼已過期這些天,那麼此帳号将不可用。

-l:顯示出目前使用者的密碼政策。

/etc/group檔案分為四部分

組名:密碼位:GID:附加組

/etc/gshadow檔案分為四部分

組名:密碼:管理者:組成員

**groupadd ------------------->用來建立組

-g GID 組名:建立指定GID的組

[root@centos7 user6]# groupadd -g 3000 admins [root@centos7 user6]# grep admins /etc/group admins:x:3000:

groupmod------------------->用來修改組資訊

-n newname 組名:修改組名

-g newGID 組名:修改GID

groupdel ------------------->用來删除組

groupdel 組名:删除組

gpasswd ----------------------->用來設定組密碼

gpasswd 組名:設定組密碼

-a username 組名:将某使用者以附加組加入到該組(管理者就可以使用)

-A username 組名:用來設定某組的管理者(僅root使用)

-d username 組名:将某個使用者從改組中删除(管理者就可以使用)

-M username 組名:設定某族的使用者清單(僅root使用)

相關指令

newgrp 組名:用來臨時改變主組,若改使用者附屬組屬于改組,則不用輸入密碼,若不屬于,則需要輸入密碼。

groupmems ---------------------用來修改使用者的附加組

-g 組名 -a username:将某使用者以附加組的方式加入到某組中

-g 組名 -d username:将某使用者從該組中删除

-g 組名 -p :清空所有組成員

-g 組名 -l:列出該組中有哪些成員

groups 使用者名 : 檢視指定使用者屬于那些組

chown------------------------------------用來更改檔案或目錄所屬人,也可以用來更改檔案或目錄的所屬組

chown 使用者名:組名

chown 使用者名.組名

-R 遞歸

--reference=file1 file2:複制所屬組

chgrp 組名 -----------------------------用來更改檔案或目錄的所屬組

權限的作用對象:owner(所屬人u)、group(所屬組g)、other(其他人o)

普通權限分為:Read(讀:r:4)、Write(寫:w:2)eXe(執行:x:1)

設定權限的指令:chmod

chmod u+rwx file :給file檔案所屬人添加rwx權限

chmod u=rwx file:給file檔案所屬人賦予rwx權限

chmod u-rwx file:給file檔案所屬人減去rwx權限

chmod g+rwx file :給file檔案所屬組添加rwx權限

chmod g=rwx file:給file檔案所屬組賦予rwx權限

chmod g-rwx file:給file檔案所屬組減去rwx權限

chmod o+rwx file :給file其他人添加rwx權限

chmod o=rwx file:給file其他人賦予rwx權限

chmod o-rwx file:給file其他人減去rwx權限

chmod a+rwx file:給全部人添加rwx權限

chmod a-rwx file:給全部人減去rwx權限

chmod a=rwx file:給全部人賦予rwx權限

chmod 777 file:給全部人添加rwx權限,第一位代表所有人,第二位代表所屬組,第三位代表其他人。

權限的作用在檔案時:

1)當僅有r權限時,使用者可以讀取檔案内容。

2)當僅有W權限時,使用者可以修改内容但是隻可以覆寫和追加。

3)當僅有x權限時,無作為。

4)當有rw權限時,使用者可以讀寫檔案。

5)當有rx權限時,使用者可以讀檔案和執行檔案。

6)當有wx時,權限等同于僅有W權限。

常見權限有r--- rw- rwx

權限作用在目錄上時:

1)當僅有r權限時,使用者可以短列出檔案名

2)當僅有w權限時,使用者無作為。

3)當僅有x權限時,使用者可以進入目錄,并且在知道檔案目錄的情況下可以通路子檔案。

4)當有rw權限時,權限等于僅有r。

5)當有rx全是時,使用者進入,長列出

6)當有wx權限時,使用者進入可以建立删除檔案,但是不能使用通配符

目錄常見權限r-x rwx

進階權限分為:Suid、Sgid、Sticky

chmod u+s file

Suid:指加入該權限後(作用在一個二進制檔案上),當執行一個二進制檔案權限會臨時更改為該二進制檔案的所屬人。

Sgid:指加入該權限後(作用在目錄或二進制檔案),當加入該權限後,對于二進制檔案來說,會臨時的擁有所屬組的權限,對與一個目錄來說,當加入該權限,在該目錄下建立檔案或目錄,所屬組會自動變成該組。

Sticky:當對一個目錄加了 該權限時,僅檔案所屬人,目錄所屬人及其root才能删除,其他人不能删除。

chattr:

+a:不能删除,不能改名,不能修改,可以追加

[root@centos7 app]# chattr +a 1.txt [root@centos7 app]# rm -rf 1.txt rm: cannot remove ‘1.txt’: Operation not permitted [root@centos7 app]# mv 1.txt 2.txt mv: cannot move ‘1.txt’ to ‘2.txt’: Operation not permitted [root@centos7 app]# touch 123 > 1.txt -bash: 1.txt: Operation not permitted [root@centos7 app]# touch 123 >> 1.txt

+i:不能删除,不能改名,不能修改,不能追加。

[root@centos7 app]# chattr +i 1.txt [root@centos7 app]# lsattr 1.txt ----i----------- 1.txt

lsattr:檢視權限。

setfacl:用來在指令行裡設定權限,可以針對使用者,組來設定權限

acl 設定格式setfacl -m u:user:rwx file

前面選項:

-m:添加acl

-X:批量導入

-d:設定預設權限

-k:清除預設權限

-b:清楚acl屬性

後面選項:

-u:指定使用者權限

-g:指定附加組權限

-m:指定上線門檻值

-o:指定其他人權限

舉例使用:

setfacl -m u:user:rwx file :給user添加rwx

setfacl -m g:user:rwx file:給user組添加rwx

setfacl -m d:u:user:rwx file:添加預設acl權限

setfacl -X file.acl file :将file.acl檔案中權限給file檔案

setfacl -k file :清除預設權限

setfacl -b file :清除acl屬性

getfacl:用來檢視設定的acl權限。

[root@centos7 app]# getfacl 1.txt

#file: 1.txt #owner: root #group: root user::rw- group::r-- other::r--

繼續閱讀