一、使用chmod命令进行权限管理
chmod字母方式 :
u--针对属主
g--针对属组
o--针对其他用户
a--针对所有用户
= 表示赋予(设置)指定权限,要注意它会覆盖原权限
+ 表示在原有权限的基础上增加指定权限
- 表示在原有权限的基础上去掉指定权限
r、w、x三种权限,既可以单独使用也可以组合使用
示例1: chmod u+x host.conf
功能:为文件host.conf的属主增加执行权限
示例2: chmod g-w,o=x host.conf
功能:将文件host.conf的属组去掉写权限,同时将其他用户设置为只有执行权限,而文件属主的权限不变
权限管理命令chmod的数字方式:
chmod:数字方式
r-----4 读权限
w-----2 写权限
x-----1 执行权限
无-----0 无权限
例如:
若要rwx属性则4+2+1=7;
若要rw-属性则4+2=6;
若要r-x属性则4+1=5。
示例-1:
示例-2:
chmod 777 myFile
上述命令等同于chmod a=rwx myFile。
chmod权限管理的案例:
# chmod 命令示例集(字母方式 + 数字方式)
字母方式示例 (u/g/o/a +/- r/w/x):
【1】 给所有者添加执行权限: chmod u+x file.txt
【2】 给组和其他用户移除写权限: chmod go-w file.txt
【3】 给所有用户添加读权限: chmod a+r file.txt
【4】 仅设置所有者有读写权限: chmod u=rw file.txt
【5】 递归给目录下所有文件添加组读权限: chmod -R g+r dir/
【6】 设置所有者有全部权限,组和其他只有读: chmod u=rwx,go=r file.txt
【7】 给其他用户添加执行权限(忽略符号链接): chmod -h o+x symlink
【8】 同时设置不同用户的不同权限: chmod u=rwx,g=rx,o= file.txt
【9】 移除所有用户的执行权限: chmod a-x script.sh
【10】 设置组权限等于所有者权限: chmod g=u config.conf
数字方式示例 (0-7权限值):
【11】 典型权限755(所有者rwx,组和其他rx): chmod 755 script.sh
【12】 完全私有权限600(仅所有者rw): chmod 600 private.key
【13】 目录默认权限775: chmod 775 shared_dir/
【14】 完全开放权限777(慎用): chmod 777 tmp_file
【15】 只读权限444: chmod 444 readonly.txt
【16】 递归设置目录和内容为700: chmod -R 700 secure_dir/
【17】 设置SUID位(二进制文件): chmod 4755 /usr/bin/special
【18】 设置SGID位(目录): chmod 2775 shared_team_dir/
【19】 设置粘滞位(/tmp类目录): chmod 1777 /tmp/mytmp
【20】 组合权限(所有