Linux文件权限
chmod
在说文件权限之前,先介绍一下Linux
下关于文件的一些知识:
root@ubuntu:~# ll
total 48
drwx------ 6 root root 4096 Aug 29 15:29 ./
drwxr-xr-x 19 root root 4096 Jul 18 21:50 ../
-rw------- 1 root root 76 Aug 29 10:06 .bash_history
-rw-r--r-- 1 root root 3106 Oct 15 2021 .bashrc
drwx------ 3 root root 4096 Jul 8 03:01 .cache/
-rw------- 1 root root 20 Jul 8 03:08 .lesshst
-rw-r--r-- 1 root root 161 Jul 9 2019 .profile
drwx------ 2 root root 4096 Jul 8 02:41 .ssh/
-rw------- 1 root root 2713 Aug 29 15:29 .viminfo
-rw-r--r-- 1 root root 40 Aug 29 15:29 1.txt
-rw-r--r-- 1 root root 0 Aug 29 15:22 2.txt
-rw-r--r-- 1 root root 0 Aug 29 15:06 3.txt
-rw-r--r-- 1 root root 0 Aug 29 15:22 4.txt
drwxr-xr-x 2 root root 4096 Aug 29 15:18 Music/
drwx------ 3 root root 4096 Jul 8 03:05 snap/
我们使用ll
查看文件的时候,可以看到文件的详细信息,文件详细信息的第一列
是文件的格式,d
表示该文件是一个目录,-
表示该文件是一个普通文件,c
表示为串行端口设备,如键盘鼠标等。
详细信息中的第二到第四个
字符是文件所有者对该文件拥有的权限,r
表示读权限,通常使用数字4
表示,w
表示写文件,通常使用数字2
表示,x
表示执行权限,通常使用数字1
表示,如果拥有对应权限就会有对应的字符,如果是-
表示没有对应权限。
接下来的三个
字符是当前用户所在组对该文件拥有的权限,再接着的三个字符
是其他用户对该文件的权限。
这时我们想对文件的权限进行修改时就可以通过chmod
命令加上需要修改的权限对应的数字组合进行修改,比如我想修改4.txt
的属性为只允许我自己拥有读写权限,其余人没有任何权限,就可以使用chomd 600 文件名
,600
中的6
由4+2
得来,即读权限+写权限
,6
对应的是前三个字符
即当前用户
的权限,第二个0
对应的是当前用户所属组
的权限,第三个0
对应的是其他用户
对于这个文件的权限。
root@ubuntu:~/Music# ll
total 12
drwxr-xr-x 2 root root 4096 Aug 29 15:37 ./
drwx------ 6 root root 4096 Aug 29 15:37 ../
-rw-r--r-- 1 root root 40 Aug 29 15:29 1.txt
-rw-r--r-- 1 root root 0 Aug 29 15:22 2.txt
-rw-r--r-- 1 root root 0 Aug 29 15:06 3.txt
-rw-r--r-- 1 root root 0 Aug 29 15:22 4.txt
root@ubuntu:~/Music# chmod 600 4.txt
root@ubuntu:~/Music# ll
total 12
drwxr-xr-x 2 root root 4096 Aug 29 15:37 ./
drwx------ 6 root root 4096 Aug 29 15:37 ../
-rw-r--r-- 1 root root 40 Aug 29 15:29 1.txt
-rw-r--r-- 1 root root 0 Aug 29 15:22 2.txt
-rw-r--r-- 1 root root 0 Aug 29 15:06 3.txt
-rw------- 1 root root 0 Aug 29 15:22 4.txt