Linux之权限

一、权限基础

1、定义

权限:指定某个特定的用户具有特定的系统资源使用权力,还有操作、内容访问范围。

2、权限和身份之间的关系

在linux系统中,每个文件都有三种身份,分别是文件拥有者u(owner)、文件所属群组g(group)、其他o(other),每一个身份都有三种权限:可读(r)、可写(w)、可执行(x),还有一个特殊权限(s)

 二、权限使用

1、chmod设定权限

'''
命令:chmod options who=权限 + 文件名
命令:chmod options number(权限数字) 文件名/目录
选项参数:
--------options--------
-R: 对目前目录下的所有文件与子目录进行相同的权限变更(比较常用)
-v: 显示权限变更的详细资料
-f: 若该文件权限无法被更改也不要显示错误讯息
-c: 若该文件权限确实已经更改,才显示其更改动作
----------who----------
a:所有用户
u:文件拥有者
g: 文件所属组
o: 其他用户 
---------权限----------
rwx:读写执行
增加权限:+   取消权限:-  更改权限:=
'''

(1)根据身份设定

[root@VM-4-16-centos /home/test]#chmod a=rwx file #给文件三种身份配置rwx权限
[root@VM-4-16-centos /home/test]#chmod a=-rwx file #取消该文件所有身份拥有的权限
[root@VM-4-16-centos /home/test]#chmod u=rwx,g=rw,o=- file #给该文件属主配置可读写执行权限,属组读写,其他人无权限
[root@VM-4-16-centos /home/test]#chmod ug=rwx,o=r file #主+组配置读写执行,其他人读权限

[root@VM-4-16-centos /home/test1]#chmod u-rwx file #取消主的读写执行权限
[root@VM-4-16-centos /home/test1]#ll file
----r-xr-x 1 root root 0 Jan 10 16:16 file
[root@VM-4-16-centos /home/test1]#chmod u+rw file  #添加属主的读写权限
[root@VM-4-16-centos /home/test1]#ll file
-rw-r-xr-x 1 root root 0 Jan 10 16:16 file
[root@VM-4-16-centos /home/test1]#

(2)根据linux 权限数字来设定

'''
-----linux权限对照表如下-----
读取权限(r): 4
写入权限(w): 2
执行权限(x): 1
没有权限(-): 0
------常见linux权限数字------
444 r--r--r--
600 rw-------
644 rw-r--r--
666 rw-rw-rw-
700 rwx------
744 rwxr--r--
755 rwxr-xr-x
777 rwxrwxrwx
-------------------linux权限数字解析------------------- 755 7: 4+2+1 rwx 所有者具有读取、写入、执行权限; 5: 4+0+1 r-x 同组用户具有读取、执行权限但没有写入权限; 5: 4+0+1 r-x 其他用户具有读取、执行权限但没有写入权限;
'''
[root@VM-4-16-centos /home/test]#chmod 777 mulu/ #修改目录允许所有人访问
[root@VM-4-16-centos /home/test]#chmod 755 file #修改file权限
[root@VM-4-16-centos /home/test]#chmod -R 755 mulu/ #修改mulu目录下所有文件的权限

2、更改文件拥有者和文件所属组

'''
--------组变更命令---------
chgrp options 所属群组 文件或目录
--------options参数-----------
-c或--changes 效果类似"-v"参数,但仅回报更改的部分。
-f或--quiet或--silent  不显示错误信息。
-h或--no-dereference  只对符号连接的文件作修改,而不更动其他任何相关文件。
-R或--recursive  递归处理,将指定目录下的所有文件及子目录一并处理。(常用)
-v或--verbose  显示指令执行过程。
--help  在线帮助。
--reference=<参考文件或目录>  把指定文件或目录的所属群组全部设成和参考文件或目录的所属群组相同。
--version  显示版本信息
'''
[root@VM-4-16-centos ~]#chgrp -R hr /home/test/  #将test文件夹所有文件属组更改为hr
[root@VM-4-16-centos ~]#ll /home/test
total 4
-rwxr-xr-x 1 root hr    0 Jan 10 15:26 file
drwxr-xr-x 2 root hr 4096 Jan 10 15:53 mulu
'''
-----------chown 命令格式:-----------
chown [option] [所有者][:[组]] 文件/目录
-----------必要options参数------------
-c 显示更改的部分的信息
-f 忽略错误信息
-h 修复符号链接
-R 处理指定目录以及其子目录下的所有文件
-v 显示详细的处理信息
-deference 作用于符号链接的指向,而不是链接文件本身
'''
#案例
[root@VM-4-16-centos /home/test1]#chown nq:hr file #更改file属主为nq,组为hr
[root@VM-4-16-centos /home/test1]#ll file
-rw-r-xr-x 1 nq hr 0 Jan 10 16:16 file
[root@VM-4-16-centos /home/test1]#chown root file 更改file属主为root
[root@VM-4-16-centos /home/test1]#ll file
-rw-r-xr-x 1 root hr 0 Jan 10 16:16 file
[root@VM-4-16-centos /home/test1]#chown :root file  更改file组为root
[root@VM-4-16-centos /home/test1]#ll file
-rw-r-xr-x 1 root root 0 Jan 10 16:16 file

总结:chown和chgrp的option参数基本一样,区别:chown可有修改所有者和所属组,chgrp只能修改所属组

三、特殊权限

1、特殊权限SUID

'''
# 所有者特殊权限 suid
# 权限数字4000  字符s(S),有x则用S。
命令: chmod u+s filename
      chmod 4755 filename   #755是linux权限数字,4是特殊权限
'''
[root@VM-4-16-centos /home/test1]#chmod 4755 file
[root@VM-4-16-centos /home/test1]#ll file
-rwsr-xr-x 1 root root 0 Jan 10 16:16 file

作用: 1.让普通用户对可执行的二进制文件,临时拥有二进制文件的所属主权限。
         2.如果设置的二进制文件没有执行权限,那么suid的权限显示就是大S。
         3.特殊权限suid仅对二进制可执行程序有效,其他文件或目录则无效。

2、特殊权限SGID

'''
#所属组特殊权限 sgid  权限数字2000  字符s(S),如果属组没有x权限,则用S,否则用s
命令: chmod g+s filename
      chmod 2755 filename  #2755的755是linux权限数字
'''    

[root@VM-4-16-centos /home/test1]#chmod 2755 file
[root@VM-4-16-centos /home/test1]#ll file
-rwxr-sr-x 1 root root 0 Jan 10 16:16 file

作用:1、针对用户组权限位修改,用户创建的目录或文件所属组和该目录的所属组一致。

           2、针对二进制可执行文件设置sgid后,所属组将会更改为该二进制程序所属组
           3、.当某个目录设置了sgid后,在该目录中新建的文件不再是创建该文件的默认所属组,而是该目录的所属组

          4、使用sgid可以使得多个用户之间共享一个目录的所有文件变得简单。

3、特殊权限SBIT

'''
# 其他用户(o)特殊权限 sbit
# Sticky(SI TI KI)粘滞位
权限数字1000  字符t(T),同样也是有x就小写,否则就大写
命令: chmod 1755 /tmp
     chmod o+t /tmp
# 注意:只对目录有用
''' [root@VM-4-16-centos /home/test1]#chmod 1755 file [root@VM-4-16-centos /home/test1]#ll file -rwxr-xr-t 1 root root 0 Jan 10 16:16 file

作用:让使用者拥有有该目录下rw权限,但只能管理自己建立的文件,一般用在/tmp目录。

 四、权限属性chattr

'''
chattr [options] [+/-/=[属性]] 文件/目录
--------------options------------------
-R 递归处理,将指定目录下的所有文件及子目录一并处理。
-v<版本编号> 设置文件或目录版本。
-V 显示指令执行过程。
-------------八种属性-----------
a:让文件或目录仅供追加。
b:不更新文件或目录的最后存取时间。
c:将文件或目录压缩后存放。
d:将文件或目录排除在倾倒操作之外。
i:不得任意更动文件或目录。
s:保密性删除文件或目录。
S:即时更新文件或目录。
u:预防意外删除。
+ 表示添加  - 表示取消  =表示等于某个权限
'''
[root@VM-4-16-centos /home/test2]#chattr +a file1
[root@VM-4-16-centos /home/test2]#chattr +i file2
[root@VM-4-16-centos /home/test2]#lsattr file1 file2
-----a-------e-- file1
----i--------e-- file2
[root@VM-4-16-centos /home/test2]#chattr -a file1
[root@VM-4-16-centos /home/test2]#lsattr file1
-------------e-- file1
[root@VM-4-16-centos /home/test2]#
[root@VM-4-16-centos /home]#chattr -R +a test2/
[root@VM-4-16-centos /home]#lsattr test2/
----ia-------e-- test2/file2
-----a-------e-- test2/file1

五、访问控制ACL

acl基本使用

'''
-m:设定 ACL 权限。如果是给予用户 ACL 权限,则使用"u:用户名:权限"格式赋予;如果是给予组 ACL 权限,则使用"g:组名:权限" 格式赋予;
-x:删除指定的 ACL 权限;
-b:删除所有的 ACL 权限;
-d:设定默认 ACL 权限。只对目录生效,指目录中新建立的文件拥有此默认权限;
-k:删除默认 ACL 权限;
-R:递归设定 ACL 权限。指设定的 ACL 权限会对目录下的所有子文件生效;
'''

1、设置acl权限
# 在file文件上增加用户nq的读写权限
[root@192 test1]# setfacl -m u:nq:rw file
# 在file文件上增加组students的读执行权限
[root@192 test1]# setfacl -m g:students:r-x file
# 在file文件上增加其他用户的读权限
[root@192 test1]# setfacl -m o::r file
#在file文件上不给用户acluser的权限
[root@192 test1]# setfacl -m u:acluser:- file
# 在file文件上增加组sa的写权限
[root@192 test1]# setfacl -m g:sa:w file
'''
注意: 如果用户同时属于不同的两个组,并且两个组设定了acl访问控制
    1.根据acl访问控制优先级进行匹配规则
    2.如有用户拥有多个组的权限不同的权限,优先使用最高权限(模糊匹配)
'''

2、查看alc权限
#查看file文件的acl权限
[root@192 test1]# getfacl file
# file: file
# owner: root
# group: root
user::rw-
user:nq:rw-
user:acluser:---
group::r--
group:sa:-w-
group:students:r-x
mask::rwx
other::r--
        
3、移除acl权限
#移除组sa在文件file中的权限
[root@192 test1]# setfacl -x g:sa file
#移除用户nq在文件file中的权限
[root@192 test1]# setfacl -x u:nq file
# 移除文件和目录所有acl权限
[root@192 home]# setfacl -b test1/
# 移除默认的acl
[root@192 home]# setfacl -k file

#将file的acl权限赋值给file1
[root@192 test1]# setfacl -m u:nq:rwx,g:sa:wx,o::- file
[root@192 test1]# getfacl file | setfacl --set-file=- file1
 

六、扩展知识(进程掩码umask)

1、介绍

umask:是权限掩码,类似网络IP掩码,由3个8进制的数字组成。将现有的权限数字减去掩码后,就是建立文件时预设的权限。

一般系统默认是umask为022,创建新文件或者目录的时候,正常情况下目录的权限应该是777,但umask表示要 减去的值,所以新目录文件的权限应该是777-022=755,依次类推。

umask配置文件:/etc/bashrc   /etc/profile  ~/.bashrc  ~/.bash_profile

2、umask使用

#查看当前掩码
[root@VM-4-16-centos /home]#umask
0022
[root@VM-4-16-centos /home]#mkdir test1
[root@VM-4-16-centos /home]#ls -d -l test1/
drwxr-xr-x 2 root root 4096 Jan 10 21:27 test1/
# 777-022=755
#修改临时掩码
[root@VM-4-16-centos /home]#umask 000
[root@VM-4-16-centos /home]#umask
0000
[root@VM-4-16-centos /home]#mkdir tes
[root@VM-4-16-centos /home]#ls -d -l tes/
drwxrwxrwx 2 root root 4096 Jan 10 21:28 tes/
# 777 - 000 = 777
原文地址:https://www.cnblogs.com/nq31/p/14258826.html