0
点赞
收藏
分享

微信扫一扫

一文带你彻底搞懂Linux 文件权限管理


  • ​​Linux下文件/目录的权限和归属​​
  • ​​访问权限​​
  • ​​权限介绍​​
  • ​​权限修改​​
  • ​​归属(所有权)​​
  • ​​权限修改​​
  • ​​练习题-01​​
  • ​​ACL权限​​
  • ​​练习题-02​​
  • ​​特殊权限​​
  • ​​附加权限​​
  • ​​SET位权限​​
  • ​​粘滞位权限(Sticky)​​
  • ​​练习题-03​​

Linux下文件/目录的权限和归属

访问权限

  • 读取(r): 允许查看文件内容,显示目录列表
  • 写入(w): 允许修改文件内容,允许在目录中新建、删除、移动文件或者子目录
  • 可执行(x): 允许运行程序,切换目录
  • 无权限(-): 没有权限

权限介绍

- rw-r–r-- . 1 root root 1258 Jun 3 2019 initial-setup-ks.cfg
d rwxr-xr-x . 2 root root 6 Jun 3 2019 Music

-

代表普通文件

d

代表目录

c

代表字符型文件

l

代表链接文件

rwx

r-x

r-x

root

root

属主权限

属组权限

其他人权限

属主

属组

权限项



执行



执行



执行

字符表示

r

w

x

r

w

x

r

w

x

数字表示

4

2

1

4

2

1

4

2

1

权限分配

文件所有者(属主)

文件所有者(属主)

文件所有者(属主)

文件所属组(属组)

文件所属组(属组)

文件所属组(属组)

其他用户

其他用户

其他用户

r

w

-

r

-

-

r

-

-

4

2

0

4

0

0

4

0

0

  • 这个文件权限就是 6 4 4

权限修改

  • 格式一:
  • chmod [ugoa][±=][rwx] 文件/目录
  • u,g,o,a 分别代表 属主,数组,其他用户,所有用户
  • +,-,= 分别代表 增加,减去,设置一个权限
  • ex:

  [root@localhost ~]# touch /root/a.txt
[root@localhost ~]# ls a.txt -l
-rw-r--r--. 1 root root 0 May 21 05:56 a.txt
[root@localhost ~]# chmod g+w,o+w /root/a.txt
[root@localhost ~]# ls a.txt -l
-rw-rw-rw-. 1 root root 0 May 21 05:56 a.txt

  • 格式二
  • chmod nnn(三位八进制数) 文件/目录
  • ex:

  [root@localhost ~]# chmod 644 /root/a.txt 
[root@localhost ~]# ls a.txt -l
-rw-r--r--. 1 root root 0 May 21 05:56 a.txt

  • 常用选项
  • -R 递归修改指定目录下所有文件或子目录的权限

归属(所有权)

  • 属主: 拥有该文件或目录的用户账号
  • 属组: 拥有该文件或目录的组账号

权限修改

  • 格式:
  • chown 属主 文件/目录
  • chown :属组 文件/目录
  • chown 属主:属组 文件/目录
  • 常用选项
  • -R: 递归修改指定目录下所有文件或子目录的归属权限
  • ex:

  [root@localhost ~]# chown tom a.txt 
[root@localhost ~]# chown :manager a.txt
[root@localhost ~]# ls a.txt -l
-rw-r--r--. 1 tom manager 0 May 21 05:56 a.txt
[root@localhost ~]# chown root:root a.txt
[root@localhost ~]# ls a.txt -l
-rw-r--r--. 1 root root 0 May 21 05:56 a.txt
[root@localhost ~]#

练习题-01

  • 新建文件 /root/bb.txt,属主为 harry,属组为manager,属主可以读写执行,属组可以读写执行,其他人只读

  [root@localhost ~]# touch /root/bb.txt
[root@localhost ~]# useradd harry
[root@localhost ~]# groupadd manager
[root@localhost ~]# chown harry:manager /root/bb.txt
[root@localhost ~]# chmod 774 /root/bb.txt
[root@localhost ~]# ls bb.txt -l
-rwxrwxr--. 1 harry manager 0 May 21 10:40 bb.txt

  • 新需求
  • 设置susa用户对文件拥有读和执行权限
  • 设置group组内的成员对文件拥有读的权限
  • 此需求根据上面知识无法解决,需要设置 ACL 权限

  [root@localhost ~]# useradd susa
[root@localhost ~]# setfacl -m u:susa:rw bb.txt
[root@localhost ~]# getfacl bb.txt
# file: bb.txt
# owner: harry
# group: manager
user::rwx
user:susa:rw-
group::rwx
mask::rwx
other::r--
[root@localhost ~]# groupadd group
[root@localhost ~]# setfacl -m g:group:r bb.txt
[root@localhost ~]# getfacl bb.txt
# file: bb.txt
# owner: harry
# group: manager
user::rwx
user:susa:rw-
group::rwx
group:group:r--
mask::rwx
other::r--

ACL权限

  • ACL(Access Control List),主要目录是在提供传统的owner,group,otherd的read,write,execute权限之外细部权限设顶
  • ACL 可以针对单一使用者,或者单一文件/目录进行r,w,x的权限使用规范
  • 设置ACL

  setfacl -m u:username:rwx filename
u: 属主
username: 用户名称
rwx: 权限
filename: 文件
setfacl -m g:groupname:rwx filename
g: 属组
groupname: 组名称
查看
getfacl filename
删除
setfacl -x u:username filename


删除用户权限
[root@localhost ~]# setfacl -m u:susa:rw a.txt
[root@localhost ~]# getfacl a.txt
# file: a.txt
# owner: root
# group: root
user::rw-
user:susa:rw-
group::r--
mask::rw-
other::r--
[root@localhost ~]# setfacl -x u:susa a.txt
[root@localhost ~]# getfacl a.txt
# file: a.txt
# owner: root
# group: root
user::rw-
group::r--
mask::r--
other::r--

删除组权限
[root@localhost ~]# setfacl -m g:group:r a.txt
[root@localhost ~]# getfacl a.txt
# file: a.txt
# owner: root
# group: root
user::rw-
group::r--
group:group:r--
mask::r--
other::r--

[root@localhost ~]# setfacl -x g:group a.txt
[root@localhost ~]# getfacl a.txt
# file: a.txt
# owner: root
# group: root
user::rw-
group::r--
mask::r--
other::r--

练习题-02

  • 拷贝文件 /etc/fstab到 /var/tmp/fstab,配置文件 /var/tmp/fstab的权限
  • 文件 /var/tmp/fstab的拥有者是root用户
  • 文件 /var/tmp/fstab属于root组
  • 文件 /var/tmp/fstab对任何人都不可执行
  • 用户natasha 能够对文件 /var/tmp/fstab执行读和写操作
  • 用户harry 对文件 /var/tmp/fstab既不能读,也不能写
  • 所有其他用户(当前的和将来的)能够对文件 /var/tmp/fstab进行读操作

  [root@localhost ~]# useradd harry
[root@localhost ~]# useradd natasha
[root@localhost ~]# cp /etc/fstab /var/tmp/fstab
[root@localhost ~]# cd /var/tmp/
[root@localhost tmp]# ls fstab -l
-rw-r--r--. 1 root root 465 May 21 11:09 fstab
[root@localhost tmp]# setfacl -m u:natasha:rw fstab
[root@localhost tmp]# setfacl -m u:harry:-- fstab
[root@localhost tmp]# getfacl fstab
# file: fstab
# owner: root
# group: root
user::rw-
user:harry:---
user:natasha:rw-
group::r--
mask::rw-
other::r--

特殊权限

  • umask 反掩码 0022 权限的一种补码 777-022=755

  # root身份默认权限
[root@localhost ~]# mkdir root_dir
[root@localhost ~]# touch root_file
drwxr-xr-x. 2 root root 6 May 22 08:46 root_dir 755
-rw-r--r--. 1 root root 0 May 22 08:46 root_file 644

# 普通用户身份默认权限
[ruochen@localhost ~]$ touch ruochen_file
[ruochen@localhost ~]$ mkdir ruochen_dir
drwxrwxr-x. 2 ruochen ruochen 6 May 22 08:49 ruochen_dir 775
-rw-rw-r--. 1 ruochen ruochen 0 May 22 08:49 ruochen_file 664

[root@localhost ~]# umask 0
[root@localhost ~]# umask
0000
[root@localhost ~]# touch new_file
[root@localhost ~]# mkdir new_dir
drwxrwxrwx. 2 root root 6 May 22 08:55 new_dir
-rw-rw-rw-. 1 root root 0 May 22 08:55 new_file

附加权限

SET位权限

  • SUID:
  • 为可执行文件设置(文件具有x位权限),
  • 权限标识字符: ‘s’ u+s 4 4***
  • 当执行这个可执行文件时,调用该文件的属主的身份执行

  [root@localhost ~]# ll /etc/shadow
----------. 1 root root 1585 May 22 07:33 /etc/shadow

  • 一文带你彻底搞懂Linux 文件权限管理_特殊权限
  • 我们先拿一个 ping 命令来举例,先将 /bin/ping 拷贝到 /home/ruochen 这个用户下,然后执行 /home/ruochen/ping 127.0.0.1 看看会发生什么?

  [ruochen@localhost ~]$ /home/ruochen/ping 127.0.0.1
ping: icmp open socket: Operation not permitted

  • 显然,没有权限。那么我们如果给他设置 SUID 权限会怎么样?

  [root@localhost ~]# chmod u+s /home/ruochen/ping

  • 现在再来执行

  [ruochen@localhost ~]$ /home/ruochen/ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.019 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.069 ms

  • 显然,现在可执行。
  • 一文带你彻底搞懂Linux 文件权限管理_访问权限_02一文带你彻底搞懂Linux 文件权限管理_ACL权限_03
  • SGID: 老鼠的儿子会打洞 2***
  • 一般设置在目录上,这时候在该目录下新建的文件/目录自动继承父目录的属组
  • 我们先在ruochen 用户下新建一个文件夹和一个文件

  [ruochen@localhost ~]$ mkdir sgid_dir
[ruochen@localhost ~]$ cd sgid_dir/
[ruochen@localhost sgid_dir]$ touch ruochen.txt

  • 然后,我们在root 用户下,切换到/home/ruochen/sgid_dir 目录下,新建一个 root.txt 文件

  [root@localhost ~]# cd /home/ruochen/sgid_dir/
[root@localhost sgid_dir]# touch root.txt

  • 然后,我们给刚才新建的目录加一个 sgid 权限

  [root@localhost ruochen]# chmod g+s sgid_dir/

  • 在root 用户下,在 sgid_dir 文件夹中继续新建一个目录

  [root@localhost sgid_dir]# mkdir root_dir
[root@localhost sgid_dir]# ll
total 0
drwxr-sr-x. 2 root ruochen 6 May 22 09:37 root_dir
-rw-r--r--. 1 root root 0 May 22 09:32 root.txt
-rw-rw-r--. 1 ruochen ruochen 0 May 22 09:30 ruochen.txt

  • 这时,我们发现一文带你彻底搞懂Linux 文件权限管理_访问权限_04,我们看一下 root_dir 父目录的属组

  [root@localhost sgid_dir]# cd ..
[root@localhost ruochen]# ll
total 0
drwxrwxr-x. 2 ruochen ruochen 6 May 22 08:49 ruochen_dir
-rw-rw-r--. 1 ruochen ruochen 0 May 22 08:49 ruochen_file
drwxrwsr-x. 3 ruochen ruochen 54 May 22 09:37 sgid_dir

  • 由此,我们可看出 sgid 权限的作用,也即一文带你彻底搞懂Linux 文件权限管理_附加权限_05

粘滞位权限(Sticky)

  • 为公共目录设置(777), 标识为 ‘t’
  • 1***
  • 用户不能删除其目录中其他用户的文件
  • /tmp, /var/tmp

  drwxrwxrwt.  17 root  root  4096 May 22 09:42 tmp

  • 在roor 用户下新建一个 /test 目录

  [root@localhost ~]# mkdir /test
drwxr-xr-x. 2 root root 6 May 22 09:50 test

  • 将其权限改为 777,并在目录下新建一个 root.txt 文件

  [root@localhost ~]# chmod 777 /test/
drwxrwxrwx. 2 root root 6 May 22 09:50 test
[root@localhost test]# touch root.txt

  • 然后我们切换到 ruochen 用户,发现可以切换到 /test 目录,也能创建和查看文件

  [ruochen@localhost share]$ cd /test/
[ruochen@localhost test]$ touch ruochen.txt
[ruochen@localhost test]$ ll
total 0
-rw-r--r--. 1 root root 0 May 22 09:53 root.txt
-rw-rw-r--. 1 ruochen ruochen 0 May 22 09:54 ruochen.txt

  • 我们尝试在ruochen 用户下,删除 /test 目录中的文件

  [ruochen@localhost test]$ rm -rf root.txt
[ruochen@localhost test]$ ll
total 0
-rw-rw-r--. 1 ruochen ruochen 0 May 22 09:54 ruochen.txt
**[ruochen@localhost test]$ rm -rf ruochen.txt
[ruochen@localhost test]$ ll
total 0

  • 我们发现 ruochen 用户可以把里面所有的文件都删除,这样,一文带你彻底搞懂Linux 文件权限管理_附加权限_06,这是不可以的, 这时,一文带你彻底搞懂Linux 文件权限管理_附加权限_07就派上用场了,其可以防止用户删除其他用户的文件
  • 我们在root 用户下,给 /test/ 目录加一个粘滞位权限,然后在该目录下新建一个 root.txt 文件

  [root@localhost ~]# chmod o+t /test/
[root@localhost test]# touch root.txt

  • 我们再次尝试在 ruochen 用户下删除 root.txt 文件

  [ruochen@localhost test]$ rm -rf root.txt 
rm: cannot remove ‘root.txt’: Operation not permitted

  • 发现删除被拒绝,这样,我们就可以理解粘滞位权限了

练习题-03

  • 创建一个共享目录 /home/cnrts, 特性如下
  • /home/cnrts 目录的组所有权是 manager
  • manager组的成员对目录有读写和执行的权限,除此之外的其他所有用户没有任何权限(root用户能够访问系统中的所有文件和目录)
  • 在/home/cnrts 目录中创建文件,其组所有权会自动设置为属于manager组

  [root@localhost ~]# mkdir /home/cnrts
[root@localhost ~]# groupadd manager
[root@localhost ~]# chown :manager /home/cnrts/
[root@localhost ~]# chmod 2770 /home/cnrts/


举报

相关推荐

0 条评论