What is: hard link, symlink, and inode in Linux?
Tags: linux, filesystem, inode
Published: 2019-03-04 09:18:14

---
title: What is: hard link, symlink, and inode in Linux?
published: true
tags: Linux,filesystem,inode
canonical_url: https://rtfm.co.ua/en/what-is-hard-link-symlink-and-inode-in-linux/
---


![](https://rtfm.co.ua/wp-content/uploads/2016/01/What-is-it-e1452250201867.jpg)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](https://rtfm.co.ua/unix-chto-takoe-symlink-hardlink-i-inode/) 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](https://askubuntu.com/a/525129/314659) 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.
  

### Similar posts

- <small>08/13/2013</small> [What is: что такое symlink, hard link и inode в Linux?](https://rtfm.co.ua/unix-chto-takoe-symlink-hardlink-i-inode/)<small> (7)</small>  
- <small>12/18/2013</small> [UNIX: df и du – разные значения](https://rtfm.co.ua/unix-df-i-du-raznye-znacheniya/)<small> (0)</small>  
- <small>10/20/2018</small> [Linux: увеличение размера раздела без LVM с сохранением данных на ext4](https://rtfm.co.ua/linux-uvelichenie-razmera-razdela-bez-lvm-s-soxraneniem-dannyx-na-ext4/)<small> (0)</small>  
- <small>03/18/2017</small> [Linux: LVM – уменьшить home, увеличить root](https://rtfm.co.ua/linux-lvm-umenshit-home-uvelichit-root/)<small> (1)</small>  
- <small>12/25/2018</small> [Linux: добавить swap-file](https://rtfm.co.ua/linux-dobavit-swap-file/)<small> (0)</small>