Skip to main content

Working with Links in Linux

Hands-On Lab


Photo of Kenny Armstrong

Kenny Armstrong

Linux Training Architect II in Content





Understanding how soft and hard links work within Linux is another important skill for a system administrator. This learning activity will help you practice creating these two types of links on a file system, and explore the differences between the two.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Working with Links in Linux

Understanding how soft and hard links work within Linux is an important skill for a system administrator. A review of the ln man page might be in order before embarking on this hands-on lab to know how to make a symbolic and hard link for files.

Create a Symbolic (Soft) Link

First, we're going to use ln to create a symbolic link from the /etc/redhat-release file to a new link file named release in the cloud_user's home directory. Then, we'll use the ls command and verify the link is valid. Afterward, we'll use the cat command on the /home/cloud_user/release file to verify its contents.

First, let's make the link:

[cloud_user@host]$ ln -s /etc/redhat-release release

Now, we'll check it:

[cloud_user@host]$ ls -al

And then, see if we can read the file's contents, as well as the link's:

[cloud_user@host]$ cat /etc/redhat-release
[cloud_user@host]$ cat release

They should be the same.

Check the Inode Numbers for the Link

Using the ls command, we're going to look at the inode number of /home/cloud_user/release, and then check the inode number for /etc/redhat-release. They should be different, as the symbolic link is just a new filesystem entry that references the original file. Let's look with these commands:

[cloud_user@host]$ ls -i release
[cloud_user@host]$ ls -i /etc/redhat-release

Create a Hard Link

Let's create a directory called docs in our home directory, then copy /etc/services into it. Using the ln command again, we'll create a hard link from /home/cloud_user/docs/services to a link file named /home/cloud_user/services. We can use the ls command to verify the link's inode number, and the inode number for the original /etc/services.

These commands will do all of that:

[cloud_user@host]$ mkdir docs
[cloud_user@host]$ cp /etc/services docs/
[cloud_user@host]$ ln docs/services services
[cloud_user@host]$ ls -al

The ls command should verify for us that this is a hard link, not a soft link. It won't have an arrow pointing to the actual file it's linked to like a soft link does. Just to verify, check these two with cat and make sure they're the same:

[cloud_user@host]$ cat docs/services
[cloud_user@host]$ cat services

Then check the inodes with these commands:

[cloud_user@host]$ ls -i services
[cloud_user@host]$ ls -i docs/services

This should show the link and the file itself both have the same inode number, meaning they're essentially the same file.

Attempt to Create a Hard Link Across Filesystems

Next, using the ln command, we're going to make a hard link from /home/cloud_user/docs/services to /opt/services. Run this:

[cloud_user@host]$ ln /home/cloud_user/docs/services /opt/services

We got a failed to create hard link error. What happened?

Try the following:

[cloud_user@host]$ lsblk
xvda    202:0    0  10G  0 disk
├─xvda1 202:1    0   1M  0 part
└─xvda2 202:2    0  10G  0 part /
xvdb    202:16   0   2G  0 disk
└─xvdb1 202:17   0   2G  0 part /opt

You can see here that / and /opt are on two separate partitions. Because each partition has its own set of inodes, hard links across partitions don't work. Soft links should, though.

Attempt to Create a Soft Link Across Filesystems

Let's try to make the same sort of cross-partition link, but this time use the -s flag to make it a soft link:

[cloud_user@host]$ ln -s /etc/redhat-release /opt/release

The file and link will have different inodes (and we can check with another ls -i on each of them), but the linking will work.


You're all set. Congratulations on completing this lab!