# symlink(7) - man - phpMan

[SYMLINK(7)](https://www.chedong.com/phpMan.php/man/SYMLINK/7/markdown)                            Linux Programmer's Manual                           [SYMLINK(7)](https://www.chedong.com/phpMan.php/man/SYMLINK/7/markdown)



## NAME
       symlink - symbolic link handling

## DESCRIPTION
       Symbolic  links are files that act as pointers to other files.  To understand their behavior,
       you must first understand how hard links work.

       A hard link to a file is indistinguishable from the original file because it is  a  reference
       to  the object underlying the original filename.  (To be precise: each of the hard links to a
       file is a reference to the same _inode_ _number_, where an inode number is an index into the  in‐
       ode  table,  which contains metadata about all files on a filesystem.  See [**stat**(2)](https://www.chedong.com/phpMan.php/man/stat/2/markdown).)  Changes
       to a file are independent of the name used to reference the file.  Hard links may  not  refer
       to  directories  (to prevent the possibility of loops within the filesystem tree, which would
       confuse many programs) and may not refer to files on  different  filesystems  (because  inode
       numbers are not unique across filesystems).

       A symbolic link is a special type of file whose contents are a string that is the pathname of
       another file, the file to which the link refers.  (The contents of a  symbolic  link  can  be
       read  using  [**readlink**(2)](https://www.chedong.com/phpMan.php/man/readlink/2/markdown).)  In other words, a symbolic link is a pointer to another name, and
       not to an underlying object.  For this reason, symbolic links may refer  to  directories  and
       may cross filesystem boundaries.

       There  is  no  requirement  that the pathname referred to by a symbolic link should exist.  A
       symbolic link that refers to a pathname that does not exist is said to be a _dangling_ _link_.

       Because a symbolic link and its referenced object coexist in the filesystem name space,  con‐
       fusion  can  arise  in  distinguishing between the link itself and the referenced object.  On
       historical systems, commands and system calls adopted their own link-following conventions in
       a  somewhat  ad-hoc  fashion.   Rules for a more uniform approach, as they are implemented on
       Linux and other systems, are outlined here.  It is  important  that  site-local  applications
       also conform to these rules, so that the user interface can be as consistent as possible.

### Magic links
       There  is  a special class of symbolic-link-like objects known as "magic links", which can be
       found in certain pseudofilesystems such as  [**proc**(5)](https://www.chedong.com/phpMan.php/man/proc/5/markdown)  (examples  include  _/proc/[pid]/exe_  and
       _/proc/[pid]/fd/*_).   Unlike normal symbolic links, magic links are not resolved through path‐
       name-expansion, but instead act as direct references to the kernel's own representation of  a
       file  handle.   As such, these magic links allow users to access files which cannot be refer‐
       enced with normal paths (such as unlinked files still referenced by a running program ).

       Because they can bypass ordinary **mount**___**[namespaces**(7)](https://www.chedong.com/phpMan.php/man/namespaces/7/markdown)-based  restrictions,  magic  links  have
       been used as attack vectors in various exploits.

### Symbolic link ownership, permissions, and timestamps
       The  owner  and  group of an existing symbolic link can be changed using [**lchown**(2)](https://www.chedong.com/phpMan.php/man/lchown/2/markdown).  The only
       time that the ownership of a symbolic link matters is when the link is being removed  or  re‐
       named in a directory that has the sticky bit set (see [**stat**(2)](https://www.chedong.com/phpMan.php/man/stat/2/markdown)).

       The last access and last modification timestamps of a symbolic link can be changed using **uti**‐‐
       [**mensat**(2)](https://www.chedong.com/phpMan.php/man/mensat/2/markdown) or [**lutimes**(3)](https://www.chedong.com/phpMan.php/man/lutimes/3/markdown).

       On Linux, the permissions of an ordinary symbolic link are not used in  any  operations;  the
       permissions  are always 0777 (read, write, and execute for all user categories), and can't be
       changed.

       However, magic links do not follow this rule.  They can have a  non-0777  mode,  though  this
       mode is not currently used in any permission checks.


### Obtaining a file descriptor that refers to a symbolic link
       Using  the combination of the **O**___**PATH** and **O**___**NOFOLLOW** flags to [**open**(2)](https://www.chedong.com/phpMan.php/man/open/2/markdown) yields a file descriptor
       that can be passed as the _dirfd_ argument in system calls  such  as  [**fstatat**(2)](https://www.chedong.com/phpMan.php/man/fstatat/2/markdown),  [**fchownat**(2)](https://www.chedong.com/phpMan.php/man/fchownat/2/markdown),
       [**fchmodat**(2)](https://www.chedong.com/phpMan.php/man/fchmodat/2/markdown),  [**linkat**(2)](https://www.chedong.com/phpMan.php/man/linkat/2/markdown),  and  [**readlinkat**(2)](https://www.chedong.com/phpMan.php/man/readlinkat/2/markdown), in order to operate on the symbolic link itself
       (rather than the file to which it refers).

       By default (i.e., if the **AT**___**SYMLINK**___**FOLLOW** flag is not specified), if **name**___**to**___**handle**___**[at**(2)](https://www.chedong.com/phpMan.php/man/at/2/markdown) is
       applied to a symbolic link, it yields a handle for the symbolic link (rather than the file to
       which it refers).  One can then obtain a file descriptor for the symbolic link  (rather  than
       the  file  to  which  it  refers)  by  specifying  the  **O**___**PATH**  flag  in a subsequent call to
       **open**___**by**___**handle**___**[at**(2)](https://www.chedong.com/phpMan.php/man/at/2/markdown).  Again, that file descriptor can be used in the  aforementioned  system
       calls to operate on the symbolic link itself.

### Handling of symbolic links by system calls and commands
       Symbolic links are handled either by operating on the link itself, or by operating on the ob‐
       ject referred to by the link.  In the latter case, an application or system call is  said  to
       _follow_  the  link.  Symbolic links may refer to other symbolic links, in which case the links
       are dereferenced until an object that is not a symbolic link is found, a symbolic  link  that
       refers  to  a  file which does not exist is found, or a loop is detected.  (Loop detection is
       done by placing an upper limit on the number of links that may be followed, and an error  re‐
       sults if this limit is exceeded.)

       There are three separate areas that need to be discussed.  They are as follows:

       1. Symbolic links used as filename arguments for system calls.

       2. Symbolic  links specified as command-line arguments to utilities that are not traversing a
          file tree.

       3. Symbolic links encountered by utilities that are traversing a file tree (either  specified
          on the command line or encountered as part of the file hierarchy walk).

       Before  describing  the  treatment of symbolic links by system calls and commands, we require
       some terminology.  Given a pathname of the form _a/b/c_, the part  preceding  the  final  slash
       (i.e., _a/b_) is called the _dirname_ component, and the part following the final slash (i.e., _c_)
       is called the _basename_ component.

### Treatment of symbolic links in system calls
       The first area is symbolic links used as filename arguments for system calls.

       The treatment of symbolic links within a pathname passed to a system call is as follows:

       1. Within the dirname component of a pathname, symbolic links are always followed  in  nearly
          every  system  call.   (This is also true for commands.)  The one exception is [**openat2**(2)](https://www.chedong.com/phpMan.php/man/openat2/2/markdown),
          which provides flags that can be used to explicitly prevent following of symbolic links in
          the dirname component.

       2. Except as noted below, all system calls follow symbolic links in the basename component of
          a pathname.  For example, if there were a symbolic link _slink_  which  pointed  to  a  file
          named _afile_, the system call _open("slink"_ _...)_ would return a file descriptor referring to
          the file _afile_.

       Various system calls do not follow links in the basename component of a pathname, and operate
       on  the  symbolic  link  itself.  They are: [**lchown**(2)](https://www.chedong.com/phpMan.php/man/lchown/2/markdown), [**lgetxattr**(2)](https://www.chedong.com/phpMan.php/man/lgetxattr/2/markdown), [**llistxattr**(2)](https://www.chedong.com/phpMan.php/man/llistxattr/2/markdown), **lremovex**‐‐
       [**attr**(2)](https://www.chedong.com/phpMan.php/man/attr/2/markdown), [**lsetxattr**(2)](https://www.chedong.com/phpMan.php/man/lsetxattr/2/markdown), [**lstat**(2)](https://www.chedong.com/phpMan.php/man/lstat/2/markdown), [**readlink**(2)](https://www.chedong.com/phpMan.php/man/readlink/2/markdown), [**rename**(2)](https://www.chedong.com/phpMan.php/man/rename/2/markdown), [**rmdir**(2)](https://www.chedong.com/phpMan.php/man/rmdir/2/markdown), and [**unlink**(2)](https://www.chedong.com/phpMan.php/man/unlink/2/markdown).

       Certain other system calls optionally follow symbolic links in the basename  component  of  a
       pathname.   They are: [**faccessat**(2)](https://www.chedong.com/phpMan.php/man/faccessat/2/markdown), [**fchownat**(2)](https://www.chedong.com/phpMan.php/man/fchownat/2/markdown), [**fstatat**(2)](https://www.chedong.com/phpMan.php/man/fstatat/2/markdown), [**linkat**(2)](https://www.chedong.com/phpMan.php/man/linkat/2/markdown), **name**___**to**___**handle**___**[at**(2)](https://www.chedong.com/phpMan.php/man/at/2/markdown),
       [**open**(2)](https://www.chedong.com/phpMan.php/man/open/2/markdown), [**openat**(2)](https://www.chedong.com/phpMan.php/man/openat/2/markdown), **open**___**by**___**handle**___**[at**(2)](https://www.chedong.com/phpMan.php/man/at/2/markdown), and [**utimensat**(2)](https://www.chedong.com/phpMan.php/man/utimensat/2/markdown); see their manual  pages  for  de‐
       tails.  Because [**remove**(3)](https://www.chedong.com/phpMan.php/man/remove/3/markdown) is an alias for [**unlink**(2)](https://www.chedong.com/phpMan.php/man/unlink/2/markdown), that library function also does not fol‐
       low symbolic links.  When [**rmdir**(2)](https://www.chedong.com/phpMan.php/man/rmdir/2/markdown) is applied to a symbolic link, it  fails  with  the  error
       **ENOTDIR**.

       [**link**(2)](https://www.chedong.com/phpMan.php/man/link/2/markdown)  warrants special discussion.  POSIX.1-2001 specifies that [**link**(2)](https://www.chedong.com/phpMan.php/man/link/2/markdown) should dereference
       _oldpath_ if it is a symbolic link.  However, Linux does not do this.  (By default, Solaris  is
       the  same, but the POSIX.1-2001 specified behavior can be obtained with suitable compiler op‐
       tions.)  POSIX.1-2008 changed the specification to allow either behavior  in  an  implementa‐
       tion.

### Commands not traversing a file tree
       The  second area is symbolic links, specified as command-line filename arguments, to commands
       which are not traversing a file tree.

       Except as noted below, commands follow symbolic links named as command-line  arguments.   For
       example, if there were a symbolic link _slink_ which pointed to a file named _afile_, the command
       _cat_ _slink_ would display the contents of the file _afile_.

       It is important to realize that this rule includes commands  which  may  optionally  traverse
       file  trees;  for example, the command _chown_ _file_ is included in this rule, while the command
       _chown_ _-R_ _file_, which performs a tree traversal, is not.  (The  latter  is  described  in  the
       third area, below.)

       If it is explicitly intended that the command operate on the symbolic link instead of follow‐
       ing the symbolic link—for example, it is desired that _chown_ _slink_ change the ownership of the
       file  that  _slink_ is, whether it is a symbolic link or not—then the _-h_ option should be used.
       In the above example, _chown_ _root_ _slink_ would change the ownership of the file referred to  by
       _slink_, while _chown_ _-h_ _root_ _slink_ would change the ownership of _slink_ itself.

       There are some exceptions to this rule:

       * The  [**mv**(1)](https://www.chedong.com/phpMan.php/man/mv/1/markdown)  and [**rm**(1)](https://www.chedong.com/phpMan.php/man/rm/1/markdown) commands do not follow symbolic links named as arguments, but respec‐
         tively attempt to rename and delete them.  (Note, if the symbolic link  references  a  file
         via a relative path, moving it to another directory may very well cause it to stop working,
         since the path may no longer be correct.)

       * The [**ls**(1)](https://www.chedong.com/phpMan.php/man/ls/1/markdown) command is also an exception to this rule.  For compatibility with historic  sys‐
         tems  (when  [**ls**(1)](https://www.chedong.com/phpMan.php/man/ls/1/markdown) is not doing a tree walk—that is, _-R_ option is not specified), the [**ls**(1)](https://www.chedong.com/phpMan.php/man/ls/1/markdown)
         command follows symbolic links named as arguments if the _-H_ or _-L_ option is  specified,  or
         if  the  _-F_,  _-d_,  or _-l_ options are not specified.  (The [**ls**(1)](https://www.chedong.com/phpMan.php/man/ls/1/markdown) command is the only command
         where the _-H_ and _-L_ options affect its behavior even though it is not doing  a  walk  of  a
         file tree.)

       * The [**file**(1)](https://www.chedong.com/phpMan.php/man/file/1/markdown) command is also an exception to this rule.  The [**file**(1)](https://www.chedong.com/phpMan.php/man/file/1/markdown) command does not follow
         symbolic links named as argument by default.  The  [**file**(1)](https://www.chedong.com/phpMan.php/man/file/1/markdown)  command  does  follow  symbolic
         links named as argument if the _-L_ option is specified.

### Commands traversing a file tree
       The  following  commands either optionally or always traverse file trees: [**chgrp**(1)](https://www.chedong.com/phpMan.php/man/chgrp/1/markdown), [**chmod**(1)](https://www.chedong.com/phpMan.php/man/chmod/1/markdown),
       [**chown**(1)](https://www.chedong.com/phpMan.php/man/chown/1/markdown), [**cp**(1)](https://www.chedong.com/phpMan.php/man/cp/1/markdown), [**du**(1)](https://www.chedong.com/phpMan.php/man/du/1/markdown), [**find**(1)](https://www.chedong.com/phpMan.php/man/find/1/markdown), [**ls**(1)](https://www.chedong.com/phpMan.php/man/ls/1/markdown), [**pax**(1)](https://www.chedong.com/phpMan.php/man/pax/1/markdown), [**rm**(1)](https://www.chedong.com/phpMan.php/man/rm/1/markdown), and [**tar**(1)](https://www.chedong.com/phpMan.php/man/tar/1/markdown).

       It is important to realize that the following rules apply equally to symbolic  links  encoun‐
       tered during the file tree traversal and symbolic links listed as command-line arguments.

       The _first_ _rule_ applies to symbolic links that reference files other than directories.  Opera‐
       tions that apply to symbolic links are performed on the links themselves, but  otherwise  the
       links are ignored.

       The  command  _rm_ _-r_  _slink_ _directory_ will remove _slink_, as well as any symbolic links encoun‐
       tered in the tree traversal of _directory_, because symbolic links may be removed.  In no  case
       will [**rm**(1)](https://www.chedong.com/phpMan.php/man/rm/1/markdown) affect the file referred to by _slink_.

       The _second_ _rule_ applies to symbolic links that refer to directories.  Symbolic links that re‐
       fer to directories are never followed by default.  This is often referred to as a  "physical"
       walk, as opposed to a "logical" walk (where symbolic links that refer to directories are fol‐
       lowed).

       Certain conventions are (should be) followed as consistently as  possible  by  commands  that
       perform file tree walks:

       * A command can be made to follow any symbolic links named on the command line, regardless of
         the type of file they reference, by specifying the _-H_ (for "half-logical") flag.  This flag
         is  intended  to make the command-line name space look like the logical name space.  (Note,
         for commands that do not always do file tree traversals, the _-H_ flag will be ignored if the
         _-R_ flag is not also specified.)

         For  example,  the  command _chown_ _-HR_ _user_ _slink_ will traverse the file hierarchy rooted in
         the file pointed to by _slink_.  Note, the _-H_ is not the same as the previously discussed  _-h_
         flag.   The  _-H_ flag causes symbolic links specified on the command line to be dereferenced
         for the purposes of both the action to be performed and the tree walk, and it is as if  the
         user had specified the name of the file to which the symbolic link pointed.

       * A  command  can  be made to follow any symbolic links named on the command line, as well as
         any symbolic links encountered during the traversal, regardless of the type  of  file  they
         reference,  by  specifying  the _-L_ (for "logical") flag.  This flag is intended to make the
         entire name space look like the logical name space.  (Note, for commands that do not always
         do file tree traversals, the _-L_ flag will be ignored if the _-R_ flag is not also specified.)

         For example, the command _chown_ _-LR_ _user_ _slink_ will change the owner of the file referred to
         by _slink_.  If _slink_ refers to a directory, **chown** will traverse the file hierarchy rooted in
         the  directory  that  it references.  In addition, if any symbolic links are encountered in
         any file tree that **chown** traverses, they will be treated in the same fashion as _slink_.

       * A command can be made to provide the default behavior by specifying the _-P_ (for "physical")
         flag.   This  flag  is  intended  to make the entire name space look like the physical name
         space.

       For commands that do not by default do file tree traversals, the _-H_, _-L_, and _-P_ flags are ig‐
       nored  if the _-R_ flag is not also specified.  In addition, you may specify the _-H_, _-L_, and _-P_
       options more than once; the last one specified determines the command's  behavior.   This  is
       intended  to  permit  you to alias commands to behave one way or the other, and then override
       that behavior on the command line.

       The [**ls**(1)](https://www.chedong.com/phpMan.php/man/ls/1/markdown) and [**rm**(1)](https://www.chedong.com/phpMan.php/man/rm/1/markdown) commands have exceptions to these rules:

       * The [**rm**(1)](https://www.chedong.com/phpMan.php/man/rm/1/markdown) command operates on the symbolic link, and not the file it references, and there‐
         fore  never  follows a symbolic link.  The [**rm**(1)](https://www.chedong.com/phpMan.php/man/rm/1/markdown) command does not support the _-H_, _-L_, or _-P_
         options.

       * To maintain compatibility with historic systems, the [**ls**(1)](https://www.chedong.com/phpMan.php/man/ls/1/markdown) command acts  a  little  differ‐
         ently.   If  you  do not specify the _-F_, _-d_ or _-l_ options, [**ls**(1)](https://www.chedong.com/phpMan.php/man/ls/1/markdown) will follow symbolic links
         specified on the command line.  If the _-L_ flag is specified,  [**ls**(1)](https://www.chedong.com/phpMan.php/man/ls/1/markdown)  follows  all  symbolic
         links,  regardless  of  their type, whether specified on the command line or encountered in
         the tree walk.

## SEE ALSO
       [**chgrp**(1)](https://www.chedong.com/phpMan.php/man/chgrp/1/markdown), [**chmod**(1)](https://www.chedong.com/phpMan.php/man/chmod/1/markdown), [**find**(1)](https://www.chedong.com/phpMan.php/man/find/1/markdown),  [**ln**(1)](https://www.chedong.com/phpMan.php/man/ln/1/markdown),  [**ls**(1)](https://www.chedong.com/phpMan.php/man/ls/1/markdown),  [**mv**(1)](https://www.chedong.com/phpMan.php/man/mv/1/markdown),  [**namei**(1)](https://www.chedong.com/phpMan.php/man/namei/1/markdown),  [**rm**(1)](https://www.chedong.com/phpMan.php/man/rm/1/markdown),  [**lchown**(2)](https://www.chedong.com/phpMan.php/man/lchown/2/markdown),  [**link**(2)](https://www.chedong.com/phpMan.php/man/link/2/markdown),
       [**lstat**(2)](https://www.chedong.com/phpMan.php/man/lstat/2/markdown), [**readlink**(2)](https://www.chedong.com/phpMan.php/man/readlink/2/markdown), [**rename**(2)](https://www.chedong.com/phpMan.php/man/rename/2/markdown), [**symlink**(2)](https://www.chedong.com/phpMan.php/man/symlink/2/markdown), [**unlink**(2)](https://www.chedong.com/phpMan.php/man/unlink/2/markdown), [**utimensat**(2)](https://www.chedong.com/phpMan.php/man/utimensat/2/markdown), [**lutimes**(3)](https://www.chedong.com/phpMan.php/man/lutimes/3/markdown), **path**___**reso**‐‐
       [**lution**(7)](https://www.chedong.com/phpMan.php/man/lution/7/markdown)

## COLOPHON
       This page is part of release 5.10 of the Linux  _man-pages_  project.   A  description  of  the
       project,  information about reporting bugs, and the latest version of this page, can be found
       at <https://www.kernel.org/doc/man-pages/>.



Linux                                        2020-06-09                                   [SYMLINK(7)](https://www.chedong.com/phpMan.php/man/SYMLINK/7/markdown)
