我们linux服务器上有严格的权限等级,如果权限过高导致误操作会增加服务器的风险。所以对于了解linux系统中的各种权限及要给用户,服务等分配合理的权限十分重要
1.基本权限 UGO
=====================================================
文件权限设置: 可以赋于某个用户或组 能够以何种方式 访问某个文件

权限对象:
属主------->u
属组------->g
其他人------>o
基本权限类型:
读(read):r ---->4
写(write):w ---->2
执行: x(exec) ----->1
1.1.设置权限 chown:改变文件或目录的所属主以及所属组
chmod:为文件或目录设置访问权限
更改文件的属主(所有者)、属组 (所属组)
chown:
[[email protected] ~]# chown alice.hr file1.txt //修改属主、属组
[[email protected] ~]# chown tom file1.txt //修改属主
[[email protected] ~]# chown .it file1.txt //只改属组
[[email protected] ~]# chown -R alice.hr dir1 //递归修改---针对目录
更改权限
a. 使用符号[[email protected] ~]# chmod u+x file1.txt //属主增加执行
[[email protected] ~]# chmod a=rwx file1.txt //所有人等于读写执行
[[email protected] ~]# chmod a=- file1.txt //所有人都没有权限
[[email protected] ~]# chmod ug=rw,o=r file1.txt //属主属组等于读写,其他人只读
[[email protected] ~]# ll
-rw-rw-r--. 1 tom it 0 Nov 1 15:30 file1.txt
b.使用数字 [[email protected] ~]# chmod 644 file1.txt
[[email protected] ~]# ll file1.txt
-rw-r--r--. 1 tom it 0 Nov 1 15:30 file1.txt
[[email protected] ~]# chmod 755 file1.txt
[[email protected] ~]# ll
-rwxr-xr-x 1 root root 0 Jul 23 22:40 file1.txt
[[email protected] ~]# chmod 521 file1.txt
[[email protected] ~]# ll
-r-x-w---x 1 root root 0 Jul 23 22:40 file1.txt
权限掩码
umask 用户掩码控制用户创建文件和目录的默认权限
root用户默认权限
目录777 文件666
查看umask
[[email protected] ~]#umask
0022 root账户默认
0002 普通用户默认
修改umask
[[email protected] ~]#umask 0111
通过计算得出root用户创建目录和文件的权限为
r、w、x权限对文件和目录的意义 对文件:
r----cat
w ---vi、vim
x ---- bash /dir/file
对目录:
r ---ls
w -----touch、rm
x ---- cd
2.2.rwx对文件的影响实战案例1:rwx对文件的影响
[[email protected] ~]# vim /home/file1
date
[[email protected] ~]# ll /home/file1
-rw-r--r--. 1 root root 5 Nov 3 15:19 /home/file1
[[email protected] ~]# su - alice #切换普通用户
[[email protected] ~]$ cat /home/file1
date
[[email protected] ~]$ /home/file1 #执行文件
-bash: /home/file1: Permission denied
[[email protected] ~]$ exit
logout
[[email protected] ~]# chmod o+x /home/file1
[[email protected] ~]$ /home/file1
Sun Nov 3 15:26:21 CST 2019
[[email protected] ~]# chmod o+w /home/file1
[[email protected] ~]$ vim /home/file1
date
123
ls
2.3.rwx对目录的影响 实战案例2:对目录没有w,对文件有rwx
[[email protected] ~]# mkdir /dir10
[[email protected] ~]# touch /dir10/file1
[[email protected] ~]# chmod 777 /dir10/file1
[[email protected] ~]# ll -d /dir10/
drwxr-xr-x. 2 root root 19 Nov 3 15:37 /dir10/
[[email protected] ~]# ll /dir10/file1
-rwxrwxrwx. 1 root root 0 Nov 3 15:37 /dir10/file1
[[email protected] ~]# vim /dir10/file1
jack
[[email protected] ~]# su - alice
Last login: Sun Nov 3 15:28:06 CST 2019 on pts/0
[[email protected] ~]$ cat /dir10/file1
jack
[[email protected] ~]$ rm -rf /dir10/file1 #权限不够
rm: cannot remove ‘/dir10/file1’: Permission denied
[[email protected] ~]$ touch /dir10/file2 #权限不够
touch: cannot touch ‘/dir10/file2’: Permission denied
实战案例3:对目录有w,对文件没有任何权限
[[email protected] ~]# chmod 777 /dir10/
[[email protected] ~]# chmod 000 /dir10/file1
[[email protected] ~]# ll -d /dir10/
drwxrwxrwx. 2 root root 19 Nov 3 15:38 /dir10/
[[email protected] ~]# ll /dir10/file1
----------. 1 root root 5 Nov 3 15:38 /dir10/file1
[[email protected] ~]# su - alice #切换普通用户
Last login: Sun Nov 3 15:38:53 CST 2019 on pts/0
[[email protected] ~]$ cat /dir10/file1
cat: /dir10/file1: Permission denied #没有权限
[[email protected] ~]$ rm -rf /dir10/file1
[[email protected] ~]$ touch /dir10/file2
小结
对目录有w权限,可以在目录中创建新文件,可以删除目录中的文件(跟文件权限无关)
注意事项
文件: x 权限小心给予
目录: w 权限小心给予
高级权限
高级权限 suid,sgid,sticky
问题1: 为什么会失败!
[[email protected] ~]# chown root.root /root/file1.txt
[[email protected] ~]# vim /root/file1.txt
123
[[email protected] ~]# ll /root/file1.txt
-rw-r--r--. 1 root root 0 Nov 1 15:30 /root/file1.txt
[[email protected] ~]# su - alice
Last login: Sun Nov 3 15:57:41 CST 2019 on pts/0
[[email protected] ~]$ cat /root/file1.txt
cat: /root/file1.txt: Permission denied
1.1.高级权限的类型 suid ==== 4 提权 (只对二进制命令文件生效,其他不管用)
sgid ==== 2 组权限继承 (只能对目录设置)
sticky == 1 (t权限) 权限控制
1.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 普通用户通过suid提权 <针对文件>
在进程文件(二进制,可执行的命令文件)上增加suid权限
[[email protected] ~]# chmod u+s /usr/bin/cat
[[email protected] ~]# chmod u+s /usr/bin/rm
[[email protected] ~]# su - alice
Last login: Wed Nov 6 17:40:40 CST 2019 on pts/0
[[email protected] ~]$ cat /root/file1.txt
123
[[email protected] ~]$ rm -rf /root/file1.txt
Set UID 那么这个特殊权限的特殊性的作用是什么呢?
1、SUID权限仅对命令文件(二进制文件)有效;
2、执行者将具有该程序拥有者(owner)的权限。
取消提权 [[email protected] ~]# ll /usr/bin/rm
-rwsr-xr-x. 1 root root 62864 Nov 6 2016 /usr/bin/rm
此时一旦给rm加上suid权限之后,普通用户相当于root用户。(即提权)
[[email protected] ~]# chmod u-s /usr/bin/rm #取消提权
案例二 首先创建一个用户组,两个用户进行这三个案例操作
Set GID把s放到文件的所属用户组的x位置上的话,就是SGID。那么SGID的功能是什么呢?和SUID一样,只是SGID是获得该程序所属用户组的权限。
SGID主要用在目录上-----如果用户在此目录下具有w权限的话,使用者在此目录下建立新文件,则创建的这个文件的群组与此目录的群组相同。
案例
[[email protected] ~]# mkdir /opt/dir1 #创建目录
[[email protected] ~]# groupadd hr #创建一个组
[[email protected] ~]# chmod 775 /opt/dir1/ #设置权限
[[email protected] ~]# ll -d /opt/dir1/
drwxrwxr-x. 2 root root 6 Nov 6 21:26 /opt/dir1/
[[email protected] ~]# chown .hr /opt/dir1/ #设置属组
[[email protected] ~]# chmod g+s /opt/dir1/ #设置sgid
[[email protected] ~]# ll -d /opt/dir1/
drwxrwsr-x. 2 root hr 6 Nov 6 21:26 /opt/dir1/
[[email protected] ~]# touch /opt/dir1/a.txt
[[email protected] ~]# ll /opt/dir1/a.txt
-rw-r--r--. 1 root hr 0 Nov 6 21:33 /opt/dir1/a.txt
[[email protected] ~]# chmod o+w /opt/dir1/ -R
[[email protected] ~]# su - alice
Last login: Wed Nov 6 21:34:59 CST 2019 on pts/2
[[email protected] ~]$ touch /opt/dir1/b.txt
[[email protected] ~]$ ll /opt/dir1/b.txt
-rw-rw-r--. 1 alice hr 0 Nov 6 21:35 /opt/dir1/b.txt
Sticky Bit 这个就是针对others来设置的了,和上面两个一样,只是功能不同而已。
SBIT(Sticky Bit)目前只针对目录有效,对于目录的作用是:当用户在该目录下建立文件或目录时,仅有自己与 root才有权力删除。
案例
[[email protected] ~]# cd /home/
[[email protected] home]# mkdir dir2
[[email protected] home]# chmod 757 dir2/
[[email protected] home]# chmod o+t dir2/
[[email protected] home]# ll -d dir2/
drwxr-xrwt. 2 root root 52 Oct 31 16:49 dir2/
[[email protected] home]# useradd jack #创建用户
[[email protected] home]# su - alice
Last login: Wed Nov 6 21:48:12 CST 2019 on pts/2
[[email protected] ~]$ touch /home/dir2/alice.txt #用户alice创建文件
[[email protected] ~]$ exit
logout
[[email protected] home]# su - jack
Last login: Wed Nov 6 21:48:36 CST 2019 on pts/2
[[email protected] ~]$ touch /home/dir2/jack.txt #用户jack创建文件
[[email protected] ~]$ rm -rf /home/dir2/alice.txt
rm: cannot remove ‘/home/dir2/alice.txt’: Operation not permitted
测试jack删除alice创建的文件,无法删除
1.3.目前两种给普通用户提权手段: sudo: 有针对性,例如针对某个用户以能够以root的身份执行某些命令。
suid: 基本针对所有用户,任何用户在执行有suid权限的程序时(例如/usr/bin/rm),都是以root身份在执行。