一、用戶組管理
1、設置修改用戶密碼密碼
設置密碼
passwd [OPTIONS] UserName: 修改指定用戶的密碼,僅root 用戶權限
passwd: 修改自己的密碼;
常用選項:
-l: 鎖定指定用戶
-u: 解鎖指定用戶
-e: 強制用戶下次登錄修改密碼
-n mindays: 指定最短使用期限
-x maxdays :最大使用期限
-w warndays :提前多少天開始警告
-i inactivedays :非活動期限;
–stdin :從標準輸入接收用戶密碼;
echo " PASSWORD " | passwd –stdin USERNAME
下一次登錄強制重設密碼
chage -d 0 tom
chage -m 0 –M 42 –W 14 –I 7 tom
chage -E 2016-09-10 tom
用戶相關的其它命令
chfn 指定個人信息
chsh 指定shell
finger
2、組的創建和修改
創建組
groupadd [OPTION]… group_name
-g GID: 指明GID 號;[GID_MIN, GID_MAX]
-r: 創建系統組;
CentOS 6: ID<500
CentOS 7: ID<1000
修改和刪除組
組屬性修改:groupmod
groupmod [OPTION]… group
-n group_name: 新名字
-g GID: 新的GID;
組刪除:groupdel
groupdel GROUP
更改組密碼
組密碼:gpasswd
gpasswd [OPTION] GROUP
-a user: 將user 添加至指定組中;
-d user: 從指定組中移除用戶user
-A user1,user2,…: 設置有管理權限的用戶列表
4
二、權限管理
1、文件和目錄的所有者權限及修改
文件屬性
權限、引用、計數、所有者、所屬組大小、創建時間、文件名、文件屬性操作
修改文件的屬主和屬組
修改文件的屬主:chown
chown [OPTION]… [OWNER][:[GROUP]] FILE…
用法:
OWNER
OWNER:GROUP
:GROUP
命令中的冒號可用. 替換;
-R: 遞歸
chown [OPTION]… –reference=RFILE FILE…
修改文件的屬組:chgrp
chgrp [OPTION]… GROUP FILE…
chgrp [OPTION]… –reference=RFILE FILE…
-R 遞歸
2、文件和目錄的基本權限及修改
文件權限
文件的權限主要針對三類對象進行定義:
owner: 屬主, u
group: 屬組, g
other: 其他, o
每個文件針對每類訪問者都定義了三種權限:
r: Readable
w: Writable
x: eXcutable
文件及目錄的權限
文件:
r: 可使用文件查看類工具獲取其內容
w: 可修改其內容
x: 可以把此文件提交內核啟動為一個進程
目錄:
r: 可以使用ls 查看此目錄中文件列表
w: 可在此目錄中創建文件,也可刪除此目錄中的文件
x: 可以使用ls -l 查看此目錄中文件列表,可以cd 進入此目錄
X:只給目錄x權限,不給文件x權限
修改文件權限
chmod [OPTION]… OCTAL-MODE FILE…
-R: 遞歸修改權限
chmod [OPTION]… MODE[,MODE]… FILE…
MODE: :
修改一類用戶的所有權限:
u= g= o= ug= a= u=,g=
修改一類用戶某位或某些位權限
u+ u- g+ g- o+ o- a+ a- + –
chmod [OPTION]… –reference=RFILE FILE…
參考RFILE 文件的權限,將FILE 的修改為同RFILE;
chgrp sales testfile
chown root:admins testfile
[root@6 testdir]# chown lvasu: a [root@6 testdir]# ll a ----------. 1 lvasu lvasu 0 8月 3 20:57 a
chmod u+wx,g-r,o=rx file
[root@6 testdir]# ll a -rw-r--r--. 1 root root 0 8月 3 20:57 a [root@6 testdir]# chmod u-rw,g-r,o-r a [root@6 testdir]# ll a ----------. 1 root root 0 8月 3 20:57 a
chmod -R g+rwX /testdir
[root@6 testdir]# ll -a a 總用量 28 drwxr-xr-x. 4 root root 4096 8月 3 21:03 . drwxr-xr-x. 16 root root 4096 8月 3 21:03 .. -rw-r--r--. 1 root root 18 8月 3 21:03 .bash_logout -rw-r--r--. 1 root root 176 8月 3 21:03 .bash_profile -rw-r--r--. 1 root root 124 8月 3 21:03 .bashrc drwxr-xr-x. 2 root root 4096 8月 3 21:03 .gnome2 drwxr-xr-x. 4 root root 4096 8月 3 21:03 .mozilla [root@6 testdir]# chmod -R g-r a [root@6 testdir]# ll -a a 總用量 28 drwx--xr-x. 4 root root 4096 8月 3 21:03 . drwxr-xr-x. 16 root root 4096 8月 3 21:03 .. -rw----r--. 1 root root 18 8月 3 21:03 .bash_logout -rw----r--. 1 root root 176 8月 3 21:03 .bash_profile -rw----r--. 1 root root 124 8月 3 21:03 .bashrc drwx--xr-x. 2 root root 4096 8月 3 21:03 .gnome2 drwx--xr-x. 4 root root 4096 8月 3 21:03 .mozilla
chmod 600 file
[root@6 testdir]# ll a ----------. 1 lvasu lvasu 0 8月 3 20:57 a [root@6 testdir]# chmod 600 a [root@6 testdir]# ll a -rw-------. 1 lvasu lvasu 0 8月 3 20:57 a
chown mage testfile
[root@6 testdir]# ll a -rw-------. 1 lvasu lvasu 0 8月 3 20:57 a [root@6 testdir]# chown root a [root@6 testdir]# ll a -rw-------. 1 root lvasu 0 8月 3 20:57 a
權限設置–umask碼
新建文件和目錄的默認權限
umask值 值 可以用來保留在創建文件權限
新建FILE 權限: 666-umask
如果所得結果某位存在執行(奇數)權限,則將其權限+1
新建DIR 權限: 777-umask
非特權用戶umask是 002
root 的umask 是 022
umask: 查看
umask #: 設定
umask 002
umask –S 模式方式顯示
umask –p 輸出可被調用
全局設置: /etc/bashrc 用戶設置:~/.bashrc
3、Linux 文件系統上的特殊權限
SUID, SGID, Sticky
三種常用權限:r, w, x user, group, other
(1) 任何一個可執行程序文件能不能啟動為進程:取決發起者
對程序文件是否擁有執行權限
判斷二進位制程序能否運行:
屬主—》屬組—–》其他用戶 u—g—o
(2) 啟動為進程之后,其進程的屬主為發起者;進程的屬組為
發起者所屬的組
(3) 進程訪問文件時的權限,取決于進程的發起者
(a) 進程的發起者,同文件的屬主:則應用文件屬主權限
(b) 進程的發起者,屬于文件屬組;則應用文件屬組權限
(c) 應用文件“其它”權限
可執行文件上SUID 權限 對象:二進制程序,目錄,腳本無意義
二進位制程序提交內核為進程,需要執行者有執行的權限
啟動為進程之后,其進程的屬主為原程序文件的屬主
SUID 只對二進制可執行程序有效,目錄無意義
用途:通常為一些程序開發者根據需要設置的SUID,出于安全的考慮,一般不會做修改。
權限設定:
chmod u+s FILE…
chmod u-s FILE…
chmod 4755 +SUID
chmod 755 -SUID (有時不會生效)
[root@6 testdir]# ll b -rwxr-xr-x. 1 root root 0 8月 3 21:42 b [root@6 testdir]# chmod u+s b [root@6 testdir]# ll b -rwsr-xr-x. 1 root root 0 8月 3 21:42 b [root@6 testdir]# chmod 755 b ###去除權限 [root@6 testdir]# ll b -rwxr-xr-x. 1 root root 0 8月 3 21:42 b [root@6 ~]# chmod u+s /bin/cat ###對cat添加SUID [root@6 ~]# ll /bin/cat -rwsr-xr-x. 1 root root 48568 5月 11 16:59 /bin/cat [root@6 ~]# su lvasu [lvasu@6 root]$ cat /etc/shadow ##普通用戶可以root身份打開shadow文件 root:$6$WCeeOGmBq7sO4OUR$7ilpCkqcbLCewVKKXKpOH1V1SBqZI4m57d1fZocbrlYZhbF9thDSGJhei1c.DYfnFT5UuamVtUS0CsESHBzqK/:17015:0:99999:7::: bin:*:17015:0:99999:7::: daemon:*:17015:0:99999:7::: [root@6 ~]# chmod 7777 /testdir [root@6 ~]# ll -d /testdir drwxrwxrwxt 16 root root 4096 8月 4 11:50 /testdir [root@6 ~]# chmod 777 /testdir ###去除權限失敗 [root@6 ~]# ll -d /testdir drwxrwxrwx. 16 root root 4096 8月 4 11:50 /testdir
可執行文件上SGID 權限 對象:可以是二進制程序 ,但權限很小,并且需要文件對該組授權。
二進位制程序提交內核為進程,需要執行者有執行的權限
啟動為進程之后,其進程的屬主為原程序文件的屬主
權限設定:
chmod g+s FILE…
chmod g-s FILE…
[root@6 ~]# chmod g+s /bin/cat [root@6 ~]# ll /bin/cat -rwxr-sr-x. 1 root root 48568 5月 11 16:59 /bin/cat [root@6 ~]# su lvasu [lvasu@6 root]$ cat /etc/shdow cat: /etc/shdow: 沒有那個文件或目錄 ###加入SGID也無法訪問,取決于root組 [lvasu@6 root]$ cat /etc/shadow cat: /etc/shadow: 權限不夠, [root@6 ~]# chmod g+r /etc/shadow ###shadow文件對root進行授權 [root@6 ~]# ll /etc/shadow ----r-----. 1 root root 2464 8月 2 16:55 /etc/shadow [root@6 ~]# su lvasu [lvasu@6 root]$ cat /etc/shadow root:$6$WCeeOGmBq7sO4OUR$7ilpCkqcbLCewVKKXKpOH1V1SBqZI4m57d1fZocbrlYZhbF9thDSGJhei1c.DYfnFT5UuamVtUS0CsESHBzqK/:17015:0:99999:7::: bin:*:17015:0:99999:7::: daemon:*:17015:0:99999:7::: adm:*:17015:0:99999:7:::
目錄上的SGID 權限 對象:目錄
默認情況下,用戶創建文件時,其屬組為此用戶所屬的主組
通過對目錄設置SGID,對此目錄有寫權限的用戶所創建的文件的屬組為目錄的屬組
用途:通常用于創建一個協作目錄
權限設定:
chmod g+s DIR…
chmod g-s DIR…
[root@6 testdir]# gpasswd -a lv -g tec 正在將用戶“lv”加入到“tec”組中 [root@6 testdir]# gpasswd -a lvasu -g tec 正在將用戶“lvasu”加入到“tec”組中 [root@6 testdir]# chown :tec a [root@6 testdir]# chmod g+s a ##對目錄a設置SGID權限,創建寫作目錄 [root@6 a]# su lvasu [lvasu@6 a]$ touch b [root@6 a]# su lv [lv@6 a]$ touch c [root@6 a]# touch a [root@6 a]# ll 總用量 0 -rw-r--r--. 1 root tec 0 8月 4 12:30 a -rw-rw-r--. 1 lvasu tec 0 8月 4 12:29 b -rw-rw-r--. 1 lv tec 0 8月 4 12:29 c [root@6 a]# su lvasu ###該組的用戶可以互相修改文件 [lvasu@6 a]$ echo abc > c [lvasu@6 a]$ cat c abc [lvasu@6 a]$ exit exit [root@6 a]# su lv [lv@6 a]$ echo 123 > b [lv@6 a]$ cat b 123
Sticky 位 粘滯位 對象:目錄 ,文件無意義
具有寫權限的目錄通常可以刪除該目錄中的任何文件,無論該文件的權限或擁有權
在目錄設置Sticky 位,只有文件的所有者或root才可以刪除該文件
用途:出于安全的考慮,通過設置防止文件被誤刪或惡意刪除操作
權限設定:
chmod o+t DIR…
chmod o-t DIR…
例如:
ls -ld /tmp
drwxrwxrwt 12 root root 4096 Nov 2 15:44 /tmp
[root@6 ~]# chmod g+w /testdir/a [root@6 ~]# cd /testdir [root@6 a]# su lvasu [lvasu@6 a]$ touch a [lvasu@6 a]$ exit exit [root@6 a]# su lv [lv@6 a]$ touch b [lv@6 a]$ rm a rm:是否刪除有寫保護的普通空文件 "a"?y [lv@6 a]$ ll 總用量 0 -rw-rw-r--. 1 lv lv 0 8月 4 11:59 b [lv@6 a]$ exit exit [root@6 a]# su lvasu [lvasu@6 a]$ rm -f b ###a目錄下創建文件的用戶都可以互相刪除 [lvasu@6 a]$ touch 1 [lvasu@6 a]$ exit exit [root@6 a]# su lv [lv@6 a]$ touch 2 [lv@6 a]$ exit exit [root@6 a]# chmod o+t ../a ###加入粘滯權 [root@6 a]# ll -a 總用量 8 drwxrwxr-t. 2 root root 4096 8月 4 12:02 . drwxr-xr-x. 16 root root 4096 8月 4 11:50 .. -rw-rw-r--. 1 lvasu lvasu 0 8月 4 12:01 1 -rw-rw-r--. 1 lv lv 0 8月 4 12:02 2 [root@6 a]# su lvasu [lvasu@6 a]$ rm -f 2 rm: 無法刪除"2": 不允許的操作 [lvasu@6 a]$ exit exit [root@6 a]# su lv [lv@6 a]$ rm -f 1 rm: 無法刪除"1": 不允許的操作 ###a目錄中其他用戶的文件不可以刪除
特殊權限數字法
SUID SGID STICKY
UMASK 碼第一位(7777)
4 suid
2 sgid
1 sticky
6 suid+sgid
7 suid+sgid+sticky
權限位映射
SUID: user, 占據屬主的執行權限位
s: 屬主擁有x 權限
S :屬主沒有x 權限
SGID: group, 占據屬組的執行權限位
s: group 擁有x 權限
S :group 沒有x 權限
Sticky: other, 占據other 的執行權限位
t: other 擁有x 權限
T :other 沒有x 權限
[root@6 ~]# ll a -rwSr-Sr--. 1 root root 0 8月 4 11:37 a
****注意:在設置s權限時文件屬主、屬組必須先設置相應的x權限,否則s權限并不能正真生效。大寫的S則表示未生效
4、設定文件特定屬性及訪問控制列表
文件的特定屬性
chattr +A 不能修改訪問時間 鎖定訪問時間
[root@6 a]# stat a File: "a" Size: 0 Blocks: 0 IO Block: 4096 普通空文件 Device: 803h/2051d Inode: 1044578 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 4323/ tec) Access: 2016-08-04 12:30:44.338011279 +0800 Modify: 2016-08-04 12:30:44.338011279 +0800 Change: 2016-08-04 12:30:44.338011279 +0800 [root@6 a]# cat a [root@6 a]# stat a File: "a" Size: 0 Blocks: 0 IO Block: 4096 普通空文件 Device: 803h/2051d Inode: 1044578 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 4323/ tec) Access: 2016-08-04 13:09:58.032009594 +0800 Modify: 2016-08-04 12:30:44.338011279 +0800 Change: 2016-08-04 12:30:44.338011279 +0800 [root@6 a]# chattr +A a [root@6 a]# cat a [root@6 a]# stat a File: "a" Size: 0 Blocks: 0 IO Block: 4096 普通空文件 Device: 803h/2051d Inode: 1044578 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 4323/ tec) Access: 2016-08-04 13:09:58.032009594 +0800 Modify: 2016-08-04 12:30:44.338011279 +0800 Change: 2016-08-04 13:10:26.478009488 +0800
chattr +i 不能刪除,改名,只讀 鎖定
chattr +a 只能增加[root@6 a]# chattr +i a [root@6 a]# mv a asd mv: 無法將"a" 移動至"asd": 不允許的操作 [root@6 a]# echo abc > a -bash: a: 權限不夠 [root@6 a]# rm -f a rm: 無法刪除"a": 不允許的操作
chattr +a 讓某個文件只能往里面追加內容,不能刪除,一般用于日志文件
[root@6 a]# echo acdfdgf > a -bash: a: 不允許的操作 [root@6 a]# rm -f a rm: 無法刪除"a": 不允許的操作 [root@6 a]# echo acdfdgf >> a ###不允許覆蓋或刪除,只可以追加
lsattr 顯示特定屬性
[root@6 a]# lsattr a ----i--------e- a
訪問控制列表:
ACL :Access Control List ,實現靈活的權限管理
除了文件的所有者,所屬組和其它人,可以對更多的用戶設
置權限
CentOS7.0 默認創建的xfs 和ext4 文件系統有ACL 功能。
CentOS7.X 之前版本,默認手工創建的ext4 文件系統無ACL
功能。需手動增加:
tune2fs –o acl /dev/sdb1
mount –o acl /dev/sdb1 /mnt
ACL 生效順序:所有者,自定義用戶,自定義組,其他人
多用戶或者組的文件和目錄賦予訪問權限rwx
? mount -o acl /directory 手動為設備增加facl權限
? getfacl file |directory 查看文件或目錄的facl信息
? setfacl -m u:wang:rwx file|directory 設定用戶的權限
[root@7 1]# setfacl -m u:lvasu:rwx a [root@7 1]# getfacl a # file: a # owner: root # group: root user::rw- user:lvasu:rwx user:user1:rw- group::r-x group:g1:--- mask::rwx other::r--
? setfacl -Rm g:sales:rwX directory 遞歸設定組權限
[root@7 1]# setfacl -Rm g:lvasu:rwX . [root@7 1]# getfacl . # file: . # owner: root # group: root user::rwx user:lvasu:rwx user:user1:rwx group::r-x group:lvasu:rwx group:g1:--- mask::rwx other::r-x default:user::rwx default:user:user1:rw- #effective:r-- default:group::r-x default:group:g1:--- default:mask::r-x default:other::r-x
? setfacl -M file.acl file|directory 批量設置用戶和組的權限
[root@7 1]# cat acl1.txt u:user1:0 u:lvasu:rw g:g1:rw [root@7 1]# setfacl -M acl1.txt 2 [root@7 1]# getfacl 2 # file: 2 # owner: root # group: root user::rwx user:lvasu:rw- user:user1:--- group::r-x group:lvasu:rwx group:g1:rw- mask::rwx other::r-x default:user::rwx default:user:user1:rw- #effective:r-- default:group::r-x default:group:g1:--- default:mask::r-x default:other::r-x
? setfacl -m g:salesgroup:rw file| directory 設定mask權限
[root@7 testdir]# setfacl -m g:g2:rw 3 [root@7 testdir]# getfacl 3 # file: 3 # owner: root # group: root user::rw- group::r-- group:g2:rw- mask::rw- other::r--
? setfacl -m d:u:wang:rx directory 設置默認的ACL權限
? setfacl -x u:wang file |directory 刪除用戶和組的權限
[root@7 testdir]# setfacl -x u:lvasu 1 [root@7 testdir]# getfacl 1 # file: 1 # owner: root # group: root user::rwx user:user1:rwx group::r-x group:lvasu:rwx group:g1:--- mask::rwx other::r-x default:user::rwx default:user:user1:rw- #effective:r-- default:group::r-x default:group:g1:--- default:mask::r-x default:other::r-x
? setfacl -X file.acl directory 批量刪除用戶和組的權限
[root@7 1]# cat acl2.txt u:user1 u:lvasu g:g1 [root@7 1]# setfacl -X acl2.txt 2 [root@7 1]# getfacl 2 # file: 2 # owner: root # group: root user::rwx group::r-x group:lvasu:rwx mask::rwx other::r-x default:user::rwx default:user:user1:rw- #effective:r-- default:group::r-x default:group:g1:--- default:mask::r-x default:other::r-x
-
setfacl -k dir 刪除默認ACL 權限
-
[root@7 testdir]# setfacl -k 1 [root@7 testdir]# getfacl 1 # file: 1 # owner: root # group: root user::rwx user:user1:rwx group::r-x group:lvasu:rwx group:g1:--- mask::rwx other::r-x
-
setfacl -b file1 清除所有ACL 權限
-
[root@7 testdir]# setfacl -Rb /testdir [root@7 testdir]# getfacl . # file: . # owner: root # group: root user::rwx group::r-x other::r-x
創建acl批量控制文件
acl1.txt
u:wang:0
u:lvasu:rw
g:it:rw
# setfacl -M acl1.txt f1
acl2.txt
u:wang
u:lvasu
g:lv
# setfacl -X acl2.txt f`1
mask遮罩碼
ACL 文件上的group 權限是mask 值(自定義用戶,自定義組,擁有組的最大權限), 而非傳統的組權限
# chmod -g=x 設置的為mask遮罩碼 存在FACL的情況下
默認ACL 權限給了x ,文件也不會繼承x權限
base ACL 不能刪除
復制權限:
getfacl file1 | setfacl –set-file=- file2 復制file1的 的acl 權限給file2
[root@7 testdir]# getfacl 3 # file: 3 # owner: root # group: root user::rw- group::r-- group:g2:rw- mask::rw- other::r [root@7 testdir]# getfacl 3 |setfacl --set-file=- 4 [root@7 testdir]# getfacl 4 # file: 4 # owner: root # group: root user::rw- group::r-- group:g2:rw- mask::rw- other::r--
作用域:mask 只影響除所有者和other 的之外(在facl添加的組、用戶和用戶的所屬組)人和組的最大權限
設置mask:
setfacl -m mask::rx file
setfacl -m m::x file
Mask需要與用戶的權限進行邏輯與運算后,才能變成有限的權限(Effective Permission)
用戶或組的設置必須存在于mask 權限設定范圍內才會生效。
重新設置ACL:
–set 選項會把原有的ACL項都刪除,用新的替代,需要注意的是一定要包含UGO的設置,不能象-m 一樣只是添加ACL 就可以.
如: setfacl –set u::rw,u:wang:rw,g::r,o::- file1
[root@7 testdir]# setfacl --set u::rw,u:lvasu:rw,g::r,o::- 3 [root@7 testdir]# getfacl 3 # file: 3 # owner: root # group: root user::rw- user:lvasu:rw- group::r-- mask::rw- other::---
備份和恢復ACL
主要的文件操作命令cp 和mv 都支持ACL,只是cp 命令需要加上-p參數。
但是tar 等常見的備份工具是不會保留目錄和文件的ACL 信息
cp -a
#getfacl -R /tmp/dir1 > acl.txt
#setfacl -R -b /tmp/dir1
#setfacl -R –set-file=acl.txt /tmp/dir1
#getfacl -R /tmp/dir1
[root@7 testdir]# getfacl -R 3 > acl.txt [root@7 testdir]# cat acl.txt # file: 3 # owner: root # group: root user::rw- user:lvasu:rw- group::r-- mask::rw- other::--- [root@7 testdir]# setfacl -R -b 3 [root@7 testdir]# getfacl 3 # file: 3 # owner: root # group: root user::rw- group::r-- other::--- [root@7 testdir]# setfacl -R --set-file=acl.txt 3 [root@7 testdir]# getfacl 3 # file: 3 # owner: root # group: root user::rw- user:lvasu:rw- group::r-- mask::rw- other::---
其他命令及小技巧
1、設置暫時禁止普通用戶登陸
#touch /etc/nologin #echo system is maintaining>>/etc/nologin #touch /run/nologin #echo hello > /run/nologin #rm -f /etc/nologin #rm -f /run/nologin
2、復制文件的方法
cp -r /etc/skel/.[^]* /home/haha cp -r `ls -A` /home/hehe cp -r /etc/skel/. /home/haha cp -r /etc/skel /home/xixi 復制文件夾
3、復制文件的方法
[root@6 home]# su - lvasu -c 'echo $USER' lvasu [root@6 home]# su - lvasu -c "echo $USER" root [root@6 home]# su - lvasu -c `echo $USER` -bash: root: command not found
原創文章,作者:lvasu,如若轉載,請注明出處:http://www.www58058.com/28556
文章整體思路清晰,對命令的常見選項總結很好,同時通過自己的實際操作驗證了結論。