linux服务器上有严格的权限等级,如果权限过高导致误操作会增加服务器的风险。所以对于了解linux系统中的各种权限及要给用户,服务等分配合理的权限十分重要。
一、基本权限UGO
1.1设置权限
1.2权限影响
二、权限掩码
三、高级权限
3.1高级权限的类型
3.2设置特殊权限
提权:目前两种给普通用户提权手段
1、放开所有命令使用权
2、放开个别命令使用权
3、访问控制权限
总结
四、隐藏权限
4.1、隐藏权限的添加与查看
4.2、取消权限
一、基本权限 UGO
文件权限设置: 可以赋于某个用户或组 能够以何种方式 访问某个文件
权限对象: 属主------->u ----->r w x 所有者 属组------->g ----->r w x 其他人----->o------>r w x 基本权限类型: 读(read):r ---->4 写(write):w ---->2 执行(exec):x ---->1
1.1设置权限
chown:改变文件或目录的所属主以及所属组
chmod:为文件或目录设置访问权限更改文件的属主(拥有者)、属组 (所属组) [root@Marve1ous ~]# chown alice.hr file1.txt #修改属主、属组 [root@Marve1ous ~]# chown tom file1.txt #修改属主 [root@Marve1ous ~]# chown .it file1.txt #只改属组 [root@Marve1ous ~]# chown -R alice.hr dir1 #递归修改---针对目录
更改权限:使用符号
[root@Marve1ous ~]# chmod u+x file1.txt #属主增加执行 [root@Marve1ous ~]# chmod a=rwx file1.txt #所有人等于读写执行 [root@Marve1ous ~]# chmod a=- file1.txt #所有人都没有权限 [root@Marve1ous ~]# chmod ug=rw,o=r file1.txt #属主属组等于读写,其他人只读 [root@Marve1ous ~]# ll -rw-rw-r--. 1 tom it 0 Nov 1 15:30 file1.txt
使用数字
r (read)-----> 4
w (write)----> 2
x (exec)----> 1
[root@Marve1ous ~]# chmod 644 file1.txt [root@Marve1ous ~]# ll file1.txt -rw-r--r--. 1 tom it 0 Aug 23 15:30 file1.txt [root@Marve1ous ~]# chmod 755 file1.txt [root@Marve1ous ~]# ll -rwxr-xr-x 1 root root 0 Aug 23 22:40 file1.txt [root@Marve1ous ~]# chmod 521 file1.txt [root@Marve1ous ~]# ll -r-x-w---x 1 root root 0 Aug 23 22:40 file1.txt
1.2权限影响
r、w、x权限对文件和目录的意义
注意事项
文件: x 权限小心给予
目录: w 权限小心给予对文件: r ----cat vim tail head more less w ---vi、vim echo > >> x ---- bash /dir/file 注意:如果没有x权限,root用户都无法执行 chmod a-x file ./file 其他人对文件只有写权限的时候 对目录: r -----ls 如果只有r权限,可以看到目录下的内容,但是有报错信息 w -----touch、rm 可以对目录下的文件创建和删除但是要有执行权限 x ---- cd 进入目录
二、权限掩码
umask权限掩码:控制用户创建文件和目录的默认权限
#root用户默认最高权限 目录---777 文件---666 #查看umask [root@Marve1ous ~]# umask 0022 root账户默认 0002 普通用户默认 #通过计算得出root用户创建目录和文件的权限为: 也是现在root用户创建完目录和文件的默认权限: 目录:755 文件:644 #修改umask [root@Marve1ous ~]# umask 0111
三、高级权限
高级权限 suid普通文件 sgid,sticky 目录
这三个权限不能给到同一个文件3.1高级权限的类型
suid ==== 4 提权 (只对二进制命令文件生效,其他不管用)
sgid ==== 2 继承属组权限 (只能对目录设置)
sticky === 1 (t权限) 权限控制3.2设置特殊权限
a、字符---语法: chmod u+s file 提权 chmod g+s dir 只能对目录设置 chmod o+t dir 即使有删除权限,也不能删除 b、数字 chmod 4777 file chmod 2770 dir chmod 1770 dir
SUID
1、SUID权限仅对命令文件(二进制文件)有效;
2、执行者将具有该程序拥有者(owner)的权限。示例
suid 普通用户通过suid提权 <针对文件> 在进程文件(二进制,可执行的命令文件)上增加suid权限 [root@Marve1ous ~]# chmod u+s /usr/bin/cat [root@Marve1ous ~]# chmod u+s /usr/bin/rm [root@Marve1ous ~]# su - alice Last login: Wed Nov 6 17:40:40 CST 2019 on pts/0 [alice@Marve1ous ~]$ cat /root/file1.txt 123 [alice@Marve1ous ~]$ rm -rf /root/file1.txt
SGID
把s放到文件的所属用户组的x位置上的话,就是SGID。那么SGID的功能是什么呢?和SUID一样,只是SGID是继承改程序的所属组。SGID主要用在目录上-----如果用户在此目录下具有w权限的话,使用者在此目录下建立新文件,则创建的这个文件的群组与此目录的群组相同。
示例 [root@Marve1ous ~]# mkdir /opt/dir1 #创建目录 [root@Marve1ous ~]# groupadd hr #创建一个组 [root@Marve1ous ~]# chmod 775 /opt/dir1/ #设置权限 [root@Marve1ous ~]# ll -d /opt/dir1/ drwxrwxr-x. 2 root root 6 Nov 6 21:26 /opt/dir1/ [root@Marve1ous ~]# chown .hr /opt/dir1/ #设置属组 [root@Marve1ous ~]# chmod g+s /opt/dir1/ #设置sgid [root@Marve1ous ~]# ll -d /opt/dir1/ drwxrwsr-x. 2 root hr 6 Nov 6 21:26 /opt/dir1/ [root@Marve1ous ~]# touch /opt/dir1/a.txt [root@Marve1ous ~]# ll /opt/dir1/a.txt -rw-r--r--. 1 root hr 0 Nov 6 21:33 /opt/dir1/a.txt [root@Marve1ous ~]# chmod o+w /opt/dir1/ -R [root@Marve1ous ~]# su - alice Last login: Wed · 6 21:34:59 CST 2023 on pts/2 [alice@Marve1ous ~]$ touch /opt/dir1/b.txt [alice@Marve1ous ~]$ ll /opt/dir1/b.txt -rw-rw-r--. 1 alice hr 0 Nov 6 21:35 /opt/dir1/b.txt
Stiky Bit这个就是针对others来设置的了,和上面两个一样,只是功能不同而已。
SBIT(Sticky Bit)目前只针对目录有效,对于目录的作用是:当用户在该目录下建立文件或目录时,仅有自己与 root才有权力删除。[root@Marve1ous ~]# cd /home/ [root@Marve1ous home]# mkdir dir2 [root@Marve1ous home]# chmod 757 dir2/ [root@Marve1ous home]# chmod o+t dir2/ [root@Marve1ous home]# ll -d dir2/ drwxr-xrwt. 2 root root 52 Oct 31 16:49 dir2/ [root@Marve1ous home]# useradd jack #创建用户 [root@Marve1ous home]# su - alice Last login: Wed Nov 6 21:48:12 CST 2019 on pts/2 [alice@Marve1ous ~]$ touch /home/dir2/alice.txt #用户alice创建文件 [alice@Marve1ous ~]$ exit logout [root@Marve1ous home]# su - jack Last login: Wed Nov 6 21:48:36 CST 2019 on pts/2 [jack@Marve1ous ~]$ touch /home/dir2/jack.txt #用户jack创建文件 [jack@Marve1ous ~]$ rm -rf /home/dir2/alice.txt rm: cannot remove ‘/home/dir2/alice.txt’: Operation not permitted 测试jack删除alice创建的文件,无法删除
提权:目前两种给普通用户提权手段
suid: 基本针对所有用户,任何用户在执行有suid权限的程序时(例如/usr/bin/rm),都是以root身份在执行。
sudo: 有针对性,例如针对某个用户以能够以root的身份执行某些命令。1、放开所有命令使用权
[root@linux-server ~]# visudo #打开配置文件 90 ## 91 ## Allow root to run any commands anywhere 92 root ALL=(ALL) ALL 93 jack ALL=(ALL) NOPASSWD: ALL #添加内容 94 ## Allows members of the 'sys' group to run networking, software, 测试 [root@linux-server ~]# su - jack Last login: Wed Nov 6 22:04:46 CST 2019 on pts/2 [jack@linux-server ~]$ sudo mkdir /test1 #配置解释: jack表示用户名 ALL=(ALL) #表示sudo可以切换到任意用户登陆所有计算机执行命令 NOPASSWD: ALL #表示所有命令都可以作为root身份执行时不需要root用户的密码
2、放开个别命令使用权
[root@Marve1ous ~]# visudo | vim /etc/sudoers 91 ## Allow root to run any commands anywhere 92 root ALL=(ALL) ALL lisi ALL=(ALL) ALL 93 jack ALL=(ALL) NOPASSWD:ALL 94 alice ALL=(ALL) NOPASSWD:/usr/bin/mkdir, /usr/bin/rm, /usr/bin/touch 95 96 ## Allows members of the 'sys' group to run networking, software, %wheel ALL=(ALL) ALL 将普通用户加入到wheel组 usermod -G wheel zhangsan gpasswd -d zhangsan wheel 测试: [root@Marve1ous~]# su - alice Last login: Fri Jul 24 00:52:13 CST 2020 on pts/1 [alice@localhost ~]$ touch /file touch: cannot touch ‘/file’: Permission denied [alice@localhost ~]$ sudo touch /file
3、访问控制权限
setfacl 针对个人设置权限 一个人查看一个文件的权限
getfacl 查看权限文件a.txt [root@xiaoming tmp]# ll -rw-r--r-- 1 root root 0 11月 26 11:25 a.txt 如果希望xiaoming用户可以rwx操作/tmp/a.txt文件 那么我们可以这样做 setfacl -m u:xiaoming:rwx /tmp/a.txt -m 设置facl权限 u: 用户,也可以指定组 g xiaoming: 需要指定的用户 rwx: 权限 getfacl /tmp/a.txt [root@xiaoming /tmp]# getfacl a.txt # file: a.txt # owner: root # group: root user::rw- user:xiaoming:rwx group::r-- mask::rwx other::r-- 我们可以看到 xiaoming 拥有了对该文件的rwx权限 收回权限 方法1、 setfacl -m u:xiaoming:--- a.txt 方法2、 setfacl -x u:xiaoming a.txt 我们也可以设置该文件为所有人所有组访问 setfacl -m u::rwx a.txt
总结
提权方法1:给某个命令提权
chmod u+s /usr/bin/rm #“命令”缺点:一旦提权,所有用户都可以像root用户一样执行命令
提权方法2:给某个用户某些命令的权限
visudo缺点:被提权的用户将会拥有某些或者全部root用户的权限
提权方法3:给某个用户设置针对某个文件的特殊权限
setfacl
四、隐藏权限
隐藏权限防止root误删除
4.1、隐藏权限的添加与查看
查看:lsattr
[root@Marve1ous ~]# lsattr +文件名
i:即Immutable,系统不允许对这个文件进行任何的修改
a:即Append Only,系统只允许在这个文件之后追加数据,不允许任何覆盖这个文件
A:即Atime,告诉系统不要修改对这个文件的最后访问时间设置:chattr
[root@Marve1ous ~]# chattr +a file1 #不允许修改,只允许追加 [root@Marve1ous ~]# chattr +i file2 #不允许做任何操作 [root@Marve1ous ~]# chattr +A file3 [root@Marve1ous ~]# lsattr file1 file2 file3 -----a---------- file1 ----i----------- file2 -------A-------- file3
4.2、取消权限
[root@Marve1ous ~]# chattr -a file1 [root@Marve1ous ~]# chattr -i file2