Linux权限
1. Linux权限相关概念
Linux
下有两种用户:超级用户(root
)、普通用户
- 超级用户:可以在系统下做任何事情,不受限制
- 普通用户:受到权限限制
超级用户的命令提示符是’#‘,普通用户的命令行提示符是’$’
理解权限
2. Linux权限相关命令
2.1 su —— 用户切换
命令 su [用户名]
功能 切换用户
su root
的时候,root
可以省略
2.2 adduser/userdel ——新增用户/删除用户
在 root
用户下,可以新增其他用户,并设置密码,也可以删除用户(建议日常练习时新建一个自己的用户,不要使用 root
用户练习!!!)
[root@hcss-ecs-0be3 /]# adduser xm
# 为xm用户设置密码,输入密码的时候是不会回显的!!!
[root@hcss-ecs-0be3 /]# passwd xm
Changing password for user xm.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
# 用户添加成功后在home目录下可以看到
[root@hcss-ecs-0be3 /]# ll /home
total 12
drwx------ 2 VS VS 4096 Feb 26 21:13 VS
drwx------ 15 wyf wyf 4096 Apr 11 20:52 wyf
drwx------ 2 xm xm 4096 Apr 12 14:08 xm
# 进入xm用户下
[root@hcss-ecs-0be3 /]# su xm
[xm@hcss-ecs-0be3 /]$ cd
[xm@hcss-ecs-0be3 ~]$ ls
[xm@hcss-ecs-0be3 ~]$ exit
# 删除xm用户
[root@hcss-ecs-0be3 /]# userdel xm
# 虽然xm目录还在,但是已经失效了,没有xm这个用户了
# 这是Linux的默认安全策略,防止误删重要数据,删了用户之后还能找回数据
# userdel xm,不会删除xm用户的文件和目录
# userdel -r xm,删除用户时连带删除家目录
[root@hcss-ecs-0be3 /]# ls -al /home
total 20
drwxr-xr-x. 5 root root 4096 Apr 12 14:08 .
dr-xr-xr-x. 19 root root 4096 Mar 30 18:56 ..
drwx------ 2 VS VS 4096 Feb 26 21:13 VS
drwx------ 15 wyf wyf 4096 Apr 11 20:52 wyf
drwx------ 2 1002 1002 4096 Apr 12 14:09 xm
# 进入xm用户家目录时会出错
[root@hcss-ecs-0be3 /]# su xm
su: user xm does not exist
[root@hcss-ecs-0be3 /]# cd /home
[root@hcss-ecs-0be3 home]# ll
total 12
drwx------ 2 VS VS 4096 Feb 26 21:13 VS
drwx------ 15 wyf wyf 4096 Apr 11 20:52 wyf
drwx------ 2 1002 1002 4096 Apr 12 14:09 xm
[root@hcss-ecs-0be3 home]# rm -rf xm
[root@hcss-ecs-0be3 home]# ll
total 8
drwx------ 2 VS VS 4096 Feb 26 21:13 VS
drwx------ 15 wyf wyf 4096 Apr 11 20:52 wyf
[root@hcss-ecs-0be3 home]#
2.3 sudo
语法
sudo [选项] 命令
功能 sudo
(SuperUser Do
)允许 授权用户以其他用户身份(通常是 root
)执行特权命令,无需直接切换用户或共享 root
密码。比如安装软件时,只有 root
才能安装,普通用户配置好配置文件之后sudo
一下就能安装软件了
- 所有
sudo
操作会被记录到日志中(centos:/var/log/secure
)
常用选项
-u
用户 以指定用户身份执行命令(默认root
)-l
列出当前用户可执行的命令权限-k
强制重新输入密码(清除缓存的身份验证状态)-v
刷新sudo
密码有效期(延长默认5
分钟的免密窗口)-b
在后台运行命令
sudo
的配置文件 /etc/sudoers
类似于一张用户白名单
有详细的配置语法规则,平时使用的时候,打开/etc/sudoers
,添加下面一行就可满足使用
用户名 ALL=(ALL) ALL
2.4 umask —— 控制默认权限
功能 定义新创建文件/目录的默认权限
- 查看当前
umask
- 修改
umask
起始权限
- 文件:
666 - umask
- 目录:
777 - umask
最终权限(默认) = 起始权限 & (~umask)
2.5 chomod —— 修改权限
语法
chmod [选项] 权限模式 文件/目录
功能 修改文件权限
常用选项
-R
递归修改目录及其子文件权限
示例
# 查看umask
[wyf@hcss-ecs-0be3 per]$ umask
0002
# 数字表示方式修改文件的权限为 rwx r-x r-x
# 最终修改的权限为755 & ~002
# 755->111 101 101
# ~002->111 111 101
# 111 101 101->775
[wyf@hcss-ecs-0be3 per]$ chmod 755 test.txt
[wyf@hcss-ecs-0be3 per]$ ll
total 4
-rwxr-xr-x 1 wyf wyf 12 Apr 12 16:15 test.txt
[wyf@hcss-ecs-0be3 per]$ chmod 666 test.txt
[wyf@hcss-ecs-0be3 per]$ ll
total 4
-rw-rw-rw- 1 wyf wyf 12 Apr 12 16:15 test.txt
# 字符表示方式移除其他用户的写权限
[wyf@hcss-ecs-0be3 per]$ chmod o-w test.txt
[wyf@hcss-ecs-0be3 per]$ ll
total 4
-rw-rw-r-- 1 wyf wyf 12 Apr 12 16:15 test.txt
# 给所有人加上读、写、执行权限
[wyf@hcss-ecs-0be3 per]$ chmod a+rwx test.txt
[wyf@hcss-ecs-0be3 per]$ ll
total 4
-rwxrwxrwx 1 wyf wyf 12 Apr 12 16:15 test.txt
[wyf@hcss-ecs-0be3 per]$
2.6 chown —— 修改文件所有者
语法
chown [选项] 用户:组 文件/目录
功能 修改文件的所有者
示例
[wyf@hcss-ecs-0be3 per]$ sudo chown root test.txt
[wyf@hcss-ecs-0be3 per]$ ll
total 4
-rw-rw-r-- 1 root wyf 12 Apr 12 16:32 test.txt
[wyf@hcss-ecs-0be3 per]$ sudo chown wyf test.txt
[wyf@hcss-ecs-0be3 per]$ ll
total 4
-rw-rw-r-- 1 wyf wyf 12 Apr 12 16:32 test.txt
[wyf@hcss-ecs-0be3 per]$
2.7 chgrp —— 修改文件所属组
语法
chgrp [选项] 组名 文件/目录
功能 修改文件所属组
示例
[wyf@hcss-ecs-0be3 per]$ ll
total 4
-rw-rw-r-- 1 wyf wyf 12 Apr 12 16:32 test.txt
[wyf@hcss-ecs-0be3 per]$ sudo chgrp root test.txt
[wyf@hcss-ecs-0be3 per]$ ll
total 4
-rw-rw-r-- 1 wyf root 12 Apr 12 16:32 test.txt
[wyf@hcss-ecs-0be3 per]$ sudo chgrp wyf test.txt
[wyf@hcss-ecs-0be3 per]$ ll
total 4
-rw-rw-r-- 1 wyf wyf 12 Apr 12 16:32 test.txt
[wyf@hcss-ecs-0be3 per]$
3. 粘滞位
粘滞位概念
粘滞位是一种 目录级权限控制机制,主要解决多用户环境下共享目录的安全问题
- 当一个目录设置了粘滞位后,只有以下用户有权删除或重命名该目录中的文件:
- 文件的所有者(
User Owner
) - 目录的所有者(
Directory Owner
) root
用户
- 文件的所有者(
- 粘滞位仅控制文件删除或重命名,不影响文件内容的读写
# / 根目录下对于other用户是没有w权限的,也就不能新建目录/文件,只有root账户可以
[wyf@hcss-ecs-0be3 /]$ ls -ld /
dr-xr-xr-x. 20 root root 4096 Apr 12 17:38 /
[wyf@hcss-ecs-0be3 /]$ sudo mkdir share_dir
[wyf@hcss-ecs-0be3 /]$ ls -ld share_dir/
drwxr-xr-x 2 root root 4096 Apr 12 17:38 share_dir/
[wyf@hcss-ecs-0be3 /]$ cd share_dir/
# 不对other用户开放w权限,就不能在这个目录下新建目录/文件
[wyf@hcss-ecs-0be3 share_dir]$ echo "Hello" > share.txt
-bash: share.txt: Permission denied
[wyf@hcss-ecs-0be3 share_dir]$ cd ..
[wyf@hcss-ecs-0be3 /]$ sudo chmod o+w share_dir/
[wyf@hcss-ecs-0be3 /]$ cd share_dir/
[wyf@hcss-ecs-0be3 share_dir]$ echo "Hello" > share.txt
[wyf@hcss-ecs-0be3 share_dir]$ ls -l
total 4
-rw-rw-r-- 1 wyf wyf 6 Apr 12 17:40 share.txt
# 将要让其共享文件的用户设置为grop用户,并且让other用户不能读写该文件
[wyf@hcss-ecs-0be3 share_dir]$ sudo chgrp VS share.txt
[wyf@hcss-ecs-0be3 share_dir]$ chmod o-rwx share.txt
[wyf@hcss-ecs-0be3 share_dir]$ ls -l
total 4
-rw-rw---- 1 wyf VS 10 Apr 12 17:41 share.txt
# 切换成对应的用户后依然能够共享文件
[wyf@hcss-ecs-0be3 share_dir]$ su VS
Password:
[VS@hcss-ecs-0be3 share_dir]$ cat share.txt
Hello
[VS@hcss-ecs-0be3 share_dir]$ echo "Hey" >> share.txt
[VS@hcss-ecs-0be3 share_dir]$ exit
# 再次打开文件都能看到双方写入的内容
[wyf@hcss-ecs-0be3 share_dir]$ cat share.txt
Hello
Hey
# 切换成other用户,不能看也不能写,但却可以删除
[wyf@hcss-ecs-0be3 share_dir]$ su zs
Password:
[zs@hcss-ecs-0be3 share_dir]$ cat share.txt
cat: share.txt: Permission denied
[zs@hcss-ecs-0be3 share_dir]$ echo >> "hello" >> share.txt
bash: share.txt: Permission denied
[zs@hcss-ecs-0be3 share_dir]$ rm share.txt
rm: remove write-protected regular file ‘share.txt’? y
[zs@hcss-ecs-0be3 share_dir]$ ll
total 0
[zs@hcss-ecs-0be3 share_dir]$ exit
[wyf@hcss-ecs-0be3 share_dir]$ ll
total 0
[wyf@hcss-ecs-0be3 share_dir]$ cd ..
[wyf@hcss-ecs-0be3 /]$ ls -ld share_dir/
drwxr-xrwx 2 root root 4096 Apr 12 17:44 share_dir/
[wyf@hcss-ecs-0be3 /]$ tree share_dir/
share_dir/
0 directories, 0 files
[wyf@hcss-ecs-0be3 /]$
3.2 设置粘滞位
符号模式
chmod +t /shared_dir # 添加粘滞位
chmod -t /shared_dir # 移除粘滞位
数字模式
粘滞位对应的八进制值为 1
(如 1777
)
chmod 1777 /shared_dir # 设置粘滞位 + 所有人可读写执行
t
或T
:表示已设置粘滞位。- 小写
t
:粘滞位 + 其他用户有执行权限(如rwxrwxrwt
) - 大写
T
:粘滞位 + 其他用户无执行权限(如rwxrwx--T
)
- 小写
[wyf@hcss-ecs-0be3 /]$ sudo mkdir share
# 让other用户共享该目录
[wyf@hcss-ecs-0be3 /]$ sudo chmod o+w share
# 给share文件设置粘滞位
[wyf@hcss-ecs-0be3 /]$ sudo chmod +t share
[wyf@hcss-ecs-0be3 /]$ ls -ld share
drwxr-xrwt 2 root root 4096 Apr 12 18:03 share
[wyf@hcss-ecs-0be3 /]$ cd share
[wyf@hcss-ecs-0be3 share]$ echo "Hello" > share.txt
[wyf@hcss-ecs-0be3 share]$ ll
total 4
-rw-rw-r-- 1 wyf wyf 6 Apr 12 18:05 share.txt
[wyf@hcss-ecs-0be3 share]$ sudo chgrp VS share.txt
[wyf@hcss-ecs-0be3 share]$ chmod o-rwx share.txt
[wyf@hcss-ecs-0be3 share]$ ll
total 4
-rw-rw---- 1 wyf VS 6 Apr 12 18:05 share.txt
[wyf@hcss-ecs-0be3 share]$ su VS
Password:
[VS@hcss-ecs-0be3 share]$ echo "Hey" >> share.txt
[VS@hcss-ecs-0be3 share]$ cat share.txt
Hello
Hey
[VS@hcss-ecs-0be3 share]$ exit
[wyf@hcss-ecs-0be3 share]$ su zs
Password:
# 不能读
[zs@hcss-ecs-0be3 share]$ cat share.txt
cat: share.txt: Permission denied
# 不能写
[zs@hcss-ecs-0be3 share]$ echo "hello" >> share.txt
bash: share.txt: Permission denied
# 不能删除
[zs@hcss-ecs-0be3 share]$ rm -rf share.txt
rm: cannot remove ‘share.txt’: Operation not permitted
[zs@hcss-ecs-0be3 share]$ rm share.txt
rm: remove write-protected regular file ‘share.txt’? y
rm: cannot remove ‘share.txt’: Operation not permitted
[zs@hcss-ecs-0be3 share]$ exit
# 只能由所有者删除
[wyf@hcss-ecs-0be3 share]$ rm share.txt
[wyf@hcss-ecs-0be3 share]$ ll
total 0
[wyf@hcss-ecs-0be3 share]$