文件和文件夹权限

文件权限

  • r 读
  • w 写
  • x 执行

文件夹权限

  • r 读取文件夹内文件列表
  • w 在文件夹内写入和修改和删除文件
  • x 进入文件夹

权限分组

-rwxrwsrws.

drwxrwxrwx.

  • 用户 -u user
  • 用户组 -g group
  • 其他人 -o other
  • 所有人 -a all

权限操作

    • 增加权限
    • 删除权限

总结

chomd修改权限

方法1:模式法

chmod

  • who:u g o a
  • opt:+ -
  • per:r w x

示例:请问执行cp /etc/issus /data/dir/ 所需要的最小权限

猜测:

/data/dir/ d-wx------
/data/ d-wx------
/etc/ dr-x------
/etc/issus d---------

1.分别建在/data 和/data/dir 以及 /etcT并在其下创建issus文件

[01:53:36 root@C8-3-55 ~]#mkdir /data
[01:53:42 root@C8-3-55 ~]#mkdir /data/dir
[01:54:04 root@C8-3-55 ~]#ll -d /data/dir/
drwxr-xr-x. 2 root root 6 3月   5 01:53 /data/dir/
[01:54:10 root@C8-3-55 ~]#ll -d /data/
drwxr-xr-x. 3 root root 17 3月   5 01:53 /data/
[01:54:17 root@C8-3-55 ~]#mkdir /etcT
[01:54:39 root@C8-3-55 ~]#touch /etcT/issus
[01:55:09 root@C8-3-55 ~]#ll /etcT/
总用量 0
-rw-r--r--. 1 root root 0 3月   5 01:54 issus
[01:55:14 root@C8-3-55 ~]#ll -d /etcT/
drwxr-xr-x. 2 root root 19 3月   5 01:54 /etcT/

2.先拷贝以下测试

[01:55:23 root@C8-3-55 ~]#cp /etcT/issus /data/dir/;ll /data/dir/
总用量 0
-rw-r--r--. 1 root root 0 3月   5 01:58 issus

在目前权限下,拷贝成功

3.重设权限
3.1 重设源文件和所在文件夹

  • 源文件直接撸没,设置为a-rwx
  • 源文件所在文件夹只需要读取和进入的权限,设置为500
  • 其后发现进入的权限都不需要,是源文件所在文件夹设置为400
  • 最后发现作为属主的root,源文件所在文件夹什么权限都可以不需要,设置为000仍然可以完成拷贝
[01:58:48 root@C8-3-55 ~]#chmod a-rwx /etcT/issus ## 将原文件权限撸为0
[02:01:43 root@C8-3-55 ~]#ll /etcT/issus
----------. 1 root root 0 3月   5 01:54 /etcT/issus
[02:01:55 root@C8-3-55 ~]#chmod 500 /etcT ## 将源文件夹权限设为500
[02:03:21 root@C8-3-55 ~]#ll /etcT
总用量 0
----------. 1 root root 0 3月   5 01:54 issus
[02:03:42 root@C8-3-55 ~]#ll -d /etcT/
dr-x------. 2 root root 19 3月   5 01:54 /etcT/
[02:03:46 root@C8-3-55 ~]#cp /etcT/issus /data/dir/;ll /data/dir/ ## 拷贝文件
cp:是否覆盖'/data/dir/issus'? y
总用量 0
-rw-r--r--. 1 root root 0 3月   5 02:05 issus ## 拷贝成功
[02:05:28 root@C8-3-55 ~]#chmod 400 /etcT ## 将源文件夹权限设为400
[02:08:00 root@C8-3-55 ~]#ll -d /etcT
dr--------. 2 root root 19 3月   5 01:54 /etcT
[02:08:11 root@C8-3-55 ~]#cp /etcT/issus /data/dir/;ll /data/dir/ ## 拷贝文件
cp:是否覆盖'/data/dir/issus'? y
总用量 0
-rw-r--r--. 1 root root 0 3月   5 02:08 issus ## 拷贝成功
[02:08:22 root@C8-3-55 ~]#chmod 000 /etcT ## 将源文件夹权限撸到000
[02:10:15 root@C8-3-55 ~]#ll -d /etcT
d---------. 2 root root 19 3月   5 01:54 /etcT
[02:10:24 root@C8-3-55 ~]#cp /etcT/issus /data/dir/;ll /data/dir/ ## 拷贝文件
cp:是否覆盖'/data/dir/issus'? y
总用量 0
-rw-r--r--. 1 root root 0 3月   5 02:10 issus ## 拷贝成功

3.2 设置目标文件夹权限

  • 有了经验,直接将目标文件夹全部撸成001,拷贝成功
[02:10:31 root@C8-3-55 ~]#ll -d /data/ /data/dir/  ## 查看现有权限设置
drwxr-xr-x. 3 root root 17 3月   5 01:53 /data/
drwxr-xr-x. 2 root root 19 3月   5 01:58 /data/dir/
[02:15:21 root@C8-3-55 ~]#chmod -R 001 /data/ ## 重设权限为001
[02:16:09 root@C8-3-55 ~]#ll -d /data/ /data/dir/
d--------x. 3 root root 17 3月   5 01:53 /data/
d--------x. 2 root root 19 3月   5 01:58 /data/dir/
[02:16:12 root@C8-3-55 ~]#cp /etcT/issus /data/dir/;ll /data/dir/ ## 拷贝文件
cp:是否覆盖'/data/dir/issus'? y
总用量 0
---------x. 1 root root 0 3月   5 02:16 issus ## 拷贝成功
  • 再次将目标文件夹属性撸成000,依旧拷贝成功

[02:16:31 root@C8-3-55 ~]#chmod -R 000 /data/
[02:18:16 root@C8-3-55 ~]#ll -d /data/ /data/dir/
d---------. 3 root root 17 3月   5 01:53 /data/
d---------. 2 root root 19 3月   5 01:58 /data/dir/
[02:18:20 root@C8-3-55 ~]#cp /etcT/issus /data/dir/;ll /data/dir/
cp:是否覆盖'/data/dir/issus'? y
总用量 0
----------. 1 root root 0 3月   5 02:18 issus

事实证明,作为权倾天下的root,即使文件和文件夹没有任何权限,也能执行cp成功

答案:所需最小权限全部为000


切换到普通用户就没那么幸运了

[02:25:58 root@C8-3-55 sun1]#chown -R sun1:sun1 /etcT
[02:26:32 root@C8-3-55 sun1]#chown -R sun1:sun1 /data
[02:27:20 root@C8-3-55 sun1]#ll -d /etcT/issus
----------. 1 sun1 sun1 0 3月   5 01:54 /etcT/issus
[02:27:43 root@C8-3-55 sun1]#ll -d /etcT
d---------. 2 sun1 sun1 19 3月   5 01:54 /etcT
[02:27:50 root@C8-3-55 sun1]#ll -d /data
d---------. 3 sun1 sun1 17 3月   5 01:53 /data
[02:27:55 root@C8-3-55 sun1]#ll -d /data/dir
d---------. 2 sun1 sun1 19 3月   5 01:58 /data/dir
[02:28:01 root@C8-3-55 sun1]#su sun1
[02:29:02 sun1@C8-3-55 ~]$cp /etcT/issus /data/dir/
cp: 访问'/data/dir/' 失败: 权限不够

目标文件夹设置好权限

[02:35:26 sun1@C8-3-55 ~]$chmod u-rx /data/dir
[02:36:10 sun1@C8-3-55 ~]$ll -d /data/dir/
d-w-------. 2 sun1 sun1 19 3月   5 01:58 /data/dir/
[02:36:23 sun1@C8-3-55 ~]$chmod u-rw /data/
[02:37:18 sun1@C8-3-55 ~]$ll -d /data/
d--x------. 3 sun1 sun1 17 3月   5 01:53 /data/
[02:38:11 sun1@C8-3-55 ~]$cp /etcT/issus /data/dir/
cp: 无法获取'/etcT/issus' 的文件状态(stat): 权限不够

源文件夹又报错了


[02:38:43 sun1@C8-3-55 ~]$ll -d /etcT/
d---------. 2 sun1 sun1 19 3月   5 01:54 /etcT/
[02:40:23 sun1@C8-3-55 ~]$ll /etcT/issus
ls: 无法访问'/etcT/issus': 权限不够
[02:40:47 sun1@C8-3-55 ~]$chmod u+r /etcT
[02:41:09 sun1@C8-3-55 ~]$ll -d /etcT/
dr--------. 2 sun1 sun1 19 3月   5 01:54 /etcT/
[02:41:20 sun1@C8-3-55 ~]$ll /etcT/issus
ls: 无法访问'/etcT/issus': 权限不够
[02:41:25 sun1@C8-3-55 ~]$cp /etcT/issus /data/dir/
cp: 无法获取'/etcT/issus' 的文件状态(stat): 权限不够
[02:41:47 sun1@C8-3-55 ~]$chmod u+x /etcT
[02:42:09 sun1@C8-3-55 ~]$ll -d /etcT/
dr-x------. 2 sun1 sun1 19 3月   5 01:54 /etcT/ ## 源文件夹 修改成500
[02:42:13 sun1@C8-3-55 ~]$cp /etcT/issus /data/dir/ ## 拷贝文件
cp: 无法获取'/data/dir/issus' 的文件状态(stat): 权限不够 ## 拷贝成功,但未显示

修改目标文件夹权限

[02:42:57 sun1@C8-3-55 ~]$chmod 300 /data/dir
[02:50:35 sun1@C8-3-55 ~]$ll -d /data/dir
d-wx------. 2 sun1 sun1 19 3月   5 01:58 /data/dir
[02:50:43 sun1@C8-3-55 ~]$chmod 100 /data
[02:51:04 sun1@C8-3-55 ~]$ll -d /data/
d--x------. 3 sun1 sun1 17 3月   5 01:53 /data/
[02:51:10 sun1@C8-3-55 ~]$cp /etcT/issus /data/dir/

在非root用户条件下,作为文件夹的拥有者:

  • 源文件最小权限 000
  • 源文件夹最小权限500
  • 目标外层文件/data夹最小权限 100
  • 目标内层文件夹/data/dir最小权限300
* * * 胖并快乐着的死肥宅 * * *
原文地址:https://www.cnblogs.com/bpzblog/p/14486687.html