Linux基礎_用戶組和權限管理

講師_@王曉春

▼用戶與組

  • 資源分派:
    Authentication:認證
    Authorization:授權
    Accouting|Audition:審計

  • 用戶user

    • Linux用戶:Username/UID
    • 管理員:root, 0
    • 普通用戶:1-60000 自動分配
      • 系統用戶:1-499, 1-999 (CentOS7)
        對守護進程獲取資源進行權限分配
      • 登錄用戶:500+, 1000+(CentOS7)
        交互式登錄
  • 組group

    • Linux組:Groupname/GID
    • 管理員組:root, 0
    • 普通組:
      • 系統組:1-499, 1-999(CENTOS7)
      • 普通組:500+, 1000+(CENTOS7)
  • Linux安全上下文

    • 運行中的程序:進程 (process)
    • 以進程發起者的身份運行:
      • root: /bin/cat
      • mage: /bin/cat
    • 進程所能夠訪問資源的權限取決於進程的運行者的身份
  • Linux組的類別

    • 用戶的主要組(primary group)
      • 用戶必須屬於一個且只有一個主組
      • 組名同用戶名,且僅包含一個用戶,私有組
    • 用戶的附加組(supplementary group)
      • 一個用戶可以屬於零個或多個附加組

▷用戶和組的配置文件

  • 主要配置文件:
    /etc/passwd:用戶及其屬性信息(名稱、UID、主組ID等)
    /etc/group:組及其屬性信息
    /etc/shadow:用戶密碼及其相關屬性
    /etc/gshadow:組密碼及其相關屬性

passwd文件格式

[root@centos7 ~]$cat /etc/passwd
chen:x:1000:1000:chen:/home/chen:/bin/bash
  1. login name:登錄用名(wang)
  2. passwd:密碼 (x)
  3. UID:用戶身份編號 (1000)
  4. GID:登錄默認所在組編號 (1000)
  5. GECOS:用戶全名或註釋
  6. home directory:用戶主目錄 (/home/wang)
  7. shell:用戶默認使用shell (/bin/bash)

在這裏插入圖片描述

  • 還可以使用命令vipw 相當於vi /etc/passwd

shadow文件格式

[root@centos7 ~]$cat /etc/shadow
chen:*::0:99999:7:::
  1. 登錄用名
  2. 用戶密碼:一般用sha512加密
  3. 從1970年1月1日起到密碼最近一次被更改的時間
  4. 密碼再過幾天可以被變更(0表示隨時可被變更)
  5. 密碼再過幾天必須被變更(99999表示永不過期)
  6. 密碼過期前幾天系統提醒用戶(默認爲一週)
  7. 密碼過期幾天後帳號會被鎖定
  8. 從1970年1月1日算起,多少天后帳號失效

在這裏插入圖片描述

  • 還可以使用命令vipw -s 相當於vi /etc/shadow

group文件格式

  • 羣組名稱:就是羣組名稱
  • 羣組密碼:通常不需要設定,密碼是被記錄在 /etc/gshadow。設置組密碼可以讓普通用戶加入組(只要知道密碼即可)
  • GID:就是羣組的 ID
  • 以當前組爲附加組的用戶列表(分隔符爲逗號)
    vigr相當於vi /etc/group

gshdow文件格式

  • 羣組名稱:就是羣的名稱
  • 羣組密碼:
  • 組管理員列表:組管理員的列表,更改組密碼和成員
  • 以當前組爲附加組的用戶列表:多個用戶間用逗號分隔
    vigr -s相當於vi /etc/gshadow

密碼加密

  • 單向加密:哈希算法,原文不同,密文必不同
    md5: message digest, 128bits
    sha1: secure hash algorithm, 160bits
    sha224: 224bits
    sha256: 256bits
    sha384: 384bits
    sha512: 512bits
  • 更改加密算法:
    authconfig --passalgo=sha256 --update
  • 生成隨機口令:
    openssl rand -base64 9
    mkpasswd (需要expect包:yum install expect
[root@centos7 data]$mkpasswd
H1an6Clu;
[root@centos7 data]$openssl rand -base64 8
mImNcw4VCjg=
  • 密碼的複雜性策略
    1.足夠長
    2.使用數字、大寫字母、小寫字母及特殊字符中至少3種
    3.使用隨機密碼
    4.定期更換,不要使用最近曾經使用過的密碼

密碼期限

[OPTION] 意義
-M 最大時間 max days
-m 最小時間 min days
-W 警告時間 warn days
-I 非活躍時間 inactive days
Mon 07Mon 14Mon 21Mon 28Mon 04Mon 11-M 最大時間 -m 最小時間 -W 警告時間 -I 非活躍時間 密碼期限

▼文件權限

pwck和grpck

  • 用戶管理命令
    1.useradd
    2.usermod
    3.userdel
    4.id
    5.su
    6.passwd
    7.chage
  • 組帳號維護命令
    1.groupadd
    2.groupmod
    3.groupdel
    4.gpasswd
    5.groupmems
    6.groups

▷用戶管理命令

●新建用戶的相關文件和命令

  • 相關文件
    /etc/default/useradd
    /etc/skel/*
    /etc/login.defs
  • newusers passwd格式文件 批量創建用戶
  • chpasswd 批量修改用戶口令

useradd用戶創建

useradd [OPTION] USERNAME

[OPTION] 意義
-u UID
-o 配合-u 選項,不檢查UID的唯一性
-g GID 指明用戶所屬基本組,可爲組名,也可以GID
-c "COMMENT“ 用戶的註釋信息
-d HOME_DIR 以指定的路徑(不存在)爲家目錄
-s SHELL 指明用戶的默認shell程序,可用列表在/etc/shells文件中
-G GROUP1[,GROUP2,...] 爲用戶指明附加組,組須事先存在
-N 不創建私用組做主組,使用users組做主組
-r 創建系統用戶 CentOS 6: ID<500,CentOS 7: ID<1000
-m 創建家目錄,用於系統用戶
-M 不創建家目錄,用於非系統用戶
`創建用戶ID爲1888,組ID爲100,描述爲“The tesxt user",家目錄指定在/home/textdir,指定shell爲nologin`
[root@centos7 ~]$useradd -u 1888 -g 100 -c "The text user" -d /home/textdir -s /bin/nologin/ test_user

[root@centos7 ~]$cat /etc/passwd | grep test
test_user:x:1888:100:The text user:/home/textdir:/bin/nologin/

  • 默認值設定:/etc/default/useradd
    useradd -D 顯示默認設置
    useradd –D [OPTION] 更改默認設置
    [OPTION]與上述相同
[root@centos7 ~]$useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes

[root@centos7 ~]$useradd -D -s /bin/nologin
[root@centos7 ~]$useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/nologin
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes

[root@centos7 ~]$useradd -D -b /root
[root@centos7 ~]$useradd -D
GROUP=100
HOME=/root
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes

usermod [OPTION] USERNAME“用戶屬性修改”

[OPTION] 意義
-u UID 新UID
-g GID 新主組
-G GROUP1[,GROUP2,...[,GROUPN]]] 新附加組,原來的附加組將會被覆蓋;若保留原有,則要同時使用-a選項(追加)
-s SHELL 新的默認SHELL
-c 'COMMENT' 新的註釋信息
-d HOME 新家目錄不會自動創建;若要創建新家目錄並移動原家數據,同時使用-m選項
-l login_name 新的名字
-L lock指定用戶,在/etc/shadow 密碼欄的增加 !
-U unlock指定用戶,將 /etc/shadow 密碼欄的 ! 拿掉
-e YYYY-MM-DD 指明用戶賬號過期日期
-f INACTIVE 設定非活動期限

userdel [OPTION]… USERNAME“刪除用戶”

[OPTION] 意義
-r 刪除用戶家目錄

id [OPTION]… [USER] “查看用戶相關的ID信息”

[OPTION] 意義
-u 顯示UID
-g 顯示GID
-G 顯示用戶所屬的組的ID
-n 顯示名稱,需配合ugG使用
[root@centos7 data]$id -u chen
1000
[root@centos7 data]$id -g chen
1000
[root@centos7 ~]$id -nu 0
root
[root@centos7 ~]$id -nu 1000
chen

su [options…] [-] [user [args…]] “切換用戶或以其他用戶身份執行命令”

  • 切換用戶的方式:
  1. su UserName:非登錄式切換,即不會讀取目標用戶的配置文件,不改變當前工作目錄
  2. su - UserName:登錄式切換,會讀取目標用戶的配置文件,切換至家目錄,完全切換
  • root su至其他用戶無須密碼;非root用戶切換時需要密碼

換個身份執行命令:
su [-] UserName -c 'COMMAND'
su -l UserName 相當於 su - UserName

passwd [OPTIONS] UserName “設置密碼”

passwd [OPTIONS] UserName: 修改指定用戶的密碼

常用選項 意義
-d 刪除指定用戶密碼
-l 鎖定指定用戶
-u 解鎖指定用戶
-e 強制用戶下次登錄修改密碼
-f 強制操作
-n mindays 指定最短使用期限
-x maxdays 最大使用期限
-w warndays 提前多少天開始警告
-i inactivedays 非活動期限
--stdin 從標準輸入接收用戶密碼
`示例:echo "PASSWORD" | passwd --stdin USERNAME`
[root@centos7 data]$echo "pw123" | passwd --stdin wang
Changing password for user wang.
passwd: all authentication tokens updated successfully.
  • passwd -e wang示例
    在這裏插入圖片描述

修改用戶密碼策略

chage [OPTION]… LOGIN

[OPTION] 意義
-d LAST_DAY
-E –expiredate EXPIRE_DATE
-I –inactive INACTIVE
-m –mindays MIN_DAYS
-M –maxdays MAX_DAYS
-W –warndays WARN_DAYS
-l 顯示密碼策略
`示例:`
chage -d 0 tom 	#下一次登錄強制重設密碼.相當於`passwd -e tom
chage -m 0 –M 42 –W 14 –I 7 tom
chage -E 2016-09-10 tom

用戶相關的其它命令

  • chfn 指定個人信息
  • chsh 指定shell
  • finger

●組的相關命令

groupadd [OPTION]… group_name “創建組”

[OPTION] 意義
-g GID 指明GID號;[GID_MIN, GID_MAX]
-r 創建系統組
[root@centos7 f_dir]$groupadd -g 8888 class
[root@centos7 f_dir]$cat /etc/group
...
class:x:8888:

groupmod [OPTION]… group “修改組”

[OPTION] 意義
-n group_name 新名字
-g GID 新的GID
[root@centos7 f_dir]$cat /etc/group
...
class:x:8888:
[root@centos7 f_dir]$groupmod -g 6666 class
[root@centos7 f_dir]$cat /etc/group
...
class:x:6666:

groupdel [OPTION] GROUP “刪除組”

groupdel GROUP

[root@centos7 f_dir]$groupdel class
[root@centos7 f_dir]$cat /etc/group
...
wang:x:1002:

gpasswd [OPTION] GROUP “更改組密碼”

[OPTION] 意義
-a user 將user添加至指定組中
-d user 從指定組中移除用戶user
-A user1,user2,… 設置有管理權限的用戶列表
[root@centos7 f_dir]$gpasswd -a wang class
Adding user wang to group class
[root@centos7 f_dir]$id wang
uid=1002(wang) gid=1002(wang) groups=1002(wang),8888(class)
  • newgrp命令:臨時切換主組
    如果用戶本不屬於此組,則需要組密碼

groupmems [options] [action] 更改和查看組成員

[OPTION] 意義
-g –group groupname 更改爲指定組 (只有root)
[Actions] 意義
- -
-a –add username 指定用戶加入組
-d –delete username 從組中刪除用戶
-p –purge 從組中清除所有成員
-l –list 顯示組成員列表
[root@centos7 ~]$groupmems -g webs -l
nginx  varnish
[root@centos7 ~]$groupmems -g webs -d nginx
[root@centos7 ~]$groupmems -g webs -l
varnish

groups [OPTION].[USERNAME]… 查看用戶所屬組列表

[root@centos7 f_dir]$groups wang
wang : wang

getent

  • 總結:
    在這裏插入圖片描述

▼文件權限

  • 文件屬性
    在這裏插入圖片描述

▷文件權限

  • 文件的權限主要針對三類對象進行定義
  1. owner 屬主, u
  2. group 屬組, g
  3. other 其他, o
  • 每個文件針對每類訪問者都定義了三種權限

對文件來說:

簡寫 全寫 意義
r Readable 可使用文件查看類工具獲取其內容
w Writable 可修改其內容
x eXcutable 可以把此文件提請內核啓動爲一個進程

對目錄來說

簡寫 全寫 意義
r Readable 可以使用ls查看此目錄中文件列表
w Writable 可在此目錄中創建文件,也可刪除此目錄中的文件(文件能不能刪除,由目錄決定)
x eXcutable 可以使用ls -l查看此目錄中文件元數據(須配合r),可以cd進入此。可以訪問已知文件名的文件內容。
X 只給目錄x權限,不給文件x權限
  • 文件權限表
權限項 執行 執行 執行
字符法 r w x r w x r w x
數字法 4 2 1 4 2 1 4 2 1
權限分配 文件所有者 文件所屬組 其它用戶
示例1 rw- -wx r-x
4+2= 6 2+1= 3 4+1= 5

▷文件屬性操作

chown “設置文件的所有者”

  • chown [OPTION]... [OWNER][:[GROUP]] FILE...
[OPTION] 意義
-R 遞歸
--reference=RFILE 引用RFILE的文件權限
#":"可以用"."代替
[root@centos7 data]$chown wang.chen f1
[root@centos7 data]$ll
total 76
-rw-r--r--. 1 wang chen  2814 May 16 00:12 f1

#chown [OPTION]... --reference=RFILE FILE...
[root@centos7 data]$chown --reference=f1 vim.txt
[root@centos7 data]$ll
total 76
-rw-r--r--. 1 wang chen  2814 May 16 00:12 f1
-rw-r--r--. 1 wang chen 68974 May 15 23:39 vim.txt

chgrp “設置文件的屬組信息”

  • chgrp [OPTION]… GROUP FILE…
[OPTION] 意義
-R 遞歸
--reference=RFILE 引用RFILE的文件權限

chmod “修改文件權限”

chmod [OPTION]... MODE[,MODE]... FILE...

[OPTION] 意義
-R 遞歸修改權限
--reference=RFILE 引用RFILE的文件權限
  1. 修改一類用戶的所有權限
    u= g= o= ug= a= u= g=
[root@centos7 data]$chmod u=rwx /data
  1. 修改一類用戶某位或某些位權限
    u+ u- g+ g- o+ o- a+ a-
[root@centos7 data]$chmod u+x /data

#參考RFILE文件的權限,將FILE的修改爲同RFILE
[root@centos7 link_dir]$chmod 777 f1
[root@centos7 link_dir]$ll
total 0
-rwxrwxrwx. 1 root root 0 May 16 04:27 f1
-rw-r--r--. 1 root root 0 May 16 04:27 f2
[root@centos7 link_dir]$chmod --reference=f1 f2
[root@centos7 link_dir]$ll
total 0
-rwxrwxrwx. 1 root root 0 May 16 04:27 f1
-rwxrwxrwx. 1 root root 0 May 16 04:27 f2

#示例
#設置用戶增加“寫、執行”權限;組刪除“讀”權限;其它人權限爲“讀執行”
chmod u+wx,g-r,o=rx file
#設置目錄testdir以及目錄下的文件,組有“讀寫執行(只對目錄有執行)”
chmod -R g+rwX /testdir

▷新建文件和目錄的默認權限(umask)

umask值 可以用來保留在創建文件權限

  • 新建文件的默認權限: 666-umask,如果所得結果某位存在執行(奇數)權限,則將其權限+1
  • 新建目錄的默認權限: 777-umask
  • 非特權用戶umask是 002
  • root的umask 是 022

示例:

#模式方式顯示
[root@centos7 ~]$umask -S
u=rwx,g=rx,o=rx
#輸出可被調用
[root@centos7 /data]$umask -p
umask 0022
[root@centos7 ~]$umask
0022

#設置umask
umask 002
umask u=rw,g=r,o=
  • 全局設置: /etc/bashrc 用戶設置:~/.bashrc

  • 默認權限計算方法:777-umask
    出於安全考慮,文件需要去除x權限,目錄不變。
    即文件結果的奇數位時需-1。

示例:

序號 umask 目錄 文件
1 002 777-002=775 777-002 = 775-001 = 774
2 122 777-132=645 777-132 = 655-011 = 644
3 444 777-444=333 777-444 = 333-111 = 222

▷Linux文件系統上的特殊權限(SUID, SGID, Sticky)

(1) 任何一個可執行程序文件能不能啓動爲進程,取決發起者對程序文件是否擁有執行權限
(2) 啓動爲進程之後,其進程的屬主爲發起者,進程的屬組爲發起者所屬的組
(3) 進程訪問文件時的權限,取決於進程的發起者
(a) 進程的發起者,同文件的屬主:則應用文件屬主權限
(b) 進程的發起者,屬於文件屬組;則應用文件屬組權限
© 應用文件“其它”權限

1.SUID “可執行文件上的權限”

  • 任何一個可執行程序文件能不能啓動爲進程:取決發起者對程序文件是否擁有執行權限
  • 啓動爲進程之後,其進程的屬主爲原程序文件的屬主
  • SUID只對二進制可執行程序有效
  • SUID設置在目錄上無意義
  • 權限設定:
chmod u+s FILE...
chmod u-s FILE...

2. SGID “可執行文件上的權限”

  • 任何一個可執行程序文件能不能啓動爲進程:取決發起者對程序文件是否擁有執行權限
  • 啓動爲進程之後,其進程的屬組爲原程序文件的屬組
  • 權限設定:
chmod g+s FILE...
chmod g-s FILE...
  • 目錄上的SGID權限
  • 默認情況下,用戶創建文件時,其屬組爲此用戶所屬的主組
  • 一旦某目錄被設定了SGID,則對此目錄有寫權限的用戶在此目錄中創建的文件所屬的組爲此目錄的屬組
  • 通常用於創建一個協作目錄
  • 權限設定:
chmod g+s DIR...
chmod g-s DIR...
[wang@centos7 test_dir]$ touch f1
[root@centos7 test_dir]$ll
total 0
-rw-rw-r--. 1 wang wang 0 May 16 05:01 f1

[root@centos7 test_dir]$chmod g+s /data/test_dir

[wang@centos7 test_dir]$ touch f2
[wang@centos7 test_dir]$ ll
total 0
-rw-rw-r--. 1 wang wang 0 May 16 05:01 f1
-rw-rw-r--. 1 wang root 0 May 16 05:01 f2
[wang@centos7 test_dir]$

3. Sticky 位

  • 具有寫權限的目錄通常用戶可以刪除該目錄中的任何文件,無論該文件的權限或擁有權
  • 在目錄設置Sticky 位,只有文件的所有者或root可以刪除該文件
  • sticky 設置在文件上無意義
  • 權限設定:
chmod o+t DIR...
chmod o-t DIR...
[root@centos7 test_dir]$chmod o+t .
[root@centos7 test_dir]$su wang
[wang@centos7 test_dir]$ touch f1

#用戶nginx無法刪除wang用戶的文件
[nginx@centos7 test_dir]$ rm -f f1
rm: cannot remove ‘f1’: Operation not permitted
  • 數字法
SUID SGID STICKY 合計
4 2 1 7

在這裏插入圖片描述

  • 權限位映射
    • SUID: user,佔據屬主的執行權限位
      s:屬主擁有x權限
      S:屬主沒有x權限
    • SGID: group,佔據屬組的執行權限位
      s: group擁有x權限
      S:group沒有x權限
    • Sticky: other,佔據other的執行權限位
      t:other擁有x權限
      T:other沒有x權限

chattr “設定文件特定屬性”

chattr +i不能刪除,改名,更改
chattr +a 只能追加內容
lsattr 顯示特定屬性

[root@centos7 test_dir]$chattr +i f1
[root@centos7 test_dir]$lsattr
---------------- ./f3
----i----------- ./f1

[root@centos7 test_dir]$su wang
[wang@centos7 test_dir]$ mv f1 f111
mv: cannot move ‘f1’ to ‘f111’: Operation not permitted
[wang@centos7 test_dir]$

▷訪問控制列表ACL

  • ACL:Access Control List,實現靈活的權限管理
  • 除了文件的所有者,所屬組和其它人,可以對更多的用戶設置權限
  • CentOS7 默認創建的xfs和ext4文件系統具有ACL功能
  • CentOS7 之前版本,默認手工創建的ext4文件系統無ACL功能,需手動增加,方法如下:
    tune2fs –o acl /dev/sdb1
    mount –o acl /dev/sdb1 /mnt/test
  • ACL生效順序:所有者,自定義用戶,自定義組,其他人
  • 所屬組的權限變成mask的權限
  • 所有權限不能超mask權限(除了user所有者的權限)
    在這裏插入圖片描述
    示例:
  1. getfacl file |directory #獲取文件的ACL
[wang@centos7 test_dir]$ getfacl f1
# file: f1
# owner: wang
# group: root
user::rw-
group::rw-
other::r--

setfacl [OPTION] FILE “設置文件ACL”

[OPTION] 意義
-m 更改文件的訪問控制列表
-M 從文件讀取訪問控制列表條目更改
-x 根據文件中訪問控制列表移除條目
-X 從文件讀取訪問控制列表條目並刪除
-b 刪除所有擴展的acl規則,基本的acl規則(所有者,羣組,其他)將被保留。
-k 刪除缺省的acl規則。如果沒有缺省規則,將不提示。
-n 不要重新計算有效權限。setfacl默認會重新計算ACL mask,除非mask被明確的制定。
--mask 重新計算有效權限,即使ACL mask被明確指定。
-d 目錄設定默認的acl規則。
--restore=file 從文件恢復備份的acl規則(這些文件可由getfacl -R產生)。通過這種機制可以恢復整個目錄樹的acl規則。此參數不能和除–test以外的任何參數一同執行。
--test 測試模式,不會改變任何文件的acl規則,操作後的acl規格將被列出。
-R 遞歸的對所有文件及目錄進行操作。
-L 跟蹤符號鏈接,默認情況下只跟蹤符號鏈接文件,跳過符號鏈接目錄。
-P 跳過所有符號鏈接,包括符號鏈接文件。
--version 輸出setfacl的版本號並退出。
--help 輸出幫助信息。
-- 標識命令行參數結束,其後的所有參數都將被認爲是文件名
- 如果文件名是-,則setfacl將從標準輸入讀取文件名。

在這裏插入圖片描述
一些示例

  1. setfacl -m u:wang:rwx file #給文件wang用戶設置rwx
[root@centos7 test_dir]$setfacl -m u:wang:rwx f11
[root@centos7 test_dir]$getfacl f11
# file: f11
# owner: root
# group: root
user::rw-
user:wang:rwx
group::r--
mask::rwx
other::r--
  1. setfacl -Rm g:sales:rwX directory #設置目錄下所有的文件有給組sales有rwX權限
  2. setfacl -M file.acl file #從文件讀取訪問控制列表條目更改
  3. setfacl -m g:sales:rw file #設置該目錄下的文件有給組sales有rw權限
[root@centos7 test_dir]$setfacl -m g:webs:rwx f1
[root@centos7 test_dir]$getfacl f1
# file: f1
# owner: root
# group: root
user::rw-
group::r--
group:webs:rwx
mask::rwx
other::r--
  1. setfacl -m d:u:wang:rx directory #d表示默認,只能給目錄使用
[root@centos7 test_dir]$setfacl -m d:u:wang:rwx .
[root@centos7 test_dir]$getfacl .
# file: .
# owner: root
# group: root
user::rwx
group::rwx
other::rwx
default:user::rwx
default:user:wang:rwx
default:group::rwx
default:mask::rwx
default:other::rwx
  1. setfacl -x u:wang file #根據文件中訪問控制列表移除條目
[root@centos7 test_dir]$setfacl -m u:wang:rwx f1
[root@centos7 test_dir]$ll
total 0
-rw-rwxr--+ 1 root root 0 May 16 16:50 f1
-rw-rw-rw-+ 1 root root 0 May 16 16:51 f2
-rw-rw-rw-. 1 wang wang 0 May 16 16:52 f3
[root@centos7 test_dir]$getfacl f1
# file: f1
# owner: root
# group: root
user::rw-
user:wang:rwx
group::r--
group:webs:rwx
mask::rwx
other::r--

[root@centos7 test_dir]$setfacl -x u:wang f1
[root@centos7 test_dir]$getfacl f1
# file: f1
# owner: root
# group: root
user::rw-
group::r--
group:webs:rwx
mask::rwx
other::r-
  1. setfacl -X file.acl directory #從文件讀取訪問控制列表條目並刪除

  2. setfacl -b /bin/bash #清楚所有的acl規則

[root@centos7 test_dir]$getfacl f11
# file: f11
# owner: root
# group: root
user::rw-
user:wang:rwx
group::r--
mask::rwx
other::r--

[root@centos7 test_dir]$setfacl -b f11
[root@centos7 test_dir]$getfacl f11
# file: f11
# owner: root
# group: root
user::rw-
group::r--
other::r--
  • ACL文件上的group權限是mask 值(自定義用戶,自定義組,擁有組的最大權限),而非傳統的組權限

  • getfacl 可看到特殊權限:flags

  • 通過ACL賦予目錄默認x權限,目錄內文件也不會繼承x權限

  • base ACL #不能刪除

  • setfacl -k dir # 刪除默認ACL權限

  • setfacl –b file #清除所有ACL權限

  • getfacl file1 | setfacl --set-file=- file2 #複製file1的acl權限給file2

  • mask隻影響除所有者和other的之外的人和組的最大權限

  • Mask需要與用戶的權限進行邏輯與運算後,才能變成有限的權限(Effective Permission)
    用戶或組的設置必須存在於mask權限設定範圍內纔會生效
    setfacl -m mask::rx file

  • –set選項會把原有的ACL項都刪除,用新的替代,需要注意的是一定要包含UGO的設置,不能象-m一樣只是添加ACL就可以
    示例:
    setfacl --set u::rw,u:wang:rw,g::r,o::- file1

  • 備份和恢復ACL
    主要的文件操作命令cp和mv都支持ACL,只是cp命令需要加上-p 參數。但是tar等常見的備份工具是不會保留目錄和文件的ACL信息

備份:getfacl -R /tmp/dir1 > acl.txt
刪除:setfacl -R -b /tmp/dir1
恢復:setfacl -R --set-file=acl.txt /tmp/dir1
	#方式2:setfacl --restore acl.txt
查看:getfacl -R /tmp/dir1
  • 總結
    在這裏插入圖片描述

練習

  1. 創建用戶gentoo,附加組爲bin和root,默認shell爲/bin/csh,註釋信息爲"Gentoo Distribution"
  2. 創建下面的用戶、組和組成員關係
    名字爲webs 的組
    用戶nginx,使用webs 作爲附加組
    用戶varnish,使用webs 作爲附加組
    用戶mysql,不可交互登錄系統,且不是webs 的成員,nginx,varnish,mysql密碼都是magedu
  3. 當用戶docker對/testdir 目錄無執行權限時,意味着無法做哪些操作?
  4. 當用戶mongodb對/testdir 目錄無讀權限時,意味着無法做哪些操作?
  5. 當用戶redis 對/testdir 目錄無寫權限時,該目錄下的只讀文件file1是否可修改和刪除?
  6. 當用戶zabbix對/testdir 目錄有寫和執行權限時,該目錄下的只讀文件file1是否可修改和刪除?
  7. 複製/etc/fstab文件到/var/tmp下,設置文件所有者爲tomcat讀寫權限,所屬組爲apps組有讀寫權限,其他人無權限
  8. 誤刪除了用戶git的家目錄,請重建並恢復該用戶家目錄及相應的權限屬性
  9. 在/testdir/dir裏創建的新文件自動屬於webs組,組apps的成員如:tomcat能對這些新文件有讀寫權限,組dbs的成員如:mysql只能對新文件有讀權限,其它用戶(不屬於webs,apps,dbs)不能訪問這個文件夾
  10. 備份/testdir/dir裏所有文件的ACL權限到/root/acl.txt中,清除/testdir/dir中所有ACL權限,最後還原ACL權限

練習答案

  1. 創建用戶gentoo,附加組爲bin和root,默認shell爲/bin/csh,註釋信息爲"Gentoo Distribution"
[root@centos7 ~]$useradd -G bin,root -s /bin/csh -c "Gentoo Distribution" gentoo

`檢驗`
[root@centos7 ~]$cat /etc/passwd |grep gentoo
gentoo:x:1001:1001:Gentoo Distribution:/home/gentoo:/bin/csh
[root@centos7 ~]$id gentoo
uid=1001(gentoo) gid=1001(gentoo) groups=1001(gentoo),0(root),1(bin)
[root@centos7 ~]$
  1. 創建下面的用戶、組和組成員關係
    名字爲webs 的組
    用戶nginx,使用webs 作爲附加組
    用戶varnish,使用webs 作爲附加組
    用戶mysql,不可交互登錄系統,且不是webs 的成員,nginx,varnish,mysql密碼都是magedu
[root@centos7 ~]$groupadd webs
[root@centos7 ~]$useradd -G webs nginx
[root@centos7 ~]$useradd -G webs varnish
[root@centos7 ~]$useradd -s /bin/nologin mysql

[root@centos7 ~]$

  1. 當用戶docker對/testdir 目錄無執行權限時,意味着無法做哪些操作?
無法進入目錄
  1. 當用戶mongodb對/testdir 目錄無讀權限時,意味着無法做哪些操作?
無法獲取目錄下的文件
  1. 當用戶redis 對/testdir 目錄無寫權限時,該目錄下的只讀文件file1是否可修改和刪除?
不能
  1. 當用戶zabbix對/testdir 目錄有寫和執行權限時,該目錄下的只讀文件file1是否可修改和刪除?
不能
  1. 複製/etc/fstab文件到/var/tmp下,設置文件所有者爲tomcat讀寫權限,所屬組爲apps組有讀寫權限,其他人無權限
[root@centos7 ]$cp /etc/fstab /var/tmp/
[root@centos7 ]$ll
total 4
-rw-r--r--. 1 root root 595 May 16 20:37 fstab
[root@centos7 ]$chown tomcat fstab
[root@centos7 ]$chgrp apps fstab
[root@centos7 ]$chmod u=rw,g=rw,o= fstab
[root@centos7 ]$ll fstab
-rw-rw----. 1 tomcat apps 595 May 16 20:37 fstab
  1. 誤刪除了用戶git的家目錄,請重建並恢復該用戶家目錄及相應的權限屬性
[root@centos7 test_dir]$cp -r /etc/skel /home/git
cp: omitting directory ‘/etc/skel’
[root@centos7 test_dir]$chmod u=rwx,g=,o= /home/git
[root@centos7 test_dir]$chown git /home/git
chown: invalid user: ‘git’
[root@centos7 test_dir]$chgrp git /hoem/git
chgrp: invalid group: ‘git’
  1. 在/testdir/dir裏創建的新文件自動屬於webs組,組apps的成員如:tomcat能對這些新文件有讀寫權限,組dbs的成員如:mysql只能對新文件有讀權限,其它用戶(不屬於webs,apps,dbs)不能訪問這個文件夾
[root@centos7 data]$useradd tomcat
[root@centos7 data]$useradd mysql
[root@centos7 data]$groupadd webs
[root@centos7 data]$groupadd dbs
[root@centos7 data]$groupadd apps
[root@centos7 data]$usermod -G webs tomcat
[root@centos7 data]$usermod -G dbs mysql

[root@centos7 data]$mkdir dir
[root@centos7 data]$chgrp webs dir
[root@centos7 data]$chmod g+ws dir
[root@centos7 data]$ll
drwxrwsr-x. 2 root webs     6 May 16 22:30 dir


[root@centos7 data]$setfacl -m g:apps:rwx dir
[root@centos7 data]$setfacl -m g:dbs:r-- dir
[root@centos7 data]$setfacl -m o::--- dir
[root@centos7 data]$setfacl -m d:g:apps:rwx dir
[root@centos7 data]$setfacl -m d:g:webs:rwx dir
[root@centos7 data]$setfacl -m d:g:dbs:r dir
[root@centos7 data]$chmod o= dir
[root@centos7 data]$ll
drwxrws---+ 2 root webs     6 May 16 22:30 dir
[root@centos7 data]$getfacl dir
#file: dir
#owner: root
#group: webs
#flags: -s-
user::rwx
group::rwx
group:apps:rwx
group:dbs:r--
mask::rwx
other::---
default:user::rwx
default:group::rwx
default:group:webs:rwx
default:group:apps:rwx
default:group:dbs:r--
default:mask::rwx
default:other::---

  1. 備份/testdir/dir裏所有文件的ACL權限到/root/acl.txt中,清除/testdir/dir中所有ACL權限,最後還原ACL權限
#查看並匹配
getfacl -R /testdir/dir >dir.acl
#刪除ACL
setfacl -R -b /testdir/dir
#恢復ACL
setfacl -R --set-file=dir.acl /testdir/dir
#查看恢復的ACL
getfacl -R /testdir/dir
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章