linux下的权限设定2

#####3.文件权限的理解###########
[root@workstation Desktop]# ls -ld /etc
drwxr-xr-x. 139 root root 8192 1月   1 15:54 /etc

U:文件拥有者对文件的权限
G:文件拥有组对文件的权限
O:其他人对于文件的权限

r: readable(查看权限)   1.对于文件可查看文件中的内容                    2.对于目录可列出目录中的文件名称
w:writeable(可写权限)   1.对于文件可更改文件记录的内容                  2.对于目录可更改目录中文件元数据
x:excutable(执行权权限) 1.对于文件可用文件名称调用文件内记录的程序      2.对于目录可进入目录

权限对文件的影响:
[root@workstation Desktop]# su - lee            ##进入lee用户
[lee@workstation ~]$ cd /mnt                    ##进入/mnt目录并查看目录
[lee@workstation mnt]$ ls
westos1  westos2  westos3  westosdir


[lee@workstation mnt]$ cat /mnt/westos1         ##有r是可显示文件内容
qqww


[root@workstation Desktop]# chmod u-r /mnt/westos1      ##新开一个shell去掉u下的r


[lee@workstation mnt]$ cat westos1              ##无法读取文件内容
cat: westos1: Permission denied


[root@workstation Desktop]# chmod u+r /mnt/westos1      ##加入u下r可在读取文件内容


[lee@workstation mnt]$ cat /mnt/westos1
qqww


[root@workstation Desktop]# chmod u-w /mnt/westos1      ##去掉u下的w文件将不可写入


[lee@workstation mnt]$ echo hello westos > /mnt/westos1 ##echo显示
-bash: /mnt/westos1: Permission denied


[root@workstation Desktop]# chmod u+w /mnt/westos1      ##加入u下的w文件可再次写入


[lee@workstation mnt]$ echo hello westos > /mnt/westos1
[lee@workstation mnt]$ cat westos1
hello westos


[lee@workstation mnt]$ vim westos1              ##编辑westos1   date


[root@workstation Desktop]# chmod u+x /mnt/westos1      ##加入调用指令


[lee@workstation mnt]$ /mnt/westos1             ##文件可被调用
2020年 01月 06日 星期一 13:59:31 EST

权限对目录的影响:
[lee@workstation mnt]$ cd /mnt/westosdir/       ##进入/mnt下的/westosdir目录


[lee@workstation westosdir]$ pwd
/mnt/westosdir

[lee@workstation westosdir]$ cd /mnt            ##再次进入mnt目录


[root@workstation Desktop]# chmod u-x /mnt/westosdir/   ##去掉调用命令westosdir目录将无法调用


[lee@workstation mnt]$ cd /mnt/westosdir/
-bash: cd: /mnt/westosdir/: Permission denied


[root@workstation Desktop]# chmod u-w /mnt/westosdir/   ##去掉w westosdir目录将无法更改文件


[lee@workstation mnt]$ touch /mnt/westosdir/linux4
touch: 无法创建 '/mnt/westosdir/linux4': Permission denied


[lee@workstation mnt]$ rm -fr /mnt/westosdir/linux1
rm: 无法删除'/mnt/westosdir/linux1': Permission denied
[root@workstation Desktop]# chmod u-r /mnt/westosdir/   ##可否列出目录下的文件


[lee@workstation mnt]$ ls /mnt/westosdir/
ls: 无法打开目录'/mnt/westosdir/': Permission denied

######4.文件权限的设定方法#############
1.字符的形式设定权限
u g o a (a表示所有)      +添加 -撤销 =设定
[root@workstation Desktop]# chmod u-w /mnt/westos1      ##撤销w


[root@workstation Desktop]# chmod ug-r /mnt/westos1     ##撤销所有人所有组r


[root@workstation Desktop]# chmod o+x /mnt/westos1      ##增加其他人的x


[root@workstation Desktop]# chmod u+r,g+w,o-r /mnt/westos1      ##所有人+r所有组+w其他人-r


[root@workstation Desktop]# chmod +r /mnt/westos1       ##所有位r + -


[root@workstation Desktop]# chmod -r /mnt/westos1


[root@workstation Desktop]# chmod -w /mnt/westos1       ##只去掉所有人的w而非所有位的
chmod: /mnt/westos1:新的权限为r-xrwxrwx,而非r-xr-xr-x
[root@workstation Desktop]# chmod a+w /mnt/westos1      ##添加 去掉 所有位的w


[root@workstation Desktop]# chmod a-w /mnt/westos1

2.数字的方式设定权限    ##八进制
权限    二进制     八进制
---         000             0
--x        001             1
-w-       010             2
-wx      011              3
r--        100              4
r-x       101              5
rw-      110              6
rwx      111              7
[root@workstation Desktop]# chmod 777 /mnt/westos2


[root@workstation Desktop]# chmod 555 /mnt/westos2


[root@workstation Desktop]# chmod 751 /mnt/westos2

3.依照模板设定权限
方式:chmod --reference=属性源文件 TAG
[root@workstation Desktop]# chmod --reference /mnt/westos3 /mnt/westos1 ##将文件3属性复制给文件1

######5.系统预留权限阀值#############
理解:
1.资源存在意义在于共享,权限开放越大,共享效果越明显,但安全性越差
2.对于系统安全而言,开放权力越小,系统越安全
3.在系统中开放应开放的权力,保留不安全的权力以确保系统功能性及安全性


umask:
1.系统中使用umask来预留权限
2.在shell中可以使用umask来查看并设定预留权限阀值
[root@workstation mnt]# umask           ##系统中预留阀值为0022(第一个0表示特殊权限)
0022


系统满权限为777
建立目录的权限为:777-022=755

建立文件的权限为:755-111=644

临时设定:
[root@workstation mnt]# umask 077       ##更改预留阀值为077
[root@workstation mnt]# umask
0077


[root@workstation mnt]# touch file1     ##新建文件file1阀值变为600


[root@workstation mnt]# mkdir zl1       ##新建目录zl1阀值变为700

[root@workstation Desktop]# cd /mnt     ##退出新开一个shell在/mnt下建立文件file2阀值变为644
[root@workstation mnt]# touch file2

永久改变权限阀值:
[root@workstation mnt]# vim /etc/bashrc ##编辑文件(shell文件)                 ##值要一致

[root@workstation mnt]# vim /etc/profile        ##编辑文件(用户环境配置文件


如果UID>199 并且用户的名称和组的名称一致的时候umask 002  (普通用户的umask)
如果UID<199 或者用户的名称和组的名称不一致的时候umask 022(超级用户的umask)

[root@workstation mnt]# vim /etc/bashrc         ##只做了更改还未生效 还未被识别


[root@workstation mnt]# vim /etc/profile        ##编辑文件(用户环境配置文件)



[root@workstation mnt]# umask                     ##只做了更改还未生效 还未被识别
0022


[root@workstation mnt]# source /etc/bashrc      ##识别
[root@workstation mnt]# source /etc/profile

[root@workstation mnt]# umask
0077


[root@workstation mnt]# touch file      ##新建文件file阀值为600


[root@workstation mnt]# mkdir zl        ##新建目录zl阀值为700

#####6.特殊权限#############
1.
SUID:只针对于二进制可执行文件
[root@workstation Desktop]# watch -n 1 'ps ax -o user,group,comm | grep cat' ##监控面板显示系统进程中的 用户,属于组,进程名字
[root@workstation pub]# ls -l /bin/cat                          ##file文件是属于超级用户 超级用户组的
-rwxr-xr-x. 1 root root 51856 1月  11 2019 /bin/cat

[westos@workstation pub]$ /bin/cat      ##在westos用户下 产生的进程属于westos


[root@workstation pub]# chmod u+s /bin/cat      ##设定用户x变为s   设置SUID在这个可执行文件上


[root@workstation pub]# ls -l /bin/cat
-rwsr-xr-x. 1 root root 51856 1月  11 2019 /bin/cat


[westos@workstation pub]$ /bin/cat      ##在westos用户下执行相同的进程 属于原来的root用户


SGID:针对二进制可执行文件:该命令发起的程序是以该命令所有组的身份去执行
[root@workstation pub]# chmod g+s /bin/cat      ##设定组x变为s   

 [root@workstation pub]# ls -l /bin/cat
-rwsr-sr-x. 1 root root 51856 1月  11 2019 /bin/cat

[westos@workstation pub]$ /bin/cat      ##在westos用户下执行相同的进程 属于原来的root组

     针对目录:目录新建文件的所属组与该目录的所有组保持一致
[root@workstation pub]# watch -n 1 ls -lR /mnt/ ##针对SGID目录的监控面板
[root@workstation mnt]# mkdir westos            ##在mnt目录下建立目录westos


[root@workstation mnt]# chown westos.westos /mnt/westos ##将用户和组都改为westos
[root@workstation mnt]# chmod 777 /mnt/westos/  ##给权限


[lei@workstation mnt]$ touch /mnt/westos/file   ##在lei用户下/mnt/westos目录下建立file文件 文件属于lei用户


[root@workstation mnt]# chmod g+s /mnt/westos/  ##将组下的x变为s


[lei@workstation mnt]$ touch /mnt/westos/file1  ##建立的新文件file1的属组改为了之前的组


STICKYID:对于文件:表示文件即使没有被程序调用也会加载到交换空间中
         对于目录:表示目录上有STICKYID的权限时,所有用户在该目录下均可创建文件,但只有文件拥有者和root用户可以删除该目录下的文件

[root@workstation mnt]# mkdir pub       ##建立目录pub

[root@workstation mnt]# chmod 777 pub   ##权限


[root@workstation Desktop]# watch -n1 'ls - ld /mnt/pub/;ls -l /mnt/pub/'       ##监控

[root@workstation mnt]# su - westos     ##新开一个shell切换至westos
上一次登录:一 1月  6 13:21:46 EST 2020pts/1 上
[westos@workstation ~]$ cd /mnt/pub
[westos@workstation pub]$ touch file    ##建立文件file

[westos@workstation pub]$ su - student  ##切换至student并建立文件file1
密码:
[student@workstation ~]$ cd /mnt/pub/
[student@workstation pub]$ touch file1


[student@workstation pub]$ rm -fr file1 ##并且可以删除文件file1


root@workstation mnt]# chmod o+t /mnt/pub/      ##限制目录中的文件只能被所有人删除

[westos@workstation ~]$ touch /mnt/pub/file1    ##在westos用户下建立文件file1


[student@workstation ~]$ cd /mnt/pub/           ##在student用户下建立文件file2
[student@workstation pub]$ touch file2


[student@workstation pub]$ rm -fr file2         ##可删除自己用户下的file2文件


[student@workstation pub]$ rm -fr file1         ##不可删除westos用户下的file1文件
rm: 无法删除'file1': Operation not permitted
2.特殊权限的设定
SUID:chmod u+s     TAG
[root@workstation mnt]# touch file      ##新建两个文件并给755权限
[root@workstation mnt]# touch westos
[root@workstation mnt]# chmod 755 /mnt/*


[root@workstation mnt]# chmod u+s /mnt/file     ##将u位下的x变为s


[root@workstation mnt]# chmod  4755 /mnt/westos ##将u位下的x变为s

SGID:chmod g+s     TAG
[root@workstation mnt]# chmod g+s /mnt/file     ##将g位下的x变为s


[root@workstation mnt]# chmod 2755 /mnt/westos  ##将g位下的x变为s

STICKYID:chmod o+t TAG
[root@workstation mnt]# chmod o+t /mnt/file     ##将o位下的x变为t


[root@workstation mnt]# chmod 7755 /mnt/westos  ##将o位下的x变为t

#####7.FACL权限列表############
[root@workstation mnt]# touch westos            ##新建一个文件westos并查看
[root@workstation mnt]# ls -l westos
-rw-r--r--. 1 root root 0 1月   7 15:48 westos

[root@workstation mnt]# setfacl -m u:westos:rw /mnt/westos ##设定权限列表并查看
[root@workstation mnt]# ls -l /mnt/westos
-rw-rw-r--+ 1 root root 0 1月   7 15:48 /mnt/westos     ##+表示有权限列表


[root@workstation mnt]# getfacl /mnt/westos             ##查看真正的权限
getfacl: Removing leading '/' from absolute path names
# file: mnt/westos              ##文件名称
# owner: root                   ##文件的所有人
# group: root                   ##文件的所有组
user::rw-                       ##用户的权力
user:westos:rw-                 ##特定用户westos的权力
group::r--                      ##组的权力
mask::rw-                       ##能够赋予特定用户最大的权力
other::r--                      ##其他人的权力


[root@workstation mnt]# su - westos             ##切换用户
上一次登录:二 1月  7 14:48:32 EST 2020pts/3 上


[westos@workstation ~]$ cd /mnt
[westos@workstation mnt]$ ls
westos
[westos@workstation mnt]$ vim westos    ##可以编辑
[westos@workstation mnt]$ cat westos
hello westos


[root@workstation mnt]# su - student            ##切换student用户
上一次登录:二 1月  7 14:34:55 EST 2020pts/4 上


[student@workstation ~]$ cd /mnt
[student@workstation mnt]$ vim westos   ##只读模式

查看权限列表:getfacl

设定权限列表:setfacl  -m设定权限 -x删除指定用户 -b关闭列表功能
root@workstation mnt]# watch -n 1 getfacl /mnt/westos   ##打开监控面板
[root@workstation mnt]# setfacl -m u:lei:00 /mnt/westos ##设定lei用户没有任何权限


[root@workstation mnt]# setfacl -m g:student:rwx /mnt/westos    ##设定student的权限


[root@workstation mnt]# setfacl -x g:lei /mnt/westos    ##删除lei组的权力


[root@workstation mnt]# setfacl -x u:lei /mnt/westos    ##删除用户lei的权力


[root@workstation mnt]# setfacl -x g:student /mnt/westos ##删除student组的权力


[root@workstation mnt]# ls -l /mnt/westos               ##权限列表用户都被删除后权限列表还在
-rw-r--r--+ 1 root root 13 1月   7 15:59 /mnt/westos


[root@workstation mnt]# setfacl -b /mnt/westos          ##关闭权限列表 +变-
[root@workstation mnt]# ls -l /mnt/westos
-rw-r--r--. 1 root root 13 1月   7 15:59 /mnt/westos

发布了11 篇原创文章 · 获赞 0 · 访问量 173

猜你喜欢

转载自blog.csdn.net/weixin_46102303/article/details/103894233