[TOC]

权限管理

什么是权限?

通俗来说权限就是某一件事情是否允许被某个人做。其中,某一件事情就对应Linux中的各种文件,而人就是指使用Linux的用户。权限就是用来约束一个人或者一个群体的。

Linux之下一切皆文件,所以Linux下的事物,其实就是一个一个文件。Linux下的权限其实就是 , 不同种类的用户 对于一个文件的 不同权限。

Linux下文件的权限:

  • 属主 user
  • 属组 group
  • 其他人 other

Linux下用户的权限:

  • r(read) 可读权限 ,4
  • w(write) 可写权限,2
  • x(execute) 可执行权限,1
  • -:没有权限,0

(注意:目录必须拥有 x 权限,否则无法查看其内容)

查看权限

## 替换命令
tr
## 使用tr命令,获取文件的数字权限
[root@web mnt]# ll
total 0
-rw-r--r-- 1 root root 0 Jul  5 15:52 1.txt
drwxr-xr-x 2 root root 6 Jul  5 15:52 hg
[root@web mnt]# ll|tr 'rwx-' '4210'|sed -n '2p'|awk -F '' '{print $2+$3+$4""$5+$6+$7""$8+$9+$10}'
644

## stat 查看 取出文件内容
[root@web mnt]# stat 1.txt 
  File: ‘1.txt’
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 803h/2051d  Inode: 675         Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2023-07-05 15:52:25.075064617 +0800
Modify: 2023-07-05 15:52:25.075064617 +0800
Change: 2023-07-05 15:52:25.075064617 +0800
 Birth: -
[root@web mnt]# stat 1.txt|awk -F '[(/]' 'NR==4{print $2}'
0644

### 正规取出文件数字权限的命令
[root@web mnt]# stat -c %a 1.txt 
644

## 权限位
属主权限位:u user
属组权限位:g group
其他用户权限位:o other

chmod修改文件权限

#修改文件权限
chmod:change mode

## 语法
chmod 权限 文件名

## 选项
-R:递归授权

#选项
chmod u=rwx 文件            #赋予用户对文件拥有读、写、执行的权限
chmod g=rwx 文件            #赋予组对文件拥有读、写、执行的权限
chmod o=rwx 文件            #赋予其他人对文件拥有读、写、执行的权限
chmod u-w 文件              #去掉用户对文件写的权限
chmod g-r 文件              #去掉组对文件读的权限
chmod o-x 文件              #去掉其他人对文件执行的权限
chmod u+r 文件              #赋予用户对文件拥有读的权限
chmod o+w 文件              #赋予其他人对文件拥有写的权限
chmod g+x 文件              #赋予组对文件拥有执行的权限
chmod number 文件           #给文件赋予数字相对的权限

#实例
chmod u=rwx 1.txt            #赋予用户对文件1.txt拥有读、写、执行的权限
chmod u-w 1.txt             #去掉用户对文件1.txt写的权限
chmod g+x 1.txt              #赋予组对文件1.txt拥有执行的权限
chmod 777 1.txt              #给文件赋予777的权限

权限对文件的影响

r:文件只能读取
w:任何编辑命令都可以写入内容,但是vim读取不出来,就会覆盖原文件内容
x:对于普通用户来说,还是什么都做不了,必须配合r权限才可以执行

rw:可读,可写,正常的使用vim编辑,但是不能执行
rx:可读,可执行,但是不能编辑
wx:可写,不可读,不可执行
rwx:可读,可写,可执行

权限对目录的影响

## 目录在只有单权限的情况下,什么都做不了
r:能看见目录下的文件,但是看不见文件的详细属性
w:什么都做不了
x:什么都做不了

rx:才能查看目录下的文件
wx:读取不了目录里的文件,但是创建文件,创建目录,删除文件,删除目录,不能用*代替
rwx:可以做任何操作,可以创建,删除,移动,复制文件或目录

修改文件属主和属组

#修改文件的属主和属组
chown:change owner
## 语法
chown [选项]... 属主.属组 文件名
chown [选项]... 属主:属组 文件名
## 选项
-R:递归修改属组和属主(将该目录下的文件也一起修改)

#选项 
chown 属主:属组 文件             #修改文件的属主和属组
chown +R 属主:组 文件            #修递归改文件的属主和属组
chown 属主 文件                 #修改文件的属主
chown :属组 文件                #修改文件的属组

## 只修改属主
[root@web mnt]# chown hg 1.txt 
[root@web mnt]# ll
total 0
-rw-r--r-- 1 hg   root 0 Jul  5 15:52 1.txt
## 只修改属组
[root@web mnt]# chown :hg 1.txt 
[root@web mnt]# ll
total 0
-rw-r--r-- 1 hg   hg   0 Jul  5 15:52 1.txt

特殊权限

SerUID

# 1.普通用户可不可以修改密码?
可以,修改自己的密码

# 2./etc/shadow文件的作用?
存储用户密码的文件

# 3./etc/shadow文件的权限?
[root@web mnt]# ll /etc/shadow
---------- 1 root root 713 Mar 27  2021 /etc/shadow

# 4.普通用户,是否可以修改/etc/shadow文件?
不可以,/etc/shadow文件,对于普通用户没有任何权限,所以不能读取,也不能写入内容

# 5.那么普通用户,为什么可以修改密码?
1)因为使用了passwd这个命令
2)passwd命令在属主权限位上,原本是x权限,变成了s权限
3)s权限在属主权限位,又叫做SetUID权限,SUID
4)作用:普通用户在使用有SUID权限的文件或命令时,会以该文件的属主身份去执行该命令

SetUID特性

# 1.原本属主位上如果有x权限,则SetUID为小写s
[root@web mnt]# ll hg -d
drwxr-xr-x 2 root root 6 Jul  5 15:52 hg
[root@web mnt]# chmod u+s hg
[root@web mnt]# ll hg -d
drwsr-xr-x 2 root root 6 Jul  5 15:52 hg

# 2.原本属主位上如果没有x权限,则SetUID为大写S
[root@web mnt]# chmod 000 hg
[root@web mnt]# ll hg -d
d--S------ 2 root root 6 Jul  5 15:52 hg

# 3.授权方式
- chmod u+s 文件名
- chmod 4xxx 文件名
[root@web mnt]# chmod u-s hg/
[root@web mnt]# ll hg -d
d--------- 2 root root 6 Jul  5 15:52 hg

# 4.SetUID权限的数字是4000
[root@web mnt]# mkdir -m 000 yyds
[root@web mnt]# ll
total 0
d--------- 2 root root 6 Jul  5 17:26 yyds
[root@web mnt]# chmod 4777 yyds
[root@web mnt]# ll  yyds/ -d
drwsrwxrwx 2 root root 6 Jul  5 17:26 yyds/

SetGID

#SGID(setGID):将目录设置为SGID权限后,如果在该目录下创建文件,都与该目录的所属组保持一致

#语法
chmod g+s file         #给目录增加特殊权限sgid
chmod g=s file
chmod 2000 file
chmod g-s file         #给目录去除特殊权限sgid

[root@web mnt]# mkdir hg
[root@web mnt]# ll
total 0
drwxr-xr-x 2 root root 6 Jul  5 17:35 hg
[root@web mnt]# stat -c %a hg/
755
[root@web mnt]# chmod g+s hg/
[root@web mnt]# ll
total 0
drwxr-sr-x 2 root root 6 Jul  5 17:35 hg
[root@web mnt]# stat -c %a hg/
2755
#由此可以得出setgid为2000

SetGID特性

root@localhost:~#ll
drwxr--r--. 2 root root    6 Mar 23 13:35 hg1
drwxr-xr-x. 2 root root    6 Mar 23 13:35 hg2
root@localhost:~#chmod g+s hg1
root@localhost:~#ll
drwxr-Sr--. 2 root root    6 Mar 23 13:35 hg1
drwxr-xr-x. 2 root root    6 Mar 23 13:35 hg2
root@localhost:~#chmod g+s hg2
root@localhost:~#ll
drwxr-Sr--. 2 root root    6 Mar 23 13:35 hg1
drwxr-sr-x. 2 root root    6 Mar 23 13:35 hg2
#特性
如果原本没有x(执行)权限 授权为setgid 会是大写 S
如果原本有x(执行)权限 授权为setgid 会是小写 s

SBIT

SBIT 权限仅对目录有效,一旦目录设定了 SBIT 权限,则用户在此目录下创建的文件或目录,就只有自己和 root 才有权利修改或删除该文件。任何用户都可以对SBIT 权限目录下的文件进行读、写。

[root@web mnt]# ll /tmp/ -d
drwxrwxrwt. 13 root root 4096 Jul  5 17:48 /tmp/
[root@web mnt]# stat -c %a /tmp
1777
#由此可以得出setgid为1000

#语法
chmod o+t  directory         #给目录增加特殊权限sbit
chmod o=t  directory
chmod 1000  directory
chmod o-t  directory         #给目录去除特殊权限sbit

SBIT特性

[root@web mnt]# ll
total 0
drwxr-xr-x 2 root root 6 Jul  5 17:50 hg
[root@web mnt]# chmod o+t hg
[root@web mnt]# ll
total 0
drwxr-xr-t 2 root root 6 Jul  5 17:50 hg

[root@web mnt]# ll
total 0
drwxr-xr-t 2 root root 6 Jul  5 17:50 hg
drw-r--r-- 2 root root 6 Jul  5 17:52 hg1
[root@web mnt]# chmod o+t hg1
[root@web mnt]# ll
total 0
drwxr-xr-t 2 root root 6 Jul  5 17:50 hg
drw-r--r-T 2 root root 6 Jul  5 17:52 hg1
#特性
如果原本有x权限 授权特殊sbit后 t
如果原本没有x权限 授权特殊sbit后 T

凌驾于root之上的权限

## 设置这个权限
chattr
i:只能查看文件内容,无法编辑
a:只能查看和追加文件内容,无法修改或覆盖源文件内容

+:设置,增加该权限
-:取消该权限

## 查看这个权限
lsattr 文件名

[root@web mnt]# ll
total 0
-rw-r--r-- 1 root root 0 Jul  5 17:55 1.txt
[root@web mnt]# chattr +a +i 1.txt 
[root@web mnt]# lsattr
----ia---------- ./1.txt

umask

# 查看当前系统默认的umask
[root@web mnt]# umask
0022

# 修改系统默认的umask
[root@web mnt]# umask 111
[root@web mnt]# umask 

0111
计算方法:
目录使用777 - umask
文件使用666 - umask
如果umask的位数上有奇数
文件使用666 - umask 在奇数位上加1