思考
我们平时使用Linux创建文件或目录时的默认权限是多少?
shell
[root@localhost test]# mkdir dir
[root@localhost test]# touch file
[root@localhost test]# ll
total 0
drwxr-xr-x 2 root root 6 Mar 8 15:23 dir #755
-rw-r--r-- 1 root root 0 Mar 8 15:23 file #644
可以看到文件创建时获得的权限是644
目录创建时获得的权限是755
通过对上面问题的思考,我们现在引入下面的概念
umask
默认情况下内核给文件分配的权限是666
,给目录分配的权限是777
而用户创建文件或目录时,会与umask的掩码相减获得对应的权限值
如下所示掩码值为0022
那么创建文件时就是,666-022=644
创建目录时为,777-022=755
shell
[root@localhost test]# umask
0022
[root@localhost test]# umask -S
u=rwx,g=rx,o=rx
接下来我们试着修改umask的值,再去创建文件和目录会发生什么?
shell
[root@localhost test]# umask 0777
[root@localhost test]# mkdir new_dir
[root@localhost test]# touch new_file
[root@localhost test]# ll
total 0
drwxr-xr-x 2 root root 6 Mar 8 15:23 dir
-rw-r--r-- 1 root root 0 Mar 8 15:23 file
d--------- 2 root root 6 Mar 8 15:45 new_dir
---------- 1 root root 0 Mar 8 15:49 new_file
可以看到新的文件于目录的权限全部为000了。
umask的值,默认是在/etc/profile中的脚本初始化后得到的
shell
cat /etc/profile
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
umask 002
else
umask 022
fi
chmod
格式1:
用户类别
u: 所有者(user)
g: 所属组(group)
o: 其他用户(others)
a: 所有用户(等同于 ugo,默认值)
操作符
+: 添加权限
-: 移除权限
=: 直接设置权限(覆盖原有权限)
shell
chmod u+x file # 给所有者添加执行权限
chmod go-w file # 移除组和其他用户的写权限
chmod a=rw file # 所有用户设置为读写权限
chmod -R o+rX dir # 递归给其他用户添加读权限,目录加执行权限
格式2:
权限计算
将权限对应的数值相加:
r (读) = 4
w (写) = 2
x (执行) = 1
shell
chmod nnn 文件或目录
chmod 777 dir_file
目录权限:
目录的 x 权限表示可进入(cd),r 权限表示可列出内容。
若目录无 x 权限,即使有 r 权限也无法读取内部文件列表。
chown
必须是root
用户和组必须存在
格式:
shell
chown 所有者 文件
chown :所属组 文件
chown 所有者:所属组 文件
chown luobozi:atri /home/ytl
chgrp
必须是root或者文件的所有者
必须是新的组的成员
格式:
shell
chgrp 所属组 文件
查看常见目录的权限
shell
[root@localhost perm]# ll /home
total 0
drwx------ 2 kotonghitoli bondband 62 Mar 7 14:57 gdyg
drwx------ 2 luobozi luobozi 62 Mar 7 10:50 luobozi
drwx------ 2 luobozi_1 luobozi_1 62 Mar 7 16:17 luobozi_1
drwx------ 2 luobozi_2 luobozi_2 62 Mar 7 16:17 luobozi_2
drwx------ 2 luobozi_3 luobozi_3 62 Mar 7 16:17 luobozi_3
drwx------ 2 luobozi_4 luobozi_4 62 Mar 7 16:17 luobozi_4
drwx------ 2 luobozi_5 luobozi_5 62 Mar 7 16:17 luobozi_5
[root@localhost perm]# ll -d /
dr-xr-xr-x. 22 root root 4096 Mar 8 16:28 /
[root@localhost perm]# ll -d /root
dr-xr-x---. 15 root root 4096 Mar 8 15:23 /root
[root@localhost perm]# ll -d /tmp/
drwxrwxrwt. 9 root root 255 Mar 8 16:30 /tmp/
sudo
授权
在Linux中root用户的权限最大
普通用户的权限很小,那么如何让普通用户也具有一定的权限呢?
修改配置文件,使得普通用户可以使用sudo提权
1.创建一个king用户,使他可以使用sudo提权执行所有命令
shell
root@localhost ~]# useradd king
[root@localhost ~]# echo "123456" |passwd king --stdin
Changing password for user king.
passwd: all authentication tokens updated successfully.
#添加如下内容
vim /etc/sudoers
king ALL=(ALL) ALL
#使用sudo 提权查看root的家目录
su - king
Last login: Sat Mar 8 17:13:00 CST 2025 on pts/1
[king@localhost ~]$ sudo ls /root
[sudo] password for king:
2025-1-14 anaconda-ks.cfg dbbackup functions grep_file shell shell_bk_db while_read.sh
2.定义命令别名USERADMINS包含useradd、userdel、passwd、groupadd、groupdel命令
shell
#添加如下内容
vim /etc/sudoers
Cmnd_Alias USERADMINS = /usr/sbin/useradd, /usr/sbin/userdel, /usr/bin/passwd,/usr/sbin/groupadd,/usr/sbin/groupdel
3.新建用户组k
,里面的成员有mikoto、reisi、weissman
设置sudoers文件允许k组里的成员可以使用USERADMINS命令别名里的命令,在任何机器上
shell
[root@localhost ~]# groupadd k
[root@localhost ~]# useradd mikoto -G k
[root@localhost ~]# useradd reisi -G k
[root@localhost ~]# useradd weissman -G k
[root@localhost ~]# echo "123456" |passwd mikoto --stdin
[root@localhost ~]# echo "123456" |passwd reisi --stdin
[root@localhost ~]# echo "123456" |passwd weissman --stdin
vim /etc/sudoers
%k ALL = USERADMINS
验证是否成功:
shell
[root@localhost ~]# su - weissman
[weissman@localhost ~]$ sudo useradd neko -G weissman
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
[sudo] password for weissman:
[weissman@localhost ~]$ id neko
uid=3013(neko) gid=3015(neko) groups=3015(neko),3012(weissman)
selinux
是Linux系统里的安全子系统(security linux)
它可以限制哪些进程能访问哪些类型的文件
默认情况下它是开启状态,一般情况下我们都是将它关闭的,修改配置文件永久关闭
shell
[root@localhost ~]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
[root@localhost ~]# setenforce 0 临时关闭命令
[root@localhost ~]# getenforce
Disabled
chattr
设置文件的隐藏属性
选项 [ ± ] 加是授予 减是取消
-R 递归修改
+i 锁定文件,任何人都不能改动,包括root用户
+a 设置文件只能增加,不能修改和删除
shell
[root@localhost ~]# mkdir test1
[root@localhost ~]# chattr +i test1/
[root@localhost ~]# lsattr -d test1
----i----------- test1
[root@localhost ~]# mkdir test1/dir1
mkdir: cannot create directory 'test1/dir1': Permission denied
[root@localhost ~]# chattr -i test1
[root@localhost ~]# mkdir test1/dir1
[root@localhost ~]# ll test1
total 0
drwxr-xr-x 2 root root 6 Mar 8 18:05 dir1
lsattr
查看文件的隐藏属性
-R 递归修改
-d 查看目录本身的属性
SET位权限
特别权限位
SUID (4): 文件以所有者权限执行(如 /usr/bin/passwd)。
SGID (2): 目录中新文件继承组权限。
粘滞位 (1): 仅文件所有者可删除目录内文件(如 /tmp)。
suid:如果摸个文件具有suid权限位,普通用户在执行这个命令的时候,会以文件拥有者身份去执行
粘滞位(sticky)
使用chmod
修改set位
shell
[root@localhost ~]# ll /bin/passwd
-rwsr-xr-x. 1 root root 27856 Apr 1 2020 /bin/passwd
#可以看到passwd的执行位置就是s
修改mkdir的suid权限位置
注意观察属主的执行位置的权限变化
第1位(可选)
(SUID=4,SGID=2,粘滞位=1)
shell
[root@localhost ~]# chmod u+s /bin/mkdir
[root@localhost ~]# ll /bin/mkdir
-rwsr-xr-x. 1 root root 79768 Aug 20 2019 /bin/mkdir
[root@localhost ~]# chmod 0755 /bin/mkdir
[root@localhost ~]# ll /bin/mkdir
-rwxr-xr-x. 1 root root 79768 Aug 20 2019 /bin/mkdir
[root@localhost ~]# chmod 4755 /bin/mkdir
[root@localhost ~]# ll /bin/mkdir
-rwsr-xr-x. 1 root root 79768 Aug 20 2019 /bin/mkdir