Module 6.0 – Understanding Hard Link and Soft Link In Linux

In this tutorial, you will not just learn how to create soft links and hard links, you will also understand how hard links and soft link in Linux work.

UNDERSTANDING THE SUBJECT MATTER

Before we get started, I will like you to put some information on your right palm. They are

1. Every file in Linux has an inode number (this basically means every file is linked to an inode number (put this on the first finger of your right palm)

2. talking about links in Linux, links to a file can either be a hard link or a soft link. (put this on the second finger of your right palm)

3. To verify the inode number of a file, use the command, “ls -i”

Hence, to verify the inode number of the file, “/etc/passwd”, use the command,

[root@HQEBPRD etc]# ls -i /etc/passwd

16787212 /etc/passwd
[root@HQEBPRD etc]# ls -il /etc/passwd

16787212 -rw-r--r--. 1 root root 2430 Apr  9 00:37 /etc/passwd
soft link in Linux

(Put this on the third finger of your right palm)

4. in number 2, I mentioned that links to a file can either be a hard link or a soft link, so what is a hard link and a soft link in Linux?

A hard link is an exact copy (including the file attributes) of an existing file it is pointed/linked to (put this on the fourth finger of your right palm)

Basically, a hard link is a file that is copied from an existing file (source file).

ln <source-file> <hard-link-file-name>

For the purpose of this course, let’s copy “/etc/passwd” file to “/tmp/passwd” so not mess up with the original “passwd” file

[root@HQEBPRD ~]# cp /etc/passwd /tmp/passwd

so, to create a hard link from /tmp/passwd, use the command

[root@HQEBPRD ~]# ln /tmp/passwd hardlink_passwd

Don’t forget I mentioned that every file has an inode number, so to verify the inode number of the hard link we just created and the file we created it from(source file), use the command,

[root@HQEBPRD ~]# ls -il /tmp/passwd hardlink_passwd

16787212 -rw-r--r--. 2 root root 2430 Apr  9 00:37 /tmp/passwd
16787212 -rw-r--r--. 2 root root 2430 Apr  9 00:37 hardlink_passwd

did you notice that both files have the same inode number and permission, yeah they do. So hard links usually have the same inode numbers and permissions as their source files.

Let’s try something again, let’s create another hard link from the previous hard link that was just created. To do this, use the command,

[root@HQEBPRD ~]# ln hardlink_passwd hardlink2_passwd

Again, let’s verify their inode numbers,

[root@HQEBPRD ~]# ls -il /tmp/passwd hardlink_passwd hardlink2_passwd

16787212 -rw-r--r--. 3 root root 2430 Apr  9 00:37 /tmp/passwd
16787212 -rw-r--r--. 3 root root 2430 Apr  9 00:37 hardlink2_passwd
16787212 -rw-r--r--. 3 root root 2430 Apr  9 00:37 hardlink_passwd

Now, let’s verify the if the contents in these files are the same,

[root@HQEBPRD ~]# cat /tmp/passwd

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

........................................
[root@HQEBPRD ~]# cat hardlink_passwd

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

.........................................
[root@HQEBPRD ~]# cat hardlink2_passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

............................................

Now check the fourth finger of your right palm again, you can see that a hard link is an exact copy of an existing file, including its file attributes. Hard links points to a source file and inherits all the attributes of the source file.

Moving forward, there are some important things you should know about hard links

Let’s delete “hardlink_passwd” file and verify if “hardlink2_passwd” file and “/etc/passwd” file still remains intact.

To delete “hardlink_passwd” file, use the command,

[root@HQEBPRD ~]# rm -rf hardlink_passwd

Now, verify if “hardlink2_passwd” file is still intact.

[root@HQEBPRD ~]# cat hardlink2_passwd

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

.....................................

To verify this, let’s create a hard link in a different filesystem, “/boot” in my case is a different filesystem on my system.

[root@HQEBPRD ~]# ln /boot/System.map-4.18.0-147.el8.x86_64 hard_link_file

ln: failed to create hard link 'hard_link_file' => '/boot/System.map-4.18.0-147.             el8.x86_64': Invalid cross-device link

For the purpose of this activity, create a file so that we will not tamper with the “/etc/passwd” file.

vi tekneed_file

The contents in your file can be like this one below or anything you wish

#Like they say, the way you lay your bed, so you lie on it

#We are on earth to lay our bed for the number of years we spend(at most 200 years)for the number of years we will forever spend (more than 999 trillion yrs & still counting) when you leave the earth

#The way you lay it, so you lie on it in the after life

#If you lay your bed with sinful acts and disobedience to your creator's law, the same way you lie on it

#For he will say to those, "depart from me, ye workers of iniquity" Matthew 7 vs 21-23.

#grace is still available now, repent now, turn to Jesus, He loves you, He is knocking at the door of your heart, release yourself to him, let him help you lay your bed so that you will forever enjoy eternity.

save your file

Let’s verify by creating a hard link from the file and change the permission as well

  • To create a hard link, use the command,
[root@HQEBPRD ~]# ln tekneed_file hlink
[root@HQEBPRD ~]# ls -il tekneed_file hlink

34338172 -rw-r--r--. 2 root root 723 Apr 25 18:19 hlink
34338172 -rw-r--r--. 2 root root 723 Apr 25 18:19 tekneed_file
  • To change the permission of (user, group and others) of the source file to read, write and execute, use the command,
[root@HQEBPRD ~]# chmod 777 tekneed_file
  • Now, verify again to see if the permission has changed
[root@HQEBPRD ~]# ls -il tekneed_file hlink

34338172 -rwxrwxrwx. 2 root root 723 Apr 25 18:19 hlink
34338172 -rwxrwxrwx. 2 root root 723 Apr 25 18:19 tekneed_file

You can see that the hard link permission has changed as well.

For Vice versa,

Again, let’s change the permission of hlink to 555

[root@HQEBPRD ~]# chmod 555 hlink

verify,

[root@HQEBPRD ~]# ls -il tekneed_file hlink

34338172 -r-xr-xr-x. 2 root root 723 Apr 25 18:19 hlink
34338172 -r-xr-xr-x. 2 root root 723 Apr 25 18:19 tekneed_file

Let’s verify this by adding some content in the beginning of the “tekneed_file” file

To do this, give a write permission back to the file and add the contents to the beginning of the file by following the steps below

[root@HQEBPRD ~]# chmod 777 tekneed_file
vi tekneed_file
Jesus loves you, he is knocking at the door of your heart, please allow him in before it is too late

Now , verify if the contents of the “hlink” file has changed

[root@HQEBPRD ~]# cat hlink

Jesus loves you, he is knocking at the door of your heart, please allow him in before it is too late
#Like they say, the way you lay your bed, so you lie on it

#We are on earth to lay our bed for the number of years we spend(at most 200 years)for the number of years we will forever spend (more than 999 trillion yrs & still counting) when you leave the earth

#The way you lay it, so you lie on it in the after life

#If you lay your bed with sinful acts and disobedience to your creator's law, the same way you lie on it

#For he will say to those, "depart from me, ye workers of iniquity" Matthew 7 vs 21-23.

#grace is still available now, repent now, turn to Jesus, He loves you, He is knocking at the door of your heart, release yourself to him, let him help you lay your bed so that you will forever enjoy eternity.
[root@HQEBPRD ~]#

Let’s verify this by creating a hard link from “/boot” directory

[root@HQEBPRD ~]# ln /boot hardlink_boot

ln: /boot: hard link not allowed for directory

you can see that this is not possible.

Now that we understand what hard link is, let’s get to understand what soft link in Linux is

A soft link in Linux, also known as symbolic link or sym link is a copy (excluding the file attributes) of an existing file it is pointed/linked to

ln -s <source-file> <soft-link-file-name>

To create a soft link from tekneed_file, use the command,

[root@HQEBPRD ~]# ln -s tekneed_file tekneed_file_softl

Let’s verify from the soft link that was created above,

[root@HQEBPRD ~]# ls -il tekneed_file tekneed_file_softl

34338172 -rwxr-xr-x. 2 root root 824 Apr 25 20:17 tekneed_file
34338167 lrwxrwxrwx. 1 root root  12 Apr 25 21:27 tekneed_file_softl -> tekneed_file

you can see that the inode number is different as well as the permission, please note that soft link will always have a full permission when it is created irrespective of the source file permission.

However, permissions are not managed on the soft link but from the source file. The full permission that is automatically given to the soft link doesn’t mean the soft link file has full permission, the real permission the soft link works with is the permission on the source file.

Let’s ascertain by changing the permission on “tekneed_file_softl” file and verify if the permission will change on the source file.

[root@HQEBPRD ~]# chmod 700 tekneed_file_softl

Now, Verify

[root@HQEBPRD ~]# ls -il tekneed_file_softl tekneed_file

34338172 -rwx------. 2 root root 824 Apr 25 20:17 tekneed_file
34338167 lrwxrwxrwx. 1 root root  12 Apr 25 21:27 tekneed_file_softl -> tekneed_file

you can see that the permission took effect on the source file and not on the soft link, so, permissions are managed from the source files.

Let’s ascertain this.

Let’s create a soft link in a different filesystem, /boot in my case is a different filesystem.

[root@HQEBPRD ~]# ln -s /boot/testfile tek_soft

You can see that no error was given as opposed to when there was error when we tried creating a hard link across filesystems.

If you use a relevant file in “/boot”, please delete the “tek_soft” file that is newly created so not to mess up with /boot.

let’s ascertain this by modifying tekneed_file_softl and tekneed_file.

To modfify tekneed_file_softl, use the command,

[root@HQEBPRD ~]# vi tekneed_file_softl
file modification 1

To modify tekneed_file, use the command,

[root@HQEBPRD ~]# vi tekneed_file
file modification 2

Now, view both files

[root@HQEBPRD ~]# cat tekneed_file_softl

file modification 1
file modification 2
Jesus loves you, he is knocking at the door of your heart, please allow him in before it is too late
#Like they say, the way you lay your bed, so you lie on it

#We are on earth to lay our bed for the number of years we spend(at most 200 years)for the number of years we will forever spend (more than 999 trillion yrs & still counting) when you leave the earth

..............
[root@HQEBPRD ~]# cat tekneed_file

file modification 1
file modification 2
Jesus loves you, he is knocking at the door of your heart, please allow him in before it is too late
#Like they say, the way you lay your bed, so you lie on it

#We are on earth to lay our bed for the number of years we spend(at most 200 years)for the number of years we will forever spend (more than 999 trillion yrs & still counting) when you leave the earth

.....

Let’s verify this.

  • First, make a directory in /tmp
[root@HQEBPRD ~]# mkdir /tmp/tekneed_dir
  • Now, create a soft link from tekneed_dir
[root@HQEBPRD ~]# ln -s /tmp/tekneed_dir tekneed_dir_soft

you can see that no error was displayed, as opposed to when we wanted to do this for a hard link.

Let’s ascertain this by deleting “tekneed_file” file and see if we are still going to have tekneed_file_softl

[root@HQEBPRD ~]# rm -rf tekneed_file
[root@HQEBPRD ~]# ls -il tekneed_file_softl

34338167 lrwxrwxrwx. 1 root root 12 Apr 25 21:27 tekneed_file_softl -> tekneed_file

you will notice that the colour on “tekneed_file_soft1” has changed to red, meaning something is wrong.

Let’s try to read the contents in the file.

[root@HQEBPRD ~]# cat tekneed_file_softl

cat: tekneed_file_softl: No such file or directory
[root@HQEBPRD ~]#

You can see that there is no such file on the system, even though you can still see the soft link file name.

see a soft link as a short cut icon you see on your Windows OS. If you delete the short cut icon, the file remains intact but if you delete the main file, the shot cut icon, though can still be seen on the desktop becomes useless because the main file is deleted

In conclusion, the understanding of inode will give a clearer picture of how hard links and soft links works in Linux.

What Is Inode In Linux

Briefly, Inode is a data structure. i.e, the way data is stored on a filesystem. Every inode has attributes like file types, permissions, file size, etc and every inode has a unique number.

So, Hard links will point to the same inode of a source file, the reason why hard links usually have the same inode numbers as their source files and same file attributes.

On the contrary, soft links will not point to the same inode of a source file, instead, it points to the file name of the source file, the reason why soft links usually have different inode numbers and different file attributes from their source files.

More so, since it is pointing to a source file name, if you delete or move the source file, the soft link becomes useless as well.

ACTION TIME

To create a hard link, use the command,

ln <source-file> <hard-link-file-name>

For example, to create a hard link named “hard-link-file” from “/tmp/tekneed-file”, use the command,

[root@HQEBPRD ~]# ln /tmp/tekneed-file hard-link-file

To verify, use the command,

[root@HQEBPRD ~]# ls -il /tmp/tekneed-file hard-link-file
34325167 -rw-r--r--. 2 root root 34 Apr 28 00:44 hard-link-file
34325167 -rw-r--r--. 2 root root 34 Apr 28 00:44 /tmp/tekneed-file
soft link in Linux

To create a soft link in Linux, use the command,

ln -s <source-file> <soft-link-file-name>

For example, to create a hard link named “soft-link-file” from “/tmp/tekneed-file”, use the command,

[root@HQEBPRD ~]# ln -s /tmp/tekneed-file soft-link-file

To verify, use the command,

[root@HQEBPRD ~]# ls -il /tmp/tekneed-file soft-link-file
34338143 lrwxrwxrwx. 1 root root 17 Apr 28 00:59 soft-link-file -> /tmp/tekneed-file
34325167 -rw-r--r--. 2 root root 34 Apr 28 00:44 /tmp/tekneed-file

Your feedback is welcomed. If you love others, you will share with others.

Be the first to comment

Leave a Reply

Your email address will not be published.


*