史诗级 带你玩转权限管理Linux(2)
chmod 数字方式设定权限#
权限波尔指表示方式
rwx = 111
— = 000
三位二进制可以表示的最大范围为8进至数
rwx=111=7
rw-=110=6
r-x=101=5
r–=100=4=r
-wx=011=3
-w-=010=2=w
–x=001=1=x
—=000=0
chmod 600 /mnt/westosfile1
rw-------
四.系统默认权限设定
#系统本身存在的意义共享资源
#从安全角度讲系统共享的资源越少,开放的权力越小系统安全性越高
#既要保证系统安全,又要系统创造价值,于是把应该开放的权力默认开放
#把不安全的权力默认保留
#如何保留权力#
#umask表示系统保留权力
umask #查看保留权力
umask 权限值 #临时设定系统预留权力
文件默认权限 = 777-umask-111
目录默认权限 = 777-umask
umask值越大系统安全性越高
#umask临时更改
umask 077
#永久更改
实验:
vim /etc/bashrc ##shell系统配置文件
74 if [ $UID -gt 199 ] && [ “id -gn
” = “id -un
” ]; then
75 umask 002–>022 #普通用户的umask
76 else
77 umask 022 #root用户的umask
78 fi
vim /etc/profile ##系统环境配置文件
59 if [ $UID -gt 199 ] && [ “id -gn
” = “id -un
” ]; then
60 umask 002–>022 #普通用户的umask
61 else
62 umask 022 #root用户的umask
63 fi
source /etc/bashrc ##source作用时使我们更改的内容立即被系统识别
source /etc/profile
五.文件用户用户组管理
chown username file ##更改文件拥有者
chgrp groupname file ##更改文件拥有组
chown username:groupname file ##同时更改文件的拥有者和拥有组
chown|chgrp -R user|group dir ##更改目录本身及目录中内容的拥有者或者拥有组
六.特殊权限
#stickyid 粘制位
#针对目录: #如果一个目录stickyid开启,那么这个目录中的文件
#只能被文件所有人删除
chmod 1原始权限 dirchmod o+t dir
实验:
mkdir /pub
chmod 777 /pub
su - westos ----> touch /pub/westosfile
exit
su - lee --------> touch /pub/leefile
rm -fr /pub/leefile #可以删除
rm -fr /pub/westosfile #不属于自己的文件也可以删除
如何解决此问题:
chmod 1777 /pub
chmod o+t /pub
以上两条命令都可以开启pub目录的t权限
su - westos ----> touch /pub/westosfile
exit
su - lee --------> touch /pub/leefile
rm -fr /pub/leefile #可以删除
rm -fr /pub/westosfile #不属于自己的文件不能删除
rm: cannot remove ‘westosfile’: Operation not permitted
#sgid 强制位#
#针对目录: 目录中新建的文件自动归属到目录的所属组中
设定:
chmod 2源文件权限 dir
chmod g+s dir
实验
group westostest
mkdir /mnt/westosdir
chmod 777 /mnt/westosdir
chgrp westostest /mnt/westosdir
watch -n1 ls -lR /mnt
root —> touch /mnt/westosdir/file ##是谁建立的文件组就是谁的
chmod g+s /mnt/westosdir
root —> touch /mnt/westosdir/file1 ##file1自动复制了/mnt/westosdir目录组#只针对二进制的可执行文件(c程序) #当运行二进制可执行文件时都是用文件拥有组身份运行,和执行用户无关
实验:
su - westos
/bin/watch -n 1 date
ps ax -o user,group,comm | grep watch
westos westos watch
用root用户身份
chmod g+s /bin/watch
su - westos
/bin/watch -n 1 date
ps ax -o user,group,comm | grep watch
westos root watch
#suid 冒险位
#只针对二进制的可执行文件(c程序) #当运行二进制可执行文件时都是用文件拥有者身份运行,和执行用户无关
chmod 4原属性 file
chmod u+s file
实验:
su - westos
/bin/watch -n 1 date
ps ax -o user,group,comm | grep watch
westos westos watch
用root用户身份
chmod u+s /bin/watch
su - westos
/bin/watch -n 1 date
ps ax -o user,group,comm | grep watch
root westos watch