用户权限管理
文件目录权限
访问权限
可读(read) --》r
可写(write) --》w
可执行(excute) --》x
归属
属主
属组
其他
查看文件权限
ls -al
常看当前目录下面文件或者文件夹的详细信息,包括文件的权限
ls -ld
常看当前目录
第一列第一个字符
-
普通文件
d
普通目录
l
链接文件
c
字符设备文件
s
socket
b
块设备文件
第一列第二到第十个字符
权限控制
#权限 r 可读 w 可写 x 可执行
第2-第4个字符
表示属主的权限
第5-第7个字符
表示属组的权限
后三个:
其他人的权限
第三列
属主
第四列
属组
操作系统
进程通信
管道
信号
共享内存
socket
消息队列
进程调度
文件系统
内存管理
网络接口
执行脚本方式
[root@mysql-binary ~]# cat test_aa.sh
echo "testaa.............."
echo $a
echo $b
[root@mysql-binary ~]# a=1
[root@mysql-binary ~]# b=2
[root@mysql-binary ~]# . test_aa.sh #使用当前bash去执行test_aa.sh, 会包含当前bash的变量
testaa..............
1
2
[root@mysql-binary ~]# bash test_aa.sh #创建一个新的bash环境去执行
testaa..............
[root@mysql-binary ~]# sh test_aa.sh #创建一个新的sh环境去执行
testaa..............
[root@mysql-binary ~]# ./test_aa.sh #执行文件本身,没有可执行权限
-bash: ./test_aa.sh: 权限不够
chmod
chmod命令
格式1:chmod [ugoa] [+-=] [rwx] 文件或目录...
常用命令选项
-R:递归修改指定目录下所有文件、子目录的权限
chmod命令
格式2:chmod nnn 文件或目录...
[root@mysql-binary ~]# ls -al group_member.sh
-rw-r--r-- 1 root root 317 11月 6 10:48 group_member.sh
[root@mysql-binary ~]# chmod u+x group_member.sh
[root@mysql-binary ~]# ls -al group_member.sh
-rwxr--r-- 1 root root 317 11月 6 10:48 group_member.sh
[root@mysql-binary ~]# chmod u-x group_member.sh
[root@mysql-binary ~]# ls -al group_member.sh
-rw-r--r-- 1 root root 317 11月 6 10:48 group_member.sh
[root@mysql-binary ~]# chmod u+x group_member.sh
[root@mysql-binary ~]# ./group_member.sh sanchuang5
sanchuang5,sanchuang11,sanchuang13,sanchuang10,sanchuang12
[sanchuang@mysql-binary ~]$ chmod u+x wenyao -R
[root@mysql-binary ~]# chmod 777 group_member.sh
[root@mysql-binary ~]# chmod 700 group_member.sh
[root@mysql-binary ~]# ls -ld group_member.sh
-rwx------ 1 root root 317 11月 6 10:48 group_member.sh
修改文件的访问权限
chown
chown命令
必须是root
用户和组必须存在
格式:chown 属主 文件
chown :属组 文件
chown 属主:属组 文件
修改文件的归属权限
[root@mysql-binary tmp]# chown sanchuang10 sanchuang_log
[root@mysql-binary tmp]# ls -al sanchuang_log
-rw-rw-r-- 1 sanchuang10 sanchuang 1116 11月 7 15:14 sanchuang_log
[root@mysql-binary tmp]# chown :sanchuang5 sanchuang_log
[root@mysql-binary tmp]# ls -al sanchuang_log
-rw-rw-r-- 1 sanchuang10 sanchuang5 1116 11月 7 15:14 sanchuang_log
[root@mysql-binary tmp]# chown sanchuang2:sanchuang sanchuang_log
[root@mysql-binary tmp]# ls -al sanchuang_log
-rw-rw-r-- 1 sanchuang2 sanchuang 1116 11月 7 15:14 sanchuang_log
chgrp
chgrp命令
格式:chgrp 属组 文件
必须是root或者是文件的所有者
必须是新组的成员
chmod root和文件所有者
chgrp root和文件所有者(必须是组成员)
chown 只有root
umask
规定默认创建文件或者文件夹的权限给多大
环境设置:/etc/profile
#uid大于199,并且当前用户的group_name和user_name名字相同的时候
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
umask 002
else
umask 022
fi
id -gn 获取当前用户组名
id -un 获取当前用户名
在内核级别,文件的初始权限666
在内核级别,文件夹的初始权限777
用umask命令控制默认权限,临时有效
umask为002 则表示新建文件的默认权限为664,新建文件的文件夹权限为775
umask为022 则表示新建文件的默认权限为644,新建文件的文件夹权限为755
想要修改某个用户的umask,永久修改
在家目录.bashrc下添加umask
/etc/profile , /etc/bashrc 基本上不要更改。
设置文件或者目录的隐藏属性
chattr命令: 设置文件的隐藏属性
格式:chattr [+-=] [ai] 文件或目录
常用命令选项
-R:递归修改
-a:可以增加文件内容,但不能修改和删除
-i: 锁定保护文件
[root@mysql-binary pem]# chattr +i hosts #设置锁定保护文件
[root@mysql-binary pem]# rm -rf hosts
rm: 无法删除"hosts": 不允许的操作
[root@mysql-binary pem]# vim hosts
[root@mysql-binary pem]# mv hosts{,.bak}
mv: 无法将"hosts" 移动至"hosts.bak": 不允许的操作
[root@mysql-binary pem]# ls -al hosts
-rwxr-x--- 1 root root 158 11月 7 16:24 hosts
[root@mysql-binary pem]# lsattr hosts
----i----------- hosts
[root@mysql-binary pem]# chattr -i hosts #去除锁定
[root@mysql-binary pem]# lsattr hosts
lsattr命令: 查看文件的隐藏属性
格式:lsattr [Rda] 文件或目录
常用命令选项
-R:递归修改
-d: 查看目录
粘滞位和set位权限设置
操作对象 特殊权限 功能
文件 SUID 以文件属主的权限执行文件
SGID 以文件属组的权限执行文件
文件夹 sticky 文件夹中新建的对象只有root和拥有者可以删除
SGID 文件夹中新建的对象都属于文件夹的属组
SET位权限
主要用途:
为可执行(有 x 权限的)文件设置,权限字符为“s”
其他用户执行该文件时,将拥有属主或属组用户的权限
SET位权限类型:
SUID:表示对属主用户增加SET位权限
SGID:表示对属组内的用户增加SET位权限
[root@mysql-binary pem]# ls -ld /pem
drwxr-xr-x 2 root root 47 11月 7 16:49 /pem
让/pem有普通用户创建写入的权限?
1、设置/pem的权限为777
2、设置mkdir suid权限
[root@mysql-binary pem]# chmod u+s /bin/mkdir
[root@mysql-binary pem]# su - sanchuang
上一次登录:六 11月 7 16:56:31 CST 2020pts/11 上
-bash: /tmp/sanchuang_log: 权限不够
[sanchuang@mysql-binary ~]$ cd /prm
-bash: cd: /prm: 没有那个文件或目录
[sanchuang@mysql-binary ~]$ cd /pem
[sanchuang@mysql-binary pem]$ ls
hosts passwd shadow
[sanchuang@mysql-binary pem]$ mkdir aa
[sanchuang@mysql-binary pem]$ ls
aa hosts passwd shadow
粘滞位权限(Sticky)
主要用途:
为公共目录(例如,权限为777的)设置,权限字符为“t”
用户不能删除该目录中其他用户的文件
应用示例:/tmp、/var/tmp
[sanchuang10@mysql-binary ~]$ touch /tmp/sanchuang10
[sanchuang@mysql-binary ~]$ touch /tmp/sanchuang
[sanchuang10@mysql-binary ~]$ rm -rf /tmp/sanchuang #sanchuang10用户不能删除sanchuang用户创建的文件
rm: 无法删除"/tmp/sanchuang/aa": 权限不够
设置SET位、粘滞位权限
使用权限字符
chmod ug±s 可执行文件...
chmod o±t 目录名...
使用权限数字:
chmod mnnn 可执行文件...
m为4时,对应SUID,2对应SGID,1对应粘滞位,可叠加
[root@mysql-binary pem]# chmod 1777 /pem
[root@mysql-binary pem]# ls -ld /pem
drwxrwxrwt 3 root root 57 11月 7 17:11 /pem
[root@mysql-binary pem]# ls -ld /bin/mkdir
-rwxr-xr-x. 1 root root 79768 8月 20 2019 /bin/mkdir
[root@mysql-binary pem]# chmod 4755 /bin/mkdir
[root@mysql-binary pem]# ls -ld /bin/mkdir
-rwsr-xr-x. 1 root root 79768 8月 20 2019 /bin/mkdir
[root@mysql-binary pem]# chmod 755 /bin/mkdir