Linux学习——文件权限

Linux学习——文件权限

4.1 基本权限

U:owner 属主

G:group 属组

O:other 其他用户

r:read 读取,数字设定为4

w:write 写入,数字设定为2

x:execute 执行,数字设定为1

“ ll ”——查看文件属性内容

从2~10字符开始,三个字符为一组,分别代表属主、属组,其他用户的权限

[root@localhost test]# ll abc.txt
-rw-r--r--. 1 root root 4 11月 23 23:54 abc.txt

4.1.1 设置文件属性与权限

chown :修改文件属主、属组

chgrp:修改文件属组

chmod:修改文件权限

修改单个属主

[root@localhost test]# ll abc.txt
-rw-r--r--. 1 root root 4 11月 23 23:54 abc.txt
[root@localhost test]# chown siso abc.txt
[root@localhost test]# ll abc.txt
-rw-r--r--. 1 siso root 4 11月 23 23:54 abc.txt

修改属主的同时修改属组,用“ . ”或“ 。 ”隔开

[root@localhost test]# chown qf1.linux abc.txt
[root@localhost test]# ll abc.txt
-rw-r--r--. 1 qf1 linux 4 11月 23 23:54 abc.txt

只需更改文件的属组而不需要更改属主

[root@localhost test]# chgrp hhh abc.txt
[root@localhost test]# ll abc.txt
-rw-r--r--. 1 qf1 hhh 4 11月 23 23:54 abc.txt

“ chown/chgrp -G ”——将某目录下的所有子目录或文件同时修改属主或属组

[root@localhost test]# mkdir dir01
[root@localhost test]# touch dir01/file{1..10}
[root@localhost test]# ll -d dir01/
drwxr-xr-x. 2 root root 137 11月 25 14:00 dir01/
[root@localhost test]# chown -R siso:hhh dir01/
[root@localhost test]# ll -d dir01/
drwxr-xr-x. 2 siso hhh 137 11月 25 14:00 dir01/
[root@localhost test]# ll dir01/
总用量 0
-rw-r--r--. 1 siso hhh 0 11月 25 14:00 file1
-rw-r--r--. 1 siso hhh 0 11月 25 14:00 file10
-rw-r--r--. 1 siso hhh 0 11月 25 14:00 file2
-rw-r--r--. 1 siso hhh 0 11月 25 14:00 file3
-rw-r--r--. 1 siso hhh 0 11月 25 14:00 file4
-rw-r--r--. 1 siso hhh 0 11月 25 14:00 file5
-rw-r--r--. 1 siso hhh 0 11月 25 14:00 file6
-rw-r--r--. 1 siso hhh 0 11月 25 14:00 file7
-rw-r--r--. 1 siso hhh 0 11月 25 14:00 file8
-rw-r--r--. 1 siso hhh 0 11月 25 14:00 file9

在使用chmod命令修改文件权限时,有两种方式,一种为字符,一种为数字

对象 赋值符 权限类型
u + r
g - w
o = x
a

对文件单个对象增加一定的权限

[root@localhost test]# ll abc.txt
-rw-r--r--. 1 qf1 hhh 4 11月 23 23:54 abc.txt
[root@localhost test]# chmod u+x abc.txt
[root@localhost test]# ll abc.txt
-rwxr--r--. 1 qf1 hhh 4 11月 23 23:54 abc.txt

对所有对象增加一定的权限

[root@localhost test]# chmod a=rwx abc.txt
[root@localhost test]# ll abc.txt
-rwxrwxrwx. 1 qf1 hhh 4 11月 23 23:54 abc.txt

同时给所有对象删除某一个权限

[root@localhost test]# chmod a-x abc.txt
[root@localhost test]# ll abc.txt
-rw-rw-rw-. 1 qf1 hhh 4 11月 23 23:54 abc.txt

一次分别给不同的对象增加或删除不同的权限

[root@localhost test]# chmod u=rx,g=r,o+x abc.txt
[root@localhost test]# ll abc.txt
-r-xr--rwx. 1 qf1 hhh 4 11月 23 23:54 abc.txt

“ chmod -R ”——更改该目录下的所有子目录或文件同时改权限

[root@localhost test]# chmod -R a=rwx dir01/
[root@localhost test]# ll -d dir01/
drwxrwxrwx. 2 siso hhh 137 11月 25 14:00 dir01/
[root@localhost test]# ll dir01/
总用量 0
-rwxrwxrwx. 1 siso hhh 0 11月 25 14:00 file1
-rwxrwxrwx. 1 siso hhh 0 11月 25 14:00 file10
-rwxrwxrwx. 1 siso hhh 0 11月 25 14:00 file2
-rwxrwxrwx. 1 siso hhh 0 11月 25 14:00 file3
-rwxrwxrwx. 1 siso hhh 0 11月 25 14:00 file4
-rwxrwxrwx. 1 siso hhh 0 11月 25 14:00 file5
-rwxrwxrwx. 1 siso hhh 0 11月 25 14:00 file6
-rwxrwxrwx. 1 siso hhh 0 11月 25 14:00 file7
-rwxrwxrwx. 1 siso hhh 0 11月 25 14:00 file8
-rwxrwxrwx. 1 siso hhh 0 11月 25 14:00 file9

使用数字来代表各个权限

[root@localhost test]# ll abc.txt
-r-xr--rwx. 1 qf1 hhh 4 11月 23 23:54 abc.txt
[root@localhost test]# chmod 777 abc.txt
[root@localhost test]# ll abc.txt
-rwxrwxrwx. 1 qf1 hhh 4 11月 23 23:54 abc.txt
[root@localhost test]# chmod 000 abc.txt
[root@localhost test]# ll abc.txt
----------. 1 qf1 hhh 4 11月 23 23:54 abc.txt

chown&chmod的区别

  • chown:Linux系统中用来修改某个文件属性的命令
  • chmod:Linux系统中用来改变某个文件的访问模式的命令

参考资料:chown&chmod区别

4.1.2 UGO权限设置案例

基本权限意义

权限 对文件的影响 对目录的影响
r(读取) 可读取文件内容 可列出目录的内容(文件名)
w(写入) 可修改文件内容 可创建或修改目录中的任一文件
x(执行) 可将文件作为命令执行 可访问目录的内容(取决于目录中文件的权限)
  1. r,w,x对文件的影响

    <pre>[root@localhost test]# chown root:root abc.txt
    [root@localhost test]# ll abc.txt
    -rw-r--r--. 1 root root 4 11月 23 23:54 abc.txt
    [root@localhost ~]# su siso
    [siso@localhost root]$ cat /root/test/abc.txt
    aaa
    [siso@localhost root]$ /root/test/abc.txt
    bash: /root/test/abc.txt: 权限不够
    
  2. r,w,x对目录的影响

    [root@localhost test]# mkdir dir02
    [root@localhost test]# touch dir02/file01.txt
    [root@localhost test]# ll -d dir02
    drwxr-xr-x. 2 root root 24 11月 25 15:00 dir02
    [root@localhost test]# ll -d dir02/file01.txt
    -rw-r--r--. 1 root root 0 11月 25 15:00 dir02/file01.txt
    [root@localhost test]# su - siso
    上一次登录:六 11月 25 14:55:14 CST 2023pts/5 上
    [siso@localhost ~]$ rm -rf /root/test/dir02/file01.txt
    rm: 无法删除"/root/test/dir02/file01.txt": 权限不够
    
  3. 文件与目录的区别

    [root@localhost test]# chmod 777 dir02
    [root@localhost test]# chmod 000 dir02/file01.txt
    [root@localhost test]# ll dir02/file01.txt
    ----------. 1 root root 0 11月 25 15:00 dir02/file01.txt
    [root@localhost test]# su - siso
    [siso@localhost root]$ cat dir02/file01.txt
    bash: dir02/file01.txt: 权限不够
    [siso@localhost root]$ dir02/file01.txt
    bash: dir02/file01.txt: 权限不够
    

    文件:x权限小心给予

    目录:w权限小心给予

           存储在磁盘上的文件就像是一个链表,表头是文件的起始地址,整个文件并不一定是连续的,可能是多个节点连接而成的。要访问某个文件是,只需要找到表头即可。删除文件,其实只是把表头删除了,后面的数据并没有删除,直到下一次进行写磁盘操作需要占用节点所在位置时,才会把相应的数据覆盖掉。

4.2 基本权限ACL

       提供传统的UGO的r,w,x权限之外的具体权限设置,可以对单一用户,单个文件或目录进行权限设置

4.2.1 ACL 基本用法

“ getfacl ”——查看ACL权限

[root@localhost test]# getfacl abc.txt
# file: abc.txt
# owner: root
# group: root
user::rw-
group::r--
other::r-x

[root@localhost test]# ll abc.txt
-rw-r--r-x. 1 root root 4 11月 23 23:54 abc.txt

“ setfacl ”——设置ACL权限,对每一个文件或目录进行更精准的权限设置

“ setfacl -m “——修改当前文件的ACL权限

root@localhost test]# setfacl -m u:siso:rw abc.txt
[root@localhost test]# getfacl abc.txt
# file: abc.txt
# owner: root
# group: root
user::rw-
user:siso:rw-
group::r--
mask::rw-
other::r-x
[root@localhost test]# setfacl -m g:hr:rw abc.txt
setfacl: Option -m: 无效的参数 near character 3
[root@localhost test]# setfacl -m g:hhh:rw abc.txt
[root@localhost test]# getfacl abc.txt
# file: abc.txt
# owner: root
# group: root
user::rw-
user:siso:rw-
group::r--
group:hhh:rw-
mask::rw-
other::r-x

“ setfacl -x “——可以删除用户对文件的所有权限

[root@localhost test]# setfacl -x u:siso abc.txt
[root@localhost test]# getfacl abc.txt
# file: abc.txt
# owner: root
# group: root
user::rw-
group::r--
group:hhh:rw-
mask::rw-
other::r-x

“ setfacl -b ”——删除所有扩展的ACL权限

[root@localhost test]# setfacl -b abc.txt
[root@localhost test]# getfacl abc.txt
# file: abc.txt
# owner: root
# group: root
user::rw-
group::r--
other::r-x

4.2.2 ACL高级特性

  1. 最大权限mask

       用来指定最大有效权限。系统给用户赋予的ACL权限需要和mask的权限逻辑“相与”,“相与”之后的权限才是用户的真正权限

[root@localhost test]# setfacl -m mask::r abc.txt
[root@localhost test]# getfacl abc.txt
# file: abc.txt
# owner: root
# group: root
user::rw-
group::r--
mask::r--
other::r-x

“相与”,mask并不能影响所有用户。

[root@localhost test]# setfacl -m mask::rw abc.txt
[root@localhost test]# setfacl -m u:siso:rx abc.txt
[root@localhost test]# getfacl abc.txt
# file: abc.txt
# owner: root
# group: root
user::rw-
user:siso:r-x
group::r--
mask::r-x
other::r-x
  1. mask的作用与特性

       mask能临时降低用户或组(除owner和other)的权限,而不是“setfacl -b ”命令删除所有的权限

[root@localhost test]# setfacl -m mask::- abc.txt
[root@localhost test]# getfacl abc.txt
# file: abc.txt
# owner: root
# group: root
user::rw-
user:siso:r-x			#effective:---
group::r--			#effective:---
mask::---
other::r-x

[root@localhost test]# setfacl -m g:hhh:r abc.txt
[root@localhost test]# getfacl abc.txt
# file: abc.txt
# owner: root
# group: root
user::rw-
user:siso:r-x
group::r--
group:hhh:r--
mask::r-x
other::r-x
  1. default继承

    [root@localhost test]# setfacl -m d:siso:rwx dir01
    [root@localhost test]# getfacl dir01
    # file: dir01
    # owner: siso
    # group: hhh
    user::rwx
    group::rwx
    other::rwx
    default:user::rwx
    default:user:siso:rwx
    default:group::rwx
    default:mask::rwx
    default:other::rwx
    
    

4.3 高级权限

       普通用户可以修改密码,siso用户运行的是/usr/bin/passwd文件,最终修改的是/etc/shadow文件,然而/etc/shadow文件只有root用户可以修改。使用“ pa aux ”查看当前进程发现,真正运行passwd却是root用户。

[siso@localhost ~]$ passwd
更改用户 siso 的密码 。
为 siso 更改 STRESS 密码。
(当前)UNIX 密码:
新的 密码:
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[siso@localhost ~]$ which passwd
/bin/passwd
[siso@localhost ~]$ ll /etc/shadow
----------. 1 root root 2229 11月 25 16:02 /etc/shadow
[siso@localhost ~]$ su 
密码:
[root@localhost siso]# ps aux | grep shadow
root      68070  0.0  0.0 112824   988 pts/6    S+   16:03   0:00 grep --color=auto shadow

4.3.1 SUID权限

       普通用户可以通过SUID提权,使用chmod命令给user增加SUID权限,切换到siso也可以查看root/test/abc.txt。

[root@localhost test]# chmod u+s abc.txt
[root@localhost test]# ll abc.txt
-rwSr-xr-x+ 1 root root 4 11月 23 23:54 abc.txt
[root@localhost test]# su - siso
上一次登录:六 11月 25 16:01:41 CST 2023pts/6 上
[siso@localhost /]$ cat root/test/abc.txt
aaa

4.3.2 SGID权限

       在一个程序上添加SGID,用户在执行过程中会获得该程序用户组的权限(相当于临时加入了程序的用户组)。

       在一个目录上添加SGID,该目录下新创建的文件会继承其属组

[root@localhost test]# chmod g+s dir01
[root@localhost test]# ll -d dir01
drwxrwsrwx+ 2 siso hhh 137 11月 25 14:00 dir01
[root@localhost test]# touch dir01/file
[root@localhost test]# ll dir01
总用量 0
-rw-rw-rw-+ 1 root hhh 0 11月 25 16:11 file

4.3.3 Sticky权限

       添加后sticky后,当用户对目录具有w,x权限,在该目录下建立的文件或目录,仅对自己与root有权删除。

[root@localhost ~]# mkdir /home/dir06
[root@localhost ~]# chmod 777 /home/dir06
[root@localhost ~]# su - siso
上一次登录:六 11月 25 16:05:56 CST 2023pts/6 上
[siso@localhost ~]$ touch /home/dir06/file
[siso@localhost ~]$ cd /home/dir06
[siso@localhost dir06]$ ls
file
[siso@localhost dir06]$ rm -rf /home/dir06/file
[siso@localhost dir06]$ ls
[siso@localhost dir06]$ 

添加“ t ”参数之后,用户只能删除自己的文件

[root@localhost dir06]# chmod o+t /home/dir06
[root@localhost dir06]# ll /home/dir06
总用量 0
[root@localhost dir06]# ll /home/dir06/
总用量 0
[root@localhost dir06]# su - siso
上一次登录:六 11月 25 16:18:29 CST 2023pts/6 上
[siso@localhost ~]$ touch /home/dir06/file01
[none@localhost ~]$ cd /home/dir06
[none@localhost dir06]$ ls
file01
[none@localhost dir06]$ rm -rf file01
rm: 无法删除"file01": 不允许的操作

       权限的字符位只有9位,增加特殊权限会占用“ x ”的位置。为了区分目录或文件是否含有“ x ”权限,系统会以特殊权限的大小写方式给予提示。当符号为大写时,表示不含有“ x ”权限;当符号为小写时,表示含有“ x ”权限。

[root@localhost ~]# ll -d /home/dir06
drwxrwxrwt. 2 root root 20 11月 25 16:25 /home/dir06
[root@localhost ~]# chmod o-x /home/dir06
[root@localhost ~]# ll -d /home/dir06
drwxrwxrwT. 2 root root 20 11月 25 16:25 /home/dir06
[root@localhost ~]# chmod 7777 /home/dir06
[root@localhost ~]# ll -d /home/dir06
drwsrwsrwt. 2 root root 20 11月 25 16:25 /home/dir06
[root@localhost ~]# chmod -x /home/dir06
[root@localhost ~]# ll -d /home/dir06
drwSrwSrwT. 2 root root 20 11月 25 16:25 /home/dir06

高级权限用法表

高级权限 数字符号 文件 目录
SUID 4 以属主身份执行
GUID 2 以属组身份执行 继承属组
Sticky 1 用户只能删除自己的文件

4.4 文件属性 chattr

“ chattr ”——改变文件的隐藏属性,仅对EXT2/EXT3/EXT4文件系统完整有效,其他文件系统可能仅支持部分隐藏属性或根本不支持隐藏属性。

“ lsatrr ”——查看文件的隐藏属性

“man chattr ”——查看chatter使用方法

“ chattr +a ”——不能使用Vim编辑器写入文本,需要使用echo命令追加的方式写入。此属性一般用于日志文件,因为日志文件内容是在后面追加,前面的内容不能覆盖,整个文件也不能被删除,当需要截取某段日志时,去除该属性即可。

[root@localhost test]# chattr +a abc.txt
[root@localhost test]# lsattr abc.txt
-----a---------- abc.txt
[root@localhost test]# echo "www.qfe.com">abc.txt
bash: abc.txt: 不允许的操作
[root@localhost test]# rm -rf abc.txt
rm: 无法删除"abc.txt": 不允许的操作

“ chattr +i ”——该文件不接受任何形式的修改,只能读取。例如,生产环境中在没有需求的情况下并不希望有人创建用户,为了防止黑客进入随意创建,一般会给/etc/passwd文件增加“ i ”属性确保安全。

[root@localhost test]# touch aaa.txt
[root@localhost test]# chattr +i aaa.txt
[root@localhost test]# echo "www.qfe.com">aaa.txt
bash: aaa.txt: 权限不够
[root@localhost test]# rm -rf aaa.txt
rm: 无法删除"aaa.txt": 不允许的操作

4.5 进程掩码 umask

       作用是指定权限的默认值。为系统设置一个合理的umask值,确保创建的文件或目录具有所希望的缺省权限,有利于数据安全。

       umask的值表示要剪掉的权限,进程和新建文件、目录的默认值都会受到umask的影响

[root@localhost test]# mkdir dir001
[root@localhost test]# touch file001
[root@localhost test]# umask
0022
[root@localhost test]# ll -d dir001
drwxr-xr-x. 2 root root 6 11月 25 20:17 dir001
[root@localhost test]# ll file001
-rw-r--r--. 1 root root 0 11月 25 20:17 file001

[root@localhost test]# umask 0777
[root@localhost test]# mkdir dir002
[root@localhost test]# touch file002
[root@localhost test]# ll -d dir002
d---------. 2 root root 6 11月 25 20:18 dir002
[root@localhost test]# ll file002
----------. 1 root root 0 11月 25 20:18 file002

不同的进程都可以设置自己的umask

[root@localhost ~]# vim /etc/login.defs
# the permission mask will be initialized to 022.
UMASK           077  //将这个值进行更改

4.6 本章小结

       本章主要讲解了基本权限UGO与ACL的用法,ACL高级特性mask与default的作用、高级权限SUID、SGID、Sticky的意义。另外,针对所有用户设置文件属性,进程和新建文件、目录的默认权限会受到umask的影响

4.7 参考资料

Linux文件权限详解

posted @ 2023-11-25 20:32  林小满吼吼吼  阅读(890)  评论(0)    收藏  举报