目录
一、权限引入
用户和组(uid和gid)嵌入到进程里,进程去访问文件,内核会检查进程里的uid和gid对比文件允许的用户和组,最后决定是否有读、写、执行的权限
默认情况下,普通用户只能在两个地方建文件夹:
-
- 自己的家目录
- /tmp 临时目录
【根目录下,只有root用户可以新建文件或文件夹】
1.1 【补充】selinux
二、⭐查看文件/目录权限及归属
-rw-r--r-- 1 root root 475 4月 2 21:31 create_user.sh
2.1 文件类型:
- 表示普通文件
d 表示目录 directory
l 表示链接文件 link
s 表示socket文件
p 表示管道文件 pipe
c 表示字符设备文件 character 显示设备文件 tty1
b 表示块设备文件 block
[root@localhost lianxi]# ll /dev/tty1
crw--w---- 1 root tty 4, 1 4月 5 08:26 /dev/tty1
[root@localhost lianxi]# ll /dev/sda1
brw-rw---- 1 root disk 8, 1 4月 1 21:03 /dev/sda1
==》socket
2.2 不同权限对于文件和目录分别意味着什么
2.2.1 read
[root@localhost 0405]# ll
总用量 4
-rw-r--r-- 1 root root 11 4月 5 12:32 feng.txt
[root@localhost 0405]# su - lihua
[lihua@localhost ~]$ id
uid=2004(lihua) gid=2004(lihua) 组=2004(lihua)
文件:cat、vim ==》 只能查看文件夹里的内容
文件夹:ls 能看文件夹里有哪些文件和文件夹
[root@localhost 0405]# su - lihua
上一次登录:二 4月 5 12:41:27 CST 2022pts/0 上
[lihua@localhost ~]$ cd /lianxi/0405
[lihua@localhost 0405]$ ll
总用量 4
drwxr-xr-- 2 root root 25 4月 5 12:41 baodian
-rw-r--r-- 1 root root 11 4月 5 12:32 feng.txt
[lihua@localhost 0405]$ ls baodian
ls: 无法访问baodian/bixiejianpu: 权限不够
bixiejianpu
[lihua@localhost 0405]$ cd baodian ==》只有r权限,无法进入文件夹
-bash: cd: baodian: 权限不够
2.2.2 write
文件:vim、重定向==》改变文件里的内容
文件夹:mkdir、touch、rm、vim、mv、cp==》改变文件夹里的内容
2.2.3 execute
文件:可以执行文件里的命令
[root@localhost 0405]# vim echo.sh
[root@localhost 0405]# cat echo.sh
echo "hello,world"
[root@localhost 0405]# ll
总用量 8
drwxr-xr-x 2 root root 25 4月 5 12:41 baodian
-rw-r--r-- 1 root root 19 4月 5 12:49 echo.sh
-rw-r--r-- 1 root root 11 4月 5 12:32 feng.txt
[root@localhost 0405]# ./echo.sh ==》执行当前目录下的echo.sh文件
-bash: ./echo.sh: 权限不够
[root@localhost 0405]# /lianxi/0405/echo.sh
-bash: /lianxi/0405/echo.sh: 权限不够
[root@localhost 0405]# chmod +x echo.sh ==》给所有用户加上x权限
[root@localhost 0405]# ll
总用量 8
drwxr-xr-x 2 root root 25 4月 5 12:41 baodian
-rwxr-xr-x 1 root root 19 4月 5 12:49 echo.sh
-rw-r--r-- 1 root root 11 4月 5 12:32 feng.txt
[root@localhost 0405]# ./echo.sh
hello,world
[root@localhost 0405]# /lianxi/0405/echo.sh
hello,world
文件夹:能够·进入文件夹cd
[lihua@localhost 0405]$ ll
总用量 4
drwxr-xr-x 2 root root 25 4月 5 12:41 baodian
-rw-r--r-- 1 root root 11 4月 5 12:32 feng.txt
[lihua@localhost 0405]$ cd baodian
[lihua@localhost baodian]$
2.2.4 ❗注:root用户有特权
root用户可以不受限制
[root@localhost 0405]# chmod -rwx baodian
[root@localhost 0405]# ll
总用量 8
d--------- 2 root root 25 4月 5 12:41 baodian
-rwxr-xr-x 1 root root 19 4月 5 12:49 echo.sh
-rw-r--r-- 1 root root 11 4月 5 12:32 feng.txt
[root@localhost 0405]# cd baodian
[root@localhost baodian]# ls
bixiejianpu
三、设置文件/目录权限及归属
3.1 chmod
改变文件的模式
普通用户也可以使用该命令
3.1.1 使用字母
u 属主
g 属组
o 其他用户
a 所有用户
【举例】
[root@localhost 0405]# ll
总用量 0
-rw-r--r-- 1 root root 0 4月 5 21:24 abcd.txt
[root@localhost 0405]# chmod u+x,g+w,o+w abcd.txt
[root@localhost 0405]# ll
总用量 0
-rwxrw-rw- 1 root root 0 4月 5 21:24 abcd.txt
[root@localhost 0405]# chmod a-x abcd.txt
[root@localhost 0405]# ll
总用量 0
-rw-rw-rw- 1 root root 0 4月 5 21:24 abcd.txt
[root@localhost 0405]# chmod +rwx abcd.txt
[root@localhost 0405]# ll
总用量 0
-rwxrwxrwx 1 root root 0 4月 5 21:24 abcd.txt
[root@localhost 0405]# chmod -x abcd.txt
[root@localhost 0405]# ll
总用量 0
-rw-rw-rw- 1 root root 0 4月 5 21:24 abcd.txt
[root@localhost 0405]# chmod u=rwx,g=r,o=r abcd.txt
[root@localhost 0405]# ll
总用量 0
-rwxr--r-- 1 root root 0 4月 5 21:24 abcd.txt
-R 递归修改
【举例】
[root@localhost 0405]# chmod -R u=rwx,g=-,o=- sc
[root@localhost 0405]# ll
总用量 0
drwx------ 5 root root 40 4月 5 21:44 sc
[root@localhost 0405]# ll sc/
总用量 0
drwx------ 2 root root 6 4月 5 21:44 feng
drwx------ 2 root root 6 4月 5 21:44 li
drwx------ 2 root root 6 4月 5 21:44 zhao
3.1.2 使用数字
3.2 chown
更改文件的拥有者和组
-R 递归修改指定目录下所有文件和子目录的归属
【举例】
[root@localhost 0405]# ll sc/
总用量 0
drwx------ 2 root root 6 4月 5 21:44 feng
drwx------ 2 root root 6 4月 5 21:44 li
drwx------ 2 root root 6 4月 5 21:44 zhao
[root@localhost 0405]# cd sc
[root@localhost sc]# chown lihua:lihua li
[root@localhost sc]# ll
总用量 0
drwx------ 2 root root 6 4月 5 21:44 feng
drwx------ 2 lihua lihua 6 4月 5 21:44 li
drwx------ 2 root root 6 4月 5 21:44 zhao
[root@localhost sc]# chown :yitianming li
[root@localhost sc]# ll
总用量 0
drwx------ 2 root root 6 4月 5 21:44 feng
drwx------ 2 lihua yitianming 6 4月 5 21:44 li
drwx------ 2 root root 6 4月 5 21:44 zhao
[root@localhost sc]# chown feng li
[root@localhost sc]# ll
总用量 0
drwx------ 2 root root 6 4月 5 21:44 feng
drwx------ 2 feng yitianming 6 4月 5 21:44 li
drwx------ 2 root root 6 4月 5 21:44 zhao
3.3 chgrp
3.4 umask
3.4.1 权限的默认情况
root用户:
文件夹:755
文件:644
普通用户:
文件夹:775
文件:664
[root@localhost 0405]# mkdir hello
[root@localhost 0405]# touch nihao.txt
[root@localhost 0405]# ll
总用量 0
drwxr-xr-x 2 root root 6 4月 5 23:08 hello
-rw-r--r-- 1 root root 0 4月 5 23:09 nihao.txt
[root@localhost 0405]# su - lihua
[lihua@localhost ~]$ mkdir hello
[lihua@localhost ~]$ touch nihao.txt
[lihua@localhost ~]$ ll
总用量 0
drwxrwxr-x 2 lihua lihua 6 4月 5 23:09 hello
-rw-rw-r-- 1 lihua lihua 0 4月 5 23:09 nihao.txt
3.4.2 umask的作用
在内核级别,文件的初始权限666
在内核级别,文件夹的初始权限777
umask值,用来和内核里的文件权限进行运算得到用户的默认新建的文件或文件夹的
[root@localhost 0405]# umask
0022 #如:文件夹0777-0022=0755
[root@localhost 0405]# umask -S
u=rwx,g=rx,o=rx
###不推荐修改系统默认的umask###
用umask命令控制默认权限,临时有效
[root@localhost 0405]# umask 0077
[root@localhost 0405]# mkdir xiaofeng
[root@localhost 0405]# ll
总用量 0
drwx------ 2 root root 6 4月 5 23:15 xiaofeng
[root@localhost 0405]# umask
0077
[root@localhost 0405]# umask -S
u=rwx,g=,o=
【永久有效】实现用户lihua登陆后umask是002,永久有效
vim ~lihua/.bash_profile
在该文件中添加 umask 0002
3.4.3 umask值的来源
[root@localhost 0405]# vim /etc/profile
# $UID 表示用户的uid号
# -gt 即great than 表示大于
# id -gn表示组名,id -un表示用户名
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
umask 002
else
umask 022
fi
3.5 cp -a
cp -a 复制文件的时候,保留文件的属性(权限、时间、用户、组)
四、文件或目录的隐藏属性
4.1 chattr
设置文件到隐藏属性
4.1.1 常用选项
R 递归修改
a 只能追加,不能修改和删除 append
i 不能修改,锁定某个文件 immutable
【示例】==》a
【示例】==》i
锁住文件夹:不能在文件夹里新建,删除, 复制、移动文件到此文件夹(可以复制走)
# 文件不可修改
[root@localhost ~]# chattr +i /etc/passwd
[root@localhost ~]# useradd lalala
useradd:无法打开 /etc/passwd
# 文件夹不可修改
[root@localhost lianxi]# chattr +i 0406
[root@localhost lianxi]# cd 0406
[root@localhost 0406]# mkdir feng
mkdir: 无法创建目录"feng": 权限不够
[root@localhost 0406]# ll -d .
drwxr-xr-x 2 root root 6 4月 6 09:12 .
[root@localhost lianxi]# chattr -i 0406
[root@localhost lianxi]# cd 0406
[root@localhost 0406]# mkdir feng
[root@localhost 0406]# ls
feng
4.2 lsattr
查看隐藏权限
[root@localhost lianxi]# lsattr -d 0406
----i----------- 0406
[root@localhost lianxi]# lsattr /etc/passwd
----i----------- /etc/passwd
[root@localhost lianxi]# lsattr -d /lianxi
---------------- /lianxi
ls -l 查看的是基本权限
lsattr 查看的隐藏权限
[root@localhost 0406]# lsattr
---------------- ./feng
[root@localhost 0406]# ll
总用量 0
drwxr-xr-x 2 root root 6 4月 6 09:17 feng
五、文件或目录的特别权限
5.1 suid、sgid
suid权限:当某个用户执行某个具有suid权限的命令时,会以这个命令文件的拥有者的身份去执行
不要轻易给命令授予suid权限位,因为其他用户在执行命令的时候会拥有root用户的权限。
[root@localhost ~]# which passwd
/usr/bin/passwd
[root@localhost ~]# ll /usr/bin/passwd
-rwsr-xr-x. 1 root root 27856 4月 1 2020 /usr/bin/passwd ==》用户设置密码时,具有root用户的权限
5.1.1 如何让普通用户能在根目录下新建文件夹
- 可以让mkdir具有suid权限
- 设置更目录权限为777
[root@localhost ~]# ll /usr/bin/mkdir
-rwxr-xr-x. 1 root root 79768 8月 20 2019 /usr/bin/mkdir
[root@localhost ~]# chmod u+s /usr/bin/mkdir
[root@localhost ~]# ll /usr/bin/mkdir
-rwsr-xr-x. 1 root root 79768 8月 20 2019 /usr/bin/mkdir
[root@localhost ~]# su - lihua
上一次登录:二 4月 5 23:08:22 CST 2022pts/0 上
[lihua@localhost ~]$ cd /
[lihua@localhost /]$ mkdir lihua
[lihua@localhost /]$ ls
backup boot etc lianxi lib64 media opt root sbin sys usr wulin
bin dev home lib lihua mnt proc run srv tmp var
[lihua@localhost /]$ ll -d lihua
drwxrwxr-x 2 root lihua 6 4月 6 15:54 lihua ==》文件夹属于root
[root@localhost ~]# chmod u-s /usr/bin/mkdir ==》取消suid权限位
如下图:
5.2 sticky
- 目录需要先设置权限为777
- 添加+t 特别权限
/tmp 临时存放文件或者文件夹的目录,任何一个用户都可以操作
5.3 使用权限数字
设置特别权限时建议使用权限字符
suid ==》4
sgid ==》2
sticky ==》1
[root@localhost ~]# ll /usr/bin/mkdir
-rwxr-xr-x. 1 root root 79768 8月 20 2019 /usr/bin/mkdir
[root@localhost ~]# chmod 4755 /usr/bin/mkdir
[root@localhost ~]# ll /usr/bin/mkdir
-rwsr-xr-x. 1 root root 79768 8月 20 2019 /usr/bin/mkdir
[root@localhost ~]# chmod 0755 /usr/bin/mkdir
[root@localhost ~]# ll /usr/bin/mkdir
-rwxr-xr-x. 1 root root 79768 8月 20 2019 /usr/bin/mkdir
5.4 sudo
sudo授权给普通用户去执行命令,Linux系统会记录普通用户使用sudo执行过的所有命令
日志文件:/var/log/secure会记录用户使用sudo所执行的命令(日志文件是系统里的机密文件,普通用户一般是看不了的)
5.4.1 单个用户授权
root 用户授权普通用户执行命令
哪些普通用户?在那里执行命令?执行什么命令?
user MACHINE=COMMANDS
root ALL=(ALL) ALL
feng ALL=(ALL) ALL
deng /usr/sbin/userde,/usr/sbin/userdel
第三行解释:
整体含义:root用户可以在任何机器上执行任何命令和任何选项
root代表用户名
ALL=(ALL) 第一个ALL代表当前所在机器,第二个ALL代表所有的命令
ALL 第三个ALL代表前面命令的所有的选项
第四行解释:
整体含义:feng用户可以在任何机器上执行任何命令和任何选项
第五行解释:
整体含义:只是允许deng这个用户可以执行/usr/sbin/userde,/usr/sbin/userdel
【命令需要写绝对路径,多个命令使用逗号隔开】
5.4.2 命令的别名
授权时,如果要用到一系列的命令,那要输入的内容过多,所以就可以利用别名,提高效率。
【用户和机器也可以使用别名】
## Installation and management of software
# Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum
Cmnd_Alias 是固定的语法
定义命令别名 SOFTWARE 里面包含 /bin/rpm, /usr/bin/up2date, /usr/bin/yum
5.4.3 给组授权
不管是主要组还是次要组,都会继承组里的权限
## Allows people in group wheel to run all commands
%wheel ALL=(ALL) ALL
5.4.4 为什么要使用sudo
5.4.5 NOPASSWD
sudo切换过去第一次执行命令的时候,需要输入密码。
第二次使用sudo命令,如果时间间隔比较短,可以不用输入密码。
但如果时间间隔比较长,还是需要输入密码
cali ALL=(ALL) NOPASSWD: ALL
在/etc/sudoers里加入这样一行表示:用户cali可以在任何机器上执行任何命令和任何选项,并且使用sudo的时候不用输入密码
【补充】visudo 本质上是使用vim编辑器去打开/etc/sudoers文件
visudo可以检测语法是否正确
六、ACL
拒绝权限高于一切==》只争对用户
一个组允许,一个组拒绝==》允许
【示例】
6.1 setfacl
设置文件的访问控制列表
6.1.1 示例(-m)
【铺垫】
[root@localhost ~]# cat /etc/group|grep shaolin
shaolin:x:5568:
[root@localhost ~]# cat /etc/group|grep gaibang
gaibang:x:600:
[root@localhost ~]# cat /etc/group|grep nongda
nongda:x:2009:
[root@localhost ~]# cat /etc/group|grep wudang
wudang:x:2007:
[root@localhost lianxi]# groupadd sanchuang
[root@localhost ~]# cat /etc/group|grep shaolin
shaolin:x:5568:
[root@localhost ~]# cat /etc/group|grep gaibang
gaibang:x:600:
[root@localhost ~]# cat /etc/group|grep nongda
nongda:x:2009:
[root@localhost ~]# cat /etc/group|grep wudang
wudang:x:2007:
[root@localhost ~]# cd /lianxi
[root@localhost lianxi]# touch miji.txt
[root@localhost lianxi]# ll miji.txt
-rw-r--r-- 1 root root 0 4月 6 22:39 miji.txt
[root@localhost lianxi]# useradd -g wudang dengjz
[root@localhost lianxi]# useradd -g shaolin zhangheng
[root@localhost lianxi]# useradd -g shaolin cali
[root@localhost lianxi]# useradd -g sanchuang xiaohui
[root@localhost lianxi]# useradd -g gaibang qiaofeng
[root@localhost lianxi]# useradd -g nongda yiyq
【过程】
[root@localhost lianxi]# setfacl -m g:wudang:rwx miji.txt
[root@localhost lianxi]# getfacl miji.txt
# file: miji.txt
# owner: root
# group: root
user::rw-
group::r--
group:wudang:rwx
mask::rwx
other::r--
[root@localhost lianxi]# ll miji.txt
-rw-rwxr--+ 1 root root 0 4月 6 22:39 miji.txt
[root@localhost lianxi]# setfacl -m g:shaolin:rwx miji.txt
[root@localhost lianxi]# setfacl -m g:nongda:rwx miji.txt
[root@localhost lianxi]# getfacl miji.txt
# file: miji.txt
# owner: root
# group: root
user::rw-
group::r--
group:wudang:rwx
group:nongda:rwx
group:shaolin:rwx
mask::rwx
other::r--
[root@localhost lianxi]# setfacl -m g:gaibang:--- miji.txt
[root@localhost lianxi]# setfacl -m g:sanchuang:--- miji.txt
[root@localhost lianxi]# getfacl miji.txt
# file: miji.txt
# owner: root
# group: root
user::rw-
group::r--
group:gaibang:---
group:wudang:rwx
group:nongda:rwx
group:shaolin:rwx
group:sanchuang:---
mask::rwx
other::r--
[root@localhost lianxi]# setfacl -m u:cali:--- miji.txt
[root@localhost lianxi]# getfacl miji.txt
# file: miji.txt
# owner: root
# group: root
user::rw-
user:cali:---
group::r--
group:gaibang:---
group:wudang:rwx
group:nongda:rwx
group:shaolin:rwx
group:sanchuang:---
mask::rwx
other::r--
【检验】
[root@localhost lianxi]# cat miji.txt
123456
145
[root@localhost lianxi]# su - zhangheng
[zhangheng@localhost ~]$ cat /lianxi/miji.txt
123456
145
[zhangheng@localhost ~]$ exit
登出
[root@localhost lianxi]# su - xiaohui
[xiaohui@localhost ~]$ cat /lianxi/miji.txt
cat: /lianxi/miji.txt: 权限不够
[xiaohui@localhost ~]$ exit
登出
[root@localhost lianxi]# su - cali
[cali@localhost ~]$ cat /lianxi/miji.txt
cat: /lianxi/miji.txt: 权限不够
6.1.2 mask
[root@localhost lianxi]# setfacl -m m::r miji.txt
[root@localhost lianxi]# getfacl miji.txt
# file: miji.txt
# owner: root
# group: root
user::rw-
user:cali:---
group::r--
group:gaibang:---
group:wudang:rwx #effective:r--
group:nongda:rwx #effective:r--
group:shaolin:rwx #effective:r--
group:sanchuang:---
mask::r--
other::r--
【验证】
[root@localhost lianxi]# su - zhangheng
上一次登录:三 4月 6 23:01:46 CST 2022pts/0 上
[zhangheng@localhost ~]$ cat /lianxi/miji.txt
123456
145
[zhangheng@localhost ~]$ vim /lianxi/miji.txt #修改失败
6.1.3 删除(-x、-b)
-x 删除指定的ACL规则
[root@localhost lianxi]# setfacl -x g:sanchuang miji.txt
[root@localhost lianxi]# getfacl miji.txt
# file: miji.txt
# owner: root
# group: root
user::rw-
user:cali:---
group::r--
group:gaibang:---
group:wudang:rwx
group:nongda:rwx
group:shaolin:rwx
mask::rwx
other::r--
-b 删除所有ACL规则
[root@localhost lianxi]# setfacl -b miji.txt
[root@localhost lianxi]# getfacl miji.txt
# file: miji.txt
# owner: root
# group: root
user::rw-
group::r--
other::r--
6.2 【了解】ACL类型
默认是存储型,不会递归