學神-RHEL7】 1-7-文件權限管理

本節所講內容:

      文件基本權限

      文件高級權限

      實戰-創建一個讓root都無法刪除的***文件

 

查看文件權限

[root@localhost ~]#touch liulang

[root@localhost ~]# llliulang

-rw-r--r-- 1 root root0 Nov 30 20:35 liulang

 

文件基本權限

-    rwx    r-x       r-x      user1  user1  FILENAME

類型  擁有者的權限 所屬組的權限 其他人的權限 擁有者  屬組   對象

 

對於文件:r讀  w寫  x執行

對於目錄:r讀(看到目錄裏面有什麼)       cat   more  less   ls

     w建文件、刪除、移動                  touch   mkdir   rm   mv  cp

     x進入                                        cd

 

修改權限的相關命令:

chmod      

作用:修改文件權限

u-w               user                      擁有者

g+x               group                   

o=r                other                    其他人

a+x               all                        所有人

 

[root@localhost ~]#touch  tuluo

[root@localhost ~]# lltuluo

-rw-r--r-- 1 root root0 Nov 30 20:45 tuluo

[root@localhost ~]#chmod u+x tuluo

[root@localhost ~]# lltuluo

-rwxr--r-- 1 root root0 Nov 30 20:45 tuluo

[root@localhost ~]#chmod g-r tuluo

[root@localhost ~]# lltuluo

-rwx---r-- 1 root root0 Nov 30 20:45 tuluo

[root@localhost ~]#chmod o=w tuluo

[root@localhost ~]# lltuluo

-rwx----w- 1 root root0 Nov 30 20:45 tuluo

[root@localhost ~]#chmod a+x tuluo

[root@localhost ~]# lltuluo

-rwx--x-wx 1 root root0 Nov 30 20:45 tuluo

 

chmod g+/-/=  rwx   用法同上

 

修改目錄的權限

[root@localhost ~]#mkdir fengchen

[root@localhost ~]# ll-d fengchen/

drwxr-xr-x 2 root root6 Nov 30 20:55 fengchen/

[root@localhost ~]#chmod u-w fengchen/

[root@localhost ~]# ll-d fengchen/

dr-xr-xr-x 2 root root6 Nov 30 20:55 fengchen/

[root@localhost ~]#chmod g+w fengchen/

[root@localhost ~]# ll-d fengchen/

dr-xrwxr-x 2 root root6 Nov 30 20:55 fengchen/

 

修改other權限:

chmod o+/-/=  r 、w 、 x  用法同上

chmod  o+/-/= rwx 用法同上

chmod a+/-/= rwx  用法同上

chmod  +/-/= rwx  用法同上  #不寫a也可以

 

使用數字表示權限

-    rwx    r-x        r-x    user1  user1  FILENAME

類型  擁有者的權限 所屬組的權限 其他人的權限 屬主  屬組 

 

rwx

r--      -w-   --x

100     010     001                    二進制                   進制轉換器

4            2            1                   十進制

 

rw-    的值是多少?                    4+2=6

r-x                                             4+1=5

rwxr-xr-x 的值是多少?rwx=4+2+1=7          r-x=4+1=5  r-x=4+1=5     rwxr-xr-x=755 

 

[root@localhost ~]#touch  longyu

[root@localhost ~]# lllongyu

-rw-r--r-- 1 root root0 Nov 30 21:00 longyu

[root@localhost ~]#chmod 633 longyu

[root@localhost ~]# lllongyu

-rw--wx-wx 1 root root0 Nov 30 21:00 longyu

 

chown

作用:修改文件擁有者和所屬組

語法:chown USER:GROUP 對象

chown USER 對象

chown :GROUP 對象

-R 遞歸(目錄下的所有內容全部更改,否則只修改目錄)

[root@localhost ~]#touch eshao

[root@localhost ~]# lleshao

-rw-r--r-- 1 root root0 Nov 30 21:05 eshao

[root@localhost ~]#useradd eshao

[root@localhost ~]#chown eshao:bin eshao

[root@localhost ~]# lleshao

-rw-r--r-- 1 eshao bin0 Nov 30 21:05 eshao

[root@localhost ~]#chown daemon eshao

[root@localhost ~]# lleshao

-rw-r--r-- 1 daemon bin0 Nov 30 21:05 eshao

[root@localhost ~]#chown :nobody eshao

[root@localhost ~]# lleshao

-rw-r--r-- 1 daemonnobody 0 Nov 30 21:05 eshao

 

-R遞歸

[root@localhost ~]#chown -R :daemon alen/

[root@localhost ~]# llalen/

total 0

-rw-r--r-- 1 rootdaemon 0 Nov 30 21:10 alen.txt

-rw-r--r-- 1 rootdaemon 0 Nov 30 21:10 eshao.txt

 

一個文件只有讀的權限,擁有者是否可以寫這個文件?   文件所有者一定可以寫文件

[root@localhost~]# useradd yunwu

[root@localhost ~]#touch huiji

[root@localhost ~]# cphuiji  /home/yunwu/

[root@localhost ~]# su- yunwu

[yunwu@localhost ~]$vim huiji

結果:可以正常寫入內容

 

設置文件默認權限的補碼:

系統用戶:#umask022

普通用戶:#umask002

 

計算方法:

文件默認權限=666-umask值  666-022=644

目錄默認權限=777-umask 值  777-022=755

#這是一個好的記憶方法,但不嚴謹。

 

umask掩碼爲033 . 666-033=633  結果爲: 644

計算方法:

6  6 6            umask   0  3   3

110 110110                 000 011  011  |  取反

                             111 100  100   \/

110 110110   與  111100  100

    |        /

110 100 100

6   4   4

 

瞭解:

/etc/bashrc

if [ $UID -gt 199 ]&& [ "`id -gn`" = "`id -un`" ]; then  # id -gn顯示組名,id -un 顯示用戶名

        umask 002   #普通用戶

else

        umask 022   #系統用戶

fi

 

特殊權限:

SUID               SGID                 Stickybit

s對應的數值爲:u 4,g  2,o   1

SUID

限定:只能設置在二進制可執行程序上面。對目錄文本設置無效。

功能:程序運行時的權限從執行者變更成程序所有者。

 

[root@localhost ~]# ll/usr/bin/passwd

-rwsr-xr-x. 1 root root27832 Jan 30  2014 /usr/bin/passwd

[jiangfeng@localhost~]$ less /etc/shadow

/etc/shadow: Permissiondenied

 

[root@localhost ~]#chmod u+s /usr/bin/less
[root@localhost ~]# ll /usr/bin/less

-rwsr-xr-x. 1 root root158240 Feb  4  2014 /usr/bin/less

驗證:

[jiangfeng@localhost~]$ less /etc/shadow                  成功讀取

 

注意:

[root@localhost ~]#chmod u+s /usr/bin/less                            等同於

[root@localhost ~]#chmod 4755 /usr/bin/less

 

 

SGID

限定:既可以給二進制可執行程序設置,也可以給目錄設置。

功能:在設置了SGID權限的目錄下建立文件時,新創建的文件的所屬組會繼承上級目錄的所屬組

 

[root@localhost ~]#chown :bin test/

[root@localhost ~]# ll-d test/

drwxr--r-- 2 root bin 6Nov 30 21:55 test/

[root@localhost ~]# cdtest/

[root@localhost test]#touch a.txt

[root@localhost test]#ll a.txt

-rw-r--r-- 1 root root0 Nov 30 21:56 a.txt

 

[root@localhost ~]#chmod g+s test/

[root@localhost ~]# ll-d test/

drwxr-Sr-- 2 root bin30 Nov 30 21:57 test/

驗證:

[root@localhost ~]# cdtest/

[root@localhost test]# touchb.txt

[root@localhost test]#ll b.txt

-rw-r--r-- 1 root bin 0Nov 30 21:57 b.txt

 

Stickybit

限定:只作用於目錄

功能:目錄下創建的文件只有root、文件創建者、目錄所有者才能刪除

 

[root@localhost ~]# ll-d /tmp/

drwxrwxrwt. 14 rootroot 4096 Nov 30 21:59 /tmp/

 

[root@localhost ~]#mkdir /share

[root@localhost ~]#chmod 777 /share/

[root@localhost ~]# ll-d /share/

drwxrwxrwx 2 root root6 Nov 30 22:00 /share/

[root@localhost ~]#chmod o+t /share/

[root@localhost ~]# ll-d /share/

drwxrwxrwt 2 root root6 Nov 30 22:00 /share/

 

[root@localhost ~]# su- eshao

[eshao@localhost ~]$ cd/share/

[eshao@localhostshare]$ ls

jiangfeng.txt

[eshao@localhostshare]$ rm -rf jiangfeng.txt

rm: cannot remove‘jiangfeng.txt’: Operation not permitted

[eshao@localhostshare]$ echo  aaa > jiangfeng.txt

 

擴展ACL

[root@localhost ~]#touch b.txt

 

查看

[root@localhost ~]#getfacl  b.txt

# file: b.txt

# owner: root

# group: root

user::rw-

group::r--

other::r--

 

設置

[root@localhost ~]#setfacl -m u:eshao:rwx b.txt

[root@localhost ~]#getfacl  b.txt

# file: b.txt

# owner: root

# group: root

user::rw-

user:eshao:rwx

group::r--

mask::rwx

other::r--

 

對目錄進行設置

[root@localhost ~]#setfacl -R -m u:jiangfeng:rw- test

 (-R一定要在-m前面,表示目錄下所有文件)

[root@localhost ~]#getfacl !$

getfacl test

# file: test

# owner: root

# group: bin

# flags: -s-

user::rwx

user:jiangfeng:rw-

group::r--

mask::rw-

other::r--

 

刪除acl      

[root@localhost ~]#setfacl -x u:eshao b.txt                        #刪除單個用戶的權限

[root@xuegod163~]# setfacl -b b.txt                                  #刪除所有acl權限

 

      實戰-創建一個讓root都無法刪除的***文件

 

REHL6 Linux文件系統擴展屬性:chattr  lsattr

+a  只能追加內容

+i  不能被修改

 

[root@localhost ~]#touch xiaobai

[root@localhost ~]#echo xiaobaihaoniubi  > xiaobai

[root@localhost ~]# catxiaobai

xiaobaihaoniubi

[root@localhost ~]#chattr +a xiaobai

[root@localhost ~]#echo xiaobaizhendehaoniubi > xiaobai

-bash: xiaobai:Operation not permitted

[root@localhost ~]#echo xiaobaizhendehaoniubi >> xiaobai

[root@localhost ~]# catxiaobai

xiaobaihaoniubi

xiaobaizhendehaoniubi

[root@localhost ~]# rm-rf xiaobai

rm: cannot remove‘xiaobai’: Operation not permitted

 

[root@localhost ~]#chattr +i xiaobai

[root@localhost ~]#echo xiaobaizhendehaoniubima? >> xiaobai

-bash: xiaobai:Permission denied

[root@localhost ~]# rm-rf xiaobai

rm: cannot remove‘xiaobai’: Operation not permitted

 

[root@localhost ~]#lsattr xiaobai

----ia----------xiaobai

 

[root@localhost ~]#lsattr xiaobai

----ia----------xiaobai

[root@localhost ~]#chattr -i xiaobai

[root@localhost ~]#chattr -a xiaobai

[root@localhost ~]#lsattr xiaobai

----------------xiaobai

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章