设置目录和普通文件的权限---chmod
chmod设置权限主要是设置属主,属组,其他用户对于文件或目录读(r),写(w),执行(x)的权限,即图中红框部分。
chmod设置文件或目录权限有两种方式
chmod [ugoa][+-][rwx] 文件或目录名
u:代表文件的属主
[root@sunny test]# chmod u+x 1.txt
[root@sunny test]# ll
total 0
-rwxr--r--. 1 root root 0 Aug 9 22:52 1.txt
[root@sunny test]# chmod u-rx 1.txt
[root@sunny test]# ll
total 0
--w-r--r--. 1 root root 0 Aug 9 22:52 1.txt
g:代表文件的属组
[root@sunny test]# chmod g+wx 1.txt
[root@sunny test]# ll
total 0
--w-rwxr--. 1 root root 0 Aug 9 22:52 1.txt
[root@sunny test]# chmod g-wx 1.txt
[root@sunny test]# ll
total 0
--w-r--r--. 1 root root 0 Aug 9 22:52 1.txt
o:代表其他用户
[root@sunny test]# chmod o+wx 1.txt
[root@sunny test]# ll
total 0
--w-r--rwx. 1 root root 0 Aug 9 22:52 1.txt
[root@sunny test]# chmod o-wx 1.txt
[root@sunny test]# ll
total 0
--w-r--r--. 1 root root 0 Aug 9 22:52 1.txt
a:默认选项,代表所有用户
[root@sunny test]# chmod a+x 1.txt
[root@sunny test]# ll
total 0
--wxr-xr-x. 1 root root 0 Aug 9 22:52 1.txt
[root@sunny test]# chmod a-x 1.txt
[root@sunny test]# ll
total 0
--w-r--r--. 1 root root 0 Aug 9 22:52 1.txt
rwx,可以看作三位二进制数,有权限则当位为1,没有权限则当位为0,那么可读可写可操作则为(111)2转换成十进制即为7
若设置所有用户都有可读可写可操作的权限,就可以输入如下的命令
[root@sunny test]# chmod 777 1.txt
[root@sunny test]# ll
total 0
-rwxrwxrwx. 1 root root 0 Aug 9 22:52 1.txt
若仅有属主有可读可写可操作的权限,除属主外的用户对文件没有任何权限,则可输入下面的命令
[root@sunny test]# chmod 700 1.txt
[root@sunny test]# ll
total 0
-rwx------. 1 root root 0 Aug 9 22:52 1.txt
设置目录文件的归属---chown,chgrp
设置目录文件的归属,即设置上图红框中的内容,用到的命令有:chown,chgrp
chown 属主 文件或目录
[root@sunny test]# chown sl010 1.txt
[root@sunny test]# ll
total 0
-rwx------. 1 sl010 root 0 Aug 9 22:52 1.txt
chgrp 属组 文件或目录
[root@sunny test]# chgrp sl011 1.txt
[root@sunny test]# ll
total 0
-rwx------. 1 sl010 sl011 0 Aug 9 22:52 1.txt
chown 属主:属组 文件或目录 / chown 属主.属组 文件或目录
[root@sunny test]# chown root:root 1.txt
[root@sunny test]# ll
total 0
-rwx------. 1 root root 0 Aug 9 22:52 1.txt
[root@sunny test]# chown sl011.sl011 1.txt
[root@sunny test]# ll
total 0
-rwx------. 1 sl011 sl011 0 Aug 9 22:52 1.txt
-R:递归修改指定目录下所有子目录机文件的归属
[root@sunny mnt]# chown sl011.sl011 -R test
[root@sunny mnt]# ll -R
.:
total 0
drwxr-xr-x. 3 sl011 sl011 45 Aug 9 22:53 test
./test:
total 0
-rwx------. 1 sl011 sl011 0 Aug 9 22:52 1.txt
-rw-r--r--. 1 sl011 sl011 0 Aug 9 22:52 2.txt
drwxr-xr-x. 2 sl011 sl011 6 Aug 9 22:53 test1
./test/test1:
total 0