Linuxファイルシステム権限


Linuxファイルシステム権限
一、属主、属グループ
linuxファイルシステムでは、ユーザーがファイルを操作する場合は、まずファイルの権限をチェックし、まずユーザーがこのファイルの所有者であるかどうかを判断します.そうでなければ、そのユーザーがファイルのグループ内のユーザーであるかどうかを確認し、そうであれば、other権限を実行しない場合は、グループ権限を実行します.
二、ファイルとディレクトリの読み書き実行
1.ファイルの読み取り、書き込み、実行
ファイルが読み書きできるかどうかは、ユーザーがファイルに対して読み書き実行権限を持っているかどうかによって異なります.
ユーザーがファイルの読み取り権限を持っている場合、このファイルにアクセスし、ファイル内の内容を表示できますが、内容の変更はできません.例:
[root@centos7 data]# echo hello world > a
[root@centos7 data]# cat a
hello world
[root@centos7 data]# chmod 004 a
[root@centos7 data]# ll
total 4
-------r-- 1 root root 12 Mar 14 23:20 a
[root@centos7 data]# su masuri
[masuri@centos7 data]$ ll
total 4
-------r-- 1 root root 12 Mar 14 23:20 a
[masuri@centos7 data]$ cat a
hello world
[masuri@centos7 data]$ echo hello word >a
bash: a: Permission denied

ユーザーはファイルの書き込み権限を持っていれば、このファイルの内容を変更できます.ユーザーが書き込み権限しかない場合は、内部容量は表示できませんが、内部の内容を変更できます.例:
[root@centos7 data]# chmod 002 a
[root@centos7 data]# cat a
hello world
[root@centos7 data]# ls a
a
[root@centos7 data]# ll a
--------w- 1 root root 12 Mar 14 23:20 a
[root@centos7 data]# su masuri
[masuri@centos7 data]$ cat a
cat: a: Permission denied
[masuri@centos7 data]$ echo abc >> a
[masuri@centos7 data]$ exit
exit
[root@centos7 data]# cat a
hello world
abc

注:ユーザーがファイルの実行権限を持っている場合は、ファイルを実行できます.このような操作は非常に危険です.一般ファイルでは実行権限は推奨されません.
2.ディレクトリの読み書き実行
ディレクトリの読み取り権限:
ディレクトリに読み取り権限がある場合は、ユーザーがディレクトリを表示でき、ディレクトリをコピーできますが、ディレクトリ内のファイルを操作できません.ディレクトリ内のファイルを操作するには、実行権限が必要です.例1:読み取り権限のみが実行権限がない場合
[root@centos7 data]# mkdir -m 004 test
[root@centos7 data]# echo hello world > test/a
[root@centos7 data]# su masuri
[masuri@centos7 data]$ ls test
ls: cannot access test/a: Permission denied                         
a
[masuri@centos7 data]$ cp -R test test2
cp: cannot stat ‘test/a’: Permission denied                                     
[masuri@centos7 data]$ ll
total 0
d------r-- 2 root   root   15 Mar 14 23:37 test
d------r-- 2 masuri masuri  6 Mar 14 23:49 test2
[masuri@centos7 data]$ rm test/a                        
rm: cannot remove ‘test/a’: Permission denied                       

例2:読み取りと実行権限あり
[root@centos7 data]# chmod 005 test
[root@centos7 data]# ll test -d
d------r-x 2 root root 15 Mar 14 23:37 test
[root@centos7 data]# su masuri
[masuri@centos7 data]$ cp -R test test3                    
[masuri@centos7 data]$ ls
test  test2  test3
[masuri@centos7 data]$ ls test
a
[masuri@centos7 data]$ cat test/a   
hello world
[masuri@centos7 data]$ rm test/a
rm: remove write-protected regular file ‘test/a’? y
rm: cannot remove ‘test/a’: Permission denied               

ディレクトリの書き込み権限:
ディレクトリに書き込み権限しかない場合、ディレクトリの下のファイルに対して何の操作もできません.ディレクトリの下のファイルを操作するには、実行権限の例が必要です.1.書き込みのみ
[root@centos7 data]# mkdir -m 002 test
[root@centos7 data]# echo helloworld > test/a
[root@centos7 data]# ll
total 0
d-------w- 2 root root 15 Mar 15 00:10 test
[root@centos7 data]# su masuri
[masuri@centos7 data]$ echo hello > test/a
bash: test/a: Permission denied
[masuri@centos7 data]$ ls test
ls: cannot open directory test: Permission denied
[masuri@centos7 data]$ ls test/a
ls: cannot access test/a: Permission denied

2.書き込みと実行がある場合
[root@centos7 data]# chmod 003 test
[root@centos7 data]# ll -d test
d-------wx 2 root root 15 Mar 15 00:10 test
[root@centos7 data]# ls test
a
[root@centos7 data]# su masuri
[masuri@centos7 data]$ ls test
ls: cannot open directory test: Permission denied                
[masuri@centos7 data]$ echo hello > test/b                    ?
[masuri@centos7 data]$ rm test/a                               ?
rm: remove write-protected regular file ‘test/a’? y        
[masuri@centos7 data]$ exit                               root   。
exit
[root@centos7 data]# ls
test
[root@centos7 data]# ls test\                                    
b
[root@centos7 data]# cat b
cat: b: No such file or directory
[root@centos7 data]# cat test/b                         b           
hello

まとめ:ディレクトリの実行権限は非常に重要で、実行権がなく、ディレクトリの読み書き権限があってもディレクトリの下のファイルを操作できません.
3.chmodの大文字Xの意味ディレクトリに大Xを再帰的に適用すると、その下のすべてのディレクトリに実行権限が自動的に追加されますが、ファイルは追加されませんが、ファイルに実行権限がある場合は、その実行権限の例が追加されます.
[root@centos7 data]# mkdir testdir
[root@centos7 data]# cd testdir
[root@centos7 testdir]# touch a b                 a b    
[root@centos7 testdir]# chmod 100 a                 a    
[root@centos7 testdir]# chmod 000 b                 b      
[root@centos7 testdir]# mkdir -m 000 dir            dir        000
[root@centos7 testdir]# ll
total 0
---x------ 1 root root 0 Mar 15 00:48 a         
---------- 1 root root 0 Mar 15 00:48 b
d--------- 2 root root 6 Mar 15 00:48 dir
[root@centos7 testdir]# cd ..
[root@centos7 data]# chmod -R a+X testdir        testdir       X  
[root@centos7 data]# cd testdir/
[root@centos7 testdir]# ll
total 0
---x--x--x 1 root root 0 Mar 15 00:48 a                               
---------- 1 root root 0 Mar 15 00:48 b                       
d--x--x--x 2 root root 6 Mar 15 00:48 dir

三、特殊権限ビット
SUID:
バイナリ・プログラムでは、ユーザーがプログラムを実行するときに、プログラムを実行するアイデンティティは、ユーザー自身の例ではなく、プログラムの所有者として機能します.
[root@centos7 data]# su masuri
[masuri@centos7 data]$ ll `which cat`                                     suid    
-rwxr-xr-x. 1 root root 54160 Oct 31 03:16 /usr/bin/cat
[masuri@centos7 data]$ cat /etc/shadow                                  shadow      
cat: /etc/shadow: Permission denied
[masuri@centos7 data]$ exit
exit
[root@centos7 data]# chmod u+s `which cat`                                suid
[root@centos7 data]# ll `which cat`
-rwsr-xr-x. 1 root root 54160 Oct 31 03:16 /usr/bin/cat
[masuri@centos7 data]$ cat /etc/shadow                                    shadow      
root:$6$FBXKJJRgWCz23UDt$ji24UW5dVeYK55JOkBzBbmXaSGKAwhM1sjY9rg3TguL1GaTEmrlSzbDYNIu7p57/hehYEIE3LYLMHv2IqxIb70::0:99999:7:::
bin:*:17834:0:99999:7:::
daemon:*:17834:0:99999:7:::
adm:*:17834:0:99999:7:::
lp:*:17834:0:99999:7:::

SGID:
ディレクトリでは、ユーザーがSGID権限を持つディレクトリの下でファイルを作成すると、そのファイルのグループは自動的にそのディレクトリのグループになります.例:
[root@centos7 data]# groupadd wang
[root@centos7 data]# mkdir testdir
[root@centos7 data]# chown .wang testdir
[root@centos7 data]# ll
total 0
drwxr-xr-x 2 root wang 6 Mar 15 01:01 testdir
[root@centos7 data]# chmod g+s testdir
[root@centos7 data]# touch testdir/{a,b}
[root@centos7 data]# ll testdir/
total 0
-rw-r--r-- 1 root wang 0 Mar 15 01:03 a
-rw-r--r-- 1 root wang 0 Mar 15 01:03 b

STICKY:
ディレクトリに作用します.ディレクトリの下で作成されたファイルは、ファイルの所有者のみが削除できることを示します.例:
[root@centos7 data]# mkdir -m 777 testdir                                  777   
[root@centos7 data]# ll 
total 0
drwxrwxrwx 2 root root 6 Mar 15 01:13 testdir
[root@centos7 data]# touch testdir/{a,b}                                  a b    
[root@centos7 data]# ls testdir/
a  b
[root@centos7 data]# su masuri                                         masuri  
[masuri@centos7 data]$ rm -rf testdir/a                                   root   a            
[masuri@centos7 data]$ ll testdir/
total 0
-rw-r--r-- 1 root root 0 Mar 15 01:13 b
[masuri@centos7 data]$ exit                                           root
exit
[root@centos7 data]# chmod o+t testdir                               testdir sticky  
[root@centos7 data]# su masuri                                        masuri  
[masuri@centos7 data]$ touch testdir/a                               testder     a  
[masuri@centos7 data]$ exit
exit
[root@centos7 data]# su wang                                           wang  
[wang@centos7 data]$ touch testdir/c                                    c  
[wang@centos7 data]$ rm testdir/a                       
rm: remove write-protected regular empty file ‘testdir/a’? y          masuri   a  
rm: cannot remove ‘testdir/a’: Operation not permitted                  
[wang@centos7 data]$ rm testdir/c                                              C  
[wang@centos7 data]$ ll testdir/
total 0
-rw-rw-r-- 1 masuri masuri 0 Mar 15 01:20 a
-rw-r--r-- 1 root   root   0 Mar 15 01:13 b

四、アクセス制御リストacl setfacl:コマンドフォーマット:
setfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ... 
setfacl --restore=file

説明:アクセス制御リストは、ファイルの所有者、所属グループ、その他のユーザーを除いて、より柔軟な権限設定を提供します.
オプション
パラメータ
-m
権限の設定
-R
再帰
-M
-x
acl権限の削除
-X file
fileファイルを参照してacl権限を削除
-k dir
デフォルトacl設定権限の削除
-b file
acl権限のクリア
例:1.ユーザーへのアクセス制御権限の設定
[root@centos7 data]# mkdir testdir 
[root@centos7 data]# touch testdir/{a,b}
[root@centos7 data]# setfacl -m u:masuri:--- testdir
[root@centos7 data]# su masuri
[masuri@centos7 data]$ cd testdir
bash: cd: testdir: Permission denied
[masuri@centos7 data]$ ll
total 0
drwxr-xr-x+ 2 root root 24 Mar 15 03:19 testdir
[masuri@centos7 data]$ getfacl testdir
# file: testdir
# owner: root
# group: root
user::rwx
user:masuri:---
group::r-x
mask::r-x
other::r-x

2.acl権限のバックアップとacl権限のリカバリ
[root@centos7 data]# getfacl testdir
# file: testdir
# owner: root
# group: root
user::rwx
user:masuri:---
group::r-x
mask::r-x
other::r-x

[root@centos7 data]# getfacl testdir > acl.txt           acl        
[root@centos7 data]# setfacl -b testdir                     acl  
[root@centos7 data]# ll
total 4
-rw-r--r-- 1 root root 103 Mar 15 03:24 acl.txt
drwxr-xr-x 2 root root  24 Mar 15 03:19 testdir
[root@centos7 data]# setfacl --restore=acl.txt                acl     
[root@centos7 data]# ll
total 4
-rw-r--r--  1 root root 103 Mar 15 03:24 acl.txt
drwxr-xr-x+ 2 root root  24 Mar 15 03:19 testdir
[root@centos7 data]# getfacl testdir/
# file: testdir/
# owner: root
# group: root
user::rwx
user:masuri:---
group::r-x
mask::r-x
other::r-x

3.maskプロパティ、maskの役割は高さ制限レバーで、ファイルやディレクトリが読み込まれたときの最高権限を表示します.
[root@centos7 data]# mkdir testdir
[root@centos7 data]# ll testdir -d
drwxr-xr-x 2 root root 6 Mar 15 03:35 testdir
[root@centos7 data]# setfacl -m u:masuri:rwx testdir         masuri acl  rwx
[root@centos7 data]# setfacl -m mask::r testdir               mask  r
[root@centos7 data]# su masuri                                 masuri
[masuri@centos7 data]$ touch testdir/a                       testdir     
touch: cannot touch ‘testdir/a’: Permission denied              
[masuri@centos7 data]$ getfacl testdir
# file: testdir 
# owner: root
# group: root
user::rwx
user:masuri:rwx             #effective:r--                        r
group::r-x          #effective:r--
mask::r--
other::r-x

注意事項:1.acl付きファイルをコピーする場合、必要に応じてcp-aまたは-pパラメータを使用します.そうしないとacl属性が失われます.aclの有効な順序:所有者、カスタムユーザー、カスタムグループ、その他の人.
五、その他
chattr+iこのコマンドは、重要ファイルをロックして誤削除を防止し、rootユーザーでも削除できません.chattr+aを変更できません.このコマンドはファイル削除を防止し、ファイルlsattr表示用chattrで設定した権限を追加できます.
[root@centos7 ~]# chattr +i a.txt                                a.txt  
[root@centos7 ~]# lsattr  a.txt
----i----------- a.txt
[root@centos7 ~]# rm -rf a.txt                                     
rm: cannot remove ‘a.txt’: Operation not permitted
[root@centos7 ~]# echo lsafjla >a.txt                              
-bash: a.txt: Permission denied
[root@centos7 ~]# chattr -i a.txt                                  
[root@centos7 ~]# echo hello world > a.txt                         

[root@centos7 ~]# chattr +a a.txt                                a.txt       
[root@centos7 ~]# echo hello world > a.txt                             
-bash: a.txt: Operation not permitted
[root@centos7 ~]# echo hello world >> a.txt                        
[root@centos7 ~]# cat a.txt
hello world
hello world
[root@centos7 ~]# lsattr a.txt
-----a---------- a.txt
[root@centos7 ~]# chattr -i a.txt