设置权限的四类方法
方法一:直接设置
1) chmod 数字 【文件名】
-R: 对目录进行递归更改
eg:
[root@localhost /]# mkdir file_1
[root@localhost /]# cd file_1
[root@localhost file_1]# touch test_{1..3}.txt
[root@localhost file_1]# cd ..
[root@localhost /]# ls -ld file_1
drwxr-xr-x. 2 root root 60 5月 10 19:46 file_1
[root@localhost /]# ll file_1
总用量 0
-rw-r--r--. 1 root root 0 5月 10 19:46 test_1.txt
-rw-r--r--. 1 root root 0 5月 10 19:46 test_2.txt
-rw-r--r--. 1 root root 0 5月 10 19:46 test_3.txt
[root@localhost /]# chmod -R 760 file_1 %文件夹 file_1 下的所有文件权限改为 rwx rw- ---
[root@localhost /]# ls -ld file_1
drwxrw----. 2 root root 60 5月 10 19:46 file_1
[root@localhost /]# ll file_1
总用量 0
-rwxrw----. 1 root root 0 5月 10 19:46 test_1.txt
-rwxrw----. 1 root root 0 5月 10 19:46 test_2.txt
-rwxrw----. 1 root root 0 5月 10 19:46 test_3.txt
[root@localhost /]# cd file_1
[root@localhost file_1]# touch 4.txt
[root@localhost file_1]# cd ..
[root@localhost /]# ll file_1
总用量 0
-rw-r--r--. 1 root root 0 5月 10 19:47 4.txt %新建文件不受文件夹权限影响,为默认权限
-rwxrw----. 1 root root 0 5月 10 19:46 test_1.txt
-rwxrw----. 1 root root 0 5月 10 19:46 test_2.txt
-rwxrw----. 1 root root 0 5月 10 19:46 test_3.txt
由此引申出:
创建目录的权限: 最高权限 - umask = 默认权限
file : 777 - 022 = 755
test : 666 - 022 = 644
2)字母表示法易错坑
chmod +w == chmod u+w
chmod +r == chmod u+r,g+r,o+r
chmod +x == chmod u+x,g+x,o+x
3)数字表示法个人理解
除去第一位表示文件类型之外,根据每三位数字代表一个身份(即ugo)的权限,可将权限状态理解为底层的umask
eg: rwx r-- r-- ——> 111 100 100 ——> 744
方法二:改变属组以获取对应属组的权限
chown [属主] : [属组] [文件名]
-R: 对目录进行递归更改
方法三:特殊权限设置
1) SUID :对可执行的二进制文件进行权限提升,直接相当于root
chmod u+s [文件路径](`which [命令]`)
使用ll 查看权限时,属主执行权限位置是 s
2)SGID :对目录进行权限提升,直接相当于root
chmod g+s [目录名]
使用ll 查看权限时,属组执行权限位置是 s
3)SBIT :只能被属主删除,其他用户无权删除
相同属组的用户也无权删除
使用ll 查看权限时,其他人执行权限位置是 t
4)使用sudo命令更改
#visudo
第92行
root ALL==(ALL) ALL
Root表示用户名,如果是用户组,则可以写成“%组名”
ALL:表示允许登录的主机(地址白名单)
(ALL):表示以谁的身份执行,ALL表示root身份
ALL:表示当前用户可以执行的命令,多个命令可以使用“,”分割
按照root格式设置用户的权限