文章目錄
- 前言
- 一、學習Linux權限的鋪墊知識
- 1.Linux的文件分類
- 2.Linux的用戶
- 2.1 Linux下用戶分類
- 2.2 創建普通用戶
- 2.3 切換用戶
- 2.4 sudo(提升權限的指令)
- 二、Linux權限的概念以及修改方法
- 1.權限的概念
- 2.文件訪問權限 和 訪問者身份的相關修改(設置)方法
- 2.1 文件權限屬性的8進制數值表示方法
- 2.2 文件的初始權限 和 umask 指令
- 2.3 chmod 指令
- 2.4 chown 指令
- 2.5 chgrp 指令
- 三、Linux權限的效果實踐
- 1.普通文件的權限效果
- 2.目錄文件的權限效果
- 3.文件的權限效果受其所在目錄權限的影響
- 四、粘滯位(常用于合作開發)
前言
一、學習Linux權限的鋪墊知識(Linux的文件分類 和 Linux的用戶相關知識)
二、Linux權限的概念以及修改方法(文件訪問權限 和 訪問者身份的相關修改方法)
三、Linux權限的效果實踐(普通文件的權限效果 和 目錄文件的權限效果)
四、粘滯位(常用于合作開發)
一、學習Linux權限的鋪墊知識
1.Linux的文件分類
windows操作系統區分文件類型,是用文件后綴區分的;
而Linux操作系統區分文件類型,是以文件的屬性列區分的。
文件類型:
? d:目錄文件(文件夾)
? -:普通文件 (例如:源代碼、文本文件、可執行程序、音視頻、各種文檔 和 庫文件等)
? l:軟鏈接(類似Windows的快捷方式)
? b:塊設備文件(例如硬盤、光驅等)
? p:管道文件
? c:字符設備文件(例如屏幕等串口設備)
? s:套接口文件
注: 前兩種文件類型是常見類型,后面幾種文件類型一般都是在特定場景下才會用到(不常用)。故后面只對前兩種類型的文件進行權限討論。
- Linux操作系統區分文件類型,是以文件的屬性列區分的,更改文件的后綴不會影響文件類型:
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 4
drwxrwxr-x 2 zh zh 4096 Apr 24 15:01 ppt
-rw-rw-r-- 1 zh zh 0 Apr 24 15:00 test.c
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ mv ppt ppt.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 4
drwxrwxr-x 2 zh zh 4096 Apr 24 15:01 ppt.txt
-rw-rw-r-- 1 zh zh 0 Apr 24 15:00 test.c
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ mv test.c test
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 4
drwxrwxr-x 2 zh zh 4096 Apr 24 15:01 ppt.txt
-rw-rw-r-- 1 zh zh 0 Apr 24 15:00 test
- 雖然Linux操作系統不以文件后綴區分文件類型,但這并不代表Linux下的工具不使用文件后綴(比如gcc):
兩個普通文件 test.c 和 test.txt 中的內容一模一樣,只有文件后綴不同。gcc成功編譯 test.c 文件,但編譯 test.txt 文件直接報錯。可以看出gcc工具使用了文件后綴,它只會編譯特定后綴的普通文件。
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 4
-rw-rw-r-- 1 zh zh 73 Apr 24 15:12 test.c
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat test.c
#include <stdio.h>int main()
{printf("hello world!\n");return 0;
}
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ gcc test.c
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 16
-rwxrwxr-x 1 zh zh 8360 Apr 24 15:13 a.out
-rw-rw-r-- 1 zh zh 73 Apr 24 15:12 test.c
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ./a.out
hello world!
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 4
-rw-rw-r-- 1 zh zh 73 Apr 24 15:12 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat test.txt
#include <stdio.h>int main()
{printf("hello world!\n");return 0;
}
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ gcc test.txt
test.txt: file not recognized: File format not recognized
collect2: error: ld returned 1 exit status
2.Linux的用戶
2.1 Linux下用戶分類
Linux是一個多用戶操作系統。
Linux下的用戶分為兩類:超級用戶(root)和 普通用戶。
? 超級用戶:可以在linux系統下做任何事情,不受權限限制
? 普通用戶:在linux下做有限的事情,受權限限制
( 超級用戶的命令行提示符是“#”,普通用戶的命令行提示符是“$” )
root用戶在安裝Linux操作系統時就創建好了,root用戶只有一個,而普通用戶可以有多個。
(root用戶可以創建 和 刪除普通用戶)
2.2 創建普通用戶
每個用戶都有對應的家目錄,當啟動機器時,登錄指定用戶,他們都會默認從自己的家目錄開始。
root用戶在安裝操作系統的時候,就已經內置了工作目錄: /root,這就是root用戶的家目錄,登錄root用戶時會默認從這個路徑開始。
root用戶可以創建普通用戶,每次新建?個普通用戶都會在/home目錄下為新用戶創建新的工作目錄,目錄以新用戶名稱命名。
[zh@iZbp1dr1jtgcuih41mw88oZ d3]$ cd /home
[zh@iZbp1dr1jtgcuih41mw88oZ home]$ ls
ccy zh
比如我的Linux機器下創建了兩個普通用戶,ccy 和 zh,那么他們的家目錄分別是/home/ccy 和 /home/zh
- 鋪墊完畢,接下來展示root用戶是如何創建普通用戶:
? 第一步(添加新用戶) :adduser 用戶名(自己取)
? 第二步(為新用戶設置密碼):passwd 用戶名
(注: Linux下輸密碼是默認不回顯的,為了保護用戶隱私)
[root@iZbp1dr1jtgcuih41mw88oZ zh]# adduser lisi
[root@iZbp1dr1jtgcuih41mw88oZ zh]# passwd lisi
Changing password for user lisi.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
[root@iZbp1dr1jtgcuih41mw88oZ zh]# ls /home
ccy lisi zh
- root用戶刪除普通用戶(userdel -r 用戶名):
[root@iZbp1dr1jtgcuih41mw88oZ zh]# ls /home
ccy lisi zh
[root@iZbp1dr1jtgcuih41mw88oZ zh]# userdel -r lisi
[root@iZbp1dr1jtgcuih41mw88oZ zh]# ls /home
ccy zh
2.3 切換用戶
切換用戶的兩種方法:
語法:su 用戶名
功能:切換到指定用戶(切換到root用戶時,root可省略),不會更改當前工作目錄
語法:su - 用戶名
功能:切換到指定用戶(切換到root用戶時,root可省略),將工作目錄切換到指定用戶的家目錄
注: root用戶切換到普通用戶不用輸密碼;普通用戶切換到root用戶 或 其他普通用戶都要輸對應密碼。
- su 用戶名:切換到指定用戶(切換到root用戶時,root可省略),不會更改當前工作目錄
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ whoami
zh
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ pwd
/home/zh/ppt
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ su
Password:
[root@iZbp1dr1jtgcuih41mw88oZ ppt]# whoami
root
[root@iZbp1dr1jtgcuih41mw88oZ ppt]# pwd
/home/zh/ppt
[root@iZbp1dr1jtgcuih41mw88oZ ppt]# su zh
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ whoami
zh
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ pwd
/home/zh/ppt
- su - 用戶名:切換到指定用戶(切換到root用戶時,root可省略),將工作目錄切換到指定用戶的家目錄
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ whoami
zh
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ pwd
/home/zh/ppt
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ su -
Password:
Last login: Thu Apr 24 16:39:36 CST 2025 on pts/0
[root@iZbp1dr1jtgcuih41mw88oZ ~]# whoami
root
[root@iZbp1dr1jtgcuih41mw88oZ ~]# pwd
/root
[root@iZbp1dr1jtgcuih41mw88oZ ~]# su - zh
Last login: Thu Apr 24 16:42:54 CST 2025 on pts/0
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ whoami
zh
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ pwd
/home/zh
2.4 sudo(提升權限的指令)
sudo(superuser do)是Linux系統中用于普通用戶臨時提升權限的指令,允許普通用戶以root權限執行單條命令。(該機制需通過修改/etc/sudoers配置文件實現)
- 普通用戶 zh 想將 test.txt文件 復制到 /usr路徑下,但是這個操作沒有被允許,因為權限不夠:
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cp test.txt /usr
cp: cannot create regular file ‘/usr/test.txt’: Permission denied
權限不夠,那我們就嘗試使用 sudo 指令來提升普通用戶的權限,但是 sudo 執行這條命令依然沒有成功,原因是:zh is not in the sudoers file(該用戶沒有在sudoers file文件中,這個文件實際就是/etc/sudoers配置文件)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ sudo cp test.txt /usrWe trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:#1) Respect the privacy of others.#2) Think before you type.#3) With great power comes great responsibility.[sudo] password for zh:
zh is not in the sudoers file. This incident will be reported.
- 普通用戶要想使用sudo指令提升權限,需要使用root用戶修改/etc/sudoers配置文件,root用戶是管理員,修改該配置文件將普通用戶加入,就相當于將該普通用戶放進白名單。 修改該配置文件的過程如下:
第一步:在root用戶下,打開/etc/sudoers配置文件(通過nano編輯器)。跳轉到100行左右位置找到下圖圈出的內容
[root@iZbp1dr1jtgcuih41mw88oZ zh]# nano /etc/sudoers
第二步:將普通用戶zh加入該配置文件。模仿第一行root用戶的寫法,另起一行,輸入zh后按tab鍵,再輸入ALL=(ALL),再按tab鍵,最后輸入ALL。配置完畢,保存退出。
- 將普通用戶zh加入到/etc/sudoers配置文件后。再使用 sudo 提權執行這條命令就成功了。
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ sudo cp test.txt /usr
[sudo] password for zh:
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l /usr/test.txt
-rw-r--r-- 1 root root 0 Apr 24 19:05 /usr/test.txt
二、Linux權限的概念以及修改方法
1.權限的概念
權限 = 用戶身份 + 文件的權限屬性
- 文件訪問者身份的分類:
? 文件的所有者:u—User
? 文件的所屬組(組中用戶大于等于1人):g—Group
? 其它用戶(除所有者 和 所屬組中用戶外的用戶):o—Others
(注:用戶初創建文件時,文件的所屬組默認只有你自己)
其它用戶在文件信息中不會顯示,因為它也無需顯示,只要知道文件的所有者 和 所屬組的信息也就知道了其它用戶的信息(其它用戶是除所有者 和 所屬組中用戶外的所有用戶)
- 權限屬性的分類:
? 讀(r/4):Read對普通文件而言,具有讀取文件內容的權限;對目錄來說,具有瀏覽該目錄下文件信息的權限
? 寫(w/2):Write對普通文件而言,具有修改文件內容的權限;對目錄來說具有刪除、新增 和 移動目錄內文件的權限
? 執行(x/1):execute對普通文件而言,具有執行文件的權限;對目錄來說,具有進入目錄的權限
? “—”:表示不具有該項權限
文件的權限屬性有9列,文件訪問者身份有3種,正好每種身份對應3列權限屬性。(文件的所有者對應前3列,文件的所屬組對應中間3列,其它用戶對應最后3列)
2.文件訪問權限 和 訪問者身份的相關修改(設置)方法
2.1 文件權限屬性的8進制數值表示方法
-rw-r--r-- 1 root root 0 Apr 24 19:05 /usr/test.txt
rw-r- -r- -對應110100100 轉換成8進制:644
-rw-rw-r-- 1 zh zh 0 Apr 24 18:45 test.txt
rw-rw-r- -對應110110100 轉換成8進制:664
-rwxr-xr-x 1 root root 62688 Nov 1 2021 ar
rwxr-xr-x對應111101101 轉換成8進制:755
drwxrwxr-x 2 zh zh 4096 Apr 24 16:39 ppt
rwxrwxr-x對應111111101 轉換成8進制:775
2.2 文件的初始權限 和 umask 指令
umask 指令的介紹:
使用格式:
(1)查看文件掩碼:umask
(2)修改文件掩碼:umask 新的文件掩碼值
功能:
? 查看或修改文件掩碼
? 新建普通文件的默認權限=666
? 新建目錄的默認權限=777
? 但實際上你所創建的文件和目錄,看到的權限往往不是上面這個值。原因就是創建文件或目錄的時候還要受到umask的影響。假設默認權限是mask,則實際創建的出來的文件權限是: mask&(~umask)
說明: 超級用戶默認掩碼值為0022,普通用戶默認為0002。(計算文件權限時忽略文件掩碼的第一位)
- 得到新建文件起始權限的計算過程
新建普通文件的默認權限=666 ,新建目錄的默認權限=777
但實際上你所創建的文件和目錄,看到的權限往往不是上面這個值。原因就是創建文件或目錄的時候還要受到umask的影響。假設默認權限是mask,則實際創建的出來的文件權限是: mask&(~umask)
普通用戶的權限掩碼默認為0002 (計算文件權限時忽略文件掩碼的第一位,也就是把0002當成002)
要轉換為2進制計算:權限掩碼002的2進制為000 000 010,對權限掩碼取反~得 111 111 101
新建普通文件的默認權限 = 666 轉換為2進制為 110 110 110
新建普通文件的起始權限 = 110 110 110 & 111 111 101 = 110 110 100,也就是 rw-rw-r- -
新建目錄的默認權限 = 777 轉換為2進制為 111 111 111
新建目錄的起始權限 = 111 111 111 & 111 111 101 = 111 111 101,也就是 rwxrwxr-x
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ umask
0002
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ touch test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ mkdir ppt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 4
drwxrwxr-x 2 zh zh 4096 Apr 24 21:14 ppt
-rw-rw-r-- 1 zh zh 0 Apr 24 21:13 test.txt
- 修改文件掩碼會影響文件的起始權限
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ umask 0000
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ umask
0000
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ touch test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ mkdir ppt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 4
drwxrwxrwx 2 zh zh 4096 Apr 24 21:37 ppt
-rw-rw-rw- 1 zh zh 0 Apr 24 21:36 test.txt
2.3 chmod 指令
使用格式:
(1)chmod u/g/o[+/-/=]rwx 文件名
(同時修改多個身份對應的權限要用" , "隔開)
(2)chmod a[+/-/=]rwx 文件名
(3)chmod 8進制權限寫法 文件名
功能: 設置文件的訪問權限
說明: 只有文件的擁有者和root才可以改變文件的權限
用戶符號:
? u:所有者
? g:所屬組
? o:其它用戶
? a:所有用戶
- chmod u/g/o[+/-/=]rwx 文件名
(1)chmod u/g/o+rwx 文件名
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
-r--r--r-- 1 zh zh 0 Apr 24 21:36 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ chmod u+w test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
-rw-r--r-- 1 zh zh 0 Apr 24 21:36 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ chmod g+w test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
-rw-rw-r-- 1 zh zh 0 Apr 24 21:36 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ chmod o+w test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
-rw-rw-rw- 1 zh zh 0 Apr 24 21:36 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ chmod u+x,g+x,o+x test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
-rwxrwxrwx 1 zh zh 0 Apr 24 21:36 test.txt
(2)chmod u/g/o-rwx 文件名
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
-rwxrwxrwx 1 zh zh 0 Apr 24 21:36 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ chmod u-rx,g-wx,o-x test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
--w-r--rw- 1 zh zh 0 Apr 24 21:36 test.txt
(3)chmod u/g/o=rwx 文件名
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
--w-r--rw- 1 zh zh 0 Apr 24 21:36 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ chmod u=rwx,g=rwx,o=rwx test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
-rwxrwxrwx 1 zh zh 0 Apr 24 21:36 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ chmod u=r,g=w,o=x test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
-r---w---x 1 zh zh 0 Apr 24 21:36 test.txt
- chmod a[+/-/=]rwx 文件名
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
-rwxrwxrwx 1 zh zh 0 Apr 24 21:36 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ chmod a-wx test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
-r--r--r-- 1 zh zh 0 Apr 24 21:36 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ chmod a+x test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
-r-xr-xr-x 1 zh zh 0 Apr 24 21:36 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ chmod a=w test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
--w--w--w- 1 zh zh 0 Apr 24 21:36 test.txt
- chmod 8進制權限寫法 文件名
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
--w--w--w- 1 zh zh 0 Apr 24 21:36 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ chmod 777 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
-rwxrwxrwx 1 zh zh 0 Apr 24 21:36 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ chmod 111 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
---x--x--x 1 zh zh 0 Apr 24 21:36 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ chmod 614 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
-rw---xr-- 1 zh zh 0 Apr 24 21:36 test.txt
2.4 chown 指令
格式:
(1)chown 新所有者名 文件名
(2)chown 新所有者名:新所屬組名 文件名
功能: 可以修改文件的所有者,也可以同時修改文件的所有者和所屬組(需要root權限)
說明: 新所有者名和新所屬組名必須是已存在的
- chown 新所有者名 文件名(修改文件的所有者)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
-rw---xr-- 1 zh zh 0 Apr 24 21:36 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ sudo chown ccy test.txt
[sudo] password for zh:
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
-rw---xr-- 1 ccy zh 0 Apr 24 21:36 test.txt
- chown 新所有者名:新所屬組名 文件名(同時修改文件的所有者和所屬組)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
-rw---xr-- 1 ccy ccy 0 Apr 24 21:36 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ sudo chown zh:zh test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
-rw---xr-- 1 zh zh 0 Apr 24 21:36 test.txt
2.5 chgrp 指令
格式: chgrp 新所屬組名 文件名
功能: 修改文件的所屬組(需要root權限)
說明: 新所屬組名必須是已存在的
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
-rw---xr-- 1 ccy zh 0 Apr 24 21:36 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ sudo chgrp ccy test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
-rw---xr-- 1 ccy ccy 0 Apr 24 21:36 test.txt
三、Linux權限的效果實踐
1.普通文件的權限效果
? 讀(r/4):read對普通文件而言,具有讀取文件內容的權限
? 寫(w/2):write對普通文件而言,具有修改文件內容的權限
? 執行(x/1):execute對普通文件而言,具有執行文件的權限
? “—”:表示不具有該項權限
- w 對普通文件而言,具有修改文件內容的權限;r 對普通文件而言,具有讀取文件內容的權限
test.txt 文件 的所有者具有rw權限,所屬組具有r權限,其它用戶沒有任何權限。
zh用戶屬于該文件的所有者,所以他具有讀取文件內容和修改文件內容的權限;
ccy用戶屬于該文件的所屬組,所以他具有讀取文件內容的權限;
lihua用戶屬于該文件的其它用戶,所以他不具備任何操作該文件的權限
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ ls -l
total 4
-rw-r----- 1 zh ccy 12 Apr 25 19:03 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ echo "hello world" > test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ cat test.txt
hello world
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ su ccy
Password:
[ccy@iZbp1dr1jtgcuih41mw88oZ ppt]$ echo "one piece" > test.txt
bash: test.txt: Permission denied
[ccy@iZbp1dr1jtgcuih41mw88oZ ppt]$ cat test.txt
hello world
[ccy@iZbp1dr1jtgcuih41mw88oZ ppt]$ su lihua
Password:
[lihua@iZbp1dr1jtgcuih41mw88oZ ppt]$ echo "one piece" > test.txt
bash: test.txt: Permission denied
[lihua@iZbp1dr1jtgcuih41mw88oZ ppt]$ cat test.txt
cat: test.txt: Permission denied
// Permission denied 代表權限不夠,操作不被允許
- 一個可執行文件 = 可執行權限 + 可執行能力
(一個文件具備可執行權限并不代表這個文件能執行)
(1)gcc 編譯 test.c 文件生成了可執行文件 a.out,緊接著執行 a.out,執行成功;
我們刪去了 a.out文件的x權限,然后再次執行 a.out,執行失敗(a.out文件具備可執行能力,但不具備可執行權限)
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ ls -l
total 4
-rw-rw-r-- 1 zh zh 69 Apr 25 20:14 test.c
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ cat test.c
#include <stdio.h>
int main()
{printf("one piece\n");return 0;
}
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ gcc test.c
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ ls -l
total 16
-rwxrwxr-x 1 zh zh 8360 Apr 25 20:14 a.out
-rw-rw-r-- 1 zh zh 69 Apr 25 20:14 test.c
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ ./a.out
one piece
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ chmod u-x a.out
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ ls -l
total 16
-rw-rwxr-x 1 zh zh 8360 Apr 25 20:14 a.out
-rw-rw-r-- 1 zh zh 69 Apr 25 20:14 test.c
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ ./a.out
bash: ./a.out: Permission denied
(2)test.c 文件是一個未經過編譯的普通c文件,它不具備可執行能力,即使我們賦予它可執行權限,它也依舊不能被執行(test.c 文件具備可執行權限,但不具備可執行能力)
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ ls -l
total 4
-rw-rw-r-- 1 zh zh 69 Apr 25 20:14 test.c
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ chmod u+x test.c
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ ls -l
total 4
-rwxrw-r-- 1 zh zh 69 Apr 25 20:14 test.c
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ ./test.c
./test.c: line 2: syntax error near unexpected token `('
./test.c: line 2: `int main()'
2.目錄文件的權限效果
? 讀(r/4):read對目錄來說,具有瀏覽該目錄下文件信息的權限
? 寫(w/2):write對目錄來說,具有刪除、新增 和 移動目錄下文件 以及 修改目錄下文件的文件名的權限
? 執行(x/1):execute對目錄來說,具有進入目錄的權限
? “—”:表示不具有該項權限
注: 對目錄來說,x 權限是 r 和 w 權限的前提,不具備 x 權限,即使有 rw 權限也沒有任何效果。
- r 對目錄來說,具有瀏覽該目錄下文件信息的權限;w 對目錄來說,具有刪除、新增 和 移動目錄下文件 以及 修改目錄下文件的文件名的權限
ppt目錄 的所有者具有rwx權限,所屬組具有rx權限,其它用戶具有x權限。
zh用戶屬于該目錄的所有者,所以他具有進入目錄、瀏覽該目錄下文件信息 以及 具有刪除、新增 和 移動目錄下文件 的權限;
ccy用戶屬于該目錄的所屬組,所以他具有進入目錄、瀏覽該目錄下文件信息 的權限;
lihua用戶屬于該目錄的其它用戶,所以他只具有進入目錄的權限
[zh@iZbp1dr1jtgcuih41mw88oZ ufc]$ ls -l
total 4
drwxr-x--x 3 zh ccy 4096 Apr 25 21:23 ppt
[zh@iZbp1dr1jtgcuih41mw88oZ ufc]$ ls -l ppt
total 8
drwxrwxr-x 2 zh zh 4096 Apr 25 20:55 ggb
-rw-rw-r-- 1 zh zh 0 Apr 25 20:55 hello.txt
-rw-rw-r-- 1 zh zh 69 Apr 25 20:14 test.c
[zh@iZbp1dr1jtgcuih41mw88oZ ufc]$ rm ppt/hello.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ufc]$ ls -l ppt
total 8
drwxrwxr-x 2 zh zh 4096 Apr 25 20:55 ggb
-rw-rw-r-- 1 zh zh 69 Apr 25 20:14 test.c
[zh@iZbp1dr1jtgcuih41mw88oZ ufc]$ touch ppt/dwg
[zh@iZbp1dr1jtgcuih41mw88oZ ufc]$ ls -l ppt
total 8
-rw-rw-r-- 1 zh zh 0 Apr 25 21:30 dwg
drwxrwxr-x 2 zh zh 4096 Apr 25 20:55 ggb
-rw-rw-r-- 1 zh zh 69 Apr 25 20:14 test.c
[zh@iZbp1dr1jtgcuih41mw88oZ ufc]$ mv ppt/dwg ppt/abc.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ufc]$ ls -l ppt
total 8
-rw-rw-r-- 1 zh zh 0 Apr 25 21:32 abc.txt
drwxrwxr-x 2 zh zh 4096 Apr 25 20:55 ggb
-rw-rw-r-- 1 zh zh 69 Apr 25 20:14 test.c
[zh@iZbp1dr1jtgcuih41mw88oZ ufc]$ su ccy
Password:
[ccy@iZbp1dr1jtgcuih41mw88oZ ufc]$ ls -l
total 4
drwxr-x--x 3 zh ccy 4096 Apr 25 21:32 ppt
[ccy@iZbp1dr1jtgcuih41mw88oZ ufc]$ ls -l ppt
total 8
-rw-rw-r-- 1 zh zh 0 Apr 25 21:32 abc.txt
drwxrwxr-x 2 zh zh 4096 Apr 25 20:55 ggb
-rw-rw-r-- 1 zh zh 69 Apr 25 20:14 test.c
[ccy@iZbp1dr1jtgcuih41mw88oZ ufc]$ rm ppt/abc.txt
rm: remove write-protected regular empty file ‘ppt/abc.txt’? y
rm: cannot remove ‘ppt/abc.txt’: Permission denied
[ccy@iZbp1dr1jtgcuih41mw88oZ ufc]$ touch ppt/one.txt
touch: cannot touch ‘ppt/one.txt’: Permission denied
[ccy@iZbp1dr1jtgcuih41mw88oZ ufc]$ su lihua
Password:
[lihua@iZbp1dr1jtgcuih41mw88oZ ufc]$ ls -l
total 4
drwxr-x--x 3 zh ccy 4096 Apr 25 21:32 ppt
[lihua@iZbp1dr1jtgcuih41mw88oZ ufc]$ ls -l ppt
ls: cannot open directory ppt: Permission denied
[lihua@iZbp1dr1jtgcuih41mw88oZ ufc]$ touch ppt/one.txt
touch: cannot touch ‘ppt/one.txt’: Permission denied
[lihua@iZbp1dr1jtgcuih41mw88oZ ufc]$ cd ppt
[lihua@iZbp1dr1jtgcuih41mw88oZ ppt]$ pwd
/home/zh/ufc/ppt
- 對目錄來說,x 權限是 r 和 w 權限的前提,不具備 x 權限,即使有 rw 權限也沒有任何效果
[zh@iZbp1dr1jtgcuih41mw88oZ ufc]$ ls -l
total 4
drw-rw-rw- 3 zh ccy 4096 Apr 25 21:32 ppt
[zh@iZbp1dr1jtgcuih41mw88oZ ufc]$ ls -l ppt
ls: cannot access ppt/ggb: Permission denied
ls: cannot access ppt/abc.txt: Permission denied
ls: cannot access ppt/test.c: Permission denied
total 0
-????????? ? ? ? ? ? abc.txt
d????????? ? ? ? ? ? ggb
-????????? ? ? ? ? ? test.c
[zh@iZbp1dr1jtgcuih41mw88oZ ufc]$ rm ppt/abc.txt
rm: cannot remove ‘ppt/abc.txt’: Permission denied
[zh@iZbp1dr1jtgcuih41mw88oZ ufc]$ touch ppt/one.txt
touch: cannot touch ‘ppt/one.txt’: Permission denied
3.文件的權限效果受其所在目錄權限的影響
- ccy用戶沒有進入ppt目錄的權限(我們是使用zh用戶進入ppt目錄,再切成ccy用戶),
所以即使ppt目錄中的 ggb目錄 和 test.c文件 給了ccy用戶所有權限,但是ccy用戶仍然不能對這兩個文件進行任何操作。因為ccy用戶連進入ppt目錄的權限都沒有,所以不具備操作其下文件的資格。
[zh@iZbp1dr1jtgcuih41mw88oZ ufc]$ ls -l
total 4
drwx------ 3 zh zh 4096 Apr 25 22:21 ppt
[zh@iZbp1dr1jtgcuih41mw88oZ ufc]$ cd ppt
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ ls -l
total 8
drwxrwxrwx 3 ccy ccy 4096 Apr 25 22:03 ggb
-rwxrwxrwx 1 ccy ccy 12 Apr 25 22:24 test.c
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ su ccy
Password:
[ccy@iZbp1dr1jtgcuih41mw88oZ ppt]$ echo "hello world" > test.c
bash: test.c: Permission denied
[ccy@iZbp1dr1jtgcuih41mw88oZ ppt]$ cat test.c
cat: test.c: Permission denied
[ccy@iZbp1dr1jtgcuih41mw88oZ ppt]$ ls -l ggb
ls: cannot access ggb: Permission denied
[ccy@iZbp1dr1jtgcuih41mw88oZ ppt]$ touch ggd/kfc.txt
touch: cannot touch ‘ggd/kfc.txt’: Permission denied
[ccy@iZbp1dr1jtgcuih41mw88oZ ppt]$ cd ggb
bash: cd: ggb: Permission denied
- ccy用戶具有進入ppt的權限,就可以正常操作其下文件
[zh@iZbp1dr1jtgcuih41mw88oZ ufc]$ ls -l
total 4
drwx--x--x 3 zh zh 4096 Apr 25 22:21 ppt
[zh@iZbp1dr1jtgcuih41mw88oZ ufc]$ ls -l ppt
total 8
drwxrwxrwx 3 ccy ccy 4096 Apr 25 22:03 ggb
-rwxrwxrwx 1 ccy ccy 12 Apr 25 22:24 test.c
[zh@iZbp1dr1jtgcuih41mw88oZ ufc]$ su ccy
Password:
[ccy@iZbp1dr1jtgcuih41mw88oZ ufc]$ cd ppt
[ccy@iZbp1dr1jtgcuih41mw88oZ ppt]$ echo "hello world" > test.c
[ccy@iZbp1dr1jtgcuih41mw88oZ ppt]$ cat test.c
hello world
[ccy@iZbp1dr1jtgcuih41mw88oZ ppt]$ ls -l ggb
total 4
drwxrwxr-x 2 zh zh 4096 Apr 25 22:03 lgd
[ccy@iZbp1dr1jtgcuih41mw88oZ ppt]$ touch ggb/kfc.txt
[ccy@iZbp1dr1jtgcuih41mw88oZ ppt]$ cd ggb
[ccy@iZbp1dr1jtgcuih41mw88oZ ggb]$ pwd
/home/zh/ufc/ppt/ggb
四、粘滯位(常用于合作開發)
普通用戶的家目錄只允許自己和root用戶進入;root用戶的家目錄只允許root用戶進入。所以多名普通用戶要進行合作開發的話,一般不會在家目錄下進行。
[root@iZbp1dr1jtgcuih41mw88oZ home]# pwd
/home
[root@iZbp1dr1jtgcuih41mw88oZ home]# ls -l
total 12
drwx------ 2 ccy ccy 4096 Apr 10 21:03 ccy
drwx------ 2 lihua lihua 4096 Apr 25 23:14 lihua
drwx------ 3 zh zh 4096 Apr 25 21:03 zh
[root@iZbp1dr1jtgcuih41mw88oZ /]# ls -ld root
dr-xr-x---. 6 root root 4096 Apr 14 11:02 root
想要實現多名用戶合作開發(數據共享),一般會用root用戶在家目錄外創建一個公共的目錄,給其他用戶身份放開權限(rwx),讓多名普通用戶以其他用戶身份在公共的目錄下實現合作開發:
[root@iZbp1dr1jtgcuih41mw88oZ /]# mkdir teamwork
[root@iZbp1dr1jtgcuih41mw88oZ /]# ls -ld teamwork
drwxr-xr-x 2 root root 4096 Apr 27 17:10 teamwork
[root@iZbp1dr1jtgcuih41mw88oZ /]# chmod 757 teamwork
[root@iZbp1dr1jtgcuih41mw88oZ /]# ls -ld teamwork
drwxr-xrwx 2 root root 4096 Apr 27 17:10 teamwork
多名普通用戶以其他用戶身份在公共的目錄 /teamwork 進行數據共享:
[zh@iZbp1dr1jtgcuih41mw88oZ /]$ cd teamwork
[zh@iZbp1dr1jtgcuih41mw88oZ teamwork]$ touch test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ teamwork]$ mkdir ppt
[zh@iZbp1dr1jtgcuih41mw88oZ teamwork]$ ls -l
total 4
drwxrwxr-x 2 zh zh 4096 Apr 27 17:15 ppt
-rw-rw-r-- 1 zh zh 0 Apr 27 17:15 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ teamwork]$ echo "hello world" > test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ teamwork]$ cat test.txt
hello world
[ccy@iZbp1dr1jtgcuih41mw88oZ /]$ cd teamwork
[ccy@iZbp1dr1jtgcuih41mw88oZ teamwork]$ ls -l
total 8
drwxrwxr-x 2 zh zh 4096 Apr 27 17:15 ppt
-rw-rw-r-- 1 zh zh 12 Apr 27 17:17 test.txt
[ccy@iZbp1dr1jtgcuih41mw88oZ teamwork]$ touch d1.txt
[ccy@iZbp1dr1jtgcuih41mw88oZ teamwork]$ ls -l
total 8
-rw-rw-r-- 1 ccy ccy 0 Apr 27 17:19 d1.txt
drwxrwxr-x 2 zh zh 4096 Apr 27 17:15 ppt
-rw-rw-r-- 1 zh zh 12 Apr 27 17:17 test.txt
[ccy@iZbp1dr1jtgcuih41mw88oZ teamwork]$ echo "one piece" > d1.txt
[ccy@iZbp1dr1jtgcuih41mw88oZ teamwork]$ cat d1.txt
one piece
[ccy@iZbp1dr1jtgcuih41mw88oZ teamwork]$ cat test.txt
hello world
[lihua@iZbp1dr1jtgcuih41mw88oZ /]$ cd teamwork
[lihua@iZbp1dr1jtgcuih41mw88oZ teamwork]$ ls -l
total 12
-rw-rw-r-- 1 ccy ccy 10 Apr 27 17:20 d1.txt
drwxrwxr-x 2 zh zh 4096 Apr 27 17:15 ppt
-rw-rw-r-- 1 zh zh 12 Apr 27 17:17 test.txt
[lihua@iZbp1dr1jtgcuih41mw88oZ teamwork]$ cat d1.txt
one piece
[lihua@iZbp1dr1jtgcuih41mw88oZ teamwork]$ cat test.txt
hello world
以上可以看出多名普通用戶確實在root用戶創建的公共目錄/teamwork下實現了數據共享。但其實還存在一些問題,那就是在這個目錄下普通用戶權限過大,他們可以直接刪除其他人創建的文件,如下:
[lihua@iZbp1dr1jtgcuih41mw88oZ /]$ cd teamwork
[lihua@iZbp1dr1jtgcuih41mw88oZ teamwork]$ ls -l
total 12
-rw-rw-r-- 1 ccy ccy 10 Apr 27 17:20 d1.txt
drwxrwxr-x 2 zh zh 4096 Apr 27 17:15 ppt
-rw-rw-r-- 1 zh zh 12 Apr 27 17:17 test.txt
[lihua@iZbp1dr1jtgcuih41mw88oZ teamwork]$ rm d1.txt
rm: remove write-protected regular file ‘d1.txt’? y
[lihua@iZbp1dr1jtgcuih41mw88oZ teamwork]$ rm -r ppt
rm: remove write-protected directory ‘ppt’? y
[lihua@iZbp1dr1jtgcuih41mw88oZ teamwork]$ ls -l
total 4
-rw-rw-r-- 1 zh zh 12 Apr 27 17:17 test.txt
合作開發是為了多名普通用戶之間實現數據共享,但是卻不希望自己創建的文件被其他人直接刪除,為了限制合作開發下普通用戶的權限,引入了 “粘滯位” 的用法。當一個目錄被設置為 “粘滯位” (用chmod +t),則該目錄下的文件只能由:
(1)root用戶刪除
(2)該目錄的所有者(合作開發中一般就是root用戶)刪除
(3)該文件的所有者刪除
給 /teamwork 目錄設置為"粘滯位"(用chmod +t):
[root@iZbp1dr1jtgcuih41mw88oZ /]# chmod +t teamwork
[root@iZbp1dr1jtgcuih41mw88oZ /]# ls -ld teamwork
drwxr-xrwt 2 root root 4096 Apr 27 19:24 teamwork
效果展示(普通用戶zh不能刪除其他普通用戶創建的文件,只能刪除自己創建的文件):
[zh@iZbp1dr1jtgcuih41mw88oZ /]$ cd teamwork
[zh@iZbp1dr1jtgcuih41mw88oZ teamwork]$ ls -l
total 4
-rw-rw-r-- 1 lihua lihua 0 Apr 27 19:24 hello.txt
-rw-rw-r-- 1 zh zh 12 Apr 27 17:17 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ teamwork]$ rm hello.txt
rm: remove write-protected regular empty file ‘hello.txt’? y
rm: cannot remove ‘hello.txt’: Operation not permitted
[zh@iZbp1dr1jtgcuih41mw88oZ teamwork]$ rm test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ teamwork]$ ls -l
total 0
-rw-rw-r-- 1 lihua lihua 0 Apr 27 19:24 hello.txt