linux基本許可權和歸屬、附加許可權控制

來源:互聯網
上載者:User

標籤:使用者管理、組管理、源碼包安裝

一、基本許可權和歸屬

1. 存取權限

讀取:允許查看、顯示目錄列表

寫入:允許修改,允許在目錄中建立、移動、刪除檔案或子目錄

可執行:允許運行程式、切換目錄

2. 查看檔案的許可權

# ls -l install.log

-|rw-|r--|r-- 1  root  root  26195 Dec 17 10:42  install.log

① ② ③ ④ ⑤   ⑥    ⑦       ⑧           ⑨                   ⑩

①. 檔案類型

-代表普通檔案

d代表目錄

l代表串連

②. rw-:代表檔案所有者的許可權(u)

r=讀=4

w=寫=2

x=執行=1

③. r--:代表檔案所屬組的許可權(g)

r=讀=4

w=寫=2

x=執行=1

④. r--:其他使用者的許可權(o)

r=讀=4

w=寫=2

x=執行=1

a=ugo

⑤. 檔案  永久連結數

     目錄  該目錄下有多少子目錄包括.和..

⑥. 檔案所有者

⑦. 檔案所屬組

⑧. 檔案大小

⑨. 檔案修改時間

⑩. 檔案名稱


3. 命令 (R表示遞迴)

chmod 改變許可權

chmod ugoa [+-=] [rwx] 檔案

chmod 數字 檔案


檔案最大許可權666

目錄最大許可權777

預設建立檔案的許可權644

預設建立目錄的許可權755

umask 

最大許可權的rwx-umask的rwx=預設許可權

補充:

對於目錄來說沒有x許可權,無論有沒rw,都不能進入該目錄


chown 改變所有者與所屬組

chown 所有者:所屬組  對象

chgrp 組 檔案變更檔的屬組


二、附加許可權控制

1. 特殊許可權介紹

Set UID:4User+x

Set GID:2Group+x

Sticky Bit:1Other+x

2. 特殊許可權作用

Set UID:

只能對可執行程式設定,當其他使用者執行帶SUID標記的程式時,將會使用程式所有者的身份去執行


Set GID:

可以對可執行程式設定,當其他使用者執行帶SGID標記的程式時,將會使用程式所屬組的身份去執行

可以對目錄設定,當對目錄設定SGID後,任何人在該目錄下建立檔案和目錄的所屬組自動繼承該目錄的所屬組


Sticky Bit:

對目錄設定,任何人在該目錄下建立檔案和目錄,只有root與檔案建立者有刪除許可權


3. ACL策略

getfacl檔案查看ACL策略

setfacl[選項] u:使用者名稱:許可權 檔案

setfacl[選項] g:組名:許可權 檔案

-m定義一條ACL策略

-x刪除指定的ACL策略

-b清除所有已設定的ACL策略

-R遞迴設定

-d為目錄設定預設許可權





一、基本許可權和歸屬

公司技術部有一台Linux程式開發伺服器,根據部門內項目組的構成情況,需要建立相應的使用者帳號,並對開發資料相關目錄配置存取權限。

1.檔案夾/tech/nsdhome、/tech/jsdhome,分別歸屬於nsd組、jsd組,禁止其他使用者進入。

2.建立部門公用目錄/public,技術部的所有員工(tech組)對其擁有可讀、可寫、可執行,其他使用者禁止訪問此目錄。


[[email protected] /]# mkdir -p /tech/nsdhome

[[email protected] /]# mkdir -p /tech/jsdhome

[[email protected] /]# mkdir /public

[[email protected] /]# groupadd nsd

[[email protected] /]# groupadd jsd

[[email protected] /]# groupadd tech

[[email protected] /]# useradd -g nsd nsd01

[[email protected] /]# useradd -g nsd nsd02

[[email protected] /]# useradd -g jsd jsd01

[[email protected] /]# useradd -g jsd jsd02

[[email protected] /]# useradd -g tech yg01

[[email protected] /]# useradd yg02

[[email protected] /]# chown :nsd /tech/nsdhome

[[email protected] /]# ls -l /tech/nsdhome

總計 0

[[email protected] /]# ls -ld /tech/nsdhome

drwxr-xr-x 2 root nsd 4096 07-30 11:36 /tech/nsdhome

[[email protected] /]# chmod o-rx /tech/nsdhome

[[email protected] /]# chown :jsd /tech/jsdhome

[[email protected] /]# ls -ld /tech/jsdhome

drwxr-xr-x 2 root jsd 4096 07-30 11:36 /tech/jsdhome

[[email protected] /]# chmod o-rx /tech/jsdhome

[[email protected] /]# chown :tech /public

[[email protected] /]# ls -ld /public

drwxr-xr-x 2 root tech 4096 07-30 11:36 /public

[[email protected] /]# chmod g+w /public

[[email protected] /]# ls -ld /public

drwxrwxr-x 2 root tech 4096 07-30 11:36 /public

[[email protected] /]# chmod o-rx /public

[[email protected] /]# ls -ld /public

drwxrwx--- 2 root tech 4096 07-30 11:36 /public

[[email protected] /]# ls -ld /tech/nsdhome

drwxr-x--- 2 root nsd 4096 07-30 11:36 /tech/nsdhome

[[email protected] /]# ls -ld /tech/jsdhome

drwxr-x--- 2 root jsd 4096 07-30 11:36 /tech/jsdhome

[[email protected] /]# 



二、附加許可權控制

1、Suid實驗

只能針對程式(命令)設定,當任何人在執行具有suid許可權的命令時,將使用該命令的所有者身份執行

[[email protected] ~]# ls -l /etc/shadow

[[email protected] ~]# which passwd

[[email protected] ~]# ls -l /usr/bin/passwd 

[[email protected] ~]# umask 022

[[email protected] ~]# which touch

[[email protected] ~]# cp /bin/tosuuch /bin/suidtouch

[[email protected] ~]# ls -l /bin/*touch

[[email protected] ~]# useradd lily

[[email protected] ~]# su - lily

[[email protected] ~]$ suidtouch suid-file1.txt

[[email protected] ~]$ ls -l suid-file1.txt 

[[email protected] ~]$ exit

[[email protected] ~]# ls -l /bin/suidtouch 

[[email protected] ~]# chmod u+s /bin/suidtouch 

[[email protected] ~]# ls -l /bin/suidtouch 

[[email protected] ~]# su - lily

[[email protected] ~]$ suidtouch suid-file2.txt 

[[email protected] ~]$ ls -l suid-file*

[[email protected] ~]$ exit

[[email protected] ~]# rm -rf /bin/suidtouch 


2、Sgid實驗

能對程式(命令)設定,也可以對目錄設定

當任何人在執行具有sgid許可權的命令時,將使用該命令的所屬組身份執行

[[email protected] ~]# which mkdir

[[email protected] ~]# cp /bin/mkdir /bin/sgidmkdir

[[email protected] ~]# ls -l /bin/*mkdir

[[email protected] ~]# su - lily

[[email protected] ~]$ sgidmkdir test1

[[email protected] ~]$ ls -ld test1

[[email protected] ~]$ exit

[[email protected] ~]# chmod g+s /bin/sgidmkdir 

[[email protected] ~]# ls -l /bin/sgidmkdir 

[[email protected] ~]# su - lily

[[email protected] ~]$ sgidmkdir test2

[[email protected] ~]$ ls -ld test*

[[email protected] ~]$ exit

[[email protected] ~]# rm -rf /bin/sgidmkdir 


對目錄設定Sgid,任何人在該目錄下建立的檔案或子目錄的所屬組都自動繼承該目錄本身所屬組

[[email protected] ~]# mkdir /testgid

[[email protected] ~]# ls -ld /testgid/

drwxr-xr-x 2 root root 4096 Jan  6 16:53 /testgid/

[[email protected] ~]# chmod 0757 /testgid/

[[email protected] ~]# su - lily

[[email protected] ~]$ mkdir /testgid/lilytest1

[[email protected] ~]$ touch /testgid/lilyfile1.txt

[[email protected] ~]$ ls -l /testgid/

[[email protected] ~]$ exit

[[email protected] ~]# chmod 2757 /testgid/

[[email protected] ~]# ls -ld /testgid/

[[email protected] ~]# su - lily

[[email protected] ~]$ mkdir /testgid/lilytest2

[[email protected] ~]$ touch /testgid/lilyfile2.txt

[[email protected] ~]$ ls -l /testgid/


3、t位許可權echo

針對公用目錄設定,目錄設定t位許可權後,該目錄下的檔案或子目錄只有root與檔案所有者能夠刪除

[[email protected] ~]# mkdir /soft

[[email protected] ~]# ls -ld /soft/

[[email protected] ~]# chmod o+w /soft/

[[email protected] ~]# ls -ld /soft/

[[email protected] ~]# useradd wbb

[[email protected] ~]# useradd lhq

[[email protected] ~]# su - lhq

[[email protected] ~]$ cat /soft/lhq.txt

hello,byebye

[[email protected] ~]$ exit

[[email protected] ~]# su - wbb

[[email protected] ~]$ ls -ld /soft/

[[email protected] ~]$ ls -l /soft/

[[email protected] ~]$ rm -rf /soft/lhq.txt 

[[email protected] ~]$ ls -l /soft/

[[email protected] ~]# chmod o+t /soft/

[[email protected] ~]# ls -ld /soft/

[[email protected] ~]# su - lhq

[[email protected] ~]$ cat /soft/lhq.txt

hello,byebye

[[email protected] ~]$ exit

[[email protected] ~]# su - wbb

[[email protected] ~]$ ls -l /soft/

[[email protected] ~]$ ls -ld /soft/

[[email protected] ~]$ rm -rf /soft/lhq.txt 


[[email protected] ~]# find / -type f -a -perm +6000//尋找系統中suid/sgid的程式


4、ACL使用權限設定

建立賬戶:mike  john  kaka

建立檔案:/data/file1.txt

·mike對檔案有讀寫權限,john只有讀許可權。其他使用者沒有任何許可權

·kaka具有與john相同許可權

·建立lily,lily對file1.txt具有讀執行許可權,其他使用者沒有任何許可權


[[email protected] ~]# tune2fs -l /dev/sda2 | grep acl

Default mount options:    user_xattr acl

[[email protected] ~]# tune2fs -l /dev/sda1 | grep acl

Default mount options:    user_xattr acl

[[email protected] ~]# ls -ld /data/

drwxrwxrwx 3 root root 4096 12-09 16:21 /data/

[[email protected] ~]# rm -rf /data/

[[email protected] ~]# mkdir /data

[[email protected] ~]# getfacl /data/

getfacl: Removing leading ‘/‘ from absolute path names

# file: data

# owner: root

# group: root

user::rwx

group::r-x

other::r-x


[[email protected] ~]# ls -ld /data/

drwxr-xr-x 2 root root 4096 12-09 16:27 /data/

[[email protected] ~]# setfacl -m u:mike:rwx /data/

[[email protected] ~]# ls -ld /data/

drwxrwxr-x+ 2 root root 4096 12-09 16:27 /data/

[[email protected] ~]# getfacl /data/

getfacl: Removing leading ‘/‘ from absolute path names

# file: data

# owner: root

# group: root

user::rwx

user:mike:rwx

group::r-x

mask::rwx

other::r-x

[[email protected] ~]# setfacl -m u:john:r-- /data/

[[email protected] ~]# setfacl -m u:kaka:r-- /data/

[[email protected] ~]# setfacl -m u:lily:r-x /data/

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/42/F2/wKiom1PY9y_xyI_7AAX4XkJDzFk155.jpg" style="float:none;" title="搜狗20140730192438.jpg" alt="wKiom1PY9y_xyI_7AAX4XkJDzFk155.jpg" />

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/42/F3/wKioL1PY-ErglQTVAAJ9qB1Ry9c419.jpg" style="float:none;" title="搜狗20140730192810.jpg" alt="wKioL1PY-ErglQTVAAJ9qB1Ry9c419.jpg" />

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/42/F3/wKioL1PY-EvTdZlWAAOjYMs4Lpw549.jpg" style="float:none;" title="搜狗20140730193038.jpg" alt="wKioL1PY-EvTdZlWAAOjYMs4Lpw549.jpg" />

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/42/F3/wKioL1PY-E2hzGj_AAKuYdIBx-Y636.jpg" style="float:none;" title="搜狗20140730193223.jpg" alt="wKioL1PY-E2hzGj_AAKuYdIBx-Y636.jpg" />

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/42/F3/wKioL1PY-E7g1bJYAAJlRbqKH2o564.jpg" style="float:none;" title="搜狗20140730193534.jpg" alt="wKioL1PY-E7g1bJYAAJlRbqKH2o564.jpg" />

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/42/F3/wKiom1PY9zWRFJuSAAHPRTcjumY105.jpg" style="float:none;" title="搜狗20140730193650.jpg" alt="wKiom1PY9zWRFJuSAAHPRTcjumY105.jpg" />

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/42/F3/wKiom1PY9zfxuGiSAAK_yrVtCMU079.jpg" style="float:none;" title="搜狗20140730193903.jpg" alt="wKiom1PY9zfxuGiSAAK_yrVtCMU079.jpg" />

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/42/F3/wKioL1PY-FKQF02-AALrSHqF6Ck345.jpg" style="float:none;" title="搜狗20140730194058.jpg" alt="wKioL1PY-FKQF02-AALrSHqF6Ck345.jpg" />

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/42/F3/wKiom1PY9zrA4p8gAAKivHESxc8693.jpg" style="float:none;" title="搜狗20140730195116.jpg" alt="wKiom1PY9zrA4p8gAAKivHESxc8693.jpg" />

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/42/F3/wKiom1PY9zrTca20AAGFVgYo02U630.jpg" style="float:none;" title="搜狗20140730195222.jpg" alt="wKiom1PY9zrTca20AAGFVgYo02U630.jpg" />

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/42/F3/wKioL1PY-FWAt-6vAAFedESh2ww161.jpg" style="float:none;" title="搜狗20140730195350.jpg" alt="wKioL1PY-FWAt-6vAAFedESh2ww161.jpg" />

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/42/F3/wKiom1PY9zzx_FLxAAGaF5GXSRs767.jpg" style="float:none;" title="搜狗20140730195439.jpg" alt="wKiom1PY9zzx_FLxAAGaF5GXSRs767.jpg" />

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/42/F3/wKiom1PY9z6T-DvWAATNJNv8zLs566.jpg" style="float:none;" title="搜狗20140730200317.jpg" alt="wKiom1PY9z6T-DvWAATNJNv8zLs566.jpg" />

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/42/F3/wKiom1PY90DAbbAEAAOS5GqnxKo436.jpg" style="float:none;" title="搜狗20140730200355.jpg" alt="wKiom1PY90DAbbAEAAOS5GqnxKo436.jpg" />

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/42/F3/wKioL1PY-FvTiFCnAANYOLh-6bE112.jpg" style="float:none;" title="搜狗20140730200430.jpg" alt="wKioL1PY-FvTiFCnAANYOLh-6bE112.jpg" />


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.