【8.30】特殊权限、软链接、硬链接

2.17 隐藏权限lsattr/chattr

1、i权限 (不可修改权限)

  • chattr +i 设置隐藏权限
  • lsattr 文件 查看隐藏权限
[root@arslinux-01 ~]# chattr +i 1.txt 
[root@arslinux-01 ~]# mv 1.txt 11.txt
mv: 无法将"1.txt" 移动至"11.txt": 不允许的操作
[root@arslinux-01 ~]# lsattr 1.txt 
----i----------- 1.txt
  • 在 vi 时会自动创建缓存文件,当保存退出后,会把缓存写入的内容覆盖源文件,然后缓存文件才会被删除,如果加了特殊权限,那么久无法保存,因此保存一个以 ~ 结尾的缓存文件。

  • chattr -i 删除特殊权限

[root@arslinux-01 ~]# lsattr 1.txt 
----i----------- 1.txt
[root@arslinux-01 ~]# chattr -i 1.txt 
[root@arslinux-01 ~]# !lsattr
lsattr 1.txt 
---------------- 1.txt
[root@arslinux-01 ~]# ll 1.txt 
  • 给目录增加 i 权限(不能删除、不能改名,不能在目录下增加子目录和文件,但可以更改目录下文件内容)
[root@arslinux-01 ~]# ls 111
222
[root@arslinux-01 ~]# chattr +i 111
[root@arslinux-01 ~]# lsattr -d 111
----i----------- 111
[root@arslinux-01 ~]# rm -r 111
rm:是否进入目录"111"? y
rm:是否删除目录 "111/222"?y
rm: 无法删除"111/222": 权限不够
[root@arslinux-01 ~]# mv 111 1212
mv: 无法将"111" 移动至"1212": 不允许的操作
[root@arslinux-01 ~]# touch 111/12.txt
touch: 无法创建"111/12.txt": 权限不够
  • 目录有 i 权限,依然可以更改目录下文件的内容
[root@arslinux-01 ~]# chattr -i 111
[root@arslinux-01 ~]# lsattr 111
---------------- 111/222
[root@arslinux-01 ~]# touch 111/11.txt
[root@arslinux-01 ~]# chattr +i 111
[root@arslinux-01 ~]# lsattr -d 111/
----i----------- 111/
[root@arslinux-01 ~]# head -2 /etc/passwd > 111/11.txt 
[root@arslinux-01 ~]# cat !$
cat 111/11.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

2、a权限(只可追加权限)

  • chattr +a 设置追加权限
[root@arslinux-01 ~]# chattr +a 1.txt 
[root@arslinux-01 ~]# rm 1.txt 
rm:是否删除普通文件 "1.txt"?y
rm: 无法删除"1.txt": 不允许的操作
[root@arslinux-01 ~]# head -2 /etc/passwd > 1.txt 
-bash: 1.txt: 不允许的操作
[root@arslinux-01 ~]# head -2 /etc/passwd >> 1.txt 
[root@arslinux-01 ~]# cat 1.txt 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@arslinux-01 ~]# touch 1.txt 
[root@arslinux-01 ~]# ll 1.txt 
-rw-r--r--. 1 root root 4735 3月  17 17:22 1.txt
  • a 权限可以追加、可以更改时间信息(touch);不能更改文件内容、不能删除、不能改名

  • 给目录增加a权限,可以在目录下增加子目录和子文件

  • lsattr 目录 查看目录下面子目录和子文件

  • lsattr -d 查看目录本身权限

[root@arslinux-01 ~]# lsattr -d 111/
----i----------- 111/
  • lsattr -R 查看目录下子目录和子目录下的文件权限
[root@arslinux-01 ~]# lsattr -R 111/
---------------- 111/222

111/222:

---------------- 111/11.txt
  • lsattr -a 查看全部文件权限,包括隐藏文件
[root@arslinux-01 ~]# lsattr /root/
---------------- /root/anaconda-ks.cfg
---------------- /root/123.txt
---------------- /root/11111.txt
-----a---------- /root/1.txt
---------------- /root/222.txt
---------------- /root/2.txt
---------------- /root/234
----i----------- /root/111
[root@arslinux-01 ~]# lsattr -a /root/
---------------- /root/.
---------------- /root/..
---------------- /root/.bash_logout
---------------- /root/.bash_profile
---------------- /root/.bashrc
---------------- /root/.cshrc
---------------- /root/.tcshrc
---------------- /root/anaconda-ks.cfg
---------------- /root/.bash_history
---------------- /root/.ssh
---------------- /root/.viminfo
---------------- /root/123.txt
---------------- /root/11111.txt
-----a---------- /root/1.txt
---------------- /root/.lesshst
---------------- /root/222.txt
---------------- /root/2.txt
---------------- /root/234
----i----------- /root/111

2.18 特殊权限 set_uid

  • set_uid 权限可以让普通用户在执行有 set_uid 权限的命令时,执行瞬间给普通用户赋予命令的所有者身份

  • 前提是文件必须是二进制的,可执行的文件(普通用户执行命令时拥有该命令所有者身份)
    这类文件并不多,系统中只有 passwd 命令

[root@arslinux-01 ~]# ll /usr/bin/passwd 
-rwsr-xr-x. 1 root root 27832 6月  10 2014 /usr/bin/passwd
  • chmod u+s 增加 set_uid 权限
[root@arslinux-01 ~]# su user1
[user1@arslinux-01 root]$ ls
ls: 无法打开目录.: 权限不够
[user1@arslinux-01 root]$ su
密码:
[root@arslinux-01 ~]# chmod u+s /usr/bin/ls
[root@arslinux-01 ~]# ll /usr/bin/ls
-rwsr-xr-x. 1 root root 117680 10月 31 03:16 /usr/bin/ls
[root@arslinux-01 ~]# su user1
[user1@arslinux-01 root]$ ls
111  11111.txt  123.txt  1.txt  222.txt  234  2.txt  anaconda-ks.cfg
[user1@arslinux-01 root]$ ll /usr/bin/ls
-rwsr-xr-x. 1 root root 117680 10月 31 03:16 /usr/bin/ls
  • 增加 set_uid 权限后,user1 用户就临时拥有了 root 用户的身份,从而可以使用 ls 命令

  • chmod u-s 去除 set_uid 权限

[root@arslinux-01 ~]# chmod u-s /usr/bin/ls
[root@arslinux-01 ~]# ll /usr/bin/ls
-rwxr-xr-x. 1 root root 117680 10月 31 03:16 /usr/bin/ls
  • chmod u+s ——> chmod u=rws,chmod u+x
  • 命令所有者权限显示 S 不是 s,说明所有者没有执行权限,需再加上 x 权限
[root@arslinux-01 ~]# chmod u=rws /usr/bin/ls
[root@arslinux-01 ~]# ll !$
ll /usr/bin/ls
-rwSr-xr-x. 1 root root 117680 10月 31 03:16 /usr/bin/ls
[root@arslinux-01 ~]# chmod u+x !$
chmod u+x /usr/bin/ls
[root@arslinux-01 ~]# ll /usr/bin/ls
-rwsr-xr-x. 1 root root 117680 10月 31 03:16 /usr/bin/ls
  • 其实,哪怕是 S 权限,其他用户使用 ls 也不会受到影响

  • 普通用户可否执行 root 命令,看命令的其他用户权限中是否有可执行的 x 权限

  • 目录虽然也可以设置 set_uid 命令,但是无意义。

2.19 特殊权限 set_gid

  • set_gidset_uid 类似,只不过是普通用户临时拥有所属组的身份

  • 普通用户被赋予了某命令的 sgid 权限,那么在作用在文件时,让执行该文件的普通用户临时用户拥有所属组的身份(是否可以被普通用户执行取决于命令执行的文件的所属组权限是否有 x 权限)
    被增加 s 权限的命令是否可以被其他用户使用,则看命令权限其他用户是否有 x 权限

  • chmod g+s 命令

[root@arslinux-01 ~]# chmod g+s /usr/bin/ls
[root@arslinux-01 ~]# ll !$
ll /usr/bin/ls
-rwsr-sr-x. 1 root root 117680 10月 31 03:16 /usr/bin/ls
  • set_gid 作用于目录
  • 给目录设置 set_gid 后,在目录下创建子文件和子目录时,那么这些子文件和子目录所属组会根据父级目录的所属组保持一致
[root@arslinux-01 ~]# chmod g+s 234
[root@arslinux-01 ~]# ll -d 234/
drwxrwsr-x. 2 root root 6 3月  17 14:16 234/
[root@arslinux-01 ~]# chown :user1 234/
[root@arslinux-01 ~]# ll -d 234/
drwxrwsr-x. 2 root user1 6 3月  17 14:16 234/
[root@arslinux-01 ~]# touch 234/arslinux
[root@arslinux-01 ~]# mkdir 234/ars
[root@arslinux-01 ~]# ll 234/
总用量 0
drwxr-sr-x. 2 root user1 6 3月  17 21:33 ars
-rw-r--r--. 1 root user1 0 3月  17 21:33 arslinux
[root@arslinux-01 ~]# chmod g-s 234/
[root@arslinux-01 ~]# touch 234/arslinux1
[root@arslinux-01 ~]# mkdir 234/ars1
[root@arslinux-01 ~]# ll 234/
总用量 0
drwxr-sr-x. 2 root user1 6 3月  17 21:33 ars
drwxr-xr-x. 2 root root  6 3月  17 21:34 ars1
-rw-r--r--. 1 root user1 0 3月  17 21:33 arslinux
-rw-r--r--. 1 root root  0 3月  17 21:34 arslinux1
  • set_gid 权限作用在文件上,和 set_uid 类似,可以让执行这个文件的普通用户临时拥有所属组的身份

  • set_gid 权限作用在目录上,在创建子目录和子文件时,创建的子目录和子文件所属组和该目录的所属组保持一致

  • 难理解的知识点梳理:
    普通用户能否执行 root 用户权限的命令,看该命令是否有 set_uid 和 set_gid 权限,有 set_uid 权限时,普通用户执行命令时,瞬间获取 root 用户权限,执行命令,能否成功执行,需要看 root 用户是否有x权限;而有 set_gid 权限时,普通用户执行命令时,瞬间获取 root 用户组权限,执行命令,能否成功执行,要看 root 用户组是否有 x 权限。

  • 有 set_uid 权限,当命令执行于目录时,普通用户作为 root 执行命令,而能否打开目录,是要看这时 root 用户对目录是否有 x 权限

  • 有 set_gid 权限,当命令执行于目录时,普通用户作为 root 组执行命令,能否代开目录,是要看这时 root 组是否对目录是否有 x权限

  • 举个例子:

[user1@arslinux-01 root]$ ll -d /root/
dr-xr-x---. 5 root root 274 3月  17 17:27 /root/
[user1@arslinux-01 root]$ ll /usr/bin/ls
-rwxr-sr-x. 1 root root 117680 10月 31 03:16 /usr/bin/ls
[user1@arslinux-01 root]$ ll -d /root/
dr-xr-x---. 5 root root 274 3月  17 17:27 /root/
  • ls 命令的所有者 root 拥有 set_gid 权限,普通用户 user1 执行 ls 命令时,瞬间以 root 组的身份执行了 ls 命令,而执行命令 ls 打开 /root/ 目录时,user1 就是在 root 组内,因此能否打开 /root/ 目录,完全取决于 /root/ 目录对 root 用组是否有x权限,这里是有x权限的,因此可以打开

2.20 特殊权限stick_bit

  • stick_bit 防删除位(其他用户无法删除)防止别人删除自己的文件,root 除外

  • 系统中只有/tmp/目录防删除位

[root@arslinux-01 ~]# ll -d /tmp/
drwxrwxrwt. 21 root root 4096 3月  18 20:25 /tmp/
  • 某用户创建的有 777 权限的文件,其他用户可以查看,可以修改,但是无法删除
[root@arslinux-01 ~]# ll -d /tmp/
drwxrwxrwt. 21 root root 4096 3月  18 20:25 /tmp/
[root@arslinux-01 ~]# su arslinux
[arslinux@arslinux-01 root]$ vi /tmp/stick
[arslinux@arslinux-01 root]$ chmod 777 !$
chmod 777 /tmp/stick
[arslinux@arslinux-01 root]$ ll /tmp/stick 
-rwxrwxrwx. 1 arslinux arslinux 6 3月  18 20:33 /tmp/stick
[arslinux@arslinux-01 root]$ su
密码:
[root@arslinux-01 ~]# su user1
[user1@arslinux-01 root]$ vi /tmp/stick 
[user1@arslinux-01 root]$ rm -rf /tmp/stick 
rm: 无法删除"/tmp/stick": 不允许的操作
  • 只有 root 可以删除,其他用户没有权限(当然root可以删,因为 stick 在其他用户可执行上限制)

  • 普通用户要删除一个文件,需要看文件所在的父目录有无写权限,而不是看删除的文件本身的权限
    (如果加上防删除位,那么只能更改,不能删除)

2.21 软链接文件

[root@arslinux-01 ~]# ll /bin
lrwxrwxrwx. 1 root root 7 3月  14 05:51 /bin -> usr/bin
/bin/就是/usr/bin/
  • 软链接文件本身存了一个文件或目录的路径,链接文件大小和路径长短有关

  • 软链接可以节省空间

  • 源文件更改后,软链接无需更改

  • ln -s 创建软链接:ln -s 源文件 软链接目标文件

[root@arslinux-01 tmp]# ln -s /tmp/yum.log /root/111/yum.log
[root@arslinux-01 tmp]# ll /root/111
总用量 4
-rw-rw-r--. 1 root root 65 3月  17 17:37 11.txt
drwxrwxr-x. 2 root root  6 3月  17 17:27 222
lrwxrwxrwx. 1 root root 12 3月  18 21:08 yum.log -> /tmp/yum.log
  • 软链接不仅可以链接文件,也可以链接目录
[root@arslinux-01 tmp]# ln -s /tmp/ars2 /root/111/ars3
[root@arslinux-01 tmp]# ll /root/111
总用量 4
-rw-rw-r--. 1 root root 65 3月  17 17:37 11.txt
drwxrwxr-x. 2 root root  6 3月  17 17:27 222
lrwxrwxrwx. 1 root root 10 3月  18 21:10 ars3 -> /tmp/ars2/
lrwxrwxrwx. 1 root root 12 3月  18 21:08 yum.log -> /tmp/yum.log
  • 做软链接,尽量使用绝对路径
    做相对路径的软链接,如果把软链接拷贝到其他目录,那么会导致软链接出现问题

  • 工作中实际情况:
    假设磁盘分区中,boot分区中有一个不断写入的日志,快讲boot分区占满了
    那么可以把该文件移动到更大的分区中,然后做一个软链接到boot分区中

[root@arslinux-01 ~]# df -h
文件系统        容量  已用  可用 已用% 挂载点
/dev/sda3        28G  1.2G   27G    5% /
devtmpfs        476M     0  476M    0% /dev
tmpfs           487M     0  487M    0% /dev/shm
tmpfs           487M  7.7M  479M    2% /run
tmpfs           487M     0  487M    0% /sys/fs/cgroup
/dev/sda1       197M  105M   93M   54% /boot
tmpfs            98M     0   98M    0% /run/user/0
[root@arslinux-01 ~]# touch /boot/arslinux.log
[root@arslinux-01 ~]# cp /boot/arslinux.log /arslinux.log
[root@arslinux-01 ~]# rm /boot/arslinux.log 
rm:是否删除普通空文件 "/boot/arslinux.log"?y
[root@arslinux-01 ~]# ln -s /arslinux.log /boot/arslinux.log
[root@arslinux-01 ~]# ll /boot/
总用量 88060
lrwxrwxrwx. 1 root root       13 3月  18 21:30 arslinux.log -> /arslinux.log
-rw-r--r--. 1 root root   151918 11月  9 07:43 config-3.10.0-957.el7.x86_64
drwxr-xr-x. 3 root root       17 3月  14 05:51 efi
drwxr-xr-x. 2 root root       27 3月  14 05:51 grub
drwx------. 5 root root       97 3月  14 05:58 grub2
-rw-------. 1 root root 53612300 3月  14 05:56 initramfs-0-rescue-0b3b2aee4c754c669d6ca09336428b22.img
-rw-------. 1 root root 19252882 3月  14 05:58 initramfs-3.10.0-957.el7.x86_64.img
-rw-r--r--. 1 root root   314036 11月  9 07:43 symvers-3.10.0-957.el7.x86_64.gz
-rw-------. 1 root root  3543471 11月  9 07:43 System.map-3.10.0-957.el7.x86_64
-rwxr-xr-x. 1 root root  6639904 3月  14 05:56 vmlinuz-0-rescue-0b3b2aee4c754c669d6ca09336428b22
-rwxr-xr-x. 1 root root  6639904 11月  9 07:43 vmlinuz-3.10.0-957.el7.x86_64
  • 软链接实际上就是个快捷方式,既可以给文件做软链接,也可以给目录做软链接,还可以跨分区做软链接

2.22 硬连接文件

  • 硬链接只支持对文件做硬链接,不支持对目录做硬链接
[root@arslinux-01 ~]# ln 1.txt 1_hard.txt
[root@arslinux-01 ~]# ln -s 1.txt 1_soft.txt
[root@arslinux-01 ~]# ll
总用量 36
drwxrwxr-x. 3 root root    43 3月  18 21:18 111
-rwx------. 1 root root     0 3月  17 09:22 11111.txt
drwxr-xr-x. 2 root root    21 3月  18 21:18 123
-rw-r--r--. 1 root root     0 3月  17 12:04 123.txt
-rw-r--r--. 2 root root  4735 3月  17 17:22 1_hard.txt
lrwxrwxrwx. 1 root root     5 3月  18 21:39 1_soft.txt -> 1.txt
-rw-r--r--. 2 root root  4735 3月  17 17:22 1.txt
-rw-r--r--. 1 root root     0 3月  17 13:15 222.txt
drwxrwxr-x. 4 root user1   62 3月  17 21:34 234
-rw-rw-r--. 1 root root     0 3月  17 14:16 2.txt
-rw-------. 1 root root  1418 3月  14 05:58 anaconda-ks.cfg
[root@arslinux-01 ~]# ls -i
33599017 111        33583098 123.txt     33599013 1.txt    17098153 234
17098139 11111.txt  33599013 1_hard.txt  33599015 222.txt   33599016 2.txt
50803365 123        33583110 1_soft.txt    33583074 anaconda-ks.cfg
  • 创建一个文件和另外一个文件由相同的 inode 号,那么这两个文件相互为硬链接。
  • 删除源文件的话,软链接会失效,但是硬链接不受影响
  • 硬链接的文件实际文件在 inode 里,不同名称文件可理解为同一个 inode 的不同的表皮,硬链接不占双份空间,inode 必须要有至少一张皮
  • 硬链接不会占据双份的空间,因为实际只占用了一个 inode
  • 可以对文件做硬链接,但是不可以跨分区(不同分区在格式化时已经有各自的inode体系,不同分区可能有同一个 inode 号但是不相同的文件)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值