What is behind a symbolic link?

How symbolic links are managed internally by UNIX / Linux. It is known that a symbolic link can exist even without the actual target file (Dangling link). So what is a symbolic link inside.

On Windows, the answer will be reparse point .

Questions:

Is the answer a inode on UNIX / Linux?

If so, will the inode number be the same for purposes and references?

If so, can the linked index have permissions other than permissions for the target index (if one exists)?

+8
linux unix linux-kernel symlink inode
source share
2 answers

This is not about UNIX / Linux, but about the implementation of the file system, but yes, Unix / Linux uses inodes at the kernel level, and the file system implementations have inodes (at least virtual ones).

In general, symbolic links are simply files (btw, directories are also files) that have:

  • a file-type flag in "inode" that tells the system that this file is a "symbolic link"
  • file-content: path to the target - in other words: a symbolic link is just a file that contains the name of the flag file in the inode.

Virtual file systems can also have symbolic links, so check out FUSE or other sources of file system implementations. (Ext2 / Ext3 / ufs..etc)

So,

Is the inode answer on UNIX / Linux?

depends on the implementation of the file system, but yes, usually the inode contains a β€œfile type” (and owners, permissions, timestamps, size, pointers to data blocks). There are file systems that do not have an inode (in physical implication), but have only "virtual inodes" to ensure kernel compatibility.

If so, will the inode number be the same for purposes and references?

No. Usually a symbolic link is a file with its own inode (with a file type, its own data blocks, etc.).

If so, can the inode index have permissions other than the access permissions of the target inode (if it exists)?

This is about how symlink files are handled. Typically, the kernel does not allow changes to symlink permissions - and symlinks always have default permissions. You could write your own file system that would allow different permissions for symbolic links, but you would run into a problem because regular programs like chmod do not change permissions for symbolic links, so creating such a file system is senseless anyway )

To understand the difference between hardlinks and symlinks, you must first understand directories.

Directories are files (differentiated by the inode flag) that tell the kernel "treat this file as a map from file-name to inode_number ". Hard links are simply file names that map to the same inode . Therefore, if the directory file contains:

 file_a: 1000 file_b: 1001 file_c: 1000 

the above means that there are 3 files in this directory:

  • file_a described by inode 1000
  • file_b described by inode 1001 and
  • file_c is again described by inode 1000 (therefore it is a hard link with file_a, not hardlink to file_a), because it is impossible to determine which file name was first - they are identical).

This is the main difference from symbolic links, where inode file_b (inode 1001) can have the content "file_a", and the flag means "this is a symbolic link." In this case, file_b will be a symbolic link pointing to file_a .

+13
source share

You can also easily learn this yourself:

 $ touch a $ ln -sab $ ln ac $ ls -li total 0 95905 -rw-r--r-- 1 regnarg regnarg 0 Jun 19 19:01 a 96990 lrwxrwxrwx 1 regnarg regnarg 1 Jun 19 19:01 b -> a 95905 -rw-r--r-- 2 regnarg regnarg 0 Jun 19 19:01 c 

The -i option for ls shows the handle numbers in the first column. You can see that the symbolic link has a different inode number, while hardlink has the same. You can also use the stat(1) command:

 $ stat a File: 'a' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 28h/40d Inode: 95905 Links: 2 [...] $ stat b File: 'b' -> 'a' Size: 1 Blocks: 0 IO Block: 4096 symbolic link Device: 28h/40d Inode: 96990 Links: 1 [...] 

If you want to do this programmatically, you can use the lstat(2) system call to find information about the symbolic link itself (its inode number, etc.), and stat(2) displays information about the purpose of the symbolic link, if it exists . An example in Python:

 >>> import os >>> os.stat("b").st_ino 95905 >>> os.lstat("b").st_ino 96990 
+2
source share

All Articles