set_uid
权限说明:set uid权限针对二进制可执行文件,使文件在执行阶段具有文件所有者的权限。比如passwd这个命令就具有该权限。当普通用户执行passwd命令时,可以临时获得root权限,从而可以更改密码。
作用 :set uid的作用是保证普通用户临时拥有该命令所有者的身份。给一个文件设置成set uid,前提这个文件是一个二进制,可执行的文件。 **用法:**把一个二进制可执行文件的,所有者的权限上 +s 就添加上了set uid权限。 例如把ls命令设置成set uid 。 #chmod u+s /usr/bin/ls
实例:root用户
[root@cham2 ~]# passwd root ^C[root@cham2 ~]# ls -l /usr/bin/passwd-rwsr-xr-x. 1 root root 27832 6月 10 2014 /usr/bin/passwd[root@cham2 ~]# set_uid ^C[root@cham2 ~]# passwd ^C[root@cham2 ~]# ls /etc/shadow/etc/shadow[root@cham2 ~]# ls -l !$ls -l /etc/shadow---------- 1 root root 689 10月 25 16:11 /etc/shadow[root@cham2 ~]# w 13:28:19 up 1 day, 1:02, 2 users, load average: 0.00, 0.01, 0.05USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATroot tty1 二12 25:52m 0.21s 0.21s -bashroot pts/1 192.168.230.1 12:22 3.00s 0.05s 0.00s w[root@cham2 ~]# whowho whoami [root@cham2 ~]# whoamiroot[root@cham2 ~]# rws ^C[root@cham2 ~]# ls -l /usr/bin/ls-rwxr-xr-x. 1 root root 117656 11月 6 2016 /usr/bin/ls[root@cham2 ~]# chmod u+s /usr/bin/ls[root@cham2 ~]# ls -l /usr/bin/ls-rwsr-xr-x. 1 root root 117656 11月 6 2016 /usr/bin/ls[root@cham2 ~]# chmod u-s /usr/bin/ls[root@cham2 ~]# chmod u=rws /usr/bin/ls[root@cham2 ~]# ls -l /usr/bin/ls-rwSr-xr-x. 1 root root 117656 11月 6 2016 /usr/bin/ls[root@cham2 ~]# chmod u+x /usr/bin/ls[root@cham2 ~]# ls -l /usr/bin/ls-rwsr-xr-x. 1 root root 117656 11月 6 2016 /usr/bin/ls[root@cham2 ~]#
user用户:cham
Last login: Thu Oct 26 13:36:57 2017[root@cham2 ~]# su - cham上一次登录:四 10月 26 13:36:00 CST 2017pts/1 上[cham@cham2 ~]$ ls /root/111 11.txt 123 12.txt 1.txt 22.txt 234 2.txt anaconda-ks.cfg.1[cham@cham2 ~]$ ls -ld /root/dr-xr-x---. 6 root root 284 10月 25 17:31 /root/[cham@cham2 ~]$ ls /root/ls: 无法打开目录/root/: 权限不够
****#chmod u-s /usr/bin/ls //去除set uid权限****
set_gid
set gid不仅能作用在文件上,还能作用在目录上。 权限说明: set gid权限可以作用在文件上(二进制可执行文件),也可以作用在目录上。当作用在文件上时,其功能和set,uid一样,它会使文件在执行阶段具有文件所属组的权限。目录被设置这个权限后,任何用户在此目录下创建的文件都具有和该目录所属的组相同的组。 作用: 当作用在 文件 上的时候 ,其作用和set uid是类似的,它可以让执行这个文件的普通用户临时拥有所属组的身份。 当作用在 目录 上的时候,创建子目录或子文件的所属组和该目录的所属组是保持一致的。 用法: 把一个二进制可执行文件的,所属组的权限上 +s 就添加上了set gid权限。用在 文件上的时候 ,其作用和set uid是类似的,它可以让执行这个文件的普通用户临时拥有所属组的身份。
把ls命令设置成set gid 。 #chmod g+s /usr/bin/ls
实例:
[root@cham2 ~]# ls -l /usr/bin/ls-rwxr-xr-x. 1 root root 117656 11月 6 2016 /usr/bin/ls[root@cham2 ~]# chmod g+s /usr/bin/ls[root@cham2 ~]# ls -l /usr/bin/ls-rwxr-sr-x. 1 root root 117656 11月 6 2016 /usr/bin/ls 所属组中的x变成了s切换user cham[cham@cham2 ~]$ ls -ld /root/dr-xr-x---. 6 root root 284 10月 25 17:31 /root/[cham@cham2 ~]$ ls /root/111 11.txt 123 12.txt 1.txt 22.txt 234 2.txt anaconda-ks.cfg.1[cham@cham2 ~]$ ls -ld /root/dr-xr-x---. 6 root root 284 10月 25 17:31 /root/
用在目录上的时候,创建子目录或子文件的所属组和该目录的所属组是保持一致的
实例:把234目录添加set gid权限,然后更改所属组为user1,然后在234目录下创建一个文件chamlinux,其chamlinux的所属组为user1。 如果没有添加set gid权限的话,chamlinux的所属组应该是root
[root@cham2 ~]# chmod g+s 234 给234目录加上set gid [root@cham2 ~]# ls -ld 234 drwxrwsr-x 2 root root 6 10月 25 16:41 234 所属组多了个s[root@cham2 ~]# chown :user1 234 把234的所属组改成user1[root@cham2 ~]# ls -ld 234 看一下drwxrwsr-x 2 root user1 6 10月 25 16:41 234 [root@cham2 ~]# touch 234/chamlinux 新建一个文件[root@cham2 ~]# ls -l 234总用量 0-rw-r--r-- 1 root user1 0 10月 26 14:07 chamlinux[root@cham2 ~]# mkdir 234/ch 234下面新建一个子目录[root@cham2 ~]# ls -l 234/ 所属组都是user1总用量 0drwxr-sr-x 2 root user1 6 10月 26 14:08 ch-rw-r--r-- 1 root user1 0 10月 26 14:07 chamlinux[root@cham2 ~]# chmod g-s 234 去掉set gid[root@cham2 ~]# touch 234/chamlinux111 234新建子文件[root@cham2 ~]# !ls 看一下ls -l 234/总用量 0drwxr-sr-x 2 root user1 6 10月 26 14:08 ch-rw-r--r-- 1 root user1 0 10月 26 14:07 chamlinux-rw-r--r-- 1 root root 0 10月 26 14:09 chamlinux111 所属组是root [root@cham2 ~]# mkdir 234/ch1 新建子目录 [root@cham2 ~]# !ls 看一下ls -l 234/总用量 0drwxr-sr-x 2 root user1 6 10月 26 14:08 chdrwxr-xr-x 2 root root 6 10月 26 14:09 ch1 所属组是root-rw-r--r-- 1 root user1 0 10月 26 14:07 chamlinux-rw-r--r-- 1 root root 0 10月 26 14:09 chamlinux111[root@cham2 ~]# [root@cham2 ~]# [root@cham2 ~]# ls -ld 234drwxrwxr-x 4 root user1 64 10月 26 14:09 234
stick_bit
可以理解为防删除位。文件是否可以被某用户删除,主要取决于该文件所在的目录是否对该用户具有写权限。如果没有写权限,则这个目录下的所有文件都不能删除,同时也不能添加新的文件。如果希望用户能够添加文件但不能删除该目录下其他用户的文件,则可以对父目录增加该权限。设置该权限后,就算用户对目录具有写权限,也不能删除其他用户的文件。
**作用: * 防止别人删除自己的文件,root用户除外。/tmp的权限是rwt,而不是传统的rwx。这个叫做防删除位
实例: 在cham用户下的 /tmp目录下 touch一个cham文件,并#chmod 777cham的权限设置成777,在cham里面编辑一些内容。然后切换到user1用户下,在user1用户下能查看并修改cham文件,而不能删除(除cham用户和root用户拥有修改删除的权限,其他用户都不能删除)。
[cham@cham2 ~]$ whoamicham[cham@cham2 ~]$ cd /tmp/[cham@cham2 tmp]$ ls2.txt cham2 ks-script-JG2UJk yum.log3.txt chamlinux systemd-private-5ec76fd91759498b901e85cba2554a24-vmtoolsd.service-H1l4Cy[cham@cham2 tmp]$ touch cham[cham@cham2 tmp]$ ls -l总用量 8-rw-r--r-- 1 root root 2 10月 24 21:21 2.txt-rwx------ 1 root root 0 10月 25 15:24 3.txt-rw-rw-r-- 1 cham cham 0 10月 26 16:20 chamdrw-r--r-x 2 user1 cham 19 10月 25 15:41 cham2drwxr-xr-x 4 root root 28 10月 24 12:58 chamlinux-rwx------. 1 root root 836 10月 19 07:00 ks-script-JG2UJkdrwx------ 3 root root 17 10月 24 12:21 systemd-private-5ec76fd91759498b901e85cba2554a24-vmtoolsd.service-H1l4Cy-rw-------. 1 user1 root 0 10月 19 06:55 yum.log[cham@cham2 tmp]$ vi cham[cham@cham2 tmp]$ chmod 777 cham[cham@cham2 tmp]$ ls -l总用量 12-rw-r--r-- 1 root root 2 10月 24 21:21 2.txt-rwx------ 1 root root 0 10月 25 15:24 3.txt-rwxrwxrwx 1 cham cham 55 10月 26 16:20 chamdrw-r--r-x 2 user1 cham 19 10月 25 15:41 cham2drwxr-xr-x 4 root root 28 10月 24 12:58 chamlinux-rwx------. 1 root root 836 10月 19 07:00 ks-script-JG2UJkdrwx------ 3 root root 17 10月 24 12:21 systemd-private-5ec76fd91759498b901e85cba2554a24-vmtoolsd.service-H1l4Cy-rw-------. 1 user1 root 0 10月 19 06:55 yum.log[cham@cham2 tmp]$ ls -l cham-rwxrwxrwx 1 cham cham 55 10月 26 16:20 cham[user1@cham2 tmp]$ cd /tmp/[user1@cham2 tmp]$ ls2.txt cham2 systemd-private-5ec76fd91759498b901e85cba2554a24-vmtoolsd.service-H1l4Cy3.txt chamlinux yum.logcham ks-script-JG2UJk[user1@cham2 tmp]$ vi cham user1可以修改[user1@cham2 tmp]$ rm -f cham 但不能删除rm: 无法删除"cham": 不允许的操作
实例:
[user1@cham2 tmp]$ mkdir user1[user1@cham2 tmp]$ chmod 777 user1[user1@cham2 tmp]$ ls -l总用量 12-rw-r--r-- 1 root root 2 10月 24 21:21 2.txt-rwx------ 1 root root 0 10月 25 15:24 3.txt-rwxrwxrwx 1 cham cham 60 10月 26 16:22 chamdrw-r--r-x 2 user1 cham 19 10月 25 15:41 cham2drwxr-xr-x 4 root root 28 10月 24 12:58 chamlinux-rwx------. 1 root root 836 10月 19 07:00 ks-script-JG2UJkdrwx------ 3 root root 17 10月 24 12:21 systemd-private-5ec76fd91759498b901e85cba2554a24-vmtoolsd.service-H1l4Cydrwxrwxrwx 2 user1 user1 6 10月 26 16:24 user1-rw-------. 1 user1 root 0 10月 19 06:55 yum.log[user1@cham2 tmp]$ cd uer1-bash: cd: uer1: 没有那个文件或目录[user1@cham2 tmp]$ cd user1[user1@cham2 user1]$ ls -l总用量 0-rw-rw-r-- 1 cham cham 0 10月 26 16:25 1.txtdrwxrwxr-x 2 cham cham 6 10月 26 16:25 456[cham@cham2 tmp]$ cd user1[cham@cham2 user1]$ touch 1.txt[cham@cham2 user1]$ mkdir 456[cham@cham2 user1]$ ls1.txt 456[cham@cham2 user1]$ ls -l总用量 0-rw-rw-r-- 1 cham cham 0 10月 26 16:25 1.txtdrwxrwxr-x 2 cham cham 6 10月 26 16:25 456[user1@cham2 user1]$ rm -f 1.txt[user1@cham2 user1]$ rm -r 456rm:是否删除有写保护的目录 "456"?y[user1@cham2 user1]$ ls[user1@cham2 user1]$ ls -l总用量 0[user1@cham2 user1]$ pwd/tmp/user1[cham@cham2 tmp]$ mkdir cham11[cham@cham2 tmp]$ chmod 777 cham11[cham@cham2 tmp]$ ls -l总用量 12-rw-r--r-- 1 root root 2 10月 24 21:21 2.txt-rwx------ 1 root root 0 10月 25 15:24 3.txt-rwxrwxrwx 1 cham cham 1 10月 26 16:48 chamdrwxrwxrwx 2 cham cham 6 10月 26 16:58 cham11drw-r--r-x 2 user1 cham 19 10月 25 15:41 cham2drwxr-xr-x 4 root root 28 10月 24 12:58 chamlinux-rwx------. 1 root root 836 10月 19 07:00 ks-script-JG2UJkdrwx------ 3 root root 17 10月 24 12:21 systemd-private-5ec76fd91759498b901e85cba2554a24-vmtoolsd.service-H1l4Cydrwxrwxrwx 2 user1 user1 6 10月 26 16:27 user1-rw-------. 1 user1 root 0 10月 19 06:55 yum.log[user1@cham2 tmp]$ cd cham11[user1@cham2 cham11]$ touch 123[user1@cham2 cham11]$ mkdir 456[user1@cham2 cham11]$ ls123 456[cham@cham2 tmp]$ cd cham11[cham@cham2 cham11]$ rm -v 123rm:是否删除有写保护的普通空文件 "123"?y已删除"123"[cham@cham2 cham11]$ rm -v 456rm: 无法删除"456": 是一个目录[cham@cham2 cham11]$ rm -rv 456rm:是否删除有写保护的目录 "456"?y已删除目录:"456"
总结: **一个目录下的文件能不能删除,不取决于这个目录下的文件的权限,而是取决于这个文件所在目录的权限,是否可写。**所以当一个目录的others权限是可读可写时(目录默认权限有可执行,因为打开目录,就相当于执行了该目录),其他人就可以删除这个目录下的任何文件,包括root文件。
软链接
软链接 做软链接时建议!尽量!使用绝对路径
用命令ls -l查看的第一个属性为 l 的文件就是链接文件,它类似于windows下的快捷方式。这种文件在Linux中是很常见的。如下图
可以看到这一行的第一个字母是 l ,这个 l 就代表它是一个链接文件 。/bin 相当于Windows下的快捷方式,而usr/bin才是它真正的目录。
![QQ截图20171026213621.png](https://static.oschina.net/uploads/img/201710/26235228_eWVe.png)
使用命令 #ls -l /lib64 的时候,可以看到一些粉绿色的,第一个属性为 l 文件都是一些软链接文件。
![QQ截图20171026220900.png](https://static.oschina.net/uploads/img/201710/26235355_yQTf.png)
软链接: 也称为符号链接,即“symbolic links/soft links”,是一个通过名称指向另一个不同文件的特殊的文件类型,是一个单独的文件,有着不同于原文件(目标文件)的属性,相当于Windows系统中的快捷方式。当对软链接进行大多数操作(包括打开、读取、写入等等)时,系统内核会自动在软链接的目标文件上执行;但某些操作,例如删除,会直接对链接文件本身操作。
软链接特性:
- 软链接和目标文件不是同一个文件,节点号不同。
- 目标文件的删除操作会影响软链接文件。当链接文件被删除时,不会对目标文件产生影响;当删除目标文件/目录时,由于失去了目标指向,软链接会失效,变成死链。
- 能跨分区创建软链接。(使用命令 #hf -h 查看分区的大小是使用情况的时候,挂载点那一行就是分区。)
- 命令语法: ln[-s][来源文件][目的文件]
- 命令描述: 建立软链接
- 命令参数: -s 建立软链接时,必须要加上 -s 选项,如果不加 -s 选项就是建立硬链接。
实例
ln -s 源文件 目标文件
例如:# ln -s /root/123/.1.txt /tmp/123/12/1.txt
实例:
[root@cham2 tmp]# ln -s /tmp/yum.log /root/111/yum.log[root@cham2 tmp]# ls -l /root/111/总用量 16-rw-r--r-- 1 root root 0 10月 25 17:20 12.tx~-rw-r--r-- 1 root root 195 10月 25 17:23 12.txt-rw-r--r-- 1 root root 12288 10月 25 17:20 12_txt.swpdrwxr-xr-x 2 root root 6 10月 25 17:16 222-rw-r--r-- 1 root root 0 10月 25 17:20 4913lrwxrwxrwx 1 root root 12 10月 26 20:32 yum.log -> /tmp/yum.log[root@cham2 tmp]# ls -l总用量 16-rw-r--r-- 1 root root 2 10月 24 21:21 2.txt-rwxrwxrwx 1 cham cham 6 10月 26 20:02 321321-rwx------ 1 root root 0 10月 25 15:24 3.txt-rwxrwxrwx 1 cham cham 1 10月 26 16:48 chamdrwxrwxrwx 2 cham cham 6 10月 26 16:59 cham11drw-r--r-x 2 user1 cham 19 10月 25 15:41 cham2drwxr-xr-x 4 root root 28 10月 24 12:58 chamlinux-rwx------. 1 root root 836 10月 19 07:00 ks-script-JG2UJkdrwx------ 3 root root 17 10月 24 12:21 systemd-private-5ec76fd91759498b901e85cba2554a24-vmtoolsd.service-H1l4Cydrwxrwxrwx 2 user1 user1 6 10月 26 16:27 user1-rw-------. 1 user1 root 0 10月 19 06:55 yum.log[root@cham2 tmp]# [root@cham2 tmp]# ln -s /tmp/cham2 /root/111/cham3[root@cham2 tmp]# ls -l !$ls -l /root/111/cham3lrwxrwxrwx 1 root root 10 10月 26 20:34 /root/111/cham3 -> /tmp/cham2[root@cham2 tmp]# ls2.txt cham chamlinux user1321321 cham11 ks-script-JG2UJk yum.log3.txt cham2 systemd-private-5ec76fd91759498b901e85cba2554a24-vmtoolsd.service-H1l4Cy[root@cham2 tmp]# ln -s yum.log chamlinux.log[root@cham2 tmp]# ls -l总用量 16-rw-r--r-- 1 root root 2 10月 24 21:21 2.txt-rwxrwxrwx 1 cham cham 6 10月 26 20:02 321321-rwx------ 1 root root 0 10月 25 15:24 3.txt-rwxrwxrwx 1 cham cham 1 10月 26 16:48 chamdrwxrwxrwx 2 cham cham 6 10月 26 16:59 cham11drw-r--r-x 2 user1 cham 19 10月 25 15:41 cham2drwxr-xr-x 4 root root 28 10月 24 12:58 chamlinuxlrwxrwxrwx 1 root root 7 10月 26 20:36 chamlinux.log -> yum.log-rwx------. 1 root root 836 10月 19 07:00 ks-script-JG2UJkdrwx------ 3 root root 17 10月 24 12:21 systemd-private-5ec76fd91759498b901e85cba2554a24-vmtoolsd.service-H1l4Cydrwxrwxrwx 2 user1 user1 6 10月 26 16:27 user1-rw-------. 1 user1 root 0 10月 19 06:55 yum.log[root@cham2 tmp]# mv chamlinux.log /root/111/[root@cham2 tmp]# cd /root/111/[root@cham2 111]# ls12.tx~ 12.txt 12_txt.swp 222 4913 cham3 chamlinux.log yum.log[root@cham2 111]# ls -l总用量 16-rw-r--r-- 1 root root 0 10月 25 17:20 12.tx~-rw-r--r-- 1 root root 195 10月 25 17:23 12.txt-rw-r--r-- 1 root root 12288 10月 25 17:20 12_txt.swpdrwxr-xr-x 2 root root 6 10月 25 17:16 222-rw-r--r-- 1 root root 0 10月 25 17:20 4913lrwxrwxrwx 1 root root 10 10月 26 20:34 cham3 -> /tmp/cham2lrwxrwxrwx 1 root root 7 10月 26 20:36 chamlinux.log -> yum.loglrwxrwxrwx 1 root root 12 10月 26 20:32 yum.log -> /tmp/yum.log[root@cham2 111]# cd /root/[root@cham2 ~]# ls111 11.txt 123 12.txt 1.txt 22.txt 234 2.txt anaconda-ks.cfg.1[root@cham2 ~]# mv 111/chamlinux.log 123/ .[root@cham2 ~]# cd 123[root@cham2 123]# ls -l总用量 0lrwxrwxrwx 1 root root 7 10月 26 20:36 chamlinux.log -> yum.log[root@cham2 123]# touch yum.log[root@cham2 123]# ls -l总用量 0lrwxrwxrwx 1 root root 7 10月 26 20:36 chamlinux.log -> yum.log-rw-r--r-- 1 root root 0 10月 26 20:39 yum.log[root@cham2 123]# lschamlinux.log yum.log[root@cham2 123]# rm -f yum.log[root@cham2 123]# mv ../111/yum.log . 移动上级目录到当前目录[root@cham2 123]# ls -l总用量 0lrwxrwxrwx 1 root root 7 10月 26 20:36 chamlinux.log -> yum.loglrwxrwxrwx 1 root root 12 10月 26 20:32 yum.log -> /tmp/yum.log[root@cham2 123]#
-
用 #hf -h 查看分区的大小和使用情况,可以看出/root用户下的空间大小所剩不多,而 /目录的空间很大。
比如/root下有一个 /root/dl.log 日志文件,当dl.log日志文件不停的写入日志时,所占空间是比较大的,这时候我们可以把dl.log软链接到/目录 具体操作如下。
#cp /boot/dl.log /dl.log #rm -rf /boot/dl.log #ln -s /dl.log /boot/dl.log
上述命令说明:
1.把 /boot 下的 dl.log 日志拷贝到/目录下。 2.删除 /boot 下的 dl.log 日志。 3.把 / 目录下的 /dl.log 软链接到 /boot/dl.log 这样的话,dl.log在日志的时候,依然写到了/boot/dl.log下。软链接总结
- 当我们建立软链接的时候,目的文件不可以存在,否则建立不成功。
- 所有软链接本身就只能是一个文件!这个文件可以指向目录也可以指向文件。
- 当我们建立软链接的时候,一般情况下都是绝对路径。如果建立一个相对路径的软链接,会有弊端!当我们把所有的文件拷贝到另一台机器上,会发现源文件不存在。
硬链接
硬链接: 也即“hard link”,其本质为现有文件的另一个名字,因此硬链接和原文件是无法区分的。从技术上讲,它们共享相同的inode,而inode几乎包含文件的所有信息,包括指向数据块的直接/间接指针。
硬链接不支持目录,仅支持文件,不能跨分区,可删除,因为还有其他文件使用了inode
- 创建一个文件,这个文件和另外一个文件的inode号一样,那么这两个文件相互为硬链接文件。
- 只能对文件做硬链接,不能对目录做硬链接。
- 不能跨系统分区创建硬链接。
- 只能对已存在的文件进行创建。
- 删除一个硬链接文件并不影响其他有相同inode号的文件。
[root@cham2 ~]# ln 1.txt 1_heard.txt[root@cham2 ~]# ln -s 1.txt 1_sorft[root@cham2 ~]# ls -l总用量 16drwxrwxr-- 3 root root 126 10月 26 20:41 111-rw-rw-r-- 1 root root 135 10月 25 17:44 11.txtdrwxr-xr-x 2 root root 42 10月 26 20:41 123-rw-r--r-- 1 root root 65 10月 25 17:20 12.txt-rw-r--r-- 2 root root 0 10月 25 17:31 1_heard.txtlrwxrwxrwx 1 root root 5 10月 26 21:31 1_sorft -> 1.txt-rw-r--r-- 2 root root 0 10月 25 17:31 1.txt-rw-r--r-- 1 root root 0 10月 25 17:07 22.txtdrwxrwxr-x 4 root user1 64 10月 26 14:09 234-rwx------ 1 root root 1008 10月 25 16:41 2.txt-rw-------. 1 root root 1422 10月 19 07:00 anaconda-ks.cfg.1[root@cham2 ~]# ls -l总用量 16drwxrwxr-- 3 root root 126 10月 26 20:41 111-rw-rw-r-- 1 root root 135 10月 25 17:44 11.txtdrwxr-xr-x 2 root root 42 10月 26 20:41 123-rw-r--r-- 1 root root 65 10月 25 17:20 12.txt-rw-r--r-- 2 root root 0 10月 25 17:31 1_heard.txtlrwxrwxrwx 1 root root 5 10月 26 21:31 1_sorft -> 1.txt-rw-r--r-- 2 root root 0 10月 25 17:31 1.txt-rw-r--r-- 1 root root 0 10月 25 17:07 22.txtdrwxrwxr-x 4 root user1 64 10月 26 14:09 234-rwx------ 1 root root 1008 10月 25 16:41 2.txt-rw-r--r-- 2 root root 0 10月 26 21:33 88_heard.txtlrwxrwxrwx 1 root root 6 10月 26 21:34 88_sorft.txt -> 88.txt-rw-r--r-- 2 root root 0 10月 26 21:33 88.txt-rw-------. 1 root root 1422 10月 19 07:00 anaconda-ks.cfg.1[root@cham2 ~]# ls -i33575631 111 33575641 1_heard.txt 16783440 234 33575648 88.txt33575016 11.txt 33575647 1_sorft 16777669 2.txt 33574987 anaconda-ks.cfg.1 30154 123 33575641 1.txt 33575648 88_heard.txt33575640 12.txt 33575633 22.txt 33575642 88_sorft.txt[root@cham2 ~]# rm -f 1.txt[root@cham2 ~]# ls -l总用量 16drwxrwxr-- 3 root root 126 10月 26 20:41 111-rw-rw-r-- 1 root root 135 10月 25 17:44 11.txtdrwxr-xr-x 2 root root 42 10月 26 20:41 123-rw-r--r-- 1 root root 65 10月 25 17:20 12.txt-rw-r--r-- 1 root root 0 10月 25 17:31 1_heard.txtlrwxrwxrwx 1 root root 5 10月 26 21:31 1_sorft -> 1.txt-rw-r--r-- 1 root root 0 10月 25 17:07 22.txtdrwxrwxr-x 4 root user1 64 10月 26 14:09 234-rwx------ 1 root root 1008 10月 25 16:41 2.txt-rw-r--r-- 2 root root 0 10月 26 21:33 88_heard.txtlrwxrwxrwx 1 root root 6 10月 26 21:34 88_sorft.txt -> 88.txt-rw-r--r-- 2 root root 0 10月 26 21:33 88.txt-rw-------. 1 root root 1422 10月 19 07:00 anaconda-ks.cfg.1[root@cham2 ~]# ls -i 1_heard.txt33575641 1_heard.txt[root@cham2 ~]# ln 111 cham111ln: "111": 不允许将硬链接指向目录[root@cham2 ~]# df -h文件系统 容量 已用 可用 已用% 挂载点/dev/sda3 17G 1.1G 16G 7% /devtmpfs 483M 0 483M 0% /devtmpfs 493M 0 493M 0% /dev/shmtmpfs 493M 6.8M 486M 2% /runtmpfs 493M 0 493M 0% /sys/fs/cgroup/dev/sda1 187M 109M 79M 59% /boottmpfs 99M 0 99M 0% /run/user/0[root@cham2 ~]# ls -i /boot/ 71 config-3.10.0-514.el7.x86_64 67 grub131136 grub2 76 initramfs-0-rescue-0f3cf64915c04d4e93e4dae2684b907d.img 75 initramfs-3.10.0-514.el7.x86_64.img194501 initramfs-3.10.0-514.el7.x86_64kdump.img 74 initrd-plymouth.img 72 symvers-3.10.0-514.el7.x86_64.gz 70 System.map-3.10.0-514.el7.x86_64 77 vmlinuz-0-rescue-0f3cf64915c04d4e93e4dae2684b907d 73 vmlinuz-3.10.0-514.el7.x86_64[root@cham2 ~]# ln /boot/config-3.10.0-514.el7.x86_64 /tmp/config.1ln: 无法创建硬链接"/tmp/config.1" => "/boot/config-3.10.0-514.el7.x86_64": 无效的跨设备连接[root@cham2 ~]# ls /boot/config-3.10.0-514.el7.x86_64grubgrub2initramfs-0-rescue-0f3cf64915c04d4e93e4dae2684b907d.imginitramfs-3.10.0-514.el7.x86_64.imginitramfs-3.10.0-514.el7.x86_64kdump.imginitrd-plymouth.imgsymvers-3.10.0-514.el7.x86_64.gzSystem.map-3.10.0-514.el7.x86_64vmlinuz-0-rescue-0f3cf64915c04d4e93e4dae2684b907dvmlinuz-3.10.0-514.el7.x86_64[root@cham2 ~]# ls /bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var[root@cham2 ~]# ls -i / 95 bin 16777281 etc 82 lib64 33584771 opt 8124 run 1 sys 50331713 var 64 boot 50331740 home 83 media 1 proc 99 sbin 16777288 tmp 1026 dev 98 lib 16777681 mnt 33574977 root 50331741 srv 33584746 usr[root@cham2 ~]#
- 硬链接的inode和源文件的inode是一致的。
- 硬链接的内存大小和源文件的内存大小是一致的。
- 硬链接和源文件的时间也是相同的。
-
硬链接与软链接区别:
硬链接:
- 当系统要读取一个文件时,就会先去读 inode table, 一个文件对应一个 inode, 然后再根据 inode 中的信息到块区域去将数据取出来。硬链接是直接使用了和源文件相同的 inode, 硬链接文件直接链接到文件放置的块区域。 也就是说,进行硬链接时实际上该文件内容没有任何变化, 只是制定了相同的 inode。
- 创建硬链接会增加额外的记录项以引用文件,对应于同一文件系统上一个物理文件每个文件引用相同的 inode 号 创建时链接数递增
- 删除文件时:rm 命令递减计数的链接,文件要存在,至少有一个链接数当链接数为零时,该文件被删除
- 不能跨越驱动器或分区 语法 : ln filename [linkname ]
-
软链接:
- 软链接是建立一个独立的文件, 而这个文件的作用是当读取这个链接文件时,他会把读取的行为转发到该文件所 链接 的文件上。
- 一个符号链接指向另一个文件。
- 可以对目录或文件进行软链接。
- 可以跨分区
- 指向的是另一个文件的路径;其大小为指向的路径字符串的长度。
- 不增加或减少目标文件inode 的引用计数。 语法: ln -s filename [linkname]