# Account Management & ACL Permission setting ###### tags: `centos7` `linux` [TOC] # Account Management ## User Account Manage cmd:useradd  ### Create Group 創建 Group ``` [root@meowhecker centos]# groupadd group1 ``` Group File ``` [root@meowhecker centos]# cat /etc/group group1:x:1001: ``` groupName:password Path:group ID 從 1001 遞增 Delete Group ``` [root@meowhecker centos]# groupdel group1 ``` ### Create User Account flager -c command -g main ground(GID) -G 次要grounp -e expire (account 過期日) -u 指定uid ``` [root@localhost home]# ll -d /home/meowhecker/ drwx------ 3 meowhecker meowhecker 78 Jun 7 03:05 /home/meowhecker/ ``` Directory permission -> 700 -> owner 可以對directory 進行操作 預設會建立一個與帳號一模一樣的群組名 ``` [root@localhost home]# grep meowhecker /etc/passwd /etc/shadow /etc/group /etc/passwd:meowhecker:x:1001:1001::/home/meowhecker:/bin/bash /etc/shadow:meowhecker:!!:19515:0:99999:7::: /etc/group:meowhecker:x:1001: ``` (Default) Home Directory Group ``` [root@meowhecker centos]# groupadd meow1 [root@meowhecker centos]# groupadd meow2 [root@meowhecker centos]# useradd meowmeow -c "cat" -g meow1 -G meow2 [root@meowhecker centos]# id meowmeow uid=1002(meowmeow) gid=1002(meow1) groups=1002(meow1),1003(meow2) ``` ### Setting Password #### Interaction mode ``` passwd <userAccount> ``` #### non-interaction mode(For Script) ``` [root@meowhecker centos]# echo "meow" | passwd meowmeow --stdin Changing password for user meowmeow. passwd: all authentication tokens updated successfully. ``` ### adduser Script (大量建置帳號的方法) 在編寫scrip 會用non-interaction 方式 e.g. ```sh for user in meow1 meow2 meow3 meow4; do id "$user" >/dev/null 2>&1 if [ "$?" -ne "0" ]; then echo "$user" | passwd "$user" --stdin echo "$user added to the system!" fi done ``` -ne negation(比較 operation) fi 是 if 結束(應該可以想成else 的東西) do 跟 done 可以想成 for 的{} --- ### /etc/passwd(user/systems account) ``` meowhecker:x:1001:1001::/home/meowhecker:/bin/bash ``` username : x :uid:gid:command:home directory:shell 停用帳號 可以在 x 前面加! 或把shell 改成 nologin /etc/shadow(user/systems password) ``` [root@meowhecker centos]# cat /etc/shadow root:$1$01sQLh/a$sCvCQvUGA03IvN1Qx7ssS0:19437:0:99999:7::: ``` 經過hash過的password $1$01sQLh/a$sCvCQvUGA03IvN1Qx7ssS0 system Account: 通常是不能登入(nologin) Root uid = 0 ## File/Director permission Directory permission ``` [centos@meowhecker ~]$ ls -ld meowdir/ drwxrwxr-x 2 centos centos 42 May 15 08:57 meowdir/ ``` ## 外部身份認證系統 # 細部權限規劃(ACL)的使用 access control list 主要針對 owner,group,others 的 read,write,execute 權限之外的細部權限設定 -> 可以針對單一user 跟Directory 來進行設定 ## 查看Systems 是否支援 ACL ``` [centos@localhost ~]$ dmesg | grep -i acl [ 8.371121] systemd[1]: systemd 219 running in system mode. (+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN) [ 16.009957] SGI XFS with ACLs, security attributes, no debug enabled ``` ## ACL setting (ACL 設定) getfacl -> 取得ACL 的設定項目 setfacl -> 設定ACL 項目 ### 針對特定使用者 格式 u:使用者:permission -m 設定後續ACL 給檔案使用 ``` [centos@localhost ~]$ touch test_acl1 [centos@localhost ~]$ ll test_acl1 -rw-rw-r-- 1 centos centos 0 Jun 8 02:11 test_acl1 [centos@localhost ~]$ setfacl -m u:meowhecker:rw test_acl1 [centos@localhost ~]$ ll test_acl1 -rw-rwxr--+ 1 centos centos 0 Jun 8 02:11 test_acl1 ``` owner group other 後面有個 + 表示 有ACL 設定 ### 查看 ACL 權限設定 gatfacl ``` [centos@localhost ~]$ getfacl test_acl1 # file: test_acl1 檔名 # owner: centos # group: centos user::rw- user:meowhecker:r-x # 針對meowhecker 設定的權限 group::rw- mask::rwx #最大允許的權限 other::r-- ``` 最大允許的權限 - mask -> 設定最高權限(protection) 設定的permission 會跟 mask 做 And operation e.g. r-- and rw- --> r– ### 針對特定群組 ``` [root@localhost centos]# setfacl -m g:mygroup1:rx test_acl1 [root@localhost centos]# getfacl test_acl1 # file: test_acl1 # owner: centos # group: centos user::rw- user:meowhecker:r-x group::rw- group:mygroup1:r-x # 新增Permission的部分 mask::rwx #最大允許的權限 other::r-- ``` --- 測試 ``` [meowhecker@localhost centos]$ ll test_acl1 -rw-rwxr--+ 1 centos centos 0 Jun 8 02:11 test_acl1 [meowhecker@localhost centos]$ vim test_acl1 [meowhecker@localhost centos]$ echo "meowhecker" > test_acl1 [meowhecker@localhost centos]$ cat test_acl1 meowhecker ``` 照理來說 test_acl1 Other user 是不可寫的 設定完就能寫入了 ### 針對Directory **目錄下的檔案 會去繼承在目錄下的ACL權限設定** 設定 Directory ACL ``` [centos@localhost ~]$ mkdir dirAcl [centos@localhost ~]$ ll -d dirAcl/ drwxrwx--- 2 centos centos 6 Jun 8 03:36 dirAcl/ [centos@localhost ~]$ setfacl -m d:u:meowhecker:rx ./dirAcl/ ``` --- ``` /home/centos/dirAcl [meowhecker@localhost dirAcl]$ ll test1 -rw-rw----+ 1 meowhecker meowhecker 0 Jun 8 03:49 test1 [meowhecker@localhost dirAcl]$ getfacl test1 # file: test1 # owner: meowhecker # group: meowhecker user::rw- user:meowhecker:r-x #effective:r-- group::rwx #effective:rw- mask::rw- other::--- ``` ### 取消ACL 權限設定 ``` setfacl -b ``` -b :移除『所有的』 ACL 設定參數; ### 設定一個用戶/群組沒有任何權限的 ACL user meowhecker -> 被移除了所有Permission ``` [root@localhost centos]# getfacl dirAcl/ # file: dirAcl/ # owner: centos # group: centos user::rwx user:meowhecker:--- group::rwx mask::rwx other::rwx default:user::rwx default:user:meowhecker:r-x default:group::rwx default:mask::rwx default:other::--- ``` 使用meowhecker-> 是無法cd 或 ls 該Directory ``` [meowhecker@localhost centos]$ ll dirAcl/ ls: cannot open directory dirAcl/: Permission denied ``` # 使用者的特殊 shell 與 PAM 模組 僅能使用 mail server 相關郵件服務的帳號,而該帳號並不能登入 Linux 主機 ## 特殊的 shell (/sbin/nologin) 不需要登入帳號 -> 主要是用來使用系統上的Service (使用者無法使用 bash 或其他 shell 來登入系統) ### /etc/nologin.txt 這個file 可以寫舞法登入的原因 # PAM (Pluggable Authentication Modules) 嵌入式模組 PAM 可以說是一套應用程式介面 (Application Programming Interface, API),他提供了一連串的驗證機制,只要使用者將驗證階段的需求告知 PAM 後, PAM 就能夠回報使用者驗證的結果 (成功或失敗) 
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up