linux 获取文件父目录权限,一文带你彻底搞懂Linux 文件权限管理

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

| - | - | - | - | - |

| 属主权限 | 属组权限 | 其他人权限 | 属主 | 属组权限项读写执行读写执行读写执行字符表示rwxrwxrwx

数字表示421421421

权限分配文件所有者(属主)文件所有者(属主)文件所有者(属主)文件所属组(属组)文件所属组(属组)文件所属组(属组)其他用户其他用户其他用户

| 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的权限使用规范

设置ACLsetfacl -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

$\color{red}为什么shadow 文件无权限,普通用户还能登录/修改密码?$

我们先拿一个 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

显然,现在可执行。

$\color{red}由此,我们可以看出SUID权限的作用也就能解释前面所说的普通用户登录/修改密码问题$

SGID: 老鼠的儿子会打洞一般设置在目录上,这时候在该目录下新建的文件/目录自动继承父目录的属组

我们先在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

这时,我们发现 $\color{red}root\_dir 的属组不是root,而是 ruochen$,我们看一下 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 权限的作用,也即 $\color{red}“老鼠的儿子会打洞”$

粘滞位权限(Sticky)为公共目录设置(777), 标识为 't'

1***

用户不能删除其目录中其他用户的文件

/tmp, /var/tmpdrwxrwxrwt. 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 用户可以把里面所有的文件都删除,这样,$\color{red}共享文件就可以被随意删除$,这是不可以的, 这时,$\color{red}粘滞位权限$就派上用场了,其可以防止用户删除其他用户的文件

我们在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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值