系统权限及用户受权命令(11)

命令(6个)node

chmod,chown,chgrp,setfacl,getfacl,umask
vim


11.1.chmodbash

功能:改变文件权限ide

这一命令有符号模式和绝对模式测试


符号模式:ui

语法:chmod [who] operator [permission] filenamespa


who的含义是:orm

u 文件属主权限。server

g 属组用户权限。继承

o 其余用户权限。

a 全部用户(文件属主、属组用户及其余用户)。


operator的含义:

+ 增长权限。

- 取消权限。

= 设定权限。


permission的含义:

r 读权限。

w 写权限。

x 执行权限。

s 文件属主和组set-ID。

t 粘性位*。

l 给文件加锁,使其余用户没法访问。

u,g,o 针对文件属主、属组用户及其余用户的操做。


示例:

[root@localhost scripts]# chmod a+x test.txt   #添加全部的执行权限
[root@localhost scripts]# ll test.txt
-rwxr-xr-x 1 root root 276 Jun 26 18:39 test.txt
[root@localhost scripts]# chmod a-x test.txt   #收回全部命令的执行权限
[root@localhost scripts]# ll test.txt
-rw-r--r-- 1 root root 276 Jun 26 18:39 test.txt
[root@localhost scripts]# chmod og-w test.txt  #收回属组用户和其余用户的写权限
[root@localhost scripts]# ll test.txt
-rw-r--r-- 1 root root 276 Jun 26 18:39 test.txt
[root@localhost scripts]# chmod g+w test.txt #赋予属组用户写权限
[root@localhost scripts]# chmod u+x test.txt  #赋予文件属主执行权限
[root@localhost scripts]# chmod go+x test.txt  #赋予属组用户和其余用户执行权限


11.2.chown

功能:变动文件或目录的拥有者或所属群组。

语法:chomd -R -h owner file

经常使用选项:

 -c或--changes           效果相似"-v"参数,但仅回报更改的部分。

 -f或--quite或--silent  不显示错误信息。

 -h或--no-dereference  之对符号链接的文件做修改,而不更动其余任何相关文件。

 -R或--recursive  递归处理,将指定目录下的全部文件及子目录一并处理。

 -v或--version  显示指令执行过程。

 --dereference  效果和"-h"参数相同。

 --help  在线帮助。

 --reference=<参考文件或目录>  把指定文件或目录的拥有者与所属群组所有设成和参考文件或目 录的拥有者与所属群组相同。

示例:

[root@localhost scripts]# ll test.txt
-rwxrwxr-x 1 root root 276 Jun 26 18:39 test.txt
[root@localhost scripts]# chown user001 test.txt
[root@localhost scripts]# ll test.txt
-rwxrwxr-x 1 user001 root 276 Jun 26 18:39 test.txt


11.3chgrp

功能:变动文件或目录的所属群组

语法:chgrp [-cfhRv][--help][--version][所属群组][文件或目录...]

经常使用选项:

 -c或--changes  效果相似"-v"参数,但仅回报更改的部分。

 -f或--quiet或--silent  不显示错误信息。

 -h或--no-dereference  只对符号链接的文件做修改,而不更动其余任何相关文件。

 -R或--recursive  递归处理,将指定目录下的全部文件及子目录一并处理。

  --reference=<参考文件或目录>

示例:

[root@localhost scripts]# ll test.txt
-rwxrwxr-x 1 user001 root 276 Jun 26 18:39 test.txt
[root@localhost scripts]# chgrp group1 test.txt
[root@localhost scripts]# ll test.txt
-rwxrwxr-x 1 user001 group1 276 Jun 26 18:39 test.txt


11.4.umask

功能:遮罩权限


root用户:

默认建立目录的权限:755

默认建立文件的权限:644

普通用户:

默认建立目录的权限:775

默认建立文件的权限:664

 

umask 查看当前用户的默认权限

root:

umask

0022

普通用户:

uamsk

0002

 

目录的最大权限:777

文件的最大权限:666

root:

目录的默认权限=0777-umask=0777-0022=0755

文件的默认权限=0666-umask=0666-0022=0644

普通用户:

目录的默认权限=0777-umask=0777-0002=0775

文件的默认权限=0666-umask=0666-0002=0664

 

更改用户的默认权限:
临时更改:
umask 0002 只在当前终端生效
永久生效:
vim ~/.bashrc  当前用户家目录里的文件(局部变量)
..
umask 0002
 
# source ~/.bashrc  马上立刻读取文件内容(让其立刻生效)
# . ~/.bashrc
 
vim /etc/bashrc 针对全部用户(全局)
...
umask 0066
 
. ~/.bashrc
说明:
当局部变量和全局变量冲突,以局部为准

练习:

一、建立一个文件test,要求它实现user001只读,user002,user003可读可写,user004,user005可读可执行

[root@localhost scripts]# usermod -g group1  user002
[root@localhost scripts]# usermod -g group1  user003
[root@localhost scripts]# id user002
uid=601(user002) gid=10015(group1) groups=10015(group1)
[root@localhost scripts]# id user003
uid=602(user003) gid=10015(group1) groups=10015(group1)
[root@localhost scripts]# touch test
[root@localhost scripts]# ll test
-rw-r--r-- 1 root root 78184 Jun 27 14:06 test
[root@localhost scripts]# chown user001:group1 test
[root@localhost scripts]# ll test
-rw-r--r-- 1 user001 group1 0 Jun 27 14:28 test
[root@localhost scripts]# chmod u-w,g+w,o+x test
[root@localhost scripts]# ll test
-r--rw-r-x 1 user001 group1 0 Jun 27 14:28 test

2.什么状况下才能删除一个文件或目录?

答:跟这个文件或目录自己的权限无关,跟你对它们的上一级目录是否有写权限和执行全有关。你对一个目录有写权限和执行权限,你就能够删除这个目录里任何文件


例子

[root@localhost ~]# ll -d test  #可读可执行
drwxr-xr-x 2 root root 4096 Dec 21 18:54 test
[root@localhost ~]# chown .group1 test
[root@localhost ~]# id user002
uid=601(user002) gid=10015(group1) groups=10015(group1)
[user002@localhost home]$ ll test
total 0
-rw-r--r-- 1 root group1 0 Dec 21 18:58 123
user002@localhost test]$ rm -f 123  
rm: cannot remove `123': Permission denied
说明:可读可执行是没法删除

[root@localhost ~]# ll -d /home/test  #增长w,去掉x权限
drwxrw-r-x 3 root group1 4096 Dec 21 18:58 /home/test
[user002@localhost home]$ cd test #无执行权限,是进不了文件,进不了文件夹,怎么删除文件
-bash: cd: test: Permission denied
[user002@localhost home]$ rm -f test/123
rm: cannot remove `test/123': Permission denied

[root@localhost ~]# ll -d /home/test  #去掉r,保留wx
drwx-wxr-x 3 root group1 4096 Dec 21 19:17 /home/test
[user002@localhost test]$ ll
ls: cannot open directory .: Permission denied
[user002@localhost test]$ rm -f 123
说明:没有只读权限,可是用户有wx,就能进入文件夹,删除文件


3.请问下面的状况,我一个普通用户可否删除/test/abc这个子目录

# ll -d /test
drwxr-xrwx. 3 root root 12288 Jul 16 17:42 /test
# ll /test
drwxr-xr-x. 2 root root 4096 Jul 16 17:40 abc
[user001@localhost ~]$ rm -rf /home/test/abc
答案:能够删除,由于普通如今不属于属主,也不属于属组,那就只能属于其余人了;而test权限的o位是7的权限,可写可执行,就能删掉abc

 

四、什么状况下修改了一个文件才须要强制保存,什么状况下可以成功强制保存?

解答:

状况1:目录有可读可写权限,文件只有只读权限(普通用户)

[root@localhost ~]# ll -d /home/test

drwxrw--wx 3 root group1 4096 Dec 22 16:20 /home/test

[root@localhost ~]# ll  /home/test

total 0

-rw-rw-r-- 1 root group1 0 Dec 21 19:24 123

[user002@localhost ~]$ vim /home/test/123

"/home/test/123" E212: Can't open file for writing  #不能写也不能强制保存


状况2:目录有可读可写权限,文件可读可写(普通用户)

[root@localhost ~]# ll -d /home/test

drwxrw--wx 3 root group1 4096 Dec 22 16:20 /home/test

[root@localhost ~]# ll  /home/test

total 0

-rw-rw-r-- 1 root group1 0 Dec 21 19:24 123

[user002@localhost ~]$ vim /home/test/123  #仍是不能强制保存


状况3:目录有可读可写权限,文件可读可写可执行(普通用户)

[root@localhost ~]# ll -d /home/test

drwxrw--wx 3 root group1 4096 Dec 22 16:20 /home/test

[root@localhost ~]# ll  /home/test

total 4

-rwxrw-rwx 1 root group1 4 Dec 22 17:04 123

[user002@localhost ~]$ cat /home/test/123  

my

说明:这种状况不须要强制就能保存


状况4:目录有可读可写权限,文件可写可执行(普通用户)

[root@localhost ~]# ll  /home/test

total 4

-rwxrw--wx 1 root group1 3 Dec 22 17:10 123

说明:不须要强制就能保存


状况3:目录有可读可写可执行权限,文件可读(普通用户)

[root@localhost ~]# ll -d /home/test

drwxrwx-wx 3 root group1 4096 Dec 22 16:20 /home/test

[root@localhost ~]# ll  /home/test

total 0

-rw-r--r-- 1 root group1 0 Dec 21 19:24 123

[user002@localhost ~]$ vim /home/test/123 

[user002@localhost ~]$ cat /home/test/123

yyl

说明:对一个文件有只读权限,对目录有读写执行权限,强制保存没问题,而且保存后,文件属主属组都变了当前用户


状况4:



答案:若是你对一个文件没有写权限,那么修改后,要保存就会出现强制保存的选项;

     虽然你对这个文件没有写权限,但若是你对文件所在的上一级目录有写权限,则能够强制保存成功,而且保存后,把文件的owner和group属性改为了你本身

     若是对上一级目录没有写权限,则不能够强制保存;但也有一个例外,就是若是这个文件的owner就是你,那么你对它没有写权限,对它上级目录也没写权限,也能够强制保存成功


11.5.特殊权限(s,t)

s位叫特权位,英文的叫法有setuid,setgid,suid,sgid等


1)冒险位(setuid):4000 u+s 临时拥有文件拥有者的权限,做用在属主身上,通常针对命令。皇帝临时给一个用户一个尚方宝剑.只针对前三位

对目录能够加,可是无效,由于目录不是命令,根本不能执行它

 

demo:

# chmod u+s /usr/bin/vim

# ll /usr/bin/vim

-rwsr-xr-x. 1 root root 1967072 Feb 17  2012 /usr/bin/vim

# chmod 0766 /usr/bin/vim

[root@node1 Desktop]# ll /usr/bin/vim

-rwxrw-rw-. 1 root root 1967072 Feb 17  2012 /usr/bin/vim

 

小实验:

1,测试passwd命令的s位,去掉其s位,普通用户就不能本身改本身密码了,加上s位就能够了

 

2,/bin/touch这个命令是没有s位的,你能够尝试加上s位,而后用普通用户touch文件,观察效果。作完测试后,记得改回来也就是把s位去掉

 

3,普通用户能关机吗?怎么让普通用户关机

 

能够对关机命令加s位来让普通用户能关机,在rhel6下测试reboot,shutdown能够这样作,init不能这样作

这只是一个理论讨论,实际状况不会这么作的


 

2)强制位(setgid):2000 g+s 针对目录,任何人在该目录下建立的文件或目录强制继承父目录的属组。强制继承家族企业。中间三位

demo:

chmod g+s dir1

chmod 2755 dir1

测试验证

# ll -d

drwxr-sr-x. 5 u01 sysadmin 4096 Apr  6 11:24 .


3)粘制位 :1000 o+t 针对公共目录,在该目录下的文件只有root和文件的拥有者能够删除,其余人不能删除。本身管理本身

demo:

# chmod 1777 dir2

drwxrwxrwt. 2 root root     4096 Apr  6 10:33 dir2

 

# ll -d /tmp

drwxrwxrwt. 24 root root 4096 Apr  6 11:16 /tmp

 

s位和t位注意的地方:

 

用数字表示

 

前三位上加s    4

中间三位加s    2

后三位加t        1

 

 

chmod 1777 /tmp

 

s位和t位大小写的区别 

        小写的话就表示 有x执行权限

        大写的话就表示 没有x执行权限 


设置文件的特殊属性

chattr +i 不能删除,更名,修改 chattr -i

chattr +a 只能追加内容

lsattr 显示特定属性



11.6.acl访问控制策略

ACL:Access Control List ,实现灵活的权限管理

除了文件的全部者,所属组和其它人,能够对更多的用户设置权限

CentOS7 默认建立的xfs和ext4文件系统具备ACL功能

CentOS7 以前版本,默认手工建立的ext4文件系统无ACL功能,需手动增长

tune2fs –o acl /dev/sdb1

mount –o acl /dev/sdb1 /mnt/test

ACL生效顺序:全部者,自定义用户,自定义组,其余人


setfacl:

-R:递归受权,对目录下已经存在的文件有acl策略,可是新建的文件没有acl策略

-d:递归受权,对目录下新建的文件有acl策略,老文件没有

-m:修改acl策略

-x:删除某个用户的权限

-b:删除全部的acl策略

-mask:设置除了其余人和拥有者之外的最大权限

 

下面咱们再来继续看一个例子

假如如今咱们设置test.sh的mask为read only,那么family组的用户还会有write 权限吗?

 

经过实例来了解 为多用户或者组的文件和目录赋予访问权限rwx

示例:

 

// 查看文件或者目录的ACL权限 使用 getfacl FILE|DIR
[root@localhost scripts]# getfacl  100.sh
# file: 100.sh
# owner: root
# group: root
user::rw-
group::r--
other::r--
//给文件100.sh加上mage的运行权限
[root@localhost scripts]# setfacl -m u:user_00:w 100.sh
//能够发现,在权限位的后面加上了一个加号,这就说明该文件被赋予了ACL权限
[root@localhost scripts]# ll 100.sh
-rw-rw-r--+ 1 root root 660 Sep 21 14:09 100.sh
// 查看100.sh的ACL权限
[root@localhost scripts]# getfacl 100.sh
# file: 100.sh
# owner: root
# group: root
user::rw-
user:user_00:-w-
group::r--
mask::rw-
other::r--
//若是此时user_00用户去读100.sh文件,他是不能读取的。
//指定admins组里的成员对100.sh文件有读写权限
[root@localhost scripts]# setfacl -m g:admin:rw 1001.sh
[root@localhost scripts]# getfacl 1001.sh
# file: 1001.sh
# owner: root
# group: root
user::rw-
group::r--
group:admin:rw-
mask::rw-
other::r--
//此时user_00用户能够对1001.sh文件机型写入吗
//切换到user_00用户
  [user_00@localhost ~]$ echo helloworld >> /server/scripts/1001.sh
//下面这些命令能够本身尝试一下
setfacl -m u:wang:rwx file|directory
setfacl -Rm g:sales:rwX directory //递归
setfacl -M file.acl file|directory //增长权限
setfacl -m g:salesgroup:rw file| directory
setfacl -m d:u:wang:rx directory //设置目录的默认权限
setfacl -x u:wang file |directory
setfacl -X file.acl directory 删除权限
//增长权限和删除权限的文件格式是不同的。
//定义一个文件用来增长权限
[root@localhost scripts]# nano file.acles
//先看一下f1的ACl
[root@localhost scripts]# getfacl f1
# file: f1
# owner: root
# group: root
user::rw-
group::r--
other::r--
//给file.acles增长权限
u:mage:rwx
g:admins:rw
[root@localhost scripts]# setfacl  -m u:user_00:rwx file.acles
[root@localhost scripts]# setfacl -m g:admin:rw file.acles
//执行修改权限命令
[root@localhost scripts]# setfacl -M file.acles f1
setfacl: Invalid argument in line 1 of file file.acles   #这里报错,须要在研究下
[root@localhost scripts]# setfacl -x u:user_00 file.acles
[root@localhost scripts]# getfacl file.acles
# file: file.acles
# owner: root
# group: root
user::rw-
group::r--
group:admin:rw-
mask::rw-
other::r--


ACL mask 值

一个文件的权限一般涉及到哪几类用户。 属主,属组,自定义用户,自定义组,Other,一共五类使用者。 而ACL就是对这五类用户的权限进行控制 。

ACL文件上的group权限是mask 值(自定义用户,自定义组,拥有组的最大权限),而非传统的组权限

 

[root@localhost scripts]# getfacl  f1
# file: f1
# owner: root
# group: root
user::rw-
user:user_00:rwx
group::r--
group:admin:rw-
mask::rwx
other::r--
//设置文件的mask值位 r--
[root@localhost scripts]# setfacl -m mask::r-- f1
[root@localhost scripts]# getfacl f1
# file: f1
# owner: root
# group: root
user::rw-
user:user_00:rwx        #effective:r--
group::r--
group:admin:rw-            #effective:r--
mask::r--
other::r--
设置mask值以后咱们会发现,自定义用户,自定义组,以及默认的属组,都被mask值限定住了。
能够将mask值理解为最高权限,除了全部者,Other以外,其余的都在mask 的管辖以内。
getfacl 可看到特殊权限:flags
经过ACL赋予目录默认x权限,目录内文件也不会继承x权限
base ACL 不能删除
setfacl -k dir 删除默认ACL权限
setfacl –b file1清除全部ACL权限
getfacl file1 | setfacl –set-file=- file2 复制file1的acl权限给file2
//getfacl 可看到特殊权限:flags
 //若是一个文件或目录设置特殊权限,SGID,SUID,Sticky
    # file: dir
    # owner: root
    # group: root
    # flags: -s-
    user::rwx
    group::r-x
    other::r-x
mask只影响除全部者和other的以外的人和组的最大权限Mask须要与用户的权限进行逻辑与运算后,才能变成有限的权限(Effective Permission)
用户或组的设置必须存在于mask权限设定范围内才会生效
setfacl -m mask::rx file
–set选项会把原有的ACL项都删除,用新的替代,须要注意的是必定要包含UGO的设置,不能象-m同样只是添加ACL就能够
示例: setfacl –set u::rw,u:wang:rw,g::r,o::- file1


练习一:

一、root用户新建目录/tmp/common,要求目录的所属组为tom

 

# mkdir /tmp/common
前提是有tom组
# chgrp tom /tmp/common/
或者
# chown .tom /tmp/common/

二、是否容许tom用户在/tmp/common下建立文件tomfile?若是不容许,怎么才能让它建立文件?

不容许,给属组一个w权限

 

chmod g+w /tmp/common

三、用户jack若是想修改tomfile文件,怎么作?

 

chmod o+w tomfile
或者
usermod -G tom jack
或者
gpasswd -a jack tom(组)
或者
setfacl -m u:jack:rw tomfile

 

练习二:

一、以普通用户user01身份登陆,新建目录/shared/sysadmin,而且属于sysadmin组

 

# su - user01
$ mkdir /shared/sysadmin -p
# setfacl -m u:user01:rwx /
# chgrp sysadmin /shared/sysadmin

 

二、要求组成员有读写访问的权限,其余成员没有任何权限

 

[root@node1 home]# ll -d /shared/sysadmin/-x
drwxrwxr-x. 2 user01 sysadmin 4096 Apr  6 14:44 /shared/sysadmin/
[root@node1 home]# chmod o=--- /shared/sysadmin/
[root@node1 home]# ll -d /shared/sysadmin/

 

三、要求组成员不能够删除属于其余成员的文件

 

# ll -d /shared/sysadmin/
drwxrwx--T. 2 user01 sysadmin 4096 Apr  6 14:44 /shared/sysadmin/

 

四、要求在/shared/sysadmin下面建立的文件所有属于sysadmin组

 

# ll -d /shared/sysadmin/
drwxrws--T. 2 user01 sysadmin 4096 Apr  6 14:44 /shared/sysadmin/

五、要求harry可写,natash不能读写

 

# setfacl -m u:harry:rwx /shared/sysadmin/
# setfacl -m u:natash:--- /shared/sysadmin/
相关文章
相关标签/搜索