Hard links and symbolic links
Two ways to give a file a second name: a hard link, which is another directory entry for the same inode, and a symbolic link, which is a small file containing a path.
Lesson 5 of 6 in objective 104. Devices, Linux filesystems, filesystem hierarchy standard, part of LPIC-1 Exam 101-500.
What an inode is, and what a name is
A file's data and metadata live in an inode; the name is a directory entry pointing at that inode. A hard link is simply a second directory entry pointing at the same inode, so the two names are equal in every way — there is no original. The link count in ls -l is how many names the inode has, and the data is released only when that count reaches zero and no process holds it open. ls -i prints the inode number, which is how you prove two names are the same file.
Because a hard link is a directory entry pointing at an inode, and inode numbers are only meaningful within one filesystem, a hard link cannot cross a filesystem boundary. Directories cannot be hard linked either, on ordinary systems, because it would let you build loops in the tree.
Symbolic links, and how they fail
A symbolic link is a tiny file whose content is a path. It can point anywhere — another filesystem, a device, a path that does not exist yet — and it is resolved when it is used. That flexibility is exactly its weakness: delete or move the target and the link remains, now dangling and pointing at nothing. ls -l shows it with an l in the type column and an arrow to its target.
ln makes a hard link, ln -s a symbolic one, and the argument order is target first, then the new name. A symbolic link created with a relative path is resolved relative to the LINK's directory rather than to the working directory you made it from, which is the usual cause of a link that works when created and breaks the moment you cd elsewhere.
Three commands show the path a link STORES rather than following it to what it points at: ls -l prints it after the arrow, readlink prints the bare string and nothing else, and stat reports it on a line of its own. Ordinary reading goes the other way — cat opens the target, and ls -L or stat -L follow the link too — and the split underneath is the lstat system call against stat. When a link points at another link, plain readlink still prints only the single hop it holds; readlink -f walks the whole chain and resolves every . and .. to one absolute path, -e is the strict variant that prints nothing unless every component exists, and -m the permissive one that checks nothing at all. realpath is the same canonicalisation as a command in its own right.
cd /home/carolln -s ../shared/data.csv /srv/app/data.csvreadlink /srv/app/data.csv../shared/data.csvthe stored string, unchanged. Nothing is resolved yetreadlink -f /srv/app/data.csv/srv/shared/data.csvread from /srv/app, where the link is — not /home/carol
Worth carrying in
- ln target name
- Hard link: another directory entry for the same inode.
- ln -s target name
- Symbolic link: a file containing a path.
- ls -i
- Inode numbers. Two names with one number are the same file.
- ls -l
- Link count in the second column; l and an arrow for symbolic links.
- readlink
- The one path string a link stores.
statandls -lshow the same thing. - readlink -f
- Follow the whole chain to one absolute path.
-erequires every component to exist;realpathis the same job.
What the exam does with this
- Hard links cannot cross filesystems and cannot be made to directories. Symbolic links can do both.
- Deleting the target breaks a symbolic link and does nothing to a hard link.
- There is no "original" among hard links — every name is equally the file.
- A relative symbolic link is resolved from the directory holding the LINK, never from the directory you were in when you created it.
readlinkprints one hop andreadlink -fprints the end of the chain. Commands that act on the link —ls -l,readlink,stat— against ones that follow it, likecat.
- Objective
- 104. Devices, Linux filesystems, filesystem hierarchy standard
- Share of the exam
- 25% (the whole objective)
- Questions in this lesson
- 11
- Signed for by a person
- 0
Partly checked. None of the 11 questions here has been read against the cited source by a person. 11 questions have been checked against their cited clause by an automated pass — which is not the same thing, and is not a signature.
Only questions a person has signed for are used in mock exams here. That is the whole difference between the two kinds of checking above.
How these questions are written — where each question comes from, what the verification ledger records, and what happens when one is found wrong.
Drill this lesson
A lesson is one sitting: the trainer draws a short run from these questions alone and spaces the ones you get wrong.
Practise Hard links and symbolic links
Questions in this lesson
- You run `ln /data/report.txt /data/backup.txt` and then `rm /data/report.txt`. What is the state of the data afterwards? machine-checked
- Select the THREE statements that are true about links on a Linux filesystem. machine-checked
- Type the complete command that creates a symbolic link at /usr/local/bin/python pointing to the existing executable /usr/bin/python3.11. machine-checked
- /srv/media is a separately mounted filesystem. You want /home/carol/clip.mp4 to refer to the existing file /srv/media/clip.mp4, and /home is on the root filesystem. Which command produces a working second name? machine-checked
- The paths /var/www/index.html and /srv/site/index.html sit on the same filesystem. Which command's output settles whether they are two names for one inode rather than two independent copies? machine-checked
- Working in /home/carol, you run `ln -s ../shared/data.csv /srv/app/data.csv`. Which file will /srv/app/data.csv resolve to when a program opens it? machine-checked
- /etc/localtime is already a symbolic link to a zoneinfo file, and `ln -s /usr/share/zoneinfo/Europe/Berlin /etc/localtime` fails because the destination exists. Which command replaces the link in one step? machine-checked
- The whole of /srv is one filesystem. A 4 GiB image at /srv/iso/debian.iso must also be reachable as /srv/pub/debian.iso, must consume no additional disk space, and the second name must keep working even if /srv/iso/debian.iso is later removed. Which command satisfies all three requirements? machine-checked
- /etc/localtime is a symbolic link. Which THREE commands display the path that the link stores as its target? machine-checked
- /etc/localtime may be a symbolic link that points at another symbolic link. Type the readlink command line, including the option, that prints the final canonical absolute path the chain resolves to. machine-checked
- `ls -l /srv/report.csv` shows `lrwxrwxrwx 1 root root 19 ... /srv/report.csv -> /data/q3/report.csv`, and the file at the other end is `-rw-r----- 1 root staff`. A user who is neither root nor a member of staff tries to read /srv/report.csv. What decides whether the read is allowed? machine-checked
Practise Hard links and symbolic links
The rest of objective 104
- Partitions and filesystems
- Keeping filesystems intact
- Mounting and unmounting filesystems
- File permissions, ownership and disk quotas
- Hard links and symbolic links — you are here
- Finding files and where they belong