運維基礎-文件權限管理

Linux是一個多用戶操做系統,在多用戶操做系統上咱們須要一種方法來容許或者拒絕訪問特定的文件和目錄。文件有所屬人和相關的單個組。咱們能夠設置所屬人或者租的權限,以及全部其餘人的權限。shell

文件只具備三個應用權限的用戶類別。文件的全部者,一般是建立文件的用戶。文件的全部組,一般是建立改文件的用戶所在的主要組,可是能夠進行更改。能夠爲文件的所屬人、所屬組和系統中除所屬人和所屬組的其它用戶設置不一樣的權限。bash

id命令能夠查看用戶的主要組和補充組成員ui

可使用三種權限:spa

權限,也就是對文件和目錄的影響操作系統

r(讀取):能夠讀取文件的內容,能夠累出目錄的內容(文件名)命令行

w(寫入):能夠更改文件的內容,能夠建立或者是刪除目錄中的任一文件code

x(執行):能夠做爲命令執行的文件,能夠訪問目錄的內容(取決於目錄中文件的權限)blog

使用ls -l 和ls -ld命令來查看全部權、類型,和文件的權限繼承

[root@localhost ~]# ls -l
總用量 8
-rw-------. 1 root root 1635 8月  20 19:18 anaconda-ks.cfg
-rw-r--r--. 1 root root 1666 8月  20 19:21 initial-setup-ks.cfg
[root@localhost ~]# ls -ld
dr-xr-x---. 5 root root 260 8月  23 08:18 .

一個文件的常見權限是:無,讀 或者讀/寫遞歸

一個目錄的時候,他們想要的通常是ls讀取和cd執行

一個文件的寫權限是容許修改文件(nano或者重定向),一個目錄的寫權限是容許修改目錄的內容(touch,mkdir,cp,mv,rm)

一般用戶對只讀目錄具備read和exec權限,所以他們能夠列出目錄並訪問其中的內容。若是用戶對目錄具備read訪問的權限,能夠列出其中文件的名稱,可是其餘信息都不能夠訪問。若是用戶對某個目錄具備exec訪問的權限,則他們不能列出該目錄中文件的名稱,可是他們若是已經知道具備讀取權限的問價名稱,那麼能夠經過絕對路徑的方式來訪問文件的內容。

對問價的所在目錄具備寫入權限的任何人均可以刪除該目錄下的文件,不論文件的所屬人和權限如何。

從命令行管理文件系統的權限

更改文件/目錄的權限

命令行更改權限可使用chmod(change mode 更改命令)。

符號法:

chmod whowhatwhich file|directory

-who 是指u、g、o、a(表示用戶,組,其她,所有)

-what是指+、-、=(表示添加,刪除,精確設置)

-which 是指r、w、x(表示讀取,寫入,執行)

數值法:

chmod ### file|directory

-每一個數字表明一個訪問的級別:用戶、組、其餘

-#是r=4,w=2,x=1的組合

更改目錄/文件的所屬人和所屬組

默認狀況下,我虛擬鍵文件的所屬人爲建立該文件的用戶,所屬組爲該用戶的主要組

使用chown命令能夠更改文件的全部權

配合-R選項,能夠遞歸更改這個目錄樹的全部權

chown也能夠用於更改文件的所屬組,只需在組名稱以前加上冒號(:)

練習:

一個目錄可讓ateam組中的全部成員訪問,alex建立的文件可讓zhang修改

1,在/home總中建立ateam-text目錄

[root@localhost ~]# groupadd ateam
[root@localhost ~]# mkdir /home/ateam-text

二、將ateam-text目錄的全部權更改成ateam

[root@localhost ~]# chown :ateam /home/ateam-text/

三、確保組成員在ateam-text中能夠建立和刪除文件

[root@localhost ~]# chmod g+w /home/ateam-text/

四、去報ateam-text禁止他人訪問

[root@localhost ~]# chmod 770 /home/ateam-text/
[root@localhost ~]# ls -ld /home/ateam-text/
drwxrwx---. 2 root ateam 6 8月  23 08:39 /home/ateam-text/

五、退出root,切換到alex

[root@localhost ~]# su alex
[alex@localhost root]$ uname 
Linux
[alex@localhost root]$ id
uid=1001(alex) gid=1001(alex) 組=1001(alex),30000(shakespeare) 環境=unconfined_u:unconfined_r:unconf
ined_t:s0-s0:c0.c1023

六、進入/home/ateam-text

[alex@localhost root]$ cd /home/ateam-text/
bash: cd: /home/ateam-text/: 權限不夠
[alex@localhost root]$ su 
密碼:
[root@localhost ~]# useradd -G ateam alex
useradd:用戶「alex」已存在
[root@localhost ~]# usermod -G ateam alex
[root@localhost ~]# su alex
[alex@localhost root]$ cd /home/ateam-text/
[alex@localhost ateam-text]$ 

七、建立文件alexfile3

[alex@localhost ateam-text]$ touch alexfile3
[alex@localhost ateam-text]$ ls
alexfile3
[alex@localhost ateam-text]$ 

八、觀察新文件默認的所屬人和所屬組

[alex@localhost ateam-text]$ ls -l alexfile3 
-rw-rw-r--. 1 alex alex 0 8月  23 08:55 alexfile3
[alex@localhost ateam-text]$

九、將文件的所屬組更改成ateam

[alex@localhost ateam-text]$ chown :ateam alexfile3 
[alex@localhost ateam-text]$ ls -l alexfile3 
-rw-rw-r--. 1 alex ateam 0 8月  23 08:55 alexfile3

十、切換到zhang

[alex@localhost ateam-text]$ su - zhang
密碼:
上一次登陸:四 8月 23 08:18:24 CST 2018pts/1 上
[zhang@localhost ~]$ id
uid=1000(zhang) gid=1000(zhang) 組=1000(zhang),10(wheel) 環境=unconfined_u:unconfined_r:unconfined_t
:s0-s0:c0.c1023

十一、進入/home/ateam-text,編輯alexfile3

[root@localhost zhang]# usermod -G ateam zhang
[root@localhost zhang]# su zhang
[zhang@localhost ~]$ cd /home/ateam-text/
[zhang@localhost ateam-text]$ echo 'text' >> alexfile3 
[zhang@localhost ateam-text]$ cat alexfile3 
text
[zhang@localhost ateam-text]$ 

管理默認權限和文件訪問

配置一個目錄,在這個目錄下新建立文件,目錄的所屬成員自動具備寫權限,使用特殊權限,和默認的umask設置

特殊權限

對於可執行的文件setuid(或setgid)權限表示以文件所屬人(或者所屬組)的身份運行命令,而不是以執行者的身份運行,例如passwd命令

[zhang@localhost ateam-text]$ ls -l /usr/bin/passwd 
-rwsr-xr-x. 1 root root 27832 6月  10 2014 /usr/bin/passwd
[zhang@localhost ateam-text]$ 

目錄的粘滯位(sticky)能夠設置刪除文件的特殊權限:僅文件的全部者(和root)能夠刪除目錄中的文件,例如/tmp目錄:

[zhang@localhost ateam-text]$ ls -ld /tmp/
drwxrwxrwt. 19 root root 4096 8月  23 09:05 /tmp/
[zhang@localhost ateam-text]$ 

目錄的setgid權限表示在該目錄中新建的文件將繼承該目錄的所屬組,額不是建立用戶的主要組。

設置特殊權限

用符號表示:setuid=u+s;setgid=g+s;sticky=o+t

用數值表示(第一位數字):setuid=4;setgid=2;sticky=1

在一個目錄總建立的文件自動繼承目錄的組

默認設置容許系統上的素有用戶組查看公共區域的文件,調整使得建立的文件擁有更多限制的權限。

默認文件權限

在系統中建立文件和目錄的時候都會有默認權限,文件時666,目錄是777.

可是咱們會發現建立出的文件和目錄不是666和777,這是由於其中一些權限被shell的umask屏蔽了。

bash shell的系統默認umask在/etc/profile 和/etc/bashrc文件中定義。用戶能夠經過本身的家目錄中的.bash_profile 和.bashrc 文件的設置來覆蓋系統默認值

建立新文件,查看默認umask對權限的影響

[root@localhost ateam-text]# touch newfile1
[root@localhost ateam-text]# ls -l newfile1 
-rw-r--r--. 1 root root 0 8月  23 09:12 newfile1
[root@localhost ateam-text]# mkdir newdir1
[root@localhost ateam-text]# ls -ld newdir1/
drwxr-xr-x. 2 root root 6 8月  23 09:12 newdir1/
[root@localhost ateam-text]# 

將umask設置爲0,次設置不會屏蔽新文件的任何權限

[root@localhost ateam-text]# umask 0
[root@localhost ateam-text]# touch newfile2
[root@localhost ateam-text]# ls -l newfile
ls: 沒法訪問newfile: 沒有那個文件或目錄
[root@localhost ateam-text]# ls -l newfile2
-rw-rw-rw-. 1 root root 0 8月  23 09:13 newfile2
[root@localhost ateam-text]# mkdir newdir2
[root@localhost ateam-text]# ls -ld newdir2
drwxrwxrwx. 2 root root 6 8月  23 09:13 newdir2
[root@localhost ateam-text]# 

將umask設置爲007,此設置將屏蔽其餘人的權限

[root@localhost ateam-text]# umask 007
[root@localhost ateam-text]# ls
alexfile3  newdir1  newdir2  newfile1  newfile2  newfile3
[root@localhost ateam-text]# ls -l newfile3
-rw-rw----. 1 root root 0 8月  23 09:15 newfile3
[root@localhost ateam-text]# mkdir newdir3
[root@localhost ateam-text]# ls -ld newdir3
drwxrwx---. 2 root root 6 8月  23 09:16 newdir3
[root@localhost ateam-text]# 

將umask設置爲027,次設置將屏蔽組的寫權限和其餘人的全部權限

[root@localhost ateam-text]# umask 027
[root@localhost ateam-text]# touch newfile4
[root@localhost ateam-text]# ls -l newfile4
-rw-r-----. 1 root root 0 8月  23 09:17 newfile4
[root@localhost ateam-text]# mkdir newdir4
[root@localhost ateam-text]# ls -ld newdir4
drwxr-x---. 2 root root 6 8月  23 09:17 newdir4
[root@localhost ateam-text]# 

使用root用戶登陸更改費特權用戶默認的umask,拒絕非組中的用戶全部訪問。

修改/etc/bshrc 和/etc/profile更改bash shell 用戶的默認umask,由於非特權用戶的默認umask是002,在文件中找到umask命令設置umask值,更改他們,所屬組爲007.

umask:Linux中新建文件或者目錄的初始權限

練習:

一、切換到alex用戶,肯定用戶的umask

[root@localhost ateam-text]# su alex
[alex@localhost ateam-text]$ umask 
0002
[alex@localhost ateam-text]$ 

二、建立新目錄/tmp/shared 和新文件/tmp/shared/defaults,查看默認umask對權限的影響

[alex@localhost ateam-text]$ mkdir /tmp/shared
[alex@localhost ateam-text]$ ls -ld /tmp/shared/
drwxrwxr-x. 2 alex alex 6 8月  23 09:25 /tmp/shared/
[alex@localhost ateam-text]$ touch /tmp/shared/defaults
[alex@localhost ateam-text]$ ls -l /tmp/shared/defaults 
-rw-rw-r--. 1 alex alex 0 8月  23 09:25 /tmp/shared/defaults
[alex@localhost ateam-text]$ 

三、將/tmp/share目錄所屬組改成ateam

[alex@localhost ateam-text]$ chown :ateam /tmp/shared/
[alex@localhost ateam-text]$ ls -ld /tmp/shared/
drwxrwxr-x. 2 alex ateam 22 8月  23 09:25 /tmp/shared/
[alex@localhost ateam-text]$ 

四、在/tmp/shared目錄中建立一個新的文件

[alex@localhost ateam-text]$ touch /tmp/shared/alex3
[alex@localhost ateam-text]$ ls -l /tmp/shared/alex3 
-rw-rw-r--. 1 alex alex 0 8月  23 09:27 /tmp/shared/alex3
[alex@localhost ateam-text]$ 

五、確保在/tmp/shared目錄中新建的文件可以繼承ateam組

[alex@localhost ateam-text]$ chmod g+s /tmp/shared/
[alex@localhost ateam-text]$ ls -ld /tmp/shared/
drwxrwsr-x. 2 alex ateam 35 8月  23 09:27 /tmp/shared/
[alex@localhost ateam-text]$ touch /tmp/shared/alex4
[alex@localhost ateam-text]$ ls -l /tmp/shared/alex4
-rw-rw-r--. 1 alex ateam 0 8月  23 09:28 /tmp/shared/alex4
[alex@localhost ateam-text]$ 

六、更改Alex的umask,使得建立的新文件所屬組擁有隻讀權限,其餘人,沒有訪問權限

[alex@localhost ateam-text]$ umask 
0002
[alex@localhost ateam-text]$ umask 027
[alex@localhost ateam-text]$ touch /tmp/shared/alex5
[alex@localhost ateam-text]$ ls -l /tmp/shared/alex5
-rw-r-----. 1 alex ateam 0 8月  23 09:30 /tmp/shared/alex5
[alex@localhost ateam-text]$ 

七、切換到alex,打開一個新的shell查看umask

[alex@localhost ateam-text]$ su - alex
密碼:
上一次登陸:四 8月 23 09:23:41 CST 2018pts/1 上
[alex@localhost ~]$ umask
0002
[alex@localhost ~]$ 

八、將Alex默認的umask更改成禁止不屬於組的用戶的全部訪問權限

[alex@localhost ~]$ echo 'umask 007' >> ~/.bashrc 
[alex@localhost ~]$ cat ~/.bashrc 
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions
umask 007
[alex@localhost ~]$ 

重新使用Alex登陸

[alex@localhost ~]$ umask
0007
[alex@localhost ~]$ 
相關文章
相關標籤/搜索