上犹电脑信息网我们一直在努力
您的位置:上犹电脑信息网 > 文件问题 > set_uid、set_gid、stick_bit、软链接和硬连接文件-tmp文件

set_uid、set_gid、stick_bit、软链接和硬连接文件-tmp文件

作者:上犹日期:

返回目录:文件问题

特殊权限

set_uid

设置了set_uid的二进制文件,其他用户执行时,将临时拥有该文件所有者的权限,以此执行!最常用的命令

设置set_uid的前提:必须是 二进制可执行文件目录无效

在u的权限组内没有x权限时,设置set_uid后s位将变为大写(S)。

#未设置set_uid,普通用户查看/root目录
[castiel@centos7 ~]$ /usr/bin/ls /root
/usr/bin/ls: 无法打开目录/root: 权限不够
#设置set_uid后,可以以root权限查看/root
[root@centos7 ~]# chmod u+s /usr/bin/ls
[root@centos7 ~]# ls -l /usr/bin/ls
-rwsr-xr-x. 1 root root 117656 ... /usr/bin/ls
[root@centos7 ~]# su - castiel
[castiel@centos7 ~]$ /usr/bin/ls /root
111 anaconda-ks.cfg
[root@centos7 ~]# ls -l /usr/bin/ls
-rwSr-xr-x. 1 root root 117656 ... /usr/bin/ls

set_gid

设置了set_gid的文件执行时将拥有文件所属组(group)的权限。

此外对目录设置set_gid后,执行时当前命令的用户的所属组将会成为在该目录下新创建的文件、目录的所属组(group),

没有设置的其内文件的所属组是当前用户的所属组。

[root@centos7 tmp]# mkdir test
[root@centos7 tmp]# ls -ld test/
drwxr-xr-x. 2 root root 6 ... test/
[root@centos7 tmp]# chown :castiel test/
[root@centos7 tmp]# ls -ld test/
drwxr-xr-x. 2 root castiel 6 ... test/
# 未设置前创建目录和文件,与后续进行对比
[root@centos7 tmp]# mkdir test/111
[root@centos7 tmp]# touch test/222.txt
#设置set_gid,随后创建新目录和文件
[root@centos7 tmp]# chmod g+s test/
[root@centos7 tmp]# mkdir test/333/
[root@centos7 tmp]# touch test/444.txt
[root@centos7 tmp]# ls -l test/
总用量 0
drwxr-xr-x. 2 root root 6 ... 111
-rw-r--r--. 1 root root 0 ... 222.txt
drwxr-sr-x. 2 root castiel 6 ... 333
-rw-r--r--. 1 root castiel 0 ... 444.txt
# 同样的去除掉group内的x权限,set_gid位显示为S.
[root@localhost system]# chmod g-x /test
[root@localhost system]# chmod g+s /test
[root@localhost system]# ls -ld /test
drwxr-Sr-x. 2 root root 6 ... /test

sticky_bit

防删除位,对一个目录设置sticky_bit后,一个用户将无法删除目录内其他用户创建的文件!(root用户除外)

正常情况下,只要用户对该父目录拥有x权限,就可以删除目录下的文件、子目录,不需要关心用户对文件、子目录是否拥有x权限!

添加了sticky_bit权限后,可以防止其他用户误删。

[root@centos7 test]# ls -ld /test/
drwxr-xr-x. 2 root root 16 ... /test/
# 添加sticky_bit位
[root@centos7 test]# chmod o+t /test/
[root@centos7 test]# ls -ld /test/
drwxr-xr-t. 2 root root 16 ... /test/
# 同样的当没有x权限,sticky_bit显示为T
[root@localhost system]# chmod o-x /test
[root@localhost system]# chmod o+t /test
[root@localhost system]# ls -ld /test
drwxr-xr-T. 2 root root 6 ... /test

软链接

软链接文件相当于Windows下的快捷方式,文件类型为l。 系统目录/bin/目录就是/usr/bin/目录的软连接

使用方法:ln -s 目标文件/目录 软链接

优缺点

  • 好处:占用内存小
  • 缺点:源文件/目录被删除或移动,软链接将失效

修改软链接文件的内容的实质为

实用场景

对于一个空间不多的分区,如果有一个文件被进程所使用导致所占空间不断变大,这时如果不想该分区被数据写满,

可以先将该文件拷贝到其他大空间分区,并在原分区内创建软链接!!然后删除原处文件,原进程不受影响!

[root@centos7 test]# ls -l /bin
lrwxrwxrwx. 1 root root 7 ... /bin -> usr/bin

硬链接

使用方法: ln 源文件 硬连接文件

硬链接是对同一个文件所在内存地址的引用(inode号相等),硬链接和原文件互为硬链接!

  • 删除其中一个文件,另外的文件不受影响!
  • 修改其中一个文件,另外的文件内容也变化!

硬链接无法

关于目录

!!不能对目录设置硬链接

新创建的硬链接不会占用额外的空间!!因为其空间在创建源文件的时候就已经占用了,

对应于系统内的特定内存地址,硬链接只是是对该内存地址的引用。

[root@centos7 ~]# ls -ld .
dr-xr-x---. 3 root root 173 ... .
[root@centos7 ~]# ls -ild . /root/ /root/.ssh/..
16797761 dr-xr-x---. 3 root root 173 ... .
16797761 dr-xr-x---. 3 root root 173 ... /root/
16797761 dr-xr-x---. 3 root root 173 ... /root/.ssh/..
对于一个目录而言,其内包含的3个子目录都可以表示该目录,
对目录设置硬链接会产生循环创建,陷入死循环!
[root@centos7 test]# ls -li
总用量 8
8812615 -rw-r--r--. 2 root root 8 ... 1.txt
8812615 -rw-r--r--. 2 root root 8 ... 2.txt
[root@centos7 test]# du -sh /test/
4.0K /test/
[root@centos7 test]# du -sh 1.txt
4.0K 1.txt
[root@centos7 test]# du -sh 2.txt
4.0K 2.txt
set_uid、set_gid、stick_bit、软链接和硬连接文件

相关阅读

关键词不能为空
极力推荐

电脑蓝屏_电脑怎么了_win7问题_win10问题_设置问题_文件问题_上犹电脑信息网

关于我们