![]() |
![]() |
![]() |
![]() |
![]() |
$ cd /root ←進去目錄〝/root〞內玩玩 bash: cd: /root: Permission denied ←許可拒絕 $ cat /etc/shadow ←讀看看帳號密碼的資訊檔〝/etc/shadow〞 cat: /etc/shadow: Permission denied ←許可拒絕 $ cp /etc/shadow ~/ ←複製〝/etc/shadow〞看看 cp: cannot open '/etc/shadow' for reading: Permission denied ←許可拒絕 $ rm -f /bin/ls ←刪掉指令〝/bin/ls〞 rm: cannot remove `/bin/ls': Permission denied ←許可拒絕 |
$ echo "hello Wold" > /tmp/myfile ←建立一檔案〝myfile〞 $ ls -l /tmp/myfile ←用 ls -l 列出冗長檔案資訊 -rw-rw-r-- 1 aaa aaa 11 2011-10-10 10:10 myfile ←紅字標記的為 user,綠字標記的為 group |
$ ls -l ←列出冗長檔案內容 | |||||||
drw-rw-r-- | 1 | aaa | aaa | 292 | 2011-09-07 | 11:44 | myfile |
↑ | ↑ | ↑ | |||||
permission | user | group |
權限 | 對檔案的作用 |
r (讀) | 可查看該檔案的內容,如可用 cat 或 less 或其他工具來閱讀。 |
w (寫) | 可變更其內容,如可用 vi 或累加重定向來變更檔案的內容。 |
x (執行) | 如為執行檔則可被執行,但如是 shell script 額外還要有讀的權限才可被執行(因 shell 要讀取該檔)。 |
$ ls -l /tmp/myfile -rw-rw-r-- 1 aaa aaa 11 2011-10-10 10:10 myfile |
$ echo "hello Wold" > /tmp/myfile ←建立一檔案〝myfile〞 $ ls -l /tmp/myfile ←驗證權限 -rw-rw-r-- 1 aaa aaa 15 2011-09-07 00:46 /tmp/myfile ← 同一 owner/group 可讀寫但 other 只可讀 $ cat /tmp/myfile←讀看看 (測試〝讀〞的權限) Hello Wold $ echo 'Hello Linux' >> /tmp/myfile ← 變更〝myfile〞的內容看看(測試〝寫〞的權限) $ cat /tmp/myfile ←驗證看看 Hello World Hello Linux $ su bbb ←暫時變更其他的帳號 Password: ←輸入其帳號密碼 $ cat myfile ←測試〝讀〞的權限 Hello World ←此檔 other 有〝讀〞的權限,故其他人皆可讀取帳號 aaa 所建立的檔案 Hello Linux $ echo 'Hello Unix' >> /tmp/myfile ←增加〝myfile〞的內容看看 bash: /tmp/myfile: Permission denied ←許可拒絕 (因〝other〞對此檔無〝寫〞的權限) |
$ ls -l /etc/shadow ←列出帳號密碼的資訊檔〝/etc/shadow〞的冗長檔案資訊 -r-------- 1 root root 1147 2011-09-07 11:47 /etc/shadow |
$ ls -l /bin/cat -rwxr-xr-x 1 root root 23360 2007-10-31 11:52 /bin/cat |
$ cp /bin/cat ~/ ←複製〝/bin/ls〞到家目錄 $ ls -l /bin/cat ~/cat ←列出此兩檔比較看看 -rwxr-xr-x 1 root root 23360 2007-10-31 11:52 /bin/cat -rwxr-xr-x 1 aaa aaa 23360 2011-10-10 10:10 ./cat ←注意到沒,複製過來的檔案,擁有者和群組都變操作者所有 |
權限 | 對目錄的作用 |
r | 列出目錄內的檔案或其子目錄(如用 ls 可顯示目錄內的檔案但並非可用 cat 來讀取目錄內的檔案) |
w | 增減或更名目錄內的檔案(如可對目錄內的檔案操作 rm 或 cp 或 mv 等) |
x | 進入目錄或執行目錄內的程式(如可 cd 進入目錄) |
$ ls -l /home ←列出〝/home〞內所有家目權限 drwx------ 28 aaa aaa 4096 4096 2-11-10-11 15:41 aaa drwx------ 3 bbb bbb 4096 4096 2-11-10-03 19:59 bbb $ ls /home/bbb ←看別人家裡面有什麼東西? ls: cannot open directory /home/bbb: Permission denied ←無許可權 |
$ ls -ld /tmp drwxrwxrwt 9 root root 4096 2011-10-16 12:11 /tmp ←注意 other 權限的〝rwx〞顯示為〝rwt〞, 那個小寫 t 就是 sticky bit |
$ cd /tmp ←進入目錄〝/tmp〞 $ echo 'Hello' > file ←建立一檔案〝file〞 $ ls -l file ←用 ls -l 列出冗長檔案資訊 -rw-rw-r-- 1 aaa aaa 6 2011-10-10 10:10 file ←檔案的擁有者和群組都是〝aaa〞 $ su bbb ←用指令 su 暫時變更其他使用者 Password: (輸入其他使用者的帳號密碼) $ mv file fileB ←以其他使用者來操作更名看看 mv: cannot move 'file' to 'fileB': Operation not permitted ←許可拒絕 $ rm -f file ←刪除不是屬於自己的檔案看看 rm: cannot remove 'file': Operation not permitted ←許可拒絕 $ exit ←回原登入者的帳號 $ mv -v file fileB ←更名看看 'file' -> 'FileB' ←成功了 $ rm -v fileB ←刪除看看 removed 'fileB' ←成功了 |
$ ls -l /usr/bin/wall -r-xr-sr-x 1 root tty 10712 2007-10-11 03:54 /usr/bin/wall ←注意觀察,群組為 tty,但權限的〝x〞怎變〝小s〞,小寫的 s 如出現在群組就是 SGID |
$ ls -l /usr/bin/chsh /etc/passwd ←列出〝/usr/bin/chsh〞和〝/etc/passwd〞檔案資訊 -rw-r--r-- 1 root root 1783 2015-07-21 16:58 /etc/passwd -rws--x--x 1 root root 14472 2007-10-17 04:48 /usr/bin/chsh ←注意看,擁有者權限的〝小s〞就是 SUID |
特殊權限 | 檔案/目錄 | 作用 | 但書 | ls -l 的權限顯示 |
Sticky bit | 目錄 | 在具有 Sticky bit 的目錄內, 只有該檔的擁有者或〝root〞可以刪除或更名該檔 | ohers 也要有〝x 〞權限 | --- --- --t |
Set Group ID | 檔案 | 只針對執行檔,執行時會暫時變該檔案所屬的群組 | group 也要有〝x 〞權限 | --- --s --- |
目錄 | 任何放進目錄內的檔案都會變此目錄的群組 | |||
Set User ID | 檔案 | 只針對執行檔,執行時會暫時變該檔所屬的擁有者 | user 也要有〝x 〞權限 | --s --- --- |
權限數字表示法 (Numerical permissions) |
||
# | 權限(Permission) | rwx |
7=111BIN) | full | rwx |
6=110 BIN) | read & write | rw- |
5=101 (BIN) | read & execute | r-x |
4=100 (BIN) | read only | r-- |
3=011 (BIN) | write & execute | -wx |
2=010 (BIN) | write only | -w- |
1=001 (BIN) | execute only | --x |
0=000 (BIN) | none | --- |
$ echo 'hello world' > myfile ←建立一檔案〝myfile〞 $ ls -l myfile ←用 ls -l 列出冗長檔案資訊 -rw-rw-r-- 1 aaa aaa 12 2011-10-22 16:27 myfile ←預設的文字檔一般為 other 只可讀 $ chmod 660 myfile ←更改權限成〝-rw -rw- ---〞,也就是 other 不再能讀 $ ls -l myfile ←看一下權限改變了沒 -rw-rw---- 1 aaa aaa 12 2011-10-22 16:27 myfile |
$ chmod 777 file ←權限為 rwx rwx rwx $ chmod 644 file ←權限為 rw- r-- r-- $ chmod -R 711 dir/ ←遞回改變目錄內所有的檔案權限 $ chmod 000 file ←權限為 --- --- ---,沒任權限,要幹嘛??如為檔案通常只要給〝root〞讀/寫(可能是機密文件) |
特殊權限數字表示法 | |
# | 特殊權限 |
4=100 (BIN) | SUID |
2=010 (BIN) | SGID |
1=001 (BIN) | SBIT |
$ cp /bin/cat ~/my_exec ←複製一個執行檔過來改造 $ chmod 4755 my_exec ←將執行檔的權限加 SUID (SUID 權數為 4) $ ls -l my_exec ←確認一下 -rwsr-xr-x 1 aaa aaa 23360 2007-10-31 00:52 my_exec ←擁有者權限的〝小s〞就是 SUID |
$ echo "hello" > my_text ←製造一文字檔 $ chmod 4640 my_text ←變更文字檔權限,user 拿掉〝x〞權限,且加 SUID $ chmod 1654 my_exec ←變更執行檔權限,other 拿掉〝x〞權限,且加 SBIT # ls -l my_text my_exec ←冗長列出〝root_file〞和〝root_cat〞確認一下 -rw-r-wr-T 1 aaa aaa 23360 2007-10-31 00:52 my_exec ←SBIT 的 小〝t〞變大〝T〞?? -rwSr----- 1 aaa aaa 6 2011-10-23 14:59 my_text ←SUID 的 小〝S〞變大〝S〞?? |
$ chmod -v a=rw file ←設定檔案的 user,group,other 都有 rw 權限 mode of `file' changed to 0666 (rw-rw-rw-) $ chmod -v =rw file ←同上(省略〝a〞) $ chmod -v ug=rwx,o-r file ←user 和 group 權限為〝rwx〞,other 減去〝r〞權限 mode of `file' changed to 0772 (rwxrwx-w-) $ chmod ug+x file ←owner 和 group 加〝w〞權限 (其餘沒變) $ chmod u-x,g+rw file ←owner 減去〝x〞,group 加〝rx〞權限(其餘沒變) $ chmod u+s file ←增加 SUID $ chmod g-s file ←拿掉 SGID $ chmod +t dir ←設定 SBIT (sticky bit 只能用在 other,故 o+t 中的〝o〞可省略) $ chmod a=rwx,o+t dir ←設定目錄的 owner,group,other =〝rwx〞且加 Sticky bit |
$ umask ←如沒任何選項,輸出為顯示建立檔案或目錄時的預設權限 0002 |
umask | 建立檔案其權限 | 建立目錄其權限 |
000 |
666 (rw- rw- rw-) |
777 (rwx rwx rwx) |
002 |
664 (rw- rw- r--) |
775 (rwx rwx r-x) |
022 |
644 (rw- r-- r--) |
755 (rwx r-x r-x) |
027 |
640 (rw- r-- ---) |
750 (rwx r-x ---) |
077 |
600 (rw- --- ---) |
700 (rwx --- ---) |
277 |
400 (r-- --- ---) |
500 (r-x --- ---) |
$ umask ←看一下預設權限 0002 $ echo 'umask file1' > umask_0002 ←建立檔案〝umask_0002〞 $ umask 0027 ←改變一下預設權限為〝0027〞(rw- r-- ---) $ umask ←確認看預設權限己改變了沒 0027 ←變〝0027〞了 $ echo 'umask file2' > umask_0027 ←用 umask=0027 建立檔案〝umask_0027〞 $ ls -l umask_0002 umask_0027 ←列出兩檔案的權限來比較看看 -rw-rw-r-- 1 aaa aaa 12 2011-10-10 10:10 umask_0002 ←注意比對兩個檔案的權限不一樣了 -rw-r----- 1 aaa aaa 12 2011-10-10 10:11 umask_0027 |
語法:chattr [-otpiton][+-=][mode] file/directroy | |||
指令名稱/功能/命令使用者 | 選項 | 功能 | mode的種屬性有[aAcdDisSu],意義各為 |
chattr/ (change attribute) 變更檔案屬性/ Any(主要 for Superuser) |
-R | 遞回改變檔案的屬性,將目錄下 所有的檔案及子目錄一併異動屬性 |
〝a〞:用於檔案只能累加,不能刪除。用於目錄只能修改目錄內檔案的內容,但不能增減檔案。且只有〝root〞才可使用 〝A〞:不更新檔案的 atime 〝c〞:存檔時自動壓縮檔案,讀取時自動解壓縮 〝d〞:排除 dump 資料,即執行 dump 指令時會排除此檔 〝D〞如目錄此設定,將緩衝記憶體的資料同步存到硬碟 〝i〞:讓檔案具金剛不壞之身;包括不能刪除,更名,修改內容,變更權限等且只有〝root〞才可使用 〝s〞:毀屍滅跡,當檔案被刪除時,該檔案的 block 會被填 0(讓有心人無法復原該檔) 〝S〞:存檔時不先寫入緩衝記憶體,直接寫進硬碟(效能較差,但比較可防止斷電時,檔案流失) 〝u〞:和〝s〞毀屍滅跡相反,當檔案被剛除時只刪除 inode,以利未來後悔〝可能〞可救回此檔 |
-V | 顯示異動屬性的過程 |
# echo '123' > myfile ←建立檔案〝myfile〞 # chattr +a myfile ←設定〝a〞屬性只能累加,但不能刪除 # rm -f myfile ←刪除看看 rm: cannot remove `myfile': Operation not permitted ←許可拒絕 # echo '456' > myfile ←覆寫看看 bash: myfile: Operation not permitted ←許可拒絕 # echo '456' >> myfile ←累加看看 # cat myfile ←驗證一下 123 456 # chattr -V -a myfile ←減去〝a〞屬性就可被刪除 chattr 1.40.2 (12-Jul-2007) Flags of myfile set as ---------------- |
# chattr =aAdS file ←設定檔案具有〝a〞,〝A〞,〝d〞,〝S〞屬性 # chattr -V +i -ad file ←將檔案增加〝i〞屬性並減去〝a〞,〝d〞屬性 |
語法:lsattr [-otpiton] file/directroy | ||
指令名稱/功能/命令使用者 | 選項 | 功能 |
lsattr/ (list attrbute)顯示檔案屬性/ Any |
-a | 列出所有檔案和目錄,包括隱藏檔和工作目錄和上層目錄的屬性 |
-d | 只列出目錄屬性 | |
-R | 遞回將目錄下的檔案和子目錄列出屬性 |
# echo > file # chattr =aAdS file ←設定檔案具有〝a〞,〝A〞,〝d〞,〝S〞屬性 # lsattr file ←顯示檔案屬性 --S--adA-------- file |
# lsattr -a ←列出目錄內所有檔案的屬性(包含工作目錄和其父目錄) -----a---------- ./. ←工作目錄的屬性 ---------------- ./.. ←上層目錄的屬性 -----a---------- ./jun ←目錄內檔案的屬性 |