进程安全上下文:
进程对文件的访问权限应用模型:
进程的属主与文件的属主是否相同;如果相同,则应用属主权限。否则,则检查进程的属主是否属于文件的属组;如果是,则应用属组权限。否则,只能使用other的权限。
1.基本权限UGO
文件权限设置:可以赋予某个用户或组,能够以何种方式,访问某个文件
文件权限管理之:UGO设置基本权限(r,w,x)
权限对象: 权限类型:
属主:u 读:r(4)
属组:g 写:w(2)
其他人:o 执行:x(1)
1.1设置权限
更改文件的属主,属组(chown)
chown linux file1(更改file1的属主为linux,且linux这个用户必须存在)
chown .alice file2(更改file2的属组为alice,且alice这个组必须存在)
chown linux:alice file3(同时更改file3的属主和属组)
更改文件的属组(chgrp)
chgrp alice file4(更改file4的属组)
chgrp -R alice /dir1(更改dir1目录下的所有文件和目录)
更改权限
对象 赋值符 权限类型
chmod u+r 文件名(给属主加读权限)
chmod a+x 文件名(给所有用户和其他人加执行权限)
chmod g-w file1(file1的属组去掉写权限)
chmod o=x 文件名(其他用户只有执行权限)
chmod a=- 文件名(所有人没有权限)
chmod ug=rw,o=r file(给属主属组读写权限,给其他用户读权限)
使用数字
chmod 644 file(给属组读写权限,其他用户和属组为读权限)
1.2设置权限示例
针对hr部门的访问目录设置权限,要求为:
1.root用户和hr组的员工可以读,写,执行
2.其他用户没有任何权限
groupadd hr
useradd hr01 -G hr
useradd hr02 -G hr
mkdir /home/hr
chgrp hr /home/hr
chmod 770 /home/hr
文件:
r:可获取文件的数据
w:可修改文件的数据
x:可将文件运行为进程
目录:
r:可使用ls命令获取其下的所有文件列表
w:可修改此目录下的文件列表;即创建或删除文件
x:可cd到此目录中,且可使用ls -l来获取所有文件的详细属性信息
示例:
1.在dir1目录下有file1文件,dir1目录有读和执行权限,file1所有权限都有,不能够删除file1
[root@localhost ~]# mkdir /dir1
[root@localhost ~]# touch /dir1/file1
[root@localhost ~]# chmod 777 /dir1/file1
[root@localhost ~]# ll -d /dir1/
drwxr-xr-x. 2 root root 4096 3月 11 18:37 /dir1/
[root@localhost ~]# ll /dir1/file1
-rwxrwxrwx. 1 root root 0 3月 11 18:37 /dir1/file1
[alice@localhost ~]$ rm -rf /dir1/file1
2.在dir1目录下有file1文件,dir1目录有写和执行权限,file1所有权限都有,不能够到dir1下用ls看dir1有哪些文件或目录
[root@localhost ~]# chmod 777 /dir1/file1
[root@localhost ~]# chmod o=wx /dir1
[root@localhost ~]# ll -d /dir1/
drwxr-x-wx. 2 root root 4096 3月 11 18:37 /dir1/
[root@localhost ~]# ll /dir1/file1
-rwxrwxrwx. 1 root root 0 3月 11 18:37 /dir1/file1
[alice@localhost ~]$ cd /dir1/
[alice@localhost ~]$ ls(权限不够)
3.如果dir1目录下没有执行权限,dir1目录即使有读和写权限都不能使用。其目录下的文件或目录的权限也不能使用。
[root@localhost ~]# mkdir dir1
[root@localhost ~]# touch dir1/file1
[root@localhost ~]# chmod 777 file2(file3)
[root@localhost ~]# chmod 777 file2(file3)
[root@localhost ~]# ll /root/dir1/dir2/file2(/dir3/file3)
4.如果dir1目录下有dir2目录,dir2目录下有dir3目录和file2文件,dir3目录下有文件file3,现在给file2和file3 777权限,删除dir3的执行权限,发现file3的所有权限不能使用,file2的权限可以继续使用。如果删除dir2或dir1的执行权限,file2和file3的权限都不能使用。说明层级目录都必须有执行权限
[root@localhost ~]# mkdir -p dir1/dir2/dir3
[root@localhost ~]# touch dir1/dir2/file2
[root@localhost ~]# touch dir1/dir2/dir3/file3
[root@localhost ~]# chmod 777 file2(file3)
[root@localhost ~]# ll /root/dir1/dir2/file2(/dir3/file3)
-rwxrwxrwx. 1 root root 0 3月 11 18:37 /dir1/file1
[alice@localhost ~]# chmod o-x /root/dir1
[alice@localhost ~]$ more dir1/dir2/file2(无法查看)
[root@localhost ~]$ more /dir1/dir2/dir3/file3(无法查看)
5.如果dir1目录下有dir2目录,dir2目录下有dir3目录。且dir1目录权限为777.dir2和dir3其他人权限没有w权限,则删除不了dir2,dir3,若删除只能删除dir1。若dir2其他人权限有w,dir3没有,则dir2能删除,dir3跟着dir2也删除了。如果dir3有w权限,dir2没有w权限,则dir3也删除不了,必须其上级的所有目录都有w权限。
[root@localhost ~]# mkdir -p dir1/dir2/dir3
[root@localhost ~]# chmod 777 dir1
[root@localhost ~]# chmod o+w dir3
[root@localhost ~]$ rm -rf dir1/dir2/dir3(无法删除)
[root@localhost ~]# chmod o+w dir2
[root@localhost ~]$ rm -rf dir1/dir2/dir3
6.如果文件只有写权限,则不能读取文件和执行文件
[root@localhost ~]# touch file4
[root@localhost ~]# chmod o=w file4
[root@localhost ~]$ more file4(权限不够)
[root@localhost ~]$ echo “hello” >> file4
2.基本权限ACL
setfacl命令用于管理文件的ACL规则,格式为”setfacl [参数] 文件名称”文件的ACL提供的是在所有者,所属组,其他人的读/写/执行权限之外的特殊权限之外的特殊权限控制,使用setfacl命令可以针对单一用户或用户组,单一文件或目录来进行读/写/执行权限的控制
文件权限管理之:ACL设置基本权限(r,w,x)
UGO设置基本权限:只能一个用户,一个和其他人
ACL设置基本权限:r,w,x
2.1ACL基本用法
设置:
[root@localhost ~]# touch /home/test
[root@localhost ~]# ll /home/test
-rw-r–r--. 1 root root 0 Oct 11 17:20 /home/test
[root@localhost ~]# getfacl /home/test
getfacl: Removing leading ‘/’ from absolute path names
file: home/test
owner: root
group: root
user::rw-
group::r–
other::r–
[root@localhost ~]# setfacl -m u:alice:rw /home/test(对这个文件增加用户alice权限)
[root@localhost ~]# setfacl -m o::rw /home/test.txt (给所有其他用户设置为rw权限)
查看/删除:
[root@localhost ~]# ll /home/test
-rw-r-----+ 1 root root 6 Oct 11 17:30 /home/test(此时有ACL权限)
[root@localhost ~]# getfacl /home/test
[root@localhost ~]# setfacl -x u:alice /home/test(删除用户alice对此文件设置的权限)
[root@localhost ~]# setfacl -x g:hr /home/test(删除hr组对此文件设置的权限)
[root@localhost ~]# setfacl -b /home/test(删除所有acl权限)
[root@localhost ~]# getfacl file1 | setfacl --set-file=- file2(复制file1的ACL权限给file2)
2.2ACL高级用法
mask:
用于临时降低用户或组(除属主和其他人)的权限
建议:为了方便管理文件权限,其他人的权限设置为空
[root@localhost ~]# setfacl -m m::— /home/test(设置文件的mask为000)
default:继承(默认)
要求:希望alice用户能够对/home以及以后再/home下新建的文件有读,写,执行权限
步骤一:赋予alice对/home读,写,执行权限
[root@localhost ~]# setfacl -m u:alice:rwx /home
步骤二:赋予alice与以后在/home下新建的文件有读,写,执行权限(使用alice的权限继承)
[root@localhost ~]# setfacl -m d:u:alice:rwx /home
3.进程掩码mask umask
文件权限管理之:进程mask
mask:用来控制acl的最大权限
文件权限管理之:进程umask
进程新建文件,目录默认权限会受到umask的影响,umask表示要减掉的权限
工作流程:
shell (vim,touch) =umask> 新文件或目录 权限
useradd =umask> 用户HOME
示例:
1.在shell进程中创建文件
[root@localhost ~]# umask(查看当前用户的umask权限)
0022
[root@localhost ~]# touch file1(755-111=644)
[root@localhost ~]# mkdir dir1(777-022=755)
[root@localhost ~]# ll -d dir1 file1
drwxr-xr-x. 2 root root 6 Oct 11 17:56 dir1
-rw-r–r--. 1 root root 0 Oct 11 17:56 file1
2.修改shell umask值(临时)
[root@localhost ~]# umask 000(设置umask值为000)
[root@localhost ~]# touch file2
[root@localhost ~]# mkdir dir2
[root@localhost ~]# ll -d dir2 file2
drwxrwxrwx. 2 root root 6 Oct 11 18:04 dir2
-rw-rw-rw-. 1 root root 0 Oct 11 18:04 file2
3.修改shell umask值(永久)
vi /etc/profile
59 if [ $UID -gt 199 ] && [ “/usr/bin/id -gn
” = “/usr/bin/id -un
” ]; then
60 umask 002
61 else
62 umask 022(修改此值)
63 fi
[root@localhost ~]# source /etc/profile(立即生效)
4.通过umask绝对新建用户home目录的权限
[root@localhost ~]# vi /etc/login.defs
UMASK 077(修改)
[root@localhost ~]# useradd linux
[root@localhost ~]# ll -d /home/linux/
drwx------+ 2 linux linux 76 Oct 11 18:15 /home/linux/
4.高级权限suid,sgid,sticky
文件权限管理之:高级权限
高级权限的类型:
suid 4
sgid 2
sticky 1 粘滞位
SUID是一种对二进制程序进行设置的特殊权限,可以让二进制程序的执行者临时拥有属主的权限(仅对拥有执行权限的二进制程序有效)
如果属主原本有执行权限,显示为小写s、如果没有,显示为大写S
SGID主要实现以下两种功能:
a.让执行者临时拥有属组的权限(对拥有执行权限的二进制程序进行设置)
b.在某个目录中创建的文件自动继承该目录的用户组(只可以对目录进行设置)
Sticky特殊权限位可确保用户只能删除自己的文件。而不能删除其他用户的文件。换句话说,当对某个目录设置了SBIT粘滞位权限后,那么该目录中的文件就只能被其所有者执行删除操作了
如果其原本有执行权限,显示为小写t。如果没有,则显示为大写T
系统上的/tmp和/var/tmp目录默认均有sticky权限
设置特殊权限
a.字符
chmod u+s file
chmod g+s dir
chmod o+t dir
b.数字
chmod 4777 file(SGID)
chmod 2777 file(SUID)
chmod 1777 file(Sticky)
chmod 7777 file(SGID,SUID,Sticky)
示例1:suid普通用户通过suid提权<针对文件>
[root@localhost ~]# mkdir /home/dir1
[root@localhost ~]# chmod 777 /home/dir1
测试:user1在/home/dir1建立文件, user2尝试删除(可以删除)
[root@localhost ~]# chmod o+t /home/dir1
[root@localhost ~]# ll -d /home/dir1
rwxrwxrwt 2 root root 4096 09-02 02:26 /home/dir1(现在就不可以删除)
示例2:suid普通用户通过suid提权<针对文件>
在进程文件(二进制,可执行)上增加suid权限
[root@localhost ~]# ll -d /root /root/file1
dr-xr-x—. 4 root root 4096 Oct 11 19:53 /root
-rw-r–r--. 1 root root 0 Oct 11 19:53 /root/file1
[alice@localhost ~]$ more /root/file1(权限不够)
[root@localhost ~]# whereis more
more: /usr/bin/more /usr/share/man/man1/more.1.gz
[root@localhost ~]# chmod u+s /usr/bin/more(此时more拥有root权限)
[alice@localhost ~]$ more /root/file1
示例3:sgid新建文件继承目录属组<针对目录>
[root@localhost ~]# mkdir /root/dir1
[root@localhost ~]# groupadd qq
[root@localhost ~]# chgrp qq /root/dir1
[root@localhost ~]# ll -d /root/dir1/
drwxr-xr-x. 2 root qq 6 Oct 11 20:32 /root/dir1/
[root@localhost ~]# chmod g+s /root/dir1/(让其下的创建的目录属组都是qq)
[root@localhost ~]# ll -d /root/dir1/
drwxr-sr-x. 2 root qq 6 Oct 11 20:32 /root/dir1/
[root@localhost ~]# touch /root/dir1/file1
[root@localhost ~]# ll -d /root/dir1/file1
-rw-r–r--. 1 root qq 0 Oct 11 20:33 /root/dir1/file1
[root@localhost ~]# chmod g-s /root/dir1/
[root@localhost ~]# touch /root/dir1/file2
[root@localhost ~]# ll -d /root/dir1/file2
-rw-r–r--. 1 root root 0 Oct 11 20:33 /root/dir1/file2
5.文件属性chattr
文件权限管理之:文件属性
注:设置文件属性(权限),针对所有用户,包括root
chattr命令用于设置文件的隐藏权限,格式为:“chattr [参数] 文件”。如果想要把某个隐藏功能添加到文件上,则需要在命令后面追加“+参数”,如果想要把某个隐藏功能移出文件,则需追加“-参数“。
lsattr命令用于显示文件的隐藏权限,格式为“lsattr [参数] 文件”。在linux系统中,文件的隐藏权限必须用lsattr命令来查看,平时使用的ls之类的看不出来
[root@localhost ~]# touch file1
[root@localhost ~]# mkdir dir1
[root@localhost ~]# echo “123” >> file1
[root@localhost ~]# chattr +i file1
[root@localhost ~]# chattr +i dir1/
[root@localhost ~]# echo “hello” >> file1(没权限)
[root@localhost ~]# touch dir1/file2(没权限)
[root@localhost ~]# lsattr file1
----i----------- file1
[root@localhost ~]# chattr -i file1
[root@localhost ~]# echo “456” >> file1(可以加内容)
[root@localhost ~]# chattr -i dir1
[root@localhost ~]# touch dir1/file2(可以创建)