Linux文件普通权限详解

一.文件权限的分类

  • 普通权限 用户正常情况去操作文件所具有的权限
  • 高级权限 用户对某个文件操作有特殊需求,而普通权限不能满足,需要给文件设置高级权限
  • 默认权限 用户在系统中创建一个文件,该文件默认都会有一个权限,该权限是默认有的
    【注意】:权限是设置在文件上的,而不是用户;设置权限目的是让相应的人(用户)去操作相应的文件

二.普通权限

(一)理解UGO的含义
UGO,指的是用户身份,每个字母代表不同的用户身份。
U(the user who owns it) 文件的拥有者(owner)或者创建者
G(other users in the file’s group) 在文件的所属组(默认是创建文件的用户的主组)里的用户
Oother users not in the file’s group) 既不是文件的创建者,也不在文件属组里的用户,称为其他人
a(all users)表示所有用户,包含ugo
(二) 理解普通权限rwx
含义
1、读权限—r(read) r用数字表示是4
针对目录 一个目录拥有r权限,说明可以查看该目录里的内容(ls命令列出)
针对普通文件 一个普通文件拥有r权限,说明可以查看该文件的内容(cat/head/tail/less/more等命令查看) 读权限r(read)
2、写权限—w(write) w用数字表示是2
针对目录 一个目录拥有w权限,说明可以在该目录里创建、删除、重命名等操作(mkdir/touch/mv/rm等)
针对普通文件 一个普通文件拥有w权限,说明可以修改该文件的内容(vi/vim编辑器编辑文件) 写权限w(write)
3、执行权限—x(execute) x用数字表示是1
针对目录 一个目录拥有x权限,说明可以进入或切换到该目录里(cd命令)
针对普通文件 一个普通文件拥有x权限,说明可以执行该文件(一般程序文件、脚本文件、命令都需要执行权限) 执行权限x(execute)
4、没有权限—横杠- -数字表示是0
没有任何权限用横杠-表示
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
(三) 修改文件普通权限(chmod)

  • 环境准备:
[root@192 ~]# rm -rf /tmp/*              
[root@192 ~]# mkdir /tmp/dir1
[root@192 ~]# touch /tmp/dir1/file{1..5}
[root@192 ~]# touch /tmp/dir1/test{1..3}

查看/tmp/dir1目录,显示如下

[root@192 ~]# ll /tmp/dir1
总用量 0
-rw-r--r--. 1 root root 0 6月  27 11:00 file1
-rw-r--r--. 1 root root 0 6月  27 11:00 file2
-rw-r--r--. 1 root root 0 6月  27 11:00 file3
-rw-r--r--. 1 root root 0 6月  27 11:00 file4
-rw-r--r--. 1 root root 0 6月  27 11:00 file5
-rw-r--r--. 1 root root 0 6月  27 11:01 test1
-rw-r--r--. 1 root root 0 6月  27 11:01 test2
-rw-r--r--. 1 root root 0 6月  27 11:01 test3
  • 以非文件属主用户更改file1
[root@192 ~]# su - sika
[sika@192 ~]$ echo hello > tmp/dir1/file1

更改显示结果如下:

-bash: /tmp/dir1/file1: 权限不够
  • 以文件属主用户更改file1
[sika@192 ~]$ su - root
[root@192 ~]# echo hello > file1
[root@192 ~]# echo hello > /tmp/dir1/file1

更改结果显示如下:

[root@192 ~]# cat file1
hello

chmod命令用法
chmod [选项] 模式 文件名
[常见选项]: -R, --recursive 递归更改目录和目录里文件的权限
chmod修改文件普通权限
【举例说明】
①使用字母形式修改

修改文件权限

[root@192 ~]# chmod u=rwx /tmp/dir1/file2
[root@192 ~]# ll /tmp/dir1/file2
-rwxr--r--. 1 root root 0 6月  27 11:00 /tmp/dir1/file2
[root@192 ~]# chmod g+w /tmp/dir1/file2
[root@192 ~]# ll /tmp/dir1/file2
-rwxrw-r--. 1 root root 0 6月  27 11:00 /tmp/dir1/file2
[root@192 ~]# chmod o-r /tmp/dir1/file2
[root@192 ~]# ll /tmp/dir1/file2
-rwxrw----. 1 root root 0 6月  27 11:00 /tmp/dir1/file2
[root@192 ~]# chmod u=rw,g=w,o=r /tmp/dir1/file2
[root@192 ~]# ll /tmp/dir1/file2
-rw--w-r--. 1 root root 0 6月  27 11:00 /tmp/dir1/file2
[root@192 ~]# chmod a-r /tmp/dir1/file2
[root@192 ~]# ll /tmp/dir1/file2
--w--w----. 1 root root 0 6月  27 11:00 /tmp/dir1/file2

修改目录权限
将dir1目录的所有用户权限更改为不允许read

[root@192 ~]# ll /tmp/
drwxr-xr-x. 2 root root 110 6月  27 11:01 dir1
[root@192 ~]# chmod a-r /tmp/dir1
[root@192 ~]# ll /tmp/
d-wx--x--x. 2 root root 110 6月  27 11:01 dir1

dir1目录下的文件的权限未被更改

[root@192 ~]# ll /tmp/dir1
总用量 4
-rw-r--r--. 1 root root 6 6月  27 11:18 file1
--w--w----. 1 root root 0 6月  27 11:00 file2
-rw-r--r--. 1 root root 0 6月  27 11:00 file3
-rw-r--r--. 1 root root 0 6月  27 11:00 file4
-rw-r--r--. 1 root root 0 6月  27 11:00 file5
-rw-r--r--. 1 root root 0 6月  27 11:01 test1
-rw-r--r--. 1 root root 0 6月  27 11:01 test2
-rw-r--r--. 1 root root 0 6月  27 11:01 test3

将dir1目录的所有用户权限递归更改为不允许read

[root@192 ~]# chmod -R a-r /tmp/dir1
[root@192 ~]# ll -R /tmp/dir1
/tmp/dir1:
总用量 4
--w-------. 1 root root 6 6月  27 11:18 file1
--w--w----. 1 root root 0 6月  27 11:00 file2
--w-------. 1 root root 0 6月  27 11:00 file3
--w-------. 1 root root 0 6月  27 11:00 file4
--w-------. 1 root root 0 6月  27 11:00 file5
--w-------. 1 root root 0 6月  27 11:01 test1
--w-------. 1 root root 0 6月  27 11:01 test2
--w-------. 1 root root 0 6月  27 11:01 test3

①使用数字形式修改

  • 字母和数字对应关系:
    r——4
    w——2
    x——1
    -——0
    rw- r-x r-- 用数字表示就是654(三个数字分别代表ugo三组各自之和)
    rwx rw- — 用数字表示就是760
    755 用字母表示就是rwx r-x r-x
    644 用字母表示就是rw- r-- r-
    【举例说明】
[root@192 dir1]# ll -d /tmp/dir1
d-wx--x--x. 2 root root 110 6月  27 11:01 /tmp/dir1
[root@192 dir1]# chmod 764 /tmp/dir1
[root@192 dir1]# ll -d /tmp/dir1
drwxrw-r--. 2 root root 110 6月  27 11:01 /tmp/dir1

(四)修改文件的属主和属组

  • chown命令修改
    用法:chown [选项]… [所有者][:[组]] 文件…
    chown 命令既可以修改文件的属主,也可以修改文件的属组。
    【举例说明】
[root@192 ~]# cd /tmp/dir1
[root@192 dir1]# chown user01.admin file1
[root@192 dir1]# ll
总用量 4
--w-------. 1 user01 admin 6 6月  27 11:18 file1
--w--w----. 1 root   root  0 6月  27 11:00 file2
--w-------. 1 root   root  0 6月  27 11:00 file3
--w-------. 1 root   root  0 6月  27 11:00 file4
--w-------. 1 root   root  0 6月  27 11:00 file5
--w-------. 1 root   root  0 6月  27 11:01 test1
--w-------. 1 root   root  0 6月  27 11:01 test2
--w-------. 1 root   root  0 6月  27 11:01 test3
[root@192 dir1]# chown .admin file2
[root@192 dir1]# ll
总用量 4
--w-------. 1 user01 admin 6 6月  27 11:18 file1
--w--w----. 1 root   admin 0 6月  27 11:00 file2
--w-------. 1 root   root  0 6月  27 11:00 file3
--w-------. 1 root   root  0 6月  27 11:00 file4
--w-------. 1 root   root  0 6月  27 11:00 file5
--w-------. 1 root   root  0 6月  27 11:01 test1
--w-------. 1 root   root  0 6月  27 11:01 test2
--w-------. 1 root   root  0 6月  27 11:01 test3
[root@192 dir1]# chown user03. file3
[root@192 dir1]# ll
总用量 4
--w-------. 1 user01 admin  6 6月  27 11:18 file1
--w--w----. 1 root   admin  0 6月  27 11:00 file2
--w-------. 1 user03 user03 0 6月  27 11:00 file3
--w-------. 1 root   root   0 6月  27 11:00 file4
--w-------. 1 root   root   0 6月  27 11:00 file5
--w-------. 1 root   admin  0 6月  27 11:01 test1
--w-------. 1 root   admin  0 6月  27 11:01 test2
--w-------. 1 root   admin  0 6月  27 11:01 test3

:和.都可以用

[root@192 dir1]# chown user04: file4
[root@192 dir1]# ll
总用量 4
--w-------. 1 user01 admin  6 6月  27 11:18 file1
--w--w----. 1 root   admin  0 6月  27 11:00 file2
--w-------. 1 user03 user03 0 6月  27 11:00 file3
--w-------. 1 user04 user04 0 6月  27 11:00 file4
--w-------. 1 root   root   0 6月  27 11:00 file5
--w-------. 1 root   user04 0 6月  27 11:01 test1
--w-------. 1 root   user04 0 6月  27 11:01 test2
--w-------. 1 root   user04 0 6月  27 11:01 test3
  • chgrp命令修改
    用法:chgrp [选项]… 用户组 文件…
    chgrp 命令只能修改文件的属组。
    【举例说明】
[root@192 dir1]# chgrp user04 test*
[root@192 dir1]# ll
总用量 4
--w-------. 1 user01 admin  6 6月  27 11:18 file1
--w--w----. 1 root   admin  0 6月  27 11:00 file2
--w-------. 1 user03 user03 0 6月  27 11:00 file3
--w-------. 1 root   root   0 6月  27 11:00 file4
--w-------. 1 root   root   0 6月  27 11:00 file5
--w-------. 1 root   user04 0 6月  27 11:01 test1
--w-------. 1 root   user04 0 6月  27 11:01 test2
--w-------. 1 root   user04 0 6月  27 11:01 test3

本博客更多的是对课件的二次修改和整理,目的仅为更好的记忆和理解linux文件普通权限的内容

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值