linux系统中的权限管理
一.权限查看及读取
1.权限查看
ls -l file 查看文件权限
[westos@lzy Desktop]$ ls -l test
-rw-rw-r--. 1 westos westos 30 Oct 5 12:19 test
ls -ld dir 查看目录权限
[westos@lzy Desktop]$ ls -ld /mnt/
drwxr-xr-x. 3 root root 17 Oct 21 04:34 /mnt/
2.权限的读取
“文件的属性被叫做文件的元数据(meta data)”
“一种元数据用1个byte来记录内容”
文件权限信息
- | rw-r--r-- | . | 1 | root | root | 0 | Apr 12 10:57 | westos
[1] [2] [3] [4] [5] [6] [7] [8] [9]
目录权限信息
d | rw-r--r-- | . | 2 | root | root | 0 | Apr 12 10:57 | westosdir
[1] [2] [3] [4] [5] [6] [7] [8] [9]
解释
[1] #文件类型
- 普通文件
- d 目录
l 软连接
b 快设备
c 字符设备
s socket套接字
p 管道
[2] ##用户权限
r
w
x
rw-|r--|r--
u g o
[3] 系统的selinux开启
[4] ##对于文件:文件内容被系统记录的次数(硬链接个数) ##对于目录:目录中子目录的个数
[5] ##文件拥有者
[6] ##文件拥有组
[7] ##对于文件:文件内容大小 ##对于目录:目录中子文件的元数据大小
[8] ##文件内容被修改的时间
[9] ##文件名称
二.普通权限的类型及作用
1.用户对文件的身份
u: | user 文件的拥有者,ls -l 看到的第五列信息 |
---|---|
g: | group 文件拥有组, ls -l 看到的第六列信息 |
o: | other 既不是拥有者也不是拥有组成员的其他用户的通称 |
2.权限位
rwx|r--|r--
u g o
3.用户身份匹配
user>group>other
4.权限类型
- | 权限未开启 |
---|---|
r | 可读 对于文件:可以读取文件内容; 对于目录:可以ls列出目录中的文件 |
w | 可写 #对于文件:可以更改文件内容;#对于目录:可以在目录中新建或者删除文件 |
x | 可执行 #对于文件:可以用文件名称调用文件内记录的程序 #对于目录:可以进入目录中 |
三.设定普通权限的方法
chmod
设定文件权限
chmod 复制权限
chmod --reference=/root /mnt
##复制/tmp目录的权限到/mnt上
chmod -R --reference=/root /mnt
#复制/root目录的权限到/mnt及
#目录中的子文件上-R 代表第归操作
chmod 字符方式设定权限
chmod <a|u|g|o><+|-|=><r|w|x> file
##用字符方式设定文件权限
示例:
chmod u+x,g+rw,o+r /mnt/file1
多个参数,逗号隔开
[root@lzy ~]# ls -l /mnt/file1
-rwx------. 1 root root 0 Oct 21 06:54 /mnt/file1
[root@lzy ~]# chmod u+x,g+rw,o+r /mnt/file1
[root@lzy ~]# ls -l /mnt/file1
-rwxrw-r--. 1 root root 0 Oct 21 06:54 /mnt/file1
[root@lzy ~]#
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/file1
[root@lzy ~]# chmod 600 /mnt/file1
[root@lzy ~]# ls -l /mnt/file1
-rw-------. 1 root root 0 Oct 21 06:54 /mnt/file1
四.系统默认权限设定
#系统本身存在的意义共享资源
#从安全角度讲系统共享的资源越少,开放的权力越小系统安全性越高
#既要保证系统安全,又要系统创造价值,于是把应该开放的权力默认开放
#把不安全的权力默认保留
如何保留权力?
umask表示系统保留权力
umask
#查看保留权力
umask 权限值
#临时设定系统预留权力
[root@lzy mnt]# umask
0022
[root@lzy mnt]# umask 055
[root@lzy mnt]# umask
0055
文件默认权限 = 777-umask-111
[root@lzy ~]# umask
0022
[root@lzy ~]# touch /mnt/file1
[root@lzy ~]# ls -l /mnt/file1 777-022-111=644
-rw-r--r--. 1 root root 0 Oct 21 06:54 /mnt/file1
目录默认权限 = 777-umask
[root@lzy ~]# mkdir /mnt/dir
[root@lzy ~]# ls -ld /mnt/dir 777-022=755
drwxr-xr-x. 2 root root 6 Oct 21 06:55 /mnt/dir
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
[westos@lzy Desktop]$ umask
0002
[westos@lzy Desktop]$ source /etc/bashrc
[westos@lzy Desktop]$ source /etc/profile
[westos@lzy Desktop]$ umask
0022
[westos@lzy Desktop]$
五.文件用户用户组管理
chown username file ##更改文件拥有者
chgrp groupname file ##更改文件拥有组
chown username:groupname file
##同时更改文件的拥有者和拥有组
chown username.groupname file
chown|chgrp -R user|group dir
##更改目录本身及目录中内容的拥有者或者拥有组
六.特殊权限
限制删根
useradd -s /sbin/nologin -M norm
chown norm.norm /bin/rm
chmod ug+s /bin/rm
stickyid 粘制位
针对目录
: #如果一个目录stickyid开启,那么这个目录中的文件只能被文件所有人删除
chmod 1原始权限 dir/file
chmod o+t dir/file
chmod o+t/1777 目录
sgid 强制位
针对目录: 目录中新建的文件自动归属到目录的所属组中
设定:
chmod 2源文件权限 dir
chmod g+s dir
如下图:默认在目录下建立的文件,谁建立文件的组和用户是谁,g+s,文件被建立的时候,会继承目录的组
只针对二进制的可执行文件(c程序)
当运行二进制可执行文件时都是用文件拥有组身份运行,和执行用户无关
默认情况下
suid 冒险位
只针对二进制的可执行文件(c程序)
#当运行二进制可执行文件时都是
用文件拥有者身份运行,
和执行用户无关
设定
chmod 4原属性 file
chmod u+s file
见上图sgid实验
七.acl权限列表
Aiccess Control Lists #访问控制列表
功能:
#在列表中可以设定特殊用户对与特殊文件有特殊权限
acl列表开启标识
-rw-r–r--. 1 root root 0 Oct 21 05:15 westosfile
没有"+"代表acl列表未开启
-rw-rwxr–+ 1 root root 0 Oct 21 05:15 westosfile
+代表acl列表功能开启
acl列表权限读取
getfacl westosfile
[root@lzy mnt]# getfacl westosfile
# file: westosfile
# owner: root
# group: root
user::rw-
user:westos:rwx
group::r--
group:westos:r--
mask::rwx
other::r--
显示内容分析
# file: westosfile #文件名称
# owner: root #文件拥有者
# group: root #文件拥有组
user::rw- #文件拥有者权限
user:westos:rwx #特殊指定用户权限
group::r-- #文件拥有组权限
group:westos:r#特殊指定的用户组的权限
mask::rwx #能够赋予特殊用户和特殊用户组的最大权限阀值
other::r-- #其他人的权限
“注意:”
"当文件权限列表开启,不要用ls -l 的方式来读取文件的权限"
acl列表的控制
setfacl -m u:westos:rwx westosfile #设定
setfacl -m g:westos:r westosfile
u::rwx ----> 中间的空格代表是默认组,及主组进行设定
setfacl -m u::rwx westosfile
setfacl -m g::0 westosfile
setfacl -x u:westos westosfile
##删除列表中的westos
setfacl -b westosfile
#关闭
acl 权限优先级
拥有者 > 特殊指定用户 > 权限多的组 >权限少的组 > 其他
acl mask 控制
#mask是能够赋予指定用户权限的最大阀值
问题
当设定完毕文件的acl列表之后
用chmod缩小了文件拥有组的权力
mask会发生变化
恢复:
setfacl -m u:westos:权限 文件
acl 列表的默认权限
setfacl -m u:lee:rwx /mnt/westosdir
##只对于/mnt/westosdir目录本身生效
setfacl -Rm u:lee:rwx /mnt/westosdir
##对于/mnt/westosdir目录和目录中已经存在的内容生效
#以上的命令之针对与存在的文件生效,新建文件是不会被设定的
setfacl -m d:u:lee:rwx /mnt/westosdir/
##针对与/mnt/westosdir目录中新建文件生效
八.attr权限
attr权限限制所有用户(包括root用户)
i | 不能做任何的更改 |
---|---|
a | 能添加不能删除 |
lsattr dir|file |
查看attr权限 |
chattr +i|+a|-i|-a dir|file |
设定attr权限 |
普通的权限无法限制root用户,root用户可以任意的建立和删除文件
chattr +a /mnt/lzy
当/mnt/lzy目录有了attr权限的a,此目录下,所有用户不可进行删除操作
,如图所示,root用户,可以在此目录下建立文件,但无法删除
chattr -a 目录
可以去掉attr权限的+a设定
chattr +i
不能进行任何的更改
test
1。建立目录完成以下要求
shengchan jishu caiwu组已经建立
westosadmin用户已经建立
新建目录是/sc /cw /js /pub
·/sc目录是生产部门的数据存储目录,
只能被生产部门的人员读写。
并且在sc内建立的文件都属于生产部门
·/cw目录是财务部门的数据存储目录,
只能被财务部门的人员丢写,
并且在cw中建立的文件都属于财务部门
·/js目录是技术部门的数据存储目录,
只能被技术部门的人员读写,
并且在js中建立的文件都属于技术部门
·/pub为公司人员公共目录,
可以被公司任何员工读写,
但是只能删除自己的文件
·westosadmin用户可以对/sc /cw /js /pub
作任何操作
mkdir /sc /cw /js /pub
chgrp shengchan /sc ## chmod 3770 /sc 2: g+s
chgrp shengchan /cw## chmod 3770 /cw 1: o+t
chgrp shengchan /js ## chmod 3770 /js
chmod 1777 /pub
setfacl -m u:westosadmin:rwx /sc
setfacl -m u:westosadmin:rwx /cw
setfacl -m u:westosadmin:rwx /js
setfacl -m u:westosadmin:rwx /pub