Linux Basics-14

Source: Internet
Author: User

An interesting logic to keep a file in two places, but only one copy, or two different filenames to save a file, this idea is some anti-human, but this is the hard link under Linux.

The following is the time to witness miracles, Lao Mo Seriously did this example, one is to consolidate the knowledge before learning, and second experience a hard link.

The story is this ...

One day a handsome man (Kevin), a Beauty (Alice) to find old Mo, they want to work together to write a novel, want to let old mo on the server to create two accounts, they who have free time to log on to the server to write. Because it is a joint effort to write, so they do not want to write a little by e-mail to pass, after splicing, very troublesome. Can there be a way for two people to write in their own directory, but the other side open their files are updated.

Quite dizzy, because the old Mo first Contact, so a different saying:

Kevin created the story catalogue in his home directory, which has a novel called the nine-Yang God Emperor. Kevin was on the whim of the day to write a paragraph, then meici, after the preservation of the holiday.

Alice also created the story catalogue in her home directory, because it was written together with Kevin, so Alice's stories catalogue also called the nine-Yang God. In the course of Kevin's vacation Alice opened the nine-Yang God, found that the beginning has been written, so continue to write.

The day Alice hit the Little Monster went, just Kevin back, open nine Yang Shinti found that Alice has written much better. So Kevin continued to write ...

Now old Mo understand, in the different directory of two files, like the same file, one of them modified, the other end also changed. Of course, this thinking is very boring, do not excessive, is to illustrate the problem of hard connection.

At the constant request of Alice and Kevin, Old Mo agreed.

One, hard connection

Lao MO Analysis of the requirements, the first to add two users, which is not simple, open the control center ...
No! No! This force lattice too low, how also must let old mo in handsome, beauty in front of show off a turn.

(i) Create user
#创建用户sudo useradd -m  -s /bin/bash kevinsudo useradd -m  -s /bin/bash alice#设置密码sudo passwd kevinsudo passwd alice
(ii) Create a group

Because two different users want to access the same file together, according to the previous learning knowledge, should be in the group or other people's permissions up and down. Because they co-operate together, do not want to let the third party people to destroy, so the old Mo choose to use a common group way to manage documents.

##创建组sudo groupadd story##把alice, kevin添加到组中sudo usermod -a -G story kevinsudo usermod -a -G story alice
(iii) Building structures

Kevin,alice each login to build its own directory structure.

1. Kevin Build Structure
#切换用户su - kevin#创建目录mkdir story#修改story目录的组所有者为:story组,并设置组所有者的权限为:rwxchgrp story storychmod g=rwx story#创建小说,修改小说的组所有者为story,并设置组所有者的权限为:rw-touch story/九阳神帝chgrp story story/九阳神帝chmod g=rw- story/九阳神帝
2. Alice Build Structure
#切换用户su - alice#创建目录mkdir story#修改story目录的组所有者为:story组,并设置组所有者的权限为:rwxchgrp story storychmod g=rwx story
(iv) Create a hard connection

Currently Alice does not create nine male deity this file, because the need to put Kevin's nine-Yang God file hard to connect to Alice's story directory, after a hard connection to indicate that the two files are the same file.

ln story/九阳神帝 /home/alice/story/九阳神帝

The ln command is a command that creates a link, such as a shortcut (a soft link) that is created by this command. Similar to the CP command, a file is copied to another location, but Ln is a link created in another place, not a copy of the file.

(v) Testing

Kevin was on the whim of the day to write a paragraph, then meici, after the preservation of the holiday.

A sharp pain, old Mo woke up to find this is a strange place ...
Look at your hands are full of blood, clothes and before the same, is it not through?

In the course of Kevin's vacation Alice opened the nine-Yang God, found that the beginning has been written, so continue to write ...

The moment to witness the miracle came:

After Alice logged in, she found that there was already something written in the nine-Yang emperor.

Second, hard links detailed

When Kevin creates a/home/kevin/story/Shinti, it includes a dentry,inode,data, and when a hard connection is created with the LN command, the file has only one inode and data, but there are two dentry connected to it.

The structure is as follows:

You can see a file of two file names. Now look at it with Ls-l:

[email protected]:~$ ls -l /home/alice/story/九阳神帝  -rw-rw-r-- 2 kevin story 152 7月  17 08:35 /home/alice/story/九阳神帝

We found that the number of links is 2, if you modify the file permissions? See if the other end is not changed:

First look at the permissions of the two files the same, then give others a W permission, then look at the other end of the discovery also changed, because they are the same inode.

What happens if you delete the file?

[email protected]:~/story$ rm 九阳神帝 [email protected]:~/story$ ls /home/kevin/story/九阳神帝

Alice deleted her nine-Yang emperor, but we found that the Shinti in Kevin's catalogue still exists. Check the link points again:

-rw-rw-rw- 1 kevin story 152 7月  17 08:35 九阳神帝

Found just 2, now become 1, you can see RM delete files, not the data emptied, but the chain, when the Inode connection number is 0, indicating that the file was deleted.

Third, soft link

A soft link is a shortcut that can be understood by anyone with Windows experience. The soft link itself is a file, but this file does not hold data, it holds a link, and points to other files.

The methods you create are similar to hard links:

The discovery is one more option-s means that this is a soft link.

By ls-l we find that the file type of the soft link is L, which means that the soft link is not a regular file but a symbolic link file. The symbolic link file has its own dentry, with its own inode, but the data domain holds another file name, which enables the link. The target file is automatically parsed when Linux accesses the symbolic link file.

Precautions

Soft links have several hard connections that do not appear to be problematic:

    • NULL Link: The target is missing and the connection fails.
    • Recursive link: A, B, B, a

We found that Linux was trying to multilayer and thought it was a recursive link, warning to exit.

Four, soft and hard link comparison
Hard Links Soft Links
Cannot use hard links between directories Soft links can be associated to a directory
Hard links do not have the concept of original and copy Soft links have references and referenced concepts, and deleting targets becomes an empty link
Hard links must be within the same partition Soft links can be linked in different sections
Chroot can share hard links between directories Soft links cannot reference files outside of the chroot directory

For chroot This old Mo do not understand, have not seen, first note, and so on later learned chroot again study.

Linux Basics-14

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.