linux学习(九)set_uid、set_gid、stick_bit、软链接、硬链接

时间:2021-07-20 14:02:38

一、set_uid

set_uid其实是一种特殊权限,我们看一个文件:

[root@iZ25lzba47vZ ~]# ls -l /usr/bin/passwd
-rwsr-xr-x. root root Jun /usr/bin/passwd

它的所有者的x权限位被s代替了。那么这个s代表什么意思呢?它代表执行这条命令的普通用户,能暂时拥有这个命令所有者的身份。

举个例子来说,我们可以通过passwd命令来修改密码。我们再来看看密码存放的文件:

[root@iZ25lzba47vZ ~]# ls -l /etc/shadow
---------- root root Mar /etc/shadow

我们发现这个文件的权限为000,那么普通用户是怎么改的它呢?就是这个s权限。普通用户在执行这条命令时,临时有了root的身份。

同样的,我们知道普通用户是不能进入到root用户的家目录的。那么,如果我们给ls命令set_uid,应该可以实现吧。

[root@iZ25lzba47vZ ~]# ls -l /usr/bin/ls
-rwxr-xr-x. root root Jun /usr/bin/ls
[root@iZ25lzba47vZ ~]# chmod u+s /usr/bin/ls
[root@iZ25lzba47vZ ~]# !ls
ls -l /usr/bin/ls
-rwsr-xr-x. root root Jun /usr/bin/ls
[root@iZ25lzba47vZ ~]# su ruanwenwu
[ruanwenwu@iZ25lzba47vZ root]$ ls /root
.ipt Application Document.pdf npm-debug.log ruanwenwu syncwithgit.sh
.cap a.php a.txt iptables.bak oneinstack shellscripts

二、set_gid

set_gid作用在文件时的意义和set_uid基本一样,只是普通用户在执行它时获得的是文件的所属组的身份。

set_gid作用于目录时,目录下新建的文件和目录的组名都会和该目录一致:

[root@iZ25lzba47vZ ~]# chown :ruanwenwu
[root@iZ25lzba47vZ ~]# ls -ld
drw-rwsrw- ruanwenwu ruanwenwu Oct :
[root@iZ25lzba47vZ ~]# touch /.txt
[root@iZ25lzba47vZ ~]# ls -l
total
-rw-r--r-- root ruanwenwu Oct : .txt
drwxr-xr-x ruanwenwu ruanwenwu Oct :
drwxrwxr-- root root Oct :
-rw-rw-r-- root root Oct : .txt
[root@iZ25lzba47vZ ~]#

发现设置了set_gid之后,在1目录下新建的1.txt的所属组变成了ruanwenwu。

三、stick_bit

防删除位。它的作用就是:我的文件你不能删除。但是你可以修改。/tmp/目录就有这个防删除位,在其他用户的可执行权限位的地方。

[root@iZ25lzba47vZ ~]# ls -ld /tmp
drwxrwxrwt. www www Oct : /tmp
[root@iZ25lzba47vZ ~]#

现在我们用ruanwenwu这个用户创建一个文件,然后切换到test用户,看能不能删掉。

[ruanwenwu@iZ25lzba47vZ tmp]$ ls -l ruanwenwu.txt
-rw-rw-r-- ruanwenwu ruanwenwu Oct : ruanwenwu.txt
[ruanwenwu@iZ25lzba47vZ tmp]$ passwd test
passwd: Only root can specify a user name.
[ruanwenwu@iZ25lzba47vZ tmp]$ su root
Password:
[root@iZ25lzba47vZ tmp]# passwd test
Changing password for user test.
New password:
BAD PASSWORD: The password is shorter than characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@iZ25lzba47vZ tmp]# su test
[test@iZ25lzba47vZ tmp]$ rm -rf ruanwenwu.txt
rm: cannot remove ‘ruanwenwu.txt’: Operation not permitted

发现不能删除,现在我们删除掉/tmp/的stick_bit,然后再来试一次:

[test@iZ25lzba47vZ tmp]$ chmod o-t /tmp
chmod: changing permissions of ‘/tmp’: Operation not permitted
[test@iZ25lzba47vZ tmp]$ su root
Password:
[root@iZ25lzba47vZ tmp]# chmod o-t /tmp/
[root@iZ25lzba47vZ tmp]# ls -ld /tmp
drwxrwxrwx. www www Oct : /tmp
[root@iZ25lzba47vZ tmp]# su test
[test@iZ25lzba47vZ tmp]$ rm -rf ruanwenwu.txt
[test@iZ25lzba47vZ tmp]$ ls
Aegis-<Guid(5A2C30A2-A87D-490A--6765EDAD7CBA)>
iZ25lzba47vZ.root.history-timestamp
iZ25lzba47vZ.ruanwenwu.history-timestamp
iZ25lzba47vZ.test.history-timestamp
mysql.sock
systemd-private-39567547dfdf4a37b00906a534c87627-ntpd.service-QxSGFm

发现去掉防删除位之后,test用户可以删除ruanwenwu的文件了。

四、软连接

软链接相当于windows里的快捷方式。他的生成方式:

[root@iZ25lzba47vZ tmp]# ln -s /tmp/a.txt /ccc.txt
[root@iZ25lzba47vZ tmp]# ls -l /ccc.txt
lrwxrwxrwx root root Oct : /ccc.txt -> /tmp/a.txt

软连接的特点是,占用空间小。在生成软连接时,尽量使用绝对路径,这样在移动软连接时,链接就不会失效。

五、硬链接

硬链接和原来的文件占用同一个inode地址:

[root@iZ25lzba47vZ tmp]# ln a.txt c.txt
[root@iZ25lzba47vZ tmp]# ls -l ./
total
srwxr-xr-x root root Oct : Aegis-<Guid(5A2C30A2-A87D-490A--6765EDAD7CBA)>
-rw-rw-r-- test test Oct : a.txt
lrwxrwxrwx test test Oct : b.txt -> a.txt
-rw-rw-r-- test test Oct : c.txt
-rw-r--r-- root root Oct : iZ25lzba47vZ.root.history-timestamp
-rw-rw-r-- ruanwenwu ruanwenwu Oct : iZ25lzba47vZ.ruanwenwu.history-timestamp
-rw-rw-r-- test test Oct : iZ25lzba47vZ.test.history-timestamp
srwxrwxrwx mysql mysql Oct : mysql.sock
-rw------- www www Oct : sess_vi5k84ucsecldno3kg6edqdh72
drwx------ www www May systemd-private-39567547dfdf4a37b00906a534c87627-ntpd.service-QxSGFm
[root@iZ25lzba47vZ tmp]# ls -i ./
Aegis-<Guid(5A2C30A2-A87D-490A--6765EDAD7CBA)>
a.txt
b.txt
c.txt
iZ25lzba47vZ.root.history-timestamp
iZ25lzba47vZ.ruanwenwu.history-timestamp
iZ25lzba47vZ.test.history-timestamp
mysql.sock
sess_vi5k84ucsecldno3kg6edqdh72
systemd-private-39567547dfdf4a37b00906a534c87627-ntpd.service-QxSGFm
[root@iZ25lzba47vZ tmp]# ls -i a.txt c.txt
a.txt c.txt

如果我们删除其中一个文件,另外一个文件会受影响吗?

[root@iZ25lzba47vZ tmp]# rm -rf a.txt
[root@iZ25lzba47vZ tmp]# ls -l c.txt
-rw-rw-r-- test test Oct : c.txt

发现,并没有影响到另外一个文件。

目录是不能做硬链接的,因为目录有自己的inode体系。

硬链接不能跨分区,因为每个分区都有自己的inode体系。