二、CentOS基础配置(2.权限与文本管理(vi))-权限管理

时间:2024-04-07 09:17:06

1、chmod - 修改文件或目录权限

(1.)-R - 递归改变给定目录及其子目录中所有文件或目录的权限
[root@centos /]# chmod -R 777 /test/1.txt 
[root@centos /]# ll /test/1.txt 
-rwxrwxrwx 1 root root 46 Mar 25 23:28 /test/1.txt
(2.)-c - 逐一列出更改的文件权限
[root@centos /]# chmod -c 644 /test/1.txt 
mode of ‘/test/1.txt’ changed from 0777 (rwxrwxrwx) to 0644 (rw-r--r--)
[root@centos /]# ll /test/1.txt 
-rw-r--r-- 1 root root 46 Mar 25 23:28 /test/1.txt
(3.)-v - 显示详细的更改信息
[root@centos /]# chmod -v 554 /test/home/2.txt/
mode of ‘/test/home/2.txt/’ changed from 0644 (rw-r--r--) to 0554 (r-xr-xr--)
(4.)-f - 忽略错误信息,强制执行更改
[root@centos /]# chmod -f 554 /test/home/2.txt/
[root@centos /]# ll /test/home/2.txt/
total 0
(5.)使用字母来修改权限
[root@centos /]# chmod u+rwx,g+rwx,o+rwx /test/home/2.txt/
[root@centos /]# ll /test/home/2.txt/
total 0
[root@centos /]# cd /test/home/
[root@centos home]# ls
2.txt  3.txt  kali
[root@centos home]# ls -l 
total 12
drwxrwxrwx 2 root root 4096 Apr  2 18:08 2.txt
drwxr-xr-x 2 root root 4096 Apr  2 18:08 3.txt
drwx------ 2 kali root 4096 Mar 27 09:20 kali

2、chown - 修改文件或目录的属主和属组

(1.)-R - 递归更改给定目录及其子目录中所有文件或目录的所有者
[root@centos ~]# chown -R test:test /test/home
[root@centos ~]# ls -l /test
total 24
-rwxr-xr-x 1 root root   46 Mar 25 23:28 1.txt
drw-r--r-- 2 root root 4096 Apr  2 18:07 2.txt
drw-r--r-- 2 root root 4096 Apr  2 18:07 3.txt
drwxr-xr-x 5 test test 4096 Apr  2 18:08 home
drwxr-xr-x 3 root root 4096 Mar 25 22:51 kong
drwxr-xr-x 2 root root 4096 Mar 25 22:55 nan
(2.)–reference - 设置文件或目录的所有者和组与参考文件或目录相同
[root@centos ~]# chown --reference=/test/home /test/kong
[root@centos ~]# ls -l /test/
total 24
-rwxr-xr-x 1 root root   46 Mar 25 23:28 1.txt
drw-r--r-- 2 root root 4096 Apr  2 18:07 2.txt
drw-r--r-- 2 root root 4096 Apr  2 18:07 3.txt
drwxr-xr-x 5 test test 4096 Apr  2 18:08 home
drwxr-xr-x 3 test test 4096 Mar 25 22:51 kong
drwxr-xr-x 2 root root 4096 Mar 25 22:55 nan
(3.)-v - 显示修改后的详细信息
[root@centos ~]# chown -v root:root /test/home
changed ownership of ‘/test/home’ from test:test to root:root
[root@centos ~]# ls -l /test/
total 24
-rwxr-xr-x 1 root root   46 Mar 25 23:28 1.txt
drw-r--r-- 2 root root 4096 Apr  2 18:07 2.txt
drw-r--r-- 2 root root 4096 Apr  2 18:07 3.txt
drwxr-xr-x 5 root root 4096 Apr  2 18:08 home
drwxr-xr-x 3 test test 4096 Mar 25 22:51 kong
drwxr-xr-x 2 root root 4096 Mar 25 22:55 nan

3、chgrp - 修改文件或目录的属组

(1.)-R - 递归更改给定目录及其子目录中所有文件或目录的所属组
[root@centos ~]# chgrp -R test /test/home/
[root@centos ~]# ls -l /test/
total 24
-rwxr-xr-x 1 root root   46 Mar 25 23:28 1.txt
drw-r--r-- 2 root root 4096 Apr  2 18:07 2.txt
drw-r--r-- 2 root root 4096 Apr  2 18:07 3.txt
drwxr-xr-x 5 root test 4096 Apr  2 18:08 home
drwxr-xr-x 3 test test 4096 Mar 25 22:51 kong
drwxr-xr-x 2 root root 4096 Mar 25 22:55 nan
(2.)–reference - 设置文件或目录的所属组与参考文件或目录相同
[root@centos ~]# chgrp --reference=/test/nan/ /test/home/
[root@centos ~]# ls -l /test/
total 24
-rwxr-xr-x 1 root root   46 Mar 25 23:28 1.txt
drw-r--r-- 2 root root 4096 Apr  2 18:07 2.txt
drw-r--r-- 2 root root 4096 Apr  2 18:07 3.txt
drwxr-xr-x 5 root root 4096 Apr  2 18:08 home
drwxr-xr-x 3 test test 4096 Mar 25 22:51 kong
drwxr-xr-x 2 root root 4096 Mar 25 22:55 nan
(3.)-v - 显示修改后的详细信息
[root@centos ~]# chgrp -v test /test/1.txt
changed group of ‘/test/1.txt’ from root to test
[root@centos ~]# ls -l /test/
total 24
-rwxr-xr-x 1 root test   46 Mar 25 23:28 1.txt
drw-r--r-- 2 root root 4096 Apr  2 18:07 2.txt
drw-r--r-- 2 root root 4096 Apr  2 18:07 3.txt
drwxr-xr-x 5 root root 4096 Apr  2 18:08 home
drwxr-xr-x 3 test test 4096 Mar 25 22:51 kong
drwxr-xr-x 2 root root 4096 Mar 25 22:55 nan

4、umask - 设置默认的文件和目录权限掩码

(1.)-S - 以字母形式显示当前的umask值
[root@centos ~]# umask -S
u=rwx,g=rx,o=rx
(2.)-p - 以octal数字形式显示当前的umask值
[root@centos ~]# umask -p
umask 0022
(3.)设置参数
[root@centos ~]# umask 000
[root@centos ~]# umask -p
umask 0000
[root@centos ~]# umask 022
[root@centos ~]# umask -S
u=rwx,g=rx,o=rx