Linux用户权限
我们linux服务器上有严格的权限等级,如果权限过高导致误操作会增加服务器的风险。所以对于了解linux系统中的各种权限以及要给用户,服务等分配合理的权限十分重要!
一、基本权限UGO
一个文件权限包含三个对象:
属主---->u
属组---->g
其他人---->o
基本权限类型:
读(read):r —> 4
写(white):w ---->2
执行(exec):----->1
例如:
r w x rw- r-- alice hr file1.txt
属主权限 属组权限 其他人权限 属主 属组 文件
对于该文件,属主的权限有读写执行,属组的权限有读和写,而其他人的权限只有读。文件的属主是alice 所属组为hr组 文件名为fiel1.txt
1.设置权限
chown:改变文件或目录的所属主以及所属组
chmod:为文件或目录设置访问权限
更改文件的属主(所有者)、属组(所属组)
chown:
[root@xifeng ~]# ll file1.txt
-rw-r--r--. 1 root root 0 11月 11 20:57 file1.txt
[root@xifeng ~]# chown tom.hr file1.txt
[root@xifeng ~]# ll file1.txt
-rw-r--r--. 1 tom hr 0 11月 11 20:57 file1.txt
更改文件的权限:
chmod:
-rw-r--r--. 1 tom hr 0 11月 11 20:57 file1.txt
[root@xifeng ~]# chmod u+x file1.txt //属主增加执行权
[root@xifeng ~]# ll file1.txt
-rwxr--r--. 1 tom hr 0 11月 11 20:57 file1.txt
[root@xifeng ~]# chmod a=rwx file1.txt //所有人都有读写执行权
[root@xifeng ~]# ll file1.txt
-rwxrwxrwx. 1 tom hr 0 11月 11 20:57 file1.txt
[root@xifeng ~]# chmod a=- file1.txt //所有人都没有权限
[root@xifeng ~]# ll file1.txt
----------. 1 tom hr 0 11月 11 20:57 file1.txt
[root@xifeng ~]# chmod ug=rw,o=r file1.txt //属主属组有读写权,其他人只有读的权限
[root@xifeng ~]# ll file1.txt
-rw-rw-r--. 1 tom hr 0 11月 11 20:57 file1.txt
使用数字修改权限
[root@xifeng ~]# ll file1.txt
-rw-rw-r--. 1 tom hr 0 11月 11 20:57 file1.txt
[root@xifeng ~]# chmod 644 file1.txt
[root@xifeng ~]# ll file1.txt
-rw-r--r--. 1 tom hr 0 11月 11 20:57 file1.txt
[root@xifeng ~]# chmod 755 file1.txt
[root@xifeng ~]# ll file1.txt
-rwxr-xr-x. 1 tom hr 0 11月 11 20:57 file1.txt
权限掩码
umask 用户掩码
控制用户创建文件或目录的默认权限
root用户默认权限 目录是777文件是666
0022 root账户默认
0002 普通用户默认
修改umask
[root@xifeng ~]# umask 0111
通过计算得出root用户创建目录和文件的权限为
目录:666 //777-111
文件:555 //666-111
r、w、x、权限对文件和目录的意义
对文件:
r----cat
w----vi、vim
x----bash、文件绝对路径(即执行文件)
对目录:
r----ls
w----touch、rm
x-----cd
切记!!!!
对于目录来说w权限很重要,因为有了w权限可以对目录内的文件进行创建,删除且于文件的权限无关,要小心给予。
对于文件来说x权限很重要,因为x权限给文件后,文件就是一个可执行文件,如果不清楚文件内的内容,很容易误执行。故文件的x权限应小心给予。
普通用户提权
1.sudo:有针对性,例如针对某个用户能够以root用户来执行某些命令
2.suid:基本针对所有用户,是对命令的提权,任何用户在执行有suid权限的程序时(例如/usr/bin/rm),都是以root用户在执行。
1.sudo(使用时在命令前加sudo)
放开所有命令使用权
命令:visudo
[root@xifeng ~]# visudo #打开配置文件
(可以使用shift+:打开进入尾行模式输入set nu即显示行号。)在大约90行的位置添加内容
90 ##
91 ## Allow root to run any commands anywhere
92 root ALL=(ALL) ALL
93 dingxi ALL=(ALL) NOPASSWD:ALL
94 tom ALL=(ALL) NOPASSWD:ALL #添加内容
94 ## Allows members of the 'sys' group to run networking, software,
测试
[root@xifeng ~]# su tom
[tom@xifeng root]$
[tom@xifeng root]$ sudo mkdir /test1
放开个别权限:
[root@xifeng ~]# visudo
91 ## Allow root to run any commands anywhere
92 root ALL=(ALL) ALL
93 dingxi ALL=(ALL) NOPASSWD:ALL
94 tom ALL=(ALL) NOPASSWD:/usr/bin/mkdir, /usr/bin/rm, /usr/bin/touch
95
96 ## Allows members of the 'sys' group to run networking, software,
测试:
[root@xifeng ~]# su - tom
[tom@xifeng root]$touch /file
touch: cannot touch ‘/file’: Permission denied
[tom@xifeng root]$ sudo touch /file
2.suid对命令提权
只对二进制文件生效,其他不管用
只需要在进程文件(二进制,可执行的命令文件)上增加suid权限
[root@xifeng ~]# chmod u+s /usr/bin/cat
[root@xifeng ~]# chmod u+s /usr/bin/rm
[root@xifeng ~]# su - tom
[tom@xifeng root]$ cat /root/file1.txt
123
[tom@xifeng root]$ rm -rf /root/file1.txt
[root@xifeng ~]# chmod u-s /usr/bin/rm #取消提权