What is: hard link, symlink, and inode in Linux?

By | 03/03/2019
 

In Linux, the hard link, in general, is just the same file that the link refers to.

As opposed to the hard link – the symlink is kind of “soft link”.

Let’s check the details but first – a few words about inodes as they are the best way to demonstrate the difference.

This post originally was posted in this blog at 08/13/2013 in Russian.

The Inode

The simplest way to display the difference is by using the inode – index descriptor.

The inode is a filesystem object containing information about user and group owning a file, file’s permissions, size, type, timestamps with last file’s access (atime, access time), its modifications (mtime, modification time), inode’s itself modifications time (ctime, changing time), and a counter for hard links to this file.

Each inode object has own unique ID assigned during filesystem creation.

For example, let’s check some file using the ls utility with the -i option – this will display us information about inodes used for those files:

ls -lih | grep file
475949 -rw-r--r--  1 root     setevoy     0B Aug 13 11:51 file1
475950 -rw-r--r--  1 root     setevoy     0B Aug 13 11:51 file2
475951 -rw-r--r--  1 root     setevoy     0B Aug 13 11:51 file3
  • the very first column displays inode’s number per file – 475949.
  • the second column give us information about file’s permissions – – -rw-r–r–: owner – read/write, group – read, others – read.
  • the third column here is hard links counter – 1.

Next ones are the owner, group, size, mtime and the file name.

Inodes and the “No Space Left on Device” issue

A bit out of the scope on this post – but related to inodes and their role in filesystems.

Sometimes could happen a “weird” thing: an operating system can report that “No Space Left on Device” – but df or du will show you a lot of available free space on a partition.

One of the possible reasons is exactly the lack of inodes: they are created during partition creation and have a limited number.

So when all inodes on a partition will be in use – OS will tell you “No Space Left on Device“.

You always can check available inodes using the df tool with the -i option, e.g:

df -i /dev/sdb4
Filesystem      Inodes IUsed   IFree IUse% Mounted on
dev            2042653   523 2042130    1% /dev

Here – I can have maximum 2.042.653 files and/or directories on the /dev/sdb4 partition.

But for now we are interested in the inode’s number and hard links counter in the ls -lih from the previous topic so let’s go further to see about hard links.

The Hard link

So – what is the “hard link”?

In fact – that’s the same file just with another name.

Let’s create a new hard link with the ln tool.

It’s syntax is:

ln destitation_file hard_link

For example – create a file-link with the hardlink1 name which will point to the already existing file with the file1 name:

ln file1 hardlink1

Now to make sure they are the same filesystem objects – check their inodes. In this current case it is 475949:

ls -lih | grep 475949
475949 -rw-r--r--  2 root     setevoy     0B Aug 13 11:51 file1
475949 -rw-r--r--  2 root     setevoy     0B Aug 13 11:51 hardlink1

So as you see here – we have two files with different names but with the same inode number.

Now let’s add one more hard link and check the hard links counter again:

ln file1 hardlink2
ls -lih | grep 475949
475949 -rw-r--r--  3 root     setevoy     0B Aug 13 11:51 file1
475949 -rw-r--r--  3 root     setevoy     0B Aug 13 11:51 hardlink1
475949 -rw-r--r--  3 root     setevoy     0B Aug 13 11:51 hardlink2

Now it is 3 now instead of 2.

It’s even could be better to name this counter as a “file names counter” instead of the “hard links counter” because actually, we have one object just with different names.

The symlink

Now let’s check the symlinks.

You can create symlinks using the same ln tool with the -s (symlink) option with the following syntax:

ln -s file1 symlink1

Here we created a new (!) filesystem object with the symlink1 name which points to the already existing file file1:

ls -lih | grep sym
475948 lrwxr-xr-x  1 root     setevoy     5B Aug 13 12:02 symlink1 -> file1

Pay your attention to the l letter at the start of the permissions attributes column – here it is displaying that this file is exactly a symlink to another file and also you can see in its name column – symlink1 -> file1.

Most common attributes which can be displayed by the ls here are:

  • -: simple file
  • d: directory
  • l: symlink
  • s: socket

Now let’s compare both files inode-numbers:

ls -li
...
475949 -rw-r--r--  3 root     setevoy       0 Aug 13 11:51 file1
...
475949 -rw-r--r--  3 root     setevoy       0 Aug 13 11:51 hardlink1
475949 -rw-r--r--  3 root     setevoy       0 Aug 13 11:51 hardlink2
...
475948 lrwxr-xr-x  1 root     setevoy       5 Aug 13 12:02 symlink1 -> file1

Well – they are different because for the filesystem they are different objects and own has a dedicated set of attributes.

Hard link vs symlink – key differences

And now let’s check some main differences between such filesystem objects and how do they differ in a way to manipulate them.

  • a hard link cannot be pointed to a file on another filesystem as an inode can’t belong to the another FS, but with a symlink – this can be done
  • if you’ll edit a hard link – both the hard link and a destination file will be changed, but in the case with a symlink – you can change the symlink’s name, attributes or even delete or just “change a direction” to another file without affecting the original file (but keep in mind that exactly editing a symlink will change your origin file – as in fact you’ll edit the linked object)
  • you can’t have a hard link to any directory – only to a regular file, check this answer for more details

Removing links

A few more words about deleting links as this can be not always obvious.

When you’ll delete a hard link – the original file will exist until at least 1 hard link to it present on a filesystem, but can “change” its location if the original file was removed and there is still a hard link in another place.

When you’ll delete a file which has a symlink – this symlink just will become broken but will stay on a filesystem as an independent object.

Let’s check with some examples.

Delete the file1, on which the symlink1 object is referenced:

rm file1

The symlink itself still present:

ls -la | grep file1
lrwxr-xr-x   1 root     setevoy       5 Aug 13 12:02 symlink1 -> file1

But the file1 is absent:

file file1
file1: cannot open `file1' (No such file or directory)

And the symlink1 is broken now:

file symlink1
symlink1: broken symbolic link to `file1'

Hope this helps you get more about filesystems and links.



Also published on Medium.