I am looking for an explanation of the following:
- what happens in Linux when we create soft link and hardlink
- how are they accessible
- why do symlinks always display 777 permissions
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
In Unix, a file (or any filesystem object) is represented by an inode (indirect node). All information about the file (size, timestamps, permissions) are stored in the inode, a directory is essentially just a file mapping names to inodes. An inode can be mentioned several times, in the same directory with different names or in different directories. Each will be exactly the same file, there is no primary and secondaries here. One of the data in the inode is the number of links, deleting a file just erases the name form the directory and decrease the number of links. When the count reaches 0, the file can't be found anymore and can as well be destroyed. Each of the mentions of the file is called a hard link.
A symbolic link is essentially a special file which contains the name of a file. Whenever the symbolic link is followed, the file name contained is read, and that file is then looked up normally. Note that the target of the symbolic link might well have been removed, there is no way of knowing where such links might exist to correct them. The result is a broken link.
The permissions on the symbolic link are completely irrelevant, what is relevant are the permissions on the path to the target. Conventionally symbolic links have permission 0777 (octal).
Note that there are some restrictions: Hard links can be added to a file only within the same filesystem (inodes are known locally only), and can't refer to directories (except for the
.. entries). The last is to avoid loops, which would require costly garbage collection to find out if something can be deleted or not.
Symbolic links can cross filesystems, and can point at directories. But they are subject to the permissions of the path pointed to, and can get broken.