2.18 Special permission set_uid 2.19 Special permission set_gid 2.20 Special permission stick_bit 2.21 Soft link file 2.22 Hard link file

set_uid

Permission description: The set uid permission is for binary executable files, so that the file has the permission of the file owner during the execution phase. For example, the passwd command has this permission. When an ordinary user executes the passwd command, he or she can temporarily gain root privileges, so that the password can be changed.
Role: The role of set uid is to ensure that ordinary users temporarily have the identity of the owner of the command. Set a file to set uid, provided that the file is a binary, executable file.
**Usage:** Add the set uid permission to the owner's permission +s of a binary executable file.
For example, set the ls command to set uid. #chmod u+s /usr/bin/ls

Example: root user

[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.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      二12   25:52m  0.21s  0.21s -bash
root     pts/1    192.168.230.1    12:22    3.00s  0.05s  0.00s w
[root@cham2 ~]# who
who     whoami  
[root@cham2 ~]# whoami
root
[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 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 us /usr/bin/ls //Remove set uid permission****

set_gid
set gid not only works on files, but also on directories.
Permission description: The set gid permission can act on files (binary executable files) or on directories. When acting on a file, its function is the same as set, uid, it will make the file have the permissions of the group to which the file belongs during the execution phase. After a directory is set with this permission, any files created by a user in this directory will have the same group as the group to which the directory belongs.
Function:
When acting on a file, its function is similar to set uid. It allows ordinary users who execute the file to temporarily have the identity of the group to which they belong.
When acting on a directory, the group that creates the subdirectory or subfile is the same as the group that the directory belongs to.
Usage: Add the set gid permission to the permission of the group to which a binary executable file belongs to +s.

When used on a file, its function is similar to set uid, which allows ordinary users who execute the file to temporarily have the identity of the group to which they belong.

Set the ls command to set gid. #chmod g+s /usr/bin/ls

Example:

[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/

When used on a directory, the group to which the subdirectory or subfile is created is the same as the group to which the directory belongs.

Example: Add the set gid permission to the 234 directory, then change the group to user1, and then create a file chamlinux in the 234 directory, the group of which chamlinux belongs to is user1. If the set gid permission is not added, the group to which chamlinux belongs should be 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
总用量 0
drwxr-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/
总用量 0
drwxr-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/
总用量 0
drwxr-sr-x 2 root user1 6 10月 26 14:08 ch
drwxr-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 234
drwxrwxr-x 4 root user1 64 10月 26 14:09 234  

stick_bit

 It can be understood as the anti-deletion bit. Whether a file can be deleted by a user mainly depends on whether the directory where the file is located has write permission for the user. If there is no write permission, all files in this directory cannot be deleted, and new files cannot be added. If you want users to be able to add files but not delete files of other users in the directory, you can add this permission to the parent directory. After this permission is set, even if the user has write permission to the directory, the files of other users cannot be deleted.
**Function: * Prevent others from deleting their own files, except the root user.

The permissions for /tmp are rwt, not the traditional rwx. This is called the anti-erasure bit

Example: Touch a cham file in the /tmp directory under  the cham user   , and set the permission of #chmod 777cham to 777, and edit some content in the cham. Then switch to user1, under user1, you can view and modify the cham file, but cannot delete it (except for the cham user and root user who have the permission to modify and delete, other users cannot delete it).

[cham@cham2 ~]$ whoami
cham
[cham@cham2 ~]$ cd /tmp/
[cham@cham2 tmp]$ ls
2.txt  cham2      ks-script-JG2UJk                                                          yum.log
3.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 cham
drw-r--r-x  2 user1 cham  19 10月 25 15:41 cham2
drwxr-xr-x  4 root  root  28 10月 24 12:58 chamlinux
-rwx------. 1 root  root 836 10月 19 07:00 ks-script-JG2UJk
drwx------  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 cham
drw-r--r-x  2 user1 cham  19 10月 25 15:41 cham2
drwxr-xr-x  4 root  root  28 10月 24 12:58 chamlinux
-rwx------. 1 root  root 836 10月 19 07:00 ks-script-JG2UJk
drwx------  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]$ ls
2.txt  cham2             systemd-private-5ec76fd91759498b901e85cba2554a24-vmtoolsd.service-H1l4Cy
3.txt  chamlinux         yum.log
cham   ks-script-JG2UJk
[user1@cham2 tmp]$ vi cham      user1可以修改
[user1@cham2 tmp]$ rm -f cham   但不能删除
rm: 无法删除"cham": 不允许的操作

Example:

[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 cham
drw-r--r-x  2 user1 cham   19 10月 25 15:41 cham2
drwxr-xr-x  4 root  root   28 10月 24 12:58 chamlinux
-rwx------. 1 root  root  836 10月 19 07:00 ks-script-JG2UJk
drwx------  3 root  root   17 10月 24 12:21 systemd-private-5ec76fd91759498b901e85cba2554a24-vmtoolsd.service-H1l4Cy
drwxrwxrwx  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.txt
drwxrwxr-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]$ ls
1.txt  456
[cham@cham2 user1]$ ls -l
总用量 0
-rw-rw-r-- 1 cham cham 0 10月 26 16:25 1.txt
drwxrwxr-x 2 cham cham 6 10月 26 16:25 456



[user1@cham2 user1]$ rm -f 1.txt
[user1@cham2 user1]$ rm -r 456
rm:是否删除有写保护的目录 "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 cham
drwxrwxrwx  2 cham  cham    6 10月 26 16:58 cham11
drw-r--r-x  2 user1 cham   19 10月 25 15:41 cham2
drwxr-xr-x  4 root  root   28 10月 24 12:58 chamlinux
-rwx------. 1 root  root  836 10月 19 07:00 ks-script-JG2UJk
drwx------  3 root  root   17 10月 24 12:21 systemd-private-5ec76fd91759498b901e85cba2554a24-vmtoolsd.service-H1l4Cy
drwxrwxrwx  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]$ ls
123  456


[cham@cham2 tmp]$ cd cham11
[cham@cham2 cham11]$ rm -v 123
rm:是否删除有写保护的普通空文件 "123"?y
已删除"123"
[cham@cham2 cham11]$ rm -v 456
rm: 无法删除"456": 是一个目录
[cham@cham2 cham11]$ rm -rv 456
rm:是否删除有写保护的目录 "456"?y
已删除目录:"456"


Summary: ** Whether a file in a directory can be deleted does not depend on the permissions of the files in this directory, but depends on the permissions of the directory where the file is located and whether it is writable. ** So when the others permission of a directory is readable and writable (the default permission of the directory is executable, because opening the directory is equivalent to executing the directory), others can delete any files in this directory, including root document.

 

 

soft link

Soft links are recommended when doing soft links! Try your best! use absolute path

The first file whose attribute is l viewed with the command ls -l is the link file, which is similar to the shortcut under Windows. Such files are very common in Linux. As shown below

You can see that the first letter of this line is l, which means that it is a linked file. /bin is equivalent to a shortcut under Windows, and usr/bin is its real directory.
QQ screenshot 20171026213621.png

When using the command #ls -l /lib64, you can see some pink-green files, the first attribute is l files are some soft link files.
QQ screenshot 20171026220900.png

Soft link:  also known as symbolic link, namely "symbolic links/soft links", is a special file type that points to another different file by name, is a separate file, has different attributes from the original file (target file) , which is equivalent to a shortcut in Windows. When most operations are performed on soft links (including opening, reading, writing, etc.), the system kernel will automatically perform on the target file of the soft link; but some operations, such as deletion, will directly operate on the linked file itself .

Soft link features:

  1. Soft link and target file are not the same file, and the node numbers are different.
  2. Deletion of object files affects softly linked files. When the linked file is deleted, it will not affect the target file; when the target file/directory is deleted, the soft link will fail and become a dead link because the target point is lost.
  3. Soft links can be created across partitions. (When using the command #hf -h to check the size of the partition is the usage, the line of the mount point is the partition.) 
  1. Command syntax: ln[-s][source file][destination file]
  2. Command description:  establish a soft link
  3. Command parameters: -s When establishing a soft link, the -s option must be added. If the -s option is not added, a hard link is established.

example

ln -s source file object file

For example: # ln -s /root/123/.1.txt /tmp/123/12/1.txt 

Example:

[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.swp
drwxr-xr-x 2 root root     6 10月 25 17:16 222
-rw-r--r-- 1 root root     0 10月 25 17:20 4913
lrwxrwxrwx 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 cham
drwxrwxrwx  2 cham  cham    6 10月 26 16:59 cham11
drw-r--r-x  2 user1 cham   19 10月 25 15:41 cham2
drwxr-xr-x  4 root  root   28 10月 24 12:58 chamlinux
-rwx------. 1 root  root  836 10月 19 07:00 ks-script-JG2UJk
drwx------  3 root  root   17 10月 24 12:21 systemd-private-5ec76fd91759498b901e85cba2554a24-vmtoolsd.service-H1l4Cy
drwxrwxrwx  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/cham3
lrwxrwxrwx 1 root root 10 10月 26 20:34 /root/111/cham3 -> /tmp/cham2
[root@cham2 tmp]# ls
2.txt   cham    chamlinux                                                                 user1
321321  cham11  ks-script-JG2UJk                                                          yum.log
3.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 cham
drwxrwxrwx  2 cham  cham    6 10月 26 16:59 cham11
drw-r--r-x  2 user1 cham   19 10月 25 15:41 cham2
drwxr-xr-x  4 root  root   28 10月 24 12:58 chamlinux
lrwxrwxrwx  1 root  root    7 10月 26 20:36 chamlinux.log -> yum.log
-rwx------. 1 root  root  836 10月 19 07:00 ks-script-JG2UJk
drwx------  3 root  root   17 10月 24 12:21 systemd-private-5ec76fd91759498b901e85cba2554a24-vmtoolsd.service-H1l4Cy
drwxrwxrwx  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]# ls
12.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.swp
drwxr-xr-x 2 root root     6 10月 25 17:16 222
-rw-r--r-- 1 root root     0 10月 25 17:20 4913
lrwxrwxrwx 1 root root    10 10月 26 20:34 cham3 -> /tmp/cham2
lrwxrwxrwx 1 root root     7 10月 26 20:36 chamlinux.log -> yum.log
lrwxrwxrwx 1 root root    12 10月 26 20:32 yum.log -> /tmp/yum.log
[root@cham2 111]# cd /root/
[root@cham2 ~]# ls
111  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
总用量 0
lrwxrwxrwx 1 root root 7 10月 26 20:36 chamlinux.log -> yum.log
[root@cham2 123]# touch yum.log
[root@cham2 123]# ls -l
总用量 0
lrwxrwxrwx 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]# ls
chamlinux.log  yum.log
[root@cham2 123]# rm -f yum.log
[root@cham2 123]# mv ../111/yum.log .             移动上级目录到当前目录
[root@cham2 123]# ls -l
总用量 0
lrwxrwxrwx 1 root root  7 10月 26 20:36 chamlinux.log -> yum.log
lrwxrwxrwx 1 root root 12 10月 26 20:32 yum.log -> /tmp/yum.log
[root@cham2 123]# 
  • Use #hf -h to check the size and usage of the partition. It can be seen that there is not much space left under the /root user, while the / directory has a lot of space.
    QQ screenshot 20171026233249.png
    For example, there is a /root/dl.log log file under /root. When the dl.log log file is continuously written to the log, the space occupied is relatively large. At this time, we can softly link dl.log to the / directory. The specific operations are as follows.

#cp /boot/dl.log /dl.log   
#rm -rf /boot/dl.log   
#ln -s /dl.log /boot/dl.log   

The above command description:
1. Copy the dl.log log under /boot to the / directory.
2. Delete the dl.log log under /boot.
3. Soft link /dl.log in / directory to /boot/dl.log In this case, dl.log is still written to /boot/dl.log when it is in the log.

Soft link summary

  1. When we create a soft link, the destination file cannot exist, otherwise the establishment will be unsuccessful.
  2. All soft links can only be one file by themselves! This file can point to a directory or a file.
  3. When we create a soft link, it is usually an absolute path. If a soft link with a relative path is established, there will be disadvantages! When we copy all the files to another machine, we will find that the source files do not exist.

hard link

Hard link:  Also known as "hard link", its essence is another name for the existing file, so the hard link and the original file are indistinguishable. Technically, they share the same inode, which contains almost all the information of the file, including direct/indirect pointers to data blocks.

Hard links do not support directories, only files, cannot span partitions, and can be deleted because there are other files that use inodes  

  1. Create a file with the same inode number as another file, then the two files are hard-link files to each other.
  2. Only hard links can be made to files, not directories.
  3. Hard links cannot be created across system partitions.
  4. Only existing files can be created.
  5. Deleting a hard-linked file does not affect other files with the same inode number.
[root@cham2 ~]# ln 1.txt 1_heard.txt
[root@cham2 ~]# ln -s 1.txt 1_sorft
[root@cham2 ~]# ls -l
总用量 16
drwxrwxr--  3 root root   126 10月 26 20:41 111
-rw-rw-r--  1 root root   135 10月 25 17:44 11.txt
drwxr-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.txt
lrwxrwxrwx  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.txt
drwxrwxr-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
总用量 16
drwxrwxr--  3 root root   126 10月 26 20:41 111
-rw-rw-r--  1 root root   135 10月 25 17:44 11.txt
drwxr-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.txt
lrwxrwxrwx  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.txt
drwxrwxr-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.txt
lrwxrwxrwx  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
33575631 111     33575641 1_heard.txt  16783440 234           33575648 88.txt
33575016 11.txt  33575647 1_sorft      16777669 2.txt         33574987 anaconda-ks.cfg.1
   30154 123     33575641 1.txt        33575648 88_heard.txt
33575640 12.txt  33575633 22.txt       33575642 88_sorft.txt
[root@cham2 ~]# rm -f 1.txt
[root@cham2 ~]# ls -l
总用量 16
drwxrwxr--  3 root root   126 10月 26 20:41 111
-rw-rw-r--  1 root root   135 10月 25 17:44 11.txt
drwxr-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.txt
lrwxrwxrwx  1 root root     5 10月 26 21:31 1_sorft -> 1.txt
-rw-r--r--  1 root root     0 10月 25 17:07 22.txt
drwxrwxr-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.txt
lrwxrwxrwx  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.txt
33575641 1_heard.txt
[root@cham2 ~]# ln 111 cham111
ln: "111": 不允许将硬链接指向目录
[root@cham2 ~]# df -h
文件系统        容量  已用  可用 已用% 挂载点
/dev/sda3        17G  1.1G   16G    7% /
devtmpfs        483M     0  483M    0% /dev
tmpfs           493M     0  493M    0% /dev/shm
tmpfs           493M  6.8M  486M    2% /run
tmpfs           493M     0  493M    0% /sys/fs/cgroup
/dev/sda1       187M  109M   79M   59% /boot
tmpfs            99M     0   99M    0% /run/user/0
[root@cham2 ~]# ls -i /boot/
    71 config-3.10.0-514.el7.x86_64
    67 grub
131136 grub2
    76 initramfs-0-rescue-0f3cf64915c04d4e93e4dae2684b907d.img
    75 initramfs-3.10.0-514.el7.x86_64.img
194501 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.1
ln: 无法创建硬链接"/tmp/config.1" => "/boot/config-3.10.0-514.el7.x86_64": 无效的跨设备连接
[root@cham2 ~]# ls /boot/
config-3.10.0-514.el7.x86_64
grub
grub2
initramfs-0-rescue-0f3cf64915c04d4e93e4dae2684b907d.img
initramfs-3.10.0-514.el7.x86_64.img
initramfs-3.10.0-514.el7.x86_64kdump.img
initrd-plymouth.img
symvers-3.10.0-514.el7.x86_64.gz
System.map-3.10.0-514.el7.x86_64
vmlinuz-0-rescue-0f3cf64915c04d4e93e4dae2684b907d
vmlinuz-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 ~]# 
  1. The inode of the hard link is the same as the inode of the source file.
  2. The memory size of the hard link is the same as the memory size of the source file.
  3. The time is also the same for hard links and source files.
    QQ screenshot 20171027090701.png
  4. Difference between hard link and soft link:

    Hard link:

  5. When the system wants to read a file, it will first read the inode table, a file corresponds to an inode, and then retrieve the data from the block area according to the information in the inode. A hard link directly uses the same inode as the source file, and the hard link file is directly linked to the block area where the file is placed. That is to say, when the hard link is made, there is actually no change in the content of the file, but the same inode is specified.
  6. Creating a hard link will add additional records to refer to the file, corresponding to a physical file on the same file system. Each file refers to the same inode number. The number of links is incremented when created.
  7. When deleting a file: The rm command decrements the number of links. The file must exist, and there must be at least one link number. When the number of links is zero, the file is deleted.
  8. Cannot span drives or partitions
    Syntax: ln filename [linkname ]
  9. Soft link:

  10. A soft link is to create an independent file, and the function of this file is to forward the read behavior to the file linked by the file when the linked file is read.
  11. A symbolic link points to another file.
  12. You can make soft links to directories or files.
  13. Can span partitions
  14. Points to a path to another file; its size is the length of the pointed path string.
  15. Does not increment or decrement the reference count of the object file inode.
    Syntax: ln -s filename [linkname]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325984456&siteId=291194637