linux亦步亦趋(09)文件权限管理

chmod命令:改变目录或文件的权限

chmod - change file access permissions
命令格式:chmod {u|g|o}{+|-|=}{r|w|x} 文件名或目录名大括号里面的参数表示必选参数竖线隔开表示必选其中一个或多个。

我们拆开来解释一下chmod的用法:

  • u、g、o分表表示的是文件的所有者、所属组的用户、其他用户
  • +、-、= 分别表示 增加权限、删除权限、设置权限不考虑原来
  • r、w、x 分别表示读、写、可执行三个权限。

举几个例子说明一下:

  • 给文件所有者添加可执行权限:

 

[root@localhost etc]# ls -l hosts
-rw-r--r-x 3 root root 0 07-22 12:07 hosts
[root@localhost etc]# chmod u+x hosts
[root@localhost etc]# ls -l hosts
-rwxr--r-x 3 root root 0 07-22 12:07 hosts
[root@localhost etc]#

 

  • 给文件所有者删除可写的权限:
[root@localhost etc]# ls -l hosts
-rwxr--r-x 3 root root 0 07-22 12:07 hosts
[root@localhost etc]# chmod u-x hosts
[root@localhost etc]# ls -l hosts
-rw-r--r-x 3 root root 0 07-22 12:07 hosts
[root@localhost etc]#

 

  • 给同组用户增加可读写权限:
[root@localhost etc]# ls -l hosts
-rw-r--r-x 3 root root 0 07-22 12:07 hosts
[root@localhost etc]# chmod g+rw hosts
[root@localhost etc]# ls -l hosts
-rw-rw-r-x 3 root root 0 07-22 12:07 hosts
[root@localhost etc]#

 

  • 给其他用户增加可执行权限:
[root@localhost etc]# ls -l hosts
-rw-rw-r-- 3 root root 0 07-22 12:07 hosts
[root@localhost etc]# chmod o+x hosts
[root@localhost etc]# ls -l hosts
-rw-rw-r-x 3 root root 0 07-22 12:07 hosts
[root@localhost etc]#

 

  • 给其他用户和组用户删除可执行权限:
[root@localhost etc]# ls -l hosts
-rw-rw-r-x 3 root root 0 07-22 12:07 hosts
[root@localhost etc]# chmod go-x hosts
[root@localhost etc]# ls -l hosts
-rw-rw-r-- 3 root root 0 07-22 12:07 hosts
[root@localhost etc]#

 

chmod数字的操作方式:

上面是用xwr和ugo来表示权限的,还有一种用数字来表示的方式:

其中rwx分别由数字4、2、1 来表示一组权限的数字加起来占一位,比如下面的权限

-rwxr--r-- 的数字表示为744  是因为所有者的rwx加起来是7  组和其他用户是4 再举几个例子

-rw-rw-rw-的数字表示为:666.

 

[root@localhost etc]# chmod 752 hosts
[root@localhost etc]# ls -l hosts
-rwxr-x-w- 3 root root 0 07-22 12:07 hosts
[root@localhost etc]# chmod 7 hosts
[root@localhost etc]# ls -l hosts
-------rwx 3 root root 0 07-22 12:07 hosts
[root@localhost etc]# chmod 777 hosts
[root@localhost etc]# ls -l hosts
-rwxrwxrwx 3 root root 0 07-22 12:07 hosts
[root@localhost etc]#

注意事项:

  • 如果数字到三位则会从后面开始匹配,比如只有一个7 则表示给其他用户赋权限而所有者和组则使用0
  • 该方法比用字母更常用。因为很多官方文档和脚本用这个。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值