Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand soft connection and hard connection in linux

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to understand soft connections and hard connections in linux". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand soft and hard connections in linux.

Tip: first explain the basic storage of files and directories in the Linux system, which makes it easier for us to understand and learn the hard links and soft links in the Linux system.

1. Basic storage of files and directories

As mentioned earlier, each partition can be understood as two parts, a small part of which contains the I-node information of the file, and most of which are Block file blocks.

The information saved in the I node of the file is: I node number, time, permission, save location. What is saved in the Block block is the data in the file.

Each folder will have an I node and a corresponding Block file.

But there is a question, where is the file name of a file stored?

Let's take, for example, that there is a file abc in the folder root. The file path is / root/abc.

In the root directory, all files have I node names and corresponding Block blocks, while the root directory has its own I node information. If there is I node information, there is a corresponding Block block. The data stored in the Block block of the root directory is the file name of all the subfiles and subdirectories under the root directory, and its corresponding I node number.

That is, the file name of the file in the root directory is not written in the I node of the file. Instead, it is written in the Block block of the parent directory.

If I want to find the file name of the abc file, I have to find the I node of the root folder, and then find the file name and the corresponding I node number of the abc in the Block block of the root directory. The data in the abc file is finally read through the I node number of the abc file.

Where is the file name or I node of the root directory stored?

The root directory is in the root directory, and the root directory will have its own I-node information and its own Block. To sum up, the file name and I-node number information of the root directory are placed in the Block block of the root directory.

How to get the I node number of that root?

The I node number of the root is fixed and the I node number of the root is 2. (because number 1 is occupied when the system kernel is loaded)

[root@192] # ls-ild / 2 dr-xr-xr-x. 25 root root 4096 December 27 22:27 / the first place is the I node number.

Summary

To sum up, to view the contents of the abc file, first read the data in the root Block block through the root I node number, find the I node information in the root directory, read the data in the Block block in the root directory, find the I node information in the abc file, and read the data in the Block block of the abc file.

The above is the basic storage of files and directories.

2. Introduction to the In command (1) Let's look at the basic information of the ln command, command name:

Ln original meaning: make links between file path: / bin/ln execution authority: all users. Function description: establish links between files.

(2) basic format of ln command [root@localhost ~] # ln [option] Source file destination file option:-s: establish a soft link file. If you do not add the "- s" option, the hard link file-f: mandatory is established. If the target file already exists, delete the target file and then establish a link file 3, create a hard link (1) how to create a hard link

Examples are as follows:

[root@localhost ~] # touch cangls [root@localhost ~] # ln / root/cangls / tmp/# establishes a hard link file. The target file does not have a file name and will be consistent with the original # that is, / root/cangls and / tmp/cangls are hard link files

Describe the ln command application:

# create a new file abc and check the number of hard connections in the abc file [root@192 ~] # touch abc [root@192 ~] # ls-il abc 1043285-rw-r--r--. 1 root root 0 December 29 18:18 abc# can see that the number of hard connections to the abc file is 1. # create a hard connection file for the abc file in the tmp directory, and then check the number of hard connections between the two files. [root@192 ~] # ln abc / tmp/abc_ h [root @ 192 ~] # ls-il abc / tmp/abc_h 1043285-rw-r--r--. 2 root root 0 December 29 18:18 abc1043285-rw-r--r--. 2 root root 0 December 29 18:18 / tmp/abc_h# you can see that the reference count (hard connections) of the abc file has increased by 1. # at the same time, you can see that the I node numbers of the two files above are the same. (2) hard link feature

Source files and hard-link files have the same Inode and Block. Modify any one file and change the other. Delete any file and the other can be used. Hard link is not clearly marked, it is difficult to confirm the location of the hard link file (it is difficult to determine that this is a hard link file, you may also remember that if someone else created it, I simply cannot determine that a file is a hard link file). It is not recommended. Hard links cannot link directories. Hard links cannot cross partitions (because two files need the same I node number of the same partition. So it can't be found across partitions.

(3) the principle of hard connection

I created an abc file in the root directory above, and then an abc file hard link file abc_h in the / tmp/ directory.

Root directory, abc file, tmp directory, each with its own I node number and corresponding Block block. Find the corresponding Block block by the I node number of the root directory. The name and I node number of the abc file are recorded in the Block block, and the abc file can be found. The I node number and file name of the hardwired file abc_h are recorded in the Block block of the tmp directory. And the I node number of the abc file and the abc_h hard connection file queried above is the same, so the I node number of the abc file can also be found through the abc_h information in the Block block of tmp.

So the abc file information recorded in the root directory and the abc_h file information recorded in the tmp directory point to the abc file in the root directory.

Look at the following picture:

So the picture above proves:

Modify any one file and change the other.

Delete any file and the other can be used.

4. Create soft links (1) how to create soft links

Soft connections can be directly seen as shortcuts in Windows systems.

Create a soft link file as follows:

[root@localhost ~] # touch abc [root@localhost ~] # ln-s / root/abc / tmp/

The cde file in the root directory, and then create the soft connection / tmp/cde_s file to view the two files.

As shown in the following figure:

[root@localhost ~] # touch cde [root@localhost ~] # ln-s / root/cde / tmp/cde_ s [root @ localhost ~] # ls-il / root/cde / tmp/cde_s1055972-rw-r--r--1 root root 0 February 1715 root/cde261634 1rwxrwxrwx 1root root 30 / tmp/cde_s- > / root/cde

Description:

First: you can see that the created soft link file has a clear prompt for this soft link file and where its source file is located.

/ tmp/cde_s- > / root/cde. This is why hard links are not clearly marked.

Second: the number of reference links to the file has not increased.

Third: the I node numbers of the two files are completely different.

Fourth: the source file abc is 0 bytes, empty. The file size of the soft connection is 9 bytes.

(2) soft link feature

Soft links and source files have different Inode and Block. Either of the two files is modified, and the other is changed. Delete the soft link, the source file will not be affected; delete the source file, the soft link cannot be used, because the soft link has no actual data, only save the Inode of the source file, no matter how big the source file, the size of the soft link file remains the same. The permission of the soft link is the maximum permission lrwxrwxrwx., but because there is no actual data, the final access needs to refer to the source file permissions. Soft links can link directories, and soft links can span partitions. The characteristics of soft links are obvious, so it is recommended to use soft links.

(3) the principle of soft connection

There is a cde file in the root directory, and a soft link file cde_s for the cde file is created in the tmp directory. Through the I node number of the root directory, you can find the block block of the root directory, and the file name and I node number of the cde file are recorded in the block block of the root directory. If you look up the I node number of the cde file, you will find the block block of the cde file, and you will be able to read the data inside.

The tmp directory has its own I-node number, corresponding to the block block. The block block of the tmp directory contains the file name and I-node number of the cde_s file (previously it was said that the hard-link I-node number is the same, so it points to the same file), while the I-node number of the soft-link file and the source file is not the same. Indicates that the cde_s file has its own I-node number, and the cde_s file has its own block block.

The key now is, what is stored in the block block of the cde_s file?

If the data is stored inside, then the cde file and cde_s file is not a link relationship, it is completely a copy relationship, so that the soft link file will not change when the source file is modified, and the source file will also change after modifying the soft link file.

In fact, the I node number of the source file is stored in the block block of the cde_s file.

As shown below:

This explains why the newly created soft connection file has data after it is created, and the data size does not change with the change of the source file.

It should also be noted that the permissions of soft link files are the largest, but this permission is for soft link files. The block block of soft link files does not store data. If you want to manipulate the source file, you still need to look at the source file permissions, which should be noted here.

(4) description

Shortcuts are created in the Windows system because the installation location of the software is too deep to find, so create a shortcut on the desktop. What is the purpose of creating soft links in the Linux system? anyway, they are all accessed through the directory?

In the Linux system, the most important role of soft links is to take care of the habits of administrators.

As shown below:

In the old Linux system, we are used to the location of the / etc/rc.local file, but after the system update, the location of the / etc/rc.local file has changed in order to upgrade. It will be troublesome for us to find this file. The solution to this problem is to create a soft link for the two files to solve the above problem, which takes care of both upgrade needs and usage habits.

Also note that a soft link to a def file is created with a relative path and a soft link to an anaconda-ks.cfg file with an absolute path.

You can see from the figure above that there is an error in the soft link created with the relative path.

Tip: be sure to use an absolute path when creating soft links, and hard links are not required.

At this point, I believe you have a deeper understanding of "how to understand soft connections and hard connections in linux". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report