linux 权限代码,Linux_权限(示例代码)

一、查看文件或文件夹权限

[[email protected] etc]# ll -h /etc #ll 是ls -l 的缩写方式 -h文件大小单位k

截取其中三行说明

drwxr-xr-x. 2 root root 4.0K Sep 2 08:19 init

lrwxrwxrwx. 1 root root 11 Sep 2 08:11 init.d -> rc.d/init.d

-rw-r--r--. 1 root root 884 Sep 2 00:26 inittab

分为 6 块(已其中第一行解释)

1. drwxr-xr-x. 注:最后那个点是ls把多acl和selinux属性加进去了,下面只考虑前十位

第一位表是文件类型 d:目录 -:普通文件 l:链接文件

此时需要了解权限的分组:前三位是文件所有者(user,简写u);中间三位是所在组(group,简写g);后三位其他人(other,简写o)

第二位到第10位,即rwxr-xr-x权限(r:可读,以二进制表示2^2;w:可写,以二进制表示2^1;x:可执行,以二进制表示2^0;-:无所在权限,以二进制表示0)

例:r-x 权限(可读可执行但不可写,数字表示为4+0+1=5)

那么rwxr-xr-x中,文件所有者的权限是可读可写可执行(4+2+1=7);所在组的权限是可读不可写可执行(4+0+1=5);其他人的权限是可读不可写可执行(4+0+1=5)

则rwxr-xr-x以数字表示为755

2. 2 代表文件连接数

3. root root 分别代表用户名和用户组

4. 4.0k 代表文件大小

5.Sep 2 08:19 日期

6. init 文件名

一、权限操作

1. chmod 分配权限

属性:u(user) g(group) o(other)  a(all)

a. 权限设置符号:+(添加权限)  -(去掉权限)  =(设置完整权限)

[[email protected] tmp]# touch test.txt

[[email protected] tmp]# ll

total 0

-rw-r--r--. 1 root root 0 Sep 6 19:58 test.txt

[[email protected] tmp]# chmod u+w,g-r,o=rwx test.txt #注意各组权限以,分隔

[[email protected] tmp]# ll

total 0

-rw----rwx. 1 root root 0 Sep 6 19:58 test.txt

b. 以数字形式设置权限

[[email protected] tmp]# chmod 777 test.txt #上面已解释7=r+w+x

[[email protected] tmp]# ll

total 0

-rwxrwxrwx. 1 root root 0 Sep 6 19:58 test.txt

c. 递归修改权限,即设置文件夹权限时加上-R,里面的子目录和文件都一并设置

[[email protected] test]# ll

total 4

drwxr-xr-x. 3 root root 4096 Sep 6 20:06 test1

-rw-r--r--. 1 root root 0 Sep 6 20:07 test1.txt

-rw-r--r--. 1 root root 0 Sep 6 20:07 test.txt

[[email protected] test]# chmod 777 -R ../test

[[email protected] test]# ll

total 4

drwxrwxrwx. 3 root root 4096 Sep 6 20:06 test1

-rwxrwxrwx. 1 root root 0 Sep 6 20:07 test1.txt

-rwxrwxrwx. 1 root root 0 Sep 6 20:07 test.txt

2. chown 修改拥有者

a. 修改单个文件或文件夹的拥有者

[[email protected] test]# ll

total 4

drwxrwxrwx. 3 root root 4096 Sep 6 20:06 test1

-rwxrwxrwx. 1 root root 0 Sep 6 20:07 test1.txt

-rwx------. 1 root root 0 Sep 6 20:07 test.txt

[[email protected] test]# chown eRrsr test.txt

[[email protected] test]# ll

total 4

drwxrwxrwx. 3 root root 4096 Sep 6 20:06 test1

-rwxrwxrwx. 1 root root 0 Sep 6 20:07 test1.txt

-rwx------. 1 eRrsr root 0 Sep 6 20:07 test.txt

b. 递归修改文件夹下的所有子目录和文件的拥有者

[[email protected] test]# ll

total 4

drwxrwxrwx. 3 root root 4096 Sep 6 20:06 test1

-rwxrwxrwx. 1 root root 0 Sep 6 20:07 test1.txt

-rwx------. 1 eRrsr root 0 Sep 6 20:07 test.txt

[[email protected] test]# chown -R eRrsr ../test

[[email protected] test]# ll

total 4

drwxrwxrwx. 3 eRrsr root 4096 Sep 6 20:06 test1

-rwxrwxrwx. 1 eRrsr root 0 Sep 6 20:07 test1.txt

-rwx------. 1 eRrsr root 0 Sep 6 20:07 test.txt

3. chgrp 修改所在组

a. 修改单个文件或文件夹的所在组

[[email protected] test]# ll

total 4

drwxrwxrwx. 3 eRrsr root 4096 Sep 6 20:06 test1

-rwxrwxrwx. 1 eRrsr root 0 Sep 6 20:07 test1.txt

-rwx------. 1 eRrsr root 0 Sep 6 20:07 test.txt

[[email protected] test]# chgrp eRrsr test.txt

[[email protected] test]# ll

total 4

drwxrwxrwx. 3 eRrsr root 4096 Sep 6 20:06 test1

-rwxrwxrwx. 1 eRrsr root 0 Sep 6 20:07 test1.txt

-rwx------. 1 eRrsr eRrsr 0 Sep 6 20:07 test.txt

b.  递归修改文件夹下的所有子目录和文件的

[[email protected] test]# ll

total 4

drwxrwxrwx. 3 eRrsr root 4096 Sep 6 20:06 test1

-rwxrwxrwx. 1 eRrsr root 0 Sep 6 20:07 test1.txt

-rwx------. 1 eRrsr eRrsr 0 Sep 6 20:07 test.txt

[[email protected] test]# chgrp -R eRrsr ../test

[[email protected] test]# ll

total 4

drwxrwxrwx. 3 eRrsr eRrsr 4096 Sep 6 20:06 test1

-rwxrwxrwx. 1 eRrsr eRrsr 0 Sep 6 20:07 test1.txt

-rwx------. 1 eRrsr eRrsr 0 Sep 6 20:07 test.txt

需要注意的是:

如果在/usr/tmp下创建了一个文件夹该文件夹root分给eRrsr所有权限,针对于eRrsr其中的文件或子目录并没有权限,那登录eRrsr后是否能删文件夹内的文件呢?

可以

PS:对与用户或用户组的修改请查看上一小节

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值