Create hard and soft links – RHEL 8 RHCSA

Within Linux, links allow us to refer to a target file using a different source – the link. When the link is accessed, we’re redirected to the defined target.

In these examples we will cover the Red Hat Certified System Administrator (RHCSA) objective “Create hard and soft links”.

Create Hard and Soft Links

In these examples we’ll be dealing with two types of links, soft links and hard links. We’ll be using the ‘ln’ command to create both types of links.

Hard Links

Every file on the file system is essentially a hard link to an inode. An inode (index node) is a data structure that described an object, such as a file or directory. The inode stores the actual data of the object, while the hard link is what actually points to the inode.

For example, if we create a new file:

[root@rhel8 ~]# echo "testing" > /root/testing

The file ‘/root/testing’ has been created, and is essentially a hard link to the inode containing the string of “testing”.

Creating a new hard link makes another object that points to the same inode. You can have multiple hard links pointing to an inode, as long as one hard link still exists, the actual file will remain present on the file system.

This example uses the ‘ln’ command to create a new hard link.

[root@rhel8 ~]# ln /root/testing /root/hardlink

The file /root/hardlink points to the same inode as /root/testing, which is why it just appears as another file on the file system:

[root@rhel8 ~]# ls -la /root
-rw-r--r--.  2 root  root       8 May 13 07:05 hardlink
-rw-r--r--.  2 root  root       8 May 13 07:05 testing

Note the ‘2’ before the user and group ownership, this refers to the fact that the file has two hard links to it, as both are now pointing to the same inode.

We can confirm this by viewing the content of the hardlink:

[root@rhel8 ~]#  cat /root/hardlink

If we delete the original /root/testing file, the data will still be accessible via /root/hardlink

[root@rhel8 ~]# rm -f /root/testing
[root@rhel8 ~]# ls -la /root/hardlink
-rw-r--r--. 1 root root 8 May 13 07:05 /root/hardlink

Now that /root/testing is removed, the ‘2’ from earlier is now a ‘1’, as /root/hardlink is the only hard link to the inode containing the string ‘testing’. If we delete the last link, the data will be removed.

As hard links point to the same raw data, if we edit the content of a file through a hard link, then attempt to edit the file through a different hard link, the changes from the first edit will be there – we’re not copying the files like with the ‘cp’ command, we’re linking to the same location on disk multiple times.

Hard links only work on the same file system, it is not possible to create a hard link on one file system to something on a separate file system. This is possible with symlinks, however.

Soft Links

While hard links point to the direct inode, a soft link, also known as a symbolic link, or symlink for short, simply acts as a redirection. If you open a soft link, you’ll be redirected to the location that the soft link points to. The link exists independently to the file that it points to. If the original file is deleted, the link will still exist, albeit pointing to a location that no longer exists. Likewise if the soft link is deleted, it does not affect the file that the link points to.

The following example shows how to create a soft link using the ‘ln’ command. The syntax is almost identical to the hard link example shown previously, except the ‘-s’ flag is used to specify that this is a symbolic link, instead of a hard link which is the default.

In this example, we will create a soft link to our previous hard link, though as we now know the hard link is essentially a normal file on the Linux file system.

[root@rhel8 ~]# ln -s /root/hardlink /root/softlink

Now let’s look at what we have created:

[root@rhel8 ~]# ls -la /root/softlink
lrwxrwxrwx. 1 root root 14 May 13 07:11 /root/softlink -> /root/hardlink

We can see that /root/softlink has been created, and the redirection can be identified by the ‘->’ arrow symbol, which in this case points to the underlying file /root/hardlink.

Now if we try and access the soft link, we should retrieve the contents we previously created with the echo command in the hard link section above.

[root@rhel8 ~]# cat /root/softlink

Success! This is the text we originally created in the /root/testing file, prior to creating our hard link (essentially just another reference to the actual inode that contains the string ‘testing’).

This is what we’ve done so far in a diagram:

If we move or rename the hard link, it will still point to the inode containing the string and be accessible, however the soft link will fail, as it is pointing to the name of the file ‘/root/hardlink’ rather than the inode.

Likewise if we delete the file the soft link points to, the soft link will still exist, however the contents are gone so attempting to access the destination via the soft link fails:

[root@rhel8 ~]# rm -f /root/hardlink
[root@rhel8 ~]# ls -la /root/softlink
lrwxrwxrwx. 1 root root 14 May 13 07:18 /root/softlink -> /root/hardlink
[root@rhel8 ~]# cat /root/softlink
cat: /root/softlink: No such file or directory

We can also tidy up and delete the soft link without affecting the file that it points to as follows:

[root@rhel8 ~]# rm /root/softlink
rm: remove symbolic link ‘/root/softlink’? y

This will leave the destination the link points to intact, only the soft link will be removed.

Unlike hard links, soft links can cross between different file systems. Additionally, soft links can point to directories, however hard links cannot.

Should you forget the syntax, you can simply refer to the man page through ‘man ln’ during the exam.

That’s it, you should now have a basic understanding of how to create hard and soft links in RHEL 8 for the RHCSA exam. Make sure you practice these examples yourself to get a full understanding on how the commands work.

This post is part of our Red Hat Certified System Administrator (RHCSA) exam study guide series for Red Hat Enterprise Linux (RHEL) 8. For more RHCSA related posts and information, see our full RHCSA 8 study guide.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>