> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Create and Change Hard Links

> Learn how to create and manage hard links in Linux to share files efficiently without duplicating data.

In this lesson, you’ll learn how Linux uses inodes and hard links to share files efficiently—without duplicating data. We’ll cover:

* What inodes and links are
* Creating hard links
* Deletion behavior
* Common limitations
* Permission management

## What Are Inodes and Hard Links?

Every file on a Linux filesystem is represented by an **inode**, which stores metadata (permissions, timestamps, disk block locations). A **hard link** is simply another directory entry that points to the same inode.

Consider Aaron’s photo of his dog Milo, saved as:

```bash theme={null}
/home/aaron/Pictures/family_dog.jpg
```

Create this sample file and inspect its inode:

```bash theme={null}
# Create the file
echo "Picture of Milo the dog" > ~/Pictures/family_dog.jpg

# Show metadata, including inode and link count
stat ~/Pictures/family_dog.jpg
```

Example output:

```text theme={null}
File: /home/aaron/Pictures/family_dog.jpg
Size: 24           Blocks: 8    IO Block: 4096   regular file
Device: fd00h/64768  Inode: 52946177    Links: 1
Access: (0640/-rw-r-----)  Uid: (1000/aaron)  Gid: (1005/family)
...
```

| Term  | Description                                                                    |
| ----- | ------------------------------------------------------------------------------ |
| Inode | Unique number with file metadata & data-block pointers                         |
| Links | Count of directory entries referencing this inode (starts at 1 for a new file) |
| Data  | Actual content blocks on disk                                                  |

<Callout icon="lightbulb" color="#1CB2FE">
  When you open or read a file, the kernel looks up its name, retrieves the inode (e.g., `52946177`), and accesses the data blocks. Hard links simply give you multiple names for the same inode.
</Callout>

## Sharing Files Without Duplication

Copying large directories wastes disk space:

```bash theme={null}
cp -r /home/aaron/Pictures/ /home/jane/Pictures/
```

Instead, Jane can create a hard link to Aaron’s photo:

```bash theme={null}
ln /home/aaron/Pictures/family_dog.jpg \
   /home/jane/Pictures/family_dog.jpg
```

* **Source**: `/home/aaron/Pictures/family_dog.jpg`
* **Link name**: `/home/jane/Pictures/family_dog.jpg`

Verify with `stat`:

```bash theme={null}
stat /home/jane/Pictures/family_dog.jpg
```

Now the link count is **2**:

```text theme={null}
Inode: 52946177    Links: 2
```

## Deletion Behavior

When Aaron removes his link, the data remains as long as one hard link exists:

```bash theme={null}
rm /home/aaron/Pictures/family_dog.jpg
stat /home/jane/Pictures/family_dog.jpg
```

* Link count drops to 1
* File content is still intact for Jane

Only when the last link is deleted does the filesystem free the inode and data:

```bash theme={null}
rm /home/jane/Pictures/family_dog.jpg
# inode 52946177 and its data blocks are now reclaimed
```

## Hard Link vs. Copy

| Operation | Disk Usage | Link Count | Independent Changes? |
| --------- | ---------- | ---------- | -------------------- |
| Copy      | +100%      | 1 each     | Yes                  |
| Hard Link | +0%        | >1         | No (shared inode)    |

<Callout icon="triangle-alert" color="#FF6B6B">
  * You cannot create hard links to directories (prevents filesystem loops).
  * Hard links must reside on the **same** filesystem—cross-device linking is not allowed.
</Callout>

## Managing Permissions for Shared Files

Since file permissions live in the inode, updating them on one hard link affects all links. To let both Aaron and Jane read/write:

1. Add both users to a common group (`family`):

   ```bash theme={null}
   sudo usermod -aG family aaron
   sudo usermod -aG family jane
   ```

2. Set group read/write permissions:

   ```bash theme={null}
   chmod 660 /home/aaron/Pictures/family_dog.jpg
   ```

Now both can modify the shared file seamlessly.

## Further Reading

* [stat(1) — Display File Status](https://man7.org/linux/man-pages/man1/stat.1.html)
* [ln(1) — Make Links](https://man7.org/linux/man-pages/man1/ln.1.html)
* [Understanding Linux Filesystems](https://www.kernel.org/doc/html/latest/filesystems/index.html)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/linux-professional-institute-lpic-1-exam-101/module/de71b96a-9dc0-4e92-987a-6c7055c44e8b/lesson/43f1c7d9-3aa0-4464-830d-6676106126a1" />
</CardGroup>
