首页 > 其他 > 详细

第十五章 属主属组,Umask权限设置,特殊属性

时间:2020-08-01 21:41:48      阅读:73      评论:0      收藏:0      [点我收藏+]

 

属主属组

chown   #修改属主属组
-R  #递归设置

chgrp #只能修改属组
?
只有root权限才可以任意修改属主属组
普通用户不能使用这个命令
?
[root@lxy ~]# cd /opt/
[root@lxy /opt]#
[root@lxy /opt]# ll
total 12
-rw-rw--w-. 1 yuwei yuwei 10 Dec 17 18:36 123
-rw-r---w-. 1 test test   9 Dec 17 18:41 passwd
drwxr-x-wx. 2 root root  70 Dec 17 19:04 test
-rw-r---w-. 1 root root   9 Dec 17 18:33 test.txt
[root@lxy /opt]# id test
uid=1000(test) gid=1000(test) groups=1000(test)
[root@lxy /opt]# chown test.test 123
[root@lxy /opt]# ll
total 12
-rw-rw--w-. 1 test test 10 Dec 17 18:36 123
-rw-r---w-. 1 test test  9 Dec 17 18:41 passwd
drwxr-x-wx. 2 root root 70 Dec 17 19:04 test
-rw-r---w-. 1 root root  9 Dec 17 18:33 test.txt
[root@lxy /opt]# chown yuwei passwd
[root@lxy /opt]# ll
total 12
-rw-rw--w-. 1 test test 10 Dec 17 18:36 123
-rw-r---w-. 1 yuwei test  9 Dec 17 18:41 passwd
drwxr-x-wx. 2 root root 70 Dec 17 19:04 test
-rw-r---w-. 1 root root  9 Dec 17 18:33 test.txt
[root@lxy /opt]# chown .yuwei 123
[root@lxy /opt]# ll
total 12
-rw-rw--w-. 1 test yuwei 10 Dec 17 18:36 123
-rw-r---w-. 1 yuwei test   9 Dec 17 18:41 passwd
drwxr-x-wx. 2 root root  70 Dec 17 19:04 test
-rw-r---w-. 1 root root   9 Dec 17 18:33 test.txt
[root@lxy /opt]# chown root:root 123
[root@lxy /opt]# ll
total 12
-rw-rw--w-. 1 root root 10 Dec 17 18:36 123
-rw-r---w-. 1 yuwei test  9 Dec 17 18:41 passwd
drwxr-x-wx. 2 root root 70 Dec 17 19:04 test
-rw-r---w-. 1 root root  9 Dec 17 18:33 test.txt
[root@lxy /opt]# chgrp   test 123
[root@lxy /opt]# ll
total 12
-rw-rw--w-. 1 root test 10 Dec 17 18:36 123
-rw-r---w-. 1 yuwei test  9 Dec 17 18:41 passwd
drwxr-x-wx. 2 root root 70 Dec 17 19:04 test
-rw-r---w-. 1 root root  9 Dec 17 18:33 test.txt
?
[root@lxy /opt]# chown -R test.test test #递归设置
[root@lxy /opt]# ll
total 12
-rw-rw--w-. 1 root test 10 Dec 17 18:36 123
-rw-r---w-. 1 yuwei test  9 Dec 17 18:41 passwd
drwxr-x-wx. 2 test test 70 Dec 17 19:04 test
-rw-r---w-. 1 root root  9 Dec 17 18:33 test.txt
[root@lxy /opt]# ll test
total 0
-rw-rw-r--. 1 test test 0 Dec 17 19:03 new1.txt
-rw-r--r--. 1 test test 0 Dec 17 18:50 old3.txt
-rw-r--r--. 1 test test 0 Dec 17 18:50 old4.txt
-rw-r--r--. 1 test test 0 Dec 17 18:50 old5.txt

 

Umask权限设置

umask     #权限控制命令  
022   #默认的umask值
  永久设置   /etc/profile
   
[root@lxy ~]# mkdir test
[root@lxy ~]# touch test.log
[root@lxy ~]# ll
total 0
drwxr-xr-x. 2 root root 6 Dec 18 16:58 test #新创建的目录权限为755
-rw-r--r--. 1 root root 0 Dec 18 16:58 test.log  #新创建的文件权限为644
?
umask的计算  
?
umask是个权限控制的命令,默认的值是022,系统默认创建目录的权限是怎么计算来的,由目录的最大权限777,减去umask的默认权限,得到的结果就是新创建目录的权限。系统默认创建文件的权限是怎么计算的,由文件的最大权限666,减去umask的默认权限,得到的结果就是新创建文件的权限。当文件的权限为出现奇数时,会在奇数位上面+1.
?
[root@lxy ~]# umask
0022
[root@lxy ~]# umask   033
[root@lxy ~]# umask
0033
[root@lxy ~]# mkdir data
[root@lxy ~]# ll
total 0
drwxr--r--. 2 root root 6 Dec 18 17:06 data
drwxr-xr-x. 2 root root 6 Dec 18 16:58 test
-rw-r--r--. 1 root root 0 Dec 18 16:58 test.log
[root@lxy ~]# touch test.txt
[root@lxy ~]# ll
total 0
drwxr--r--. 2 root root 6 Dec 18 17:06 data
drwxr-xr-x. 2 root root 6 Dec 18 16:58 test
-rw-r--r--. 1 root root 0 Dec 18 16:58 test.log
-rw-r--r--. 1 root root 0 Dec 18 17:06 test.txt
?

 

特殊权限

?
setuid #在运行一个命令的时候,相当于这个命令的所有者,主要作用于属主权限的x位
?
?
[root@lxy ~]# ll /usr/bin/passwd
-rwsr-xr-x. 1 root root 27832 Jun 10  2014 /usr/bin/passwd
[root@lxy ~]# stat /usr/bin/passwd
File: ‘/usr/bin/passwd’
Size: 27832     Blocks: 56         IO Block: 4096   regular file
Device: 803h/2051d Inode: 100960630   Links: 1
Access: (4755/-rwsr-xr-x) Uid: (    0/   root)   Gid: (    0/   root)
Context: system_u:object_r:passwd_exec_t:s0
Access: 2019-12-16 18:28:46.025128480 +0800
Modify: 2014-06-10 14:27:56.000000000 +0800
Change: 2019-11-26 20:24:06.355108144 +0800
Birth: -
?
#root用户设置setuid 权限
[root@lxy ~]# ll /usr/bin/rm
-rwxr-xr-x. 1 root root 62952 Oct 31  2018 /usr/bin/rm
[root@lxy ~]# chmod u+s /usr/bin/rm
[root@lxy ~]# ll /usr/bin/rm
-rwsr-xr-x. 1 root root 62952 Oct 31  2018 /usr/bin/rm
[root@lxy ~]# stat /usr/bin/rm
File: ‘/usr/bin/rm’
Size: 62952     Blocks: 128       IO Block: 4096   regular file
Device: 803h/2051d Inode: 100686169   Links: 1
Access: (4755/-rwsr-xr-x) Uid: (    0/   root)   Gid: (    0/   root)
Context: system_u:object_r:bin_t:s0
Access: 2019-12-18 03:41:02.098907636 +0800
Modify: 2018-10-31 03:16:01.000000000 +0800
Change: 2019-12-18 17:43:52.450121986 +0800
?
#普通用户测试
?
[test@lxy ~]$ rm -rf /opt/
rm: cannot remove ‘/opt/’: Permission denied
[test@lxy ~]$ rm -rf /opt/
[test@lxy ~]$ ll /opt/
ls: cannot access /opt/: No such file or directory
?
?
#如果普通对这个命令文件没有执行权限时,也会报错
?
[root@lxy ~]# mkdir /opt
[root@lxy ~]#
[root@lxy ~]#
[root@lxy ~]# chmod o-x /usr/bin/rm
[root@lxy ~]# ll /usr/bin/rm
-rwsr-xr--. 1 root root 62952 Oct 31  2018 /usr/bin/rm
?
#普通用户测试
?
[test@lxy ~]$ rm -rf /opt/
rm: cannot remove ‘/opt/’: Permission denied
?
#当一个文件属主没有执行权限时,设置setuid之后就是大S,有执行权限就是小s
[root@lxy ~]# ll
total 0
drwxr--r--. 2 root root 6 Dec 18 17:06 data
drwxr-xr-x. 2 root root 6 Dec 18 16:58 test
-rw-r--r--. 1 root root 0 Dec 18 16:58 test.log
-rw-r--r--. 1 root root 0 Dec 18 17:06 test.txt
[root@lxy ~]# chmod u+s test.log
[root@lxy ~]# ll
total 0
drwxr--r--. 2 root root 6 Dec 18 17:06 data
drwxr-xr-x. 2 root root 6 Dec 18 16:58 test
-rwSr--r--. 1 root root 0 Dec 18 16:58 test.log
-rw-r--r--. 1 root root 0 Dec 18 17:06 test.txt
?
#普通用户对文件没有执行权限时,是执行不了,即使拥有setuid的权限,也不能执行,
[test@lxy ~]$ ll
total 4
-rwSrw-r--. 1 test test 4 Dec 18 17:53 123.txt
[test@lxy ~]$ ./123.txt
-bash: ./123.txt: Permission denied
[test@lxy ~]$ ll
total 4
-rwxrw-r--. 1 test test 4 Dec 18 17:53 123.txt
[test@lxy ~]$ ./123.txt
/home/test
?
#root用户对文件没有执行权限时,是执行不了,即使拥有setuid的权限,root也是不能执行的
[root@lxy ~]# chmod -x /usr/bin/rm
[root@lxy ~]# ll /usr/bin/rm
-rwSr--r--. 1 root root 62952 Oct 31  2018 /usr/bin/rm
[root@lxy ~]# mkdir /opt
[root@lxy ~]# rm -rf /opt
-bash: /bin/rm: Permission denied
?
#这个权限不要乱用。
?
?
setgid 让一个目录下的所有新创建的目录或者文件的默认组都是该目录的组,而不是创建用户的组
?
让一个成为共享目录

主要作用于属组的x位
?
创建2个用户,创建一个目录,让其属主及属组有任何权限,其它人没有权限
?
?
#创建两个测试用户
[root@lxy ~]# useradd   test01
[root@lxy ~]# useradd   test02
[root@lxy ~]# echo ‘1‘ |passwd --stdin test01
Changing password for user test01.
passwd: all authentication tokens updated successfully.
[root@lxy ~]# echo ‘1‘ |passwd --stdin test02
Changing password for user test02.
passwd: all authentication tokens updated successfully.
?
#创建一个共享目录
[root@lxy ~]# mkdir   /opt/gongxiang
?
#创建一个共享组
[root@lxy ~]# groupadd gongxiang
?
#将共享,目录设置为共享组
[root@lxy ~]# chgrp   gongxiang /opt/gongxiang/
[root@lxy ~]# ll /opt/
total 0
drwxr-xr-x. 2 root gongxiang 6 Dec 18 18:17 gongxiang
?
#让属主属组都要任何权限,其它用户没有权限
[root@lxy ~]# chmod 770 /opt/gongxiang/
?
#将两个用户加入共享组
[root@lxy ~]# usermod -G gongxiang test01
[root@lxy ~]# usermod -G gongxiang test02
?
#给共享目录设置setgid
[root@lxy ~]# chmod g+s /opt/gongxiang/
[test01@lxy /opt]$ stat gongxiang/
File: ‘gongxiang/’
Size: 86       Blocks: 0         IO Block: 4096   directory
Device: 803h/2051d Inode: 67753409   Links: 4
Access: (2770/drwxrws---) Uid: (    0/   root)   Gid: ( 2026/gongxiang)
Context: unconfined_u:object_r:usr_t:s0
Access: 2019-12-18 18:48:13.316985658 +0800
Modify: 2019-12-18 18:48:11.411985725 +0800
Change: 2019-12-18 18:48:11.411985725 +0800
Birth: -
?
?
#test01测试
?
[test01@lxy /opt/gongxiang]$ touch test01.txt
[test01@lxy /opt/gongxiang]$ ll
total 0
-rw-rw-r--. 1 test01 gongxiang 0 Dec 18 18:43 test01.txt
[test01@lxy /opt/gongxiang]$ mkdir test01
[test01@lxy /opt/gongxiang]$ ll
total 0
drwxrwsr-x. 2 test01 gongxiang 6 Dec 18 18:43 test01
-rw-rw-r--. 1 test01 gongxiang 0 Dec 18 18:43 test01.txt
[test01@lxy /opt/gongxiang]$ ll test02
total 0
-rw-rw-r--. 1 test02 gongxiang 0 Dec 18 18:45 test02.log
[test01@lxy /opt/gongxiang]$ rm -f test02/test02.log
[test01@lxy /opt/gongxiang]$ ll test02
total 0
?
#test02用户测试
[test02@lxy /opt/gongxiang]$ touch test02.txt
[test02@lxy /opt/gongxiang]$ mkdir test02
[test02@lxy /opt/gongxiang]$ ll
total 0
drwxrwsr-x. 2 test01 gongxiang 6 Dec 18 18:43 test01
-rw-rw-r--. 1 test01 gongxiang 0 Dec 18 18:43 test01.txt
drwxrwsr-x. 2 test02 gongxiang 6 Dec 18 18:44 test02
-rw-rw-r--. 1 test02 gongxiang 0 Dec 18 18:43 test02.txt
[test02@lxy /opt/gongxiang]$ touch test02/test02.log
[test02@lxy /opt/gongxiang]$ ll test02
total 0
-rw-rw-r--. 1 test02 gongxiang 0 Dec 18 18:45 test02.log
?
sticky 粘滞位,让一个目录成为所有用户都有所有权限,目录权限是777.又让所有用户除了root和属主可以管理自己的文件或者目录,其它人没有管理的权限。
?
主要作用在其他人的x位,t表示
?
?
[root@lxy ~]# mkdir -m 777   /test
[root@lxy ~]# ll -d /test
drwxrwxrwx. 2 root root 6 Dec 18 18:58 /test
[root@lxy ~]# chmod o+t /test/
[root@lxy ~]# ll -d /test
drwxrwxrwt. 2 root root 6 Dec 18 19:00 /test
[root@lxy ~]# stat /test
File: ‘/test’
Size: 6         Blocks: 0         IO Block: 4096   directory
Device: 803h/2051d Inode: 1083835     Links: 2
Access: (1777/drwxrwxrwt) Uid: (    0/   root)   Gid: (    0/   root)
Context: unconfined_u:object_r:default_t:s0
Access: 2019-12-18 19:00:13.822960217 +0800
Modify: 2019-12-18 19:00:33.146959534 +0800
Change: 2019-12-18 19:01:09.490958251 +0800
Birth: -
?
#test01用户测试
[test01@lxy ~]$ touch /test/test01.txt
[test01@lxy ~]$ ll /test/
total 0
-rw-rw-r--. 1 test01 test01 0 Dec 18 19:01 test01.txt
-rw-rw-r--. 1 test02 test02 0 Dec 18 19:01 test02.txt
[test01@lxy ~]$ rm -f /test/test02.txt
rm: cannot remove ‘/test/test02.txt’: Operation not permitted
?
#test02用户测试
[test02@lxy ~]$ touch /test/test02.txt
[test02@lxy ~]$ ll /test/
total 0
-rw-rw-r--. 1 test01 test01 0 Dec 18 19:01 test01.txt
-rw-rw-r--. 1 test02 test02 0 Dec 18 19:01 test02.txt
[test02@lxy ~]$ rm -f /test/test01.txt
rm: cannot remove ‘/test/test01.txt’: Operation not permitted
[test02@lxy ~]$ rm -f /test/test02.txt
?

 

特殊属性

?
特殊属性不受权限的控制  
lsattr #显示文件的隐藏的属性,特殊属性
?
chattr #设置特殊属性
?
+   #添加

- #移除

a #只能表示追加,不能删除,移动,修改,给重要的文件,日志加上特殊属性

i #什么都不能操作,不能删除,移动,追加,重定向,给重要的配置文件加上特殊属性

   
?
#测试a属性
[root@lxy ~]# lsattr test.txt #显示文件的隐藏的属性,特殊属性
-----a---------- test.txt
[root@lxy ~]# cat test.txt
hostname
[root@lxy ~]# ./test.txt
lxy
[root@lxy ~]# cp test.txt /tmp/
[root@lxy ~]# rm -f test.txt
rm: cannot remove ‘test.txt’: Operation not permitted
[root@lxy ~]# mv test.txt /opt/
mv: cannot move ‘test.txt’ to ‘/opt/test.txt’: Operation not permitted
[root@lxy ~]# vim test.txt
[root@lxy ~]# echo pwd >> test.txt
[root@lxy ~]# cat test.txt
hostname
pwd
[root@lxy ~]# echo pwd > test.txt
-bash: test.txt: Operation not permitted
?
#测试i属性
[root@lxy ~]# lsattr test.txt
-----a---------- test.txt
[root@lxy ~]# chattr -a test.txt
[root@lxy ~]# lsattr test.txt
---------------- test.txt
[root@lxy ~]# chattr +i test.txt
[root@lxy ~]# cat test.txt
hostname
pwd
[root@lxy ~]# ./test.txt
lxy
/root
[root@lxy ~]# cp ./test.txt /opt/
[root@lxy ~]# mv ./test.txt /mnt/
mv: overwrite ‘/mnt/test.txt’? y
mv: cannot move ‘./test.txt’ to ‘/mnt/test.txt’: Operation not permitted
[root@lxy ~]# rm -f test.txt
rm: cannot remove ‘test.txt’: Operation not permitted
[root@lxy ~]# echo 123 >test.txt
-bash: test.txt: Permission denied
[root@lxy ~]# echo 123 >>test.txt
-bash: test.txt: Permission denied
?

 

第十五章 属主属组,Umask权限设置,特殊属性

原文:https://www.cnblogs.com/smyjs172lxy/p/13415669.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!