1. 程式人生 > >Linux-文件權限

Linux-文件權限

code uid div 文件管理 amp gre perm 只讀 ali

Linux文件權限管理

基本權限 UGO

文件權限設置: 可以賦於某個用戶或組 能夠以何種方式 訪問某個文件

文件權限管理之: UGO設置基本權限(r、w、x)

rw-r--r-- alice hr install.log
權限對象:
屬主:         u
屬組:         g
其他人:       o

權限類型:
讀:r          4
寫:w         2
執行: x        1

==設置權限

1. 更改文件是屬主、屬組

=chown:
[root@localhost ~]# chown alice.hr file1              //改屬主、屬組
[root@localhost ~]# chown alice file1 //只改屬主 [root@localhost ~]# chown .hr file1 //只改屬組 =chgrp: [root@localhost ~]# chgrp it file1 //改文件屬組 [root@localhost ~]# chgrp -R it dir1 //改文件屬組

2. 更改權限

=a. 使用符號
                    對象        賦值符        權限類型
                    u                
+ r chmod g - w file1 o = x a [root@localhost ~]# chmod u+x file1 //屬主增加執行 [root@localhost ~]# chmod a=rwx file1 //所有人等於讀寫執行 [root@localhost ~]#
chmod a=- file1 //所有人沒有權限 [root@localhost ~]# chmod ug=rw,o=r file1 //屬主屬組等於讀寫,其他人只讀 [root@localhost ~]# ll file1 //以長模式方式查看文件權限 -rw-rw-r-- 1 alice it 17 10-25 16:45 file1 //顯示的結果 =b. 使用數字 [root@localhost ~]# chmod 644 file1 [root@localhost ~]# ll file1 -rw-r--r-- 1 alice it 17 10-25 16:45 file1

===設置權限示例

針對hr部門的訪問目錄設置權限,要求如下:
1. root用戶和hr組的員工可以讀、寫、執行
2. 其他用戶沒有任何權限

[root@localhost ~]# groupadd hr
[root@localhost ~]# useradd hr01 -G hr
[root@localhost ~]# useradd hr02 -G hr
[root@localhost ~]# mkdir /home/hr

[root@localhost ~]# chgrp hr /home/hr
[root@localhost ~]# chmod 770 /home/hr
[root@localhost ~]# ll -d /home/hr/
drwxrwx---. 2 root hr 4096 3月  13 14:26 /home/hr/

重要: r、w、x權限對文件和目錄的意義

技術分享圖片

示例1: 對文件的影響

[root@localhost ~]# mkdir /dir10
[root@localhost ~]# touch /dir10/file1
[root@localhost ~]# chmod 777 /dir10/file1 

[root@localhost ~]# ll -d /dir10/
drwxr-xr-x. 2 root root 4096 3月  11 18:37 /dir10/
[root@localhost ~]# ll /dir10/file1 
-rwxrwxrwx. 1 root root 0 3月  11 18:37 /dir10/file1

[alice@tianyun ~]$ cat /dir10/file1 
[alice@tianyun ~]$ rm -rf /dir10/file1 
rm: 無法刪除"/dir10/file1": 權限不夠

示例2: 對目錄有w權限

[root@localhost ~]# chmod 777 /dir10/
[root@localhost ~]# chmod 000 /dir10/file1 
[root@localhost ~]# ll -d /dir10/
drwxrwxrwx. 2 root root 4096 3月  11 18:37 /dir10/
[root@localhost ~]# ll /dir10/file1 
----------. 1 root root 0 3月  11 18:37 /dir10/file1

[alice@tianyun ~]$ cat /dir10/file1 
cat: /dir10/file1: 權限不夠
[alice@tianyun ~]$ rm -rf /dir10/file1 
[alice@tianyun ~]$ touch /dir10/file2

問題1:

[root@localhost ~]# ll /root/install.log
-rw-r--r--. 1 root root 46571 6月   1 23:37 /root/install.log
[alice@tianyun ~]$ cat /root/install.log
cat: /root/install.log: 權限不夠

問題2: alice能刪除/下的任何文件嗎?

[root@localhost ~]# chmod 777 /
[root@localhost ~]# ll -d /
drwxrwxrwx. 27 root root 4096 6月   4 11:32 /
[alice@tianyun ~]$ rm -rf /etc

再次認識一下文件和目錄:

技術分享圖片

基本權限 ACL

文件權限管理之: ACL設置基本權限(r、w、x)
UGO設置基本權限: 只能一個用戶,一個組和其他人
ACL 設置基本權限: r,w,x

=ACL基本用法=

設置:
[root@localhost ~]# touch /home/test.txt
[root@localhost ~]# ll /home/test.txt    
-rw-r--r-- 1 root root 0 10-26 13:59 /home/test.txt

[root@localhost ~]# getfacl /home/test.txt
[root@localhost ~]# setfacl -m u:alice:rw /home/test.txt              //增加用戶alice權限
[root@localhost ~]# setfacl -m u:jack:- /home/test.txt                //增加用戶jack權限
[root@localhost ~]# setfacl -m o::rw /home/test.txt

查看/刪除:
[root@localhost ~]# ll /home/test.txt 
-rw-rw-r--+ 1 root root 0 10-26 13:59 /home/test.txt
[root@localhost ~]# getfacl /home/test.txt

[root@localhost ~]# setfacl -m g:hr:r /home/test.txt
[root@localhost ~]# setfacl -x g:hr /home/test.txt                     //刪除組hr的acl權限
[root@localhost ~]# setfacl -b /home/test.txt                          //刪除所有acl權限

=查看幫助=

[root@localhost ~]# man setfacl
/EXAMPLES
[root@localhost ~]# getfacl file1 |setfacl  --set-file=- file2       //復制file1的ACL權限給file2

=ACL高級用法=

mask:
用於臨時降低用戶或組(除屬主和其他人)的權限
建議:為了方便管理文件權限,其他人的權限置為空
[root@localhost ~]# setfacl -m m::--- /home/file100.txt


default: 繼承(默認)
要求: 希望alice能夠對/home以及以後在/home下新建的文件有讀、寫、執行權限

思路:
步驟一: 賦予alice對/home讀、寫、執行權限
[root@localhost ~]# setfacl -m u:alice:rwx /home

步驟二: 賦予alice對以後在/home下新建的文件有讀、寫、執行權限 (使alice的權限繼承)
[root@localhost ~]# setfacl -m d:u:alice:rwx /home

高級權限 suid,sgid,sticky

問題1: 為什麽會失敗!

[root@localhost ~]# ll /root/install.log
-rw-r--r--. 1 root root 46571 6月   1 23:37 /root/install.log
[alice@tianyun ~]$ cat /root/install.log
cat: /root/install.log: 權限不夠

分析:
alice           /usr/bin/cat (alice)            /root/install.log
alice           /usr/bin/passwd (root)      /etc/shadow

高級權限的類型
suid 4
sgid 2
sticky 1 粘滯位

設置特殊權限
a、字符
chmod u+s file
chmod g+s file
chmod g+s dir
chmod o+t dir

b、數字
chmod 4777 file
chmod 7777 file
chmod 2770 dir
chmod 3770 dir

示例1:suid 普通用戶通過suid提權 <針對文件>

在進程文件(二進制,可執行)上增加suid權限
[root@localhost ~]# chmod u+s /bin/cat
[root@localhost ~]# chmod u+s /bin/rm
[alice@tianyun ~]$ cat /root/install.log
普通用戶可以修改密碼:
alice           /usr/bin/passwd      /etc/shadow

[alice@tianyun ~]$ ll /etc/shadow
---------- 1 root root 1487 6月   4 13:43 /etc/shadow

[alice@tianyun ~]$ ll /usr/bin/passwd
-rwsr-xr-x. 1 root root 30768 2月  17 2012 /usr/bin/passwd

[alice@tianyun ~]$ passwd 
更改用戶 alice 的密碼 。
為 alice 更改 STRESS 密碼。
(當前)UNIX 密碼:

[root@localhost ~]# ps aux |grep passwd
root      3674  0.0  0.0 165764  1884 pts/1    S+   14:34   0:00 passwd

示例2:sticky 用戶只能刪除自己的文件 <針對目錄>

[root@localhost ~]# mkdir /home/dir1
[root@localhost ~]# chmod 777 /home/dir1
測試:user1在/home/dir1建立文件, user2嘗試刪除!

[root@localhost ~]# chmod o+t /home/dir1
[root@localhost ~]# ll -d /home/dir1
rwxrwxrwt 2 root root 4096 09-02 02:26 /home/dir1
誰可以刪除:
root
文件的所有者
目錄的所有者

示例3:sgid 新建文件繼承目錄屬組 <針對目錄>

[root@localhost ~]# mkdir /home/hr
[root@localhost ~]# chgrp hr /home/hr/
[root@localhost ~]# chmod g+s /home/hr
[root@localhost ~]# ll -d /home/hr/
drwxr-sr-x. 2 root hr 4096 Dec  5 16:03 /home/hr/

[root@localhost ~]# touch /home/hr/file9
[root@localhost ~]# ll /home/hr/
-rw-r--r--. 1 root hr   0 Dec  5 16:03 file9

=================================================================
小知識:註意以下目錄的正確權限,否則會導致程序不能正常運行
[root@wangcy ~]# ll -d /tmp /var/tmp/
drwxrwxrwt 14 root root 4096 07-26 10:15 /tmp
drwxrwxrwt  2 root root 4096 07-24 19:02 /var/tmp/
=================================================================
                文件                                                 目錄
suid      執行的時候以所有者身份執行

sqid                                                                繼承屬組 

sticky                                                              用戶只能刪除自己的文件

進程掩碼 mask umask

mask;
用於臨時降低用戶或組(除屬主和其他人)的權限
mask決定了他們的最高權限
建議:為了方便文件管理,其他人的權限置為空

文件權限管理之: 進程umask

進程 新建文件、目錄的默認權限會受到umask的影響,umask表示要減掉的權限

shell (vim,touch)      =======umask======>    新文件或目錄權限
vsftpd                  =======umask======>    新文件或目錄權限 
samba                   =======umask======>    新文件或目錄權限 
useradd                 =======umask======>    用戶HOME

示例1: 在shell進程中創建文件

[root@localhost ~]# umask                                     //查看當前用戶的umask權限
0022
[root@localhost ~]# touch file800
[root@localhost ~]# mkdir dir800
[root@localhost ~]# ll -d dir800 file800 
drwxr-xr-x. 2 root root 4096 3月  11 19:40 dir800
-rw-r--r--. 1 root root    0 3月  11 19:40 file800

示例2:修改shell umask值(臨時)

[root@localhost ~]# umask 000
[root@localhost ~]# mkdir dir900
[root@localhost ~]# touch file900
[root@localhost ~]# ll -d dir900 file900 
drwxrwxrwx. 2 root root 4096 3月  11 19:44 dir900
-rw-rw-rw-. 1 root root    0 3月  11 19:44 file900

示例3:修改shell umask值(永久)

[root@localhost ~]# vim /etc/profile   
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
    umask 002
else
    umask 022
fi
[root@localhost ~]# source /etc/profile       //立即在當前shell中生效

示例4:通過umask決定新建用戶HOME目錄的權限

[root@localhost ~]# vim /etc/login.defs 
UMASK           077
[root@localhost ~]# useradd gougou
[root@localhost ~]# ll -d /home/gougou/
drwx------. 4 gougou gougou 4096 3月  11 19:50 /home/gougou/

[root@localhost ~]# vim /etc/login.defs
UMASK           000
[root@localhost ~]# useradd yangyang
[root@localhost ~]# ll -d /home/yangyang/
drwxrwxrwx. 4 yangyang yangyang 4096 3月  11 19:53 /home/yangyang/

文件屬性 chattr

文件權限管理之: 文件屬性
註:設置文件屬性(權限),針對所有用戶,包括root

[root@localhost ~]# touch file100 file200 file300
[root@localhost ~]# lsattr file100 file200 file300 
-------------e- file100
-------------e- file200
-------------e- file300

[root@localhost ~]# man chattr
[root@localhost ~]# chattr +a file100 
[root@localhost ~]# chattr +i file200 
[root@localhost ~]# chattr +A file300

[root@localhost ~]# lsattr file100 file200 file300 
-----a-------e- file100
----i--------e- file200
-------A-----e- file300

[root@localhost ~]# echo 111 > file100                    //以覆蓋的方式寫入
bash: file100: Operation not permitted
[root@localhost ~]# rm -rf file100 
rm: cannot remove `file100: Operation not permitted
[root@localhost ~]# echo 111 >> file100                  //以追加的方式寫入,例如日誌文件

[root@localhost ~]# echo 111 > file200
bash: file200: Permission denied
[root@instructor ~]# echo 111 >> file200
bash: file200: Permission denied
[root@localhost ~]# rm -rf file200 
rm: cannot remove `file200: Operation not permitted

[root@localhost ~]# chattr -a file100
[root@localhost ~]# chattr -i file200
[root@localhost ~]# chattr -A file300

Linux-文件權限