Linux基本功系列之chmod命令实战

前言🚀🚀🚀
想要学好Linux命令是基本功企业中常用的命令大约200多个不管是写shell脚本还是管理操作系统最常用的命令必须要牢牢掌握像我们以前学乘法口诀一样烂熟于心唯有如此才能打牢基础。
💓 知识最重要的是记忆
💓 入门须知 想要人生从容必须全力以赴努力才是你最终的入场券🚀🚀🚀
💕 最后 努力成长自己愿我们都能在看不到的地方闪闪发光 一起加油进步🍺🍺🍺

一. chmod命令介绍

chmod命令来自于英文词组”change mode“的缩写其功能是用于改变文件或目录权限的命令。

默认只有文件的所有者和管理员root可以设置文件权限普通用户只能管理自己文件的权限属性。

设置权限时可以使用数字法亦可使用字母表达式对于目录文件建议加入-R参数进行递归操作意味着不仅对于目录本身也对目录内的子文件/目录都进行新权限的设定。

二. 语法格式及常用选项

常用参数
我们可以使用 --help查看所有参数的使用。

[root@mufenggrow ~]# chmod --help
用法chmod [选项]... 模式[,模式]... 文件...
 或chmod [选项]... 八进制模式 文件...
 或chmod [选项]... --reference=参考文件 文件...
Change the mode of each FILE to MODE.
With --reference, change the mode of each FILE to that of RFILE.

  -c, --changes          like verbose but report only when a change is made
  -f, --silent, --quiet  suppress most error messages
  -v, --verbose          output a diagnostic for every file processed
      --no-preserve-root  do not treat '/' specially (the default)
      --preserve-root    fail to operate recursively on '/'
      --reference=RFILE  use RFILE's mode instead of MODE values
  -R, --recursive        change files and directories recursively
      --help		显示此帮助信息并退出
      --version		显示版本信息并退出

从chmod的帮助文档中我们发现 chmod的常用参数为

在这里插入图片描述

三. 参考案例

修改权限的方式有两种

第一种 使用字符的设定进行修改

语法chmod [对谁操作] [操作符] [赋于什么权限] 文件名

u----> 用户user表示文件或目录的所有者
g---->用户组group表示文件或目录所属的用户组
o---->其它用户others
a---->所有用户all

第二种 使用八进制数字表示权限

数字型文件权限

r=4 可读权限
w=2 可写权限
x=1 执行权限
没有任何权限为0

我们一起看一下常见的权限操作

3.1 对全部用户增加写的权限

读的权限为r对所有用户增加读权限意味着 所有者所有组其他用户都设置为读的权限。

我们使用 字符设定来进行修改

[root@mufenggrow test]# touch a.txt
[root@mufenggrow test]# ll a.txt
-rw-r--r--. 1 root root 0 121 16:17 a.txt
[root@mufenggrow test]# chmod a+w a.txt
[root@mufenggrow test]# ll a.txt
-rw-rw-rw-. 1 root root 0 121 16:17 a.txt
[root@mufenggrow test]# 

可以看到原本所有组和其他用户只有读的权限使用chmod后a.txt所有用户都具有了写权限。

3.2 所有用户减去读的权限

根据3.1的写法我们可以猜测减去读的权限为 a-r 接下来我们测试下

[root@mufenggrow test]# ll a.txt
-rw-rw-rw-. 1 root root 0 121 16:17 a.txt
[root@mufenggrow test]# chmod a-r a.txt
[root@mufenggrow test]# ll a.txt
--w--w--w-. 1 root root 0 121 16:17 a.txt
[root@mufenggrow test]# 

可以看到现在a.txt文件之后写权限了。

3.3 给文件的所有者和所有组加上读写权限

读写权限为 rw
所有者和所有组为 gu
意味着我们要使用 chmod gu+rw,接下来我们测试下

[root@mufenggrow test]# ll a.txt
--w--w--w-. 1 root root 0 121 16:17 a.txt
[root@mufenggrow test]# chmod ug+rw a.txt
[root@mufenggrow test]# ll a.txt
-rw-rw--w-. 1 root root 0 121 16:17 a.txt
[root@mufenggrow test]# 

3.4 设置所有用户为读写执行的权限

读写执行为 rwx
所有用户可以用 a=rwx

[root@mufenggrow test]# ll a.txt
-rw-rw--w-. 1 root root 0 121 16:17 a.txt
[root@mufenggrow test]# chmod a=rwx a.txt
[root@mufenggrow test]# ll a.txt
-rwxrwxrwx. 1 root root 0 121 16:17 a.txt

3.5 文件拥有着为rwx所属组为rw其它为r

我们来分析下这个题目
所有组 为rwx ,意味着 u=rwx
所有组为 g=rw
其他为r ,以为着 o=r

所以我们应该执行

[root@mufenggrow test]# chmod u=rwx,g=rw,o=r a.txt
[root@mufenggrow test]# ll a.txt
-rwxrw-r--. 1 root root 0 121 16:17 a.txt
[root@mufenggrow test]# 

这里出现多个的时候一定要加逗号分割开。

3.6 去掉所有者的rw权限

去掉所有这点rw权限这里我们使用的操作符是 -

所有这是U 最终答案为 U-rw

root@mufenggrow test]# ll a.txt
-rwxrw-r--. 1 root root 0 121 16:17 a.txt
[root@mufenggrow test]# chmod u-rw a.txt
[root@mufenggrow test]# ll a.txt
---xrw-r--. 1 root root 0 121 16:17 a.txt
[root@mufenggrow test]# 

从结果可以看到a.txt文件的所有者的读写权限都被去掉了。

3.7 将test目录下所有文件都变成读写执行的权限

此时所有文件都变为读写执行即 a=rwx
涉及到所有文件我们用到参数 -R

[root@mufenggrow test]# touch {1..3}.txt
[root@mufenggrow test]# ll 
总用量 0
-rw-r--r--. 1 root root 0 121 16:31 1.txt
-rw-r--r--. 1 root root 0 121 16:31 2.txt
-rw-r--r--. 1 root root 0 121 16:31 3.txt
---xrw-r--. 1 root root 0 121 16:17 a.txt
[root@mufenggrow test]# chmod -R a=rwx ./*
[root@mufenggrow test]# ll
总用量 0
-rwxrwxrwx. 1 root root 0 121 16:31 1.txt
-rwxrwxrwx. 1 root root 0 121 16:31 2.txt
-rwxrwxrwx. 1 root root 0 121 16:31 3.txt
-rwxrwxrwx. 1 root root 0 121 16:17 a.txt

使用八进制的形式设置权限

3.8 设置所有者为读写其他为读

根据八进制可以得知只需要设置644即可

[root@mufenggrow test]# chmod 644 a.txt
[root@mufenggrow test]# ll a.txt
-rw-r--r--. 1 root root 0 121 16:17 a.txt
[root@mufenggrow test]# 

可以发现用数字相对来说更简单一些我们可以直接进行换算即可

r=4 可读权限
w=2 可写权限
x=1 执行权限
没有任何权限为0

总结

chmod在日常工作中用的比较多我们多数情况下用到的都是八进制的形式。

💕💕💕 好啦这就是今天要分享给大家的全部内容了我们下期再见✨ ✨ ✨
🍻🍻🍻如果你喜欢的话就不要吝惜你的一键三连了~

在这里插入图片描述

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: linux

“Linux基本功系列之chmod命令实战” 的相关文章