{
    "mode": "man",
    "parameter": "SYMLINK",
    "section": "7",
    "url": "https://www.chedong.com/phpMan.php/man/SYMLINK/7/json",
    "generated": "2026-05-30T06:10:50Z",
    "sections": {
        "NAME": {
            "content": "symlink - symbolic link handling\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Symbolic  links are files that act as pointers to other files.  To understand their behavior,\nyou must first understand how hard links work.\n\nA hard link to a file is indistinguishable from the original file because it is  a  reference\nto  the object underlying the original filename.  (To be precise: each of the hard links to a\nfile is a reference to the same inode number, where an inode number is an index into the  in‐\node  table,  which contains metadata about all files on a filesystem.  See stat(2).)  Changes\nto a file are independent of the name used to reference the file.  Hard links may  not  refer\nto  directories  (to prevent the possibility of loops within the filesystem tree, which would\nconfuse many programs) and may not refer to files on  different  filesystems  (because  inode\nnumbers are not unique across filesystems).\n\nA symbolic link is a special type of file whose contents are a string that is the pathname of\nanother file, the file to which the link refers.  (The contents of a  symbolic  link  can  be\nread  using  readlink(2).)  In other words, a symbolic link is a pointer to another name, and\nnot to an underlying object.  For this reason, symbolic links may refer  to  directories  and\nmay cross filesystem boundaries.\n\nThere  is  no  requirement  that the pathname referred to by a symbolic link should exist.  A\nsymbolic link that refers to a pathname that does not exist is said to be a dangling link.\n\nBecause a symbolic link and its referenced object coexist in the filesystem name space,  con‐\nfusion  can  arise  in  distinguishing between the link itself and the referenced object.  On\nhistorical systems, commands and system calls adopted their own link-following conventions in\na  somewhat  ad-hoc  fashion.   Rules for a more uniform approach, as they are implemented on\nLinux and other systems, are outlined here.  It is  important  that  site-local  applications\nalso conform to these rules, so that the user interface can be as consistent as possible.\n",
            "subsections": [
                {
                    "name": "Magic links",
                    "content": "There  is  a special class of symbolic-link-like objects known as \"magic links\", which can be\nfound in certain pseudofilesystems such as  proc(5)  (examples  include  /proc/[pid]/exe  and\n/proc/[pid]/fd/*).   Unlike normal symbolic links, magic links are not resolved through path‐\nname-expansion, but instead act as direct references to the kernel's own representation of  a\nfile  handle.   As such, these magic links allow users to access files which cannot be refer‐\nenced with normal paths (such as unlinked files still referenced by a running program ).\n\nBecause they can bypass ordinary mountnamespaces(7)-based  restrictions,  magic  links  have\nbeen used as attack vectors in various exploits.\n"
                },
                {
                    "name": "Symbolic link ownership, permissions, and timestamps",
                    "content": "The  owner  and  group of an existing symbolic link can be changed using lchown(2).  The only\ntime that the ownership of a symbolic link matters is when the link is being removed  or  re‐\nnamed in a directory that has the sticky bit set (see stat(2)).\n\nThe last access and last modification timestamps of a symbolic link can be changed using uti‐‐\nmensat(2) or lutimes(3).\n\nOn Linux, the permissions of an ordinary symbolic link are not used in  any  operations;  the\npermissions  are always 0777 (read, write, and execute for all user categories), and can't be\nchanged.\n\nHowever, magic links do not follow this rule.  They can have a  non-0777  mode,  though  this\nmode is not currently used in any permission checks.\n\n"
                },
                {
                    "name": "Obtaining a file descriptor that refers to a symbolic link",
                    "content": "Using  the combination of the OPATH and ONOFOLLOW flags to open(2) yields a file descriptor\nthat can be passed as the dirfd argument in system calls  such  as  fstatat(2),  fchownat(2),\nfchmodat(2),  linkat(2),  and  readlinkat(2), in order to operate on the symbolic link itself\n(rather than the file to which it refers).\n\nBy default (i.e., if the ATSYMLINKFOLLOW flag is not specified), if nametohandleat(2) is\napplied to a symbolic link, it yields a handle for the symbolic link (rather than the file to\nwhich it refers).  One can then obtain a file descriptor for the symbolic link  (rather  than\nthe  file  to  which  it  refers)  by  specifying  the  OPATH  flag  in a subsequent call to\nopenbyhandleat(2).  Again, that file descriptor can be used in the  aforementioned  system\ncalls to operate on the symbolic link itself.\n"
                },
                {
                    "name": "Handling of symbolic links by system calls and commands",
                    "content": "Symbolic links are handled either by operating on the link itself, or by operating on the ob‐\nject referred to by the link.  In the latter case, an application or system call is  said  to\nfollow  the  link.  Symbolic links may refer to other symbolic links, in which case the links\nare dereferenced until an object that is not a symbolic link is found, a symbolic  link  that\nrefers  to  a  file which does not exist is found, or a loop is detected.  (Loop detection is\ndone by placing an upper limit on the number of links that may be followed, and an error  re‐\nsults if this limit is exceeded.)\n\nThere are three separate areas that need to be discussed.  They are as follows:\n\n1. Symbolic links used as filename arguments for system calls.\n\n2. Symbolic  links specified as command-line arguments to utilities that are not traversing a\nfile tree.\n\n3. Symbolic links encountered by utilities that are traversing a file tree (either  specified\non the command line or encountered as part of the file hierarchy walk).\n\nBefore  describing  the  treatment of symbolic links by system calls and commands, we require\nsome terminology.  Given a pathname of the form a/b/c, the part  preceding  the  final  slash\n(i.e., a/b) is called the dirname component, and the part following the final slash (i.e., c)\nis called the basename component.\n"
                },
                {
                    "name": "Treatment of symbolic links in system calls",
                    "content": "The first area is symbolic links used as filename arguments for system calls.\n\nThe treatment of symbolic links within a pathname passed to a system call is as follows:\n\n1. Within the dirname component of a pathname, symbolic links are always followed  in  nearly\nevery  system  call.   (This is also true for commands.)  The one exception is openat2(2),\nwhich provides flags that can be used to explicitly prevent following of symbolic links in\nthe dirname component.\n\n2. Except as noted below, all system calls follow symbolic links in the basename component of\na pathname.  For example, if there were a symbolic link slink  which  pointed  to  a  file\nnamed afile, the system call open(\"slink\" ...) would return a file descriptor referring to\nthe file afile.\n\nVarious system calls do not follow links in the basename component of a pathname, and operate\non  the  symbolic  link  itself.  They are: lchown(2), lgetxattr(2), llistxattr(2), lremovex‐‐\nattr(2), lsetxattr(2), lstat(2), readlink(2), rename(2), rmdir(2), and unlink(2).\n\nCertain other system calls optionally follow symbolic links in the basename  component  of  a\npathname.   They are: faccessat(2), fchownat(2), fstatat(2), linkat(2), nametohandleat(2),\nopen(2), openat(2), openbyhandleat(2), and utimensat(2); see their manual  pages  for  de‐\ntails.  Because remove(3) is an alias for unlink(2), that library function also does not fol‐\nlow symbolic links.  When rmdir(2) is applied to a symbolic link, it  fails  with  the  error\nENOTDIR.\n\nlink(2)  warrants special discussion.  POSIX.1-2001 specifies that link(2) should dereference\noldpath if it is a symbolic link.  However, Linux does not do this.  (By default, Solaris  is\nthe  same, but the POSIX.1-2001 specified behavior can be obtained with suitable compiler op‐\ntions.)  POSIX.1-2008 changed the specification to allow either behavior  in  an  implementa‐\ntion.\n"
                },
                {
                    "name": "Commands not traversing a file tree",
                    "content": "The  second area is symbolic links, specified as command-line filename arguments, to commands\nwhich are not traversing a file tree.\n\nExcept as noted below, commands follow symbolic links named as command-line  arguments.   For\nexample, if there were a symbolic link slink which pointed to a file named afile, the command\ncat slink would display the contents of the file afile.\n\nIt is important to realize that this rule includes commands  which  may  optionally  traverse\nfile  trees;  for example, the command chown file is included in this rule, while the command\nchown -R file, which performs a tree traversal, is not.  (The  latter  is  described  in  the\nthird area, below.)\n\nIf it is explicitly intended that the command operate on the symbolic link instead of follow‐\ning the symbolic link—for example, it is desired that chown slink change the ownership of the\nfile  that  slink is, whether it is a symbolic link or not—then the -h option should be used.\nIn the above example, chown root slink would change the ownership of the file referred to  by\nslink, while chown -h root slink would change the ownership of slink itself.\n\nThere are some exceptions to this rule:\n\n* The  mv(1)  and rm(1) commands do not follow symbolic links named as arguments, but respec‐\ntively attempt to rename and delete them.  (Note, if the symbolic link  references  a  file\nvia a relative path, moving it to another directory may very well cause it to stop working,\nsince the path may no longer be correct.)\n\n* The ls(1) command is also an exception to this rule.  For compatibility with historic  sys‐\ntems  (when  ls(1) is not doing a tree walk—that is, -R option is not specified), the ls(1)\ncommand follows symbolic links named as arguments if the -H or -L option is  specified,  or\nif  the  -F,  -d,  or -l options are not specified.  (The ls(1) command is the only command\nwhere the -H and -L options affect its behavior even though it is not doing  a  walk  of  a\nfile tree.)\n\n* The file(1) command is also an exception to this rule.  The file(1) command does not follow\nsymbolic links named as argument by default.  The  file(1)  command  does  follow  symbolic\nlinks named as argument if the -L option is specified.\n"
                },
                {
                    "name": "Commands traversing a file tree",
                    "content": "The  following  commands either optionally or always traverse file trees: chgrp(1), chmod(1),\nchown(1), cp(1), du(1), find(1), ls(1), pax(1), rm(1), and tar(1).\n\nIt is important to realize that the following rules apply equally to symbolic  links  encoun‐\ntered during the file tree traversal and symbolic links listed as command-line arguments.\n\nThe first rule applies to symbolic links that reference files other than directories.  Opera‐\ntions that apply to symbolic links are performed on the links themselves, but  otherwise  the\nlinks are ignored.\n\nThe  command  rm -r  slink directory will remove slink, as well as any symbolic links encoun‐\ntered in the tree traversal of directory, because symbolic links may be removed.  In no  case\nwill rm(1) affect the file referred to by slink.\n\nThe second rule applies to symbolic links that refer to directories.  Symbolic links that re‐\nfer to directories are never followed by default.  This is often referred to as a  \"physical\"\nwalk, as opposed to a \"logical\" walk (where symbolic links that refer to directories are fol‐\nlowed).\n\nCertain conventions are (should be) followed as consistently as  possible  by  commands  that\nperform file tree walks:\n\n* A command can be made to follow any symbolic links named on the command line, regardless of\nthe type of file they reference, by specifying the -H (for \"half-logical\") flag.  This flag\nis  intended  to make the command-line name space look like the logical name space.  (Note,\nfor commands that do not always do file tree traversals, the -H flag will be ignored if the\n-R flag is not also specified.)\n\nFor  example,  the  command chown -HR user slink will traverse the file hierarchy rooted in\nthe file pointed to by slink.  Note, the -H is not the same as the previously discussed  -h\nflag.   The  -H flag causes symbolic links specified on the command line to be dereferenced\nfor the purposes of both the action to be performed and the tree walk, and it is as if  the\nuser had specified the name of the file to which the symbolic link pointed.\n\n* A  command  can  be made to follow any symbolic links named on the command line, as well as\nany symbolic links encountered during the traversal, regardless of the type  of  file  they\nreference,  by  specifying  the -L (for \"logical\") flag.  This flag is intended to make the\nentire name space look like the logical name space.  (Note, for commands that do not always\ndo file tree traversals, the -L flag will be ignored if the -R flag is not also specified.)\n\nFor example, the command chown -LR user slink will change the owner of the file referred to\nby slink.  If slink refers to a directory, chown will traverse the file hierarchy rooted in\nthe  directory  that  it references.  In addition, if any symbolic links are encountered in\nany file tree that chown traverses, they will be treated in the same fashion as slink.\n\n* A command can be made to provide the default behavior by specifying the -P (for \"physical\")\nflag.   This  flag  is  intended  to make the entire name space look like the physical name\nspace.\n\nFor commands that do not by default do file tree traversals, the -H, -L, and -P flags are ig‐\nnored  if the -R flag is not also specified.  In addition, you may specify the -H, -L, and -P\noptions more than once; the last one specified determines the command's  behavior.   This  is\nintended  to  permit  you to alias commands to behave one way or the other, and then override\nthat behavior on the command line.\n\nThe ls(1) and rm(1) commands have exceptions to these rules:\n\n* The rm(1) command operates on the symbolic link, and not the file it references, and there‐\nfore  never  follows a symbolic link.  The rm(1) command does not support the -H, -L, or -P\noptions.\n\n* To maintain compatibility with historic systems, the ls(1) command acts  a  little  differ‐\nently.   If  you  do not specify the -F, -d or -l options, ls(1) will follow symbolic links\nspecified on the command line.  If the -L flag is specified,  ls(1)  follows  all  symbolic\nlinks,  regardless  of  their type, whether specified on the command line or encountered in\nthe tree walk.\n"
                }
            ]
        },
        "SEE ALSO": {
            "content": "chgrp(1), chmod(1), find(1),  ln(1),  ls(1),  mv(1),  namei(1),  rm(1),  lchown(2),  link(2),\nlstat(2), readlink(2), rename(2), symlink(2), unlink(2), utimensat(2), lutimes(3), pathreso‐‐\nlution(7)\n",
            "subsections": []
        },
        "COLOPHON": {
            "content": "This page is part of release 5.10 of the Linux  man-pages  project.   A  description  of  the\nproject,  information about reporting bugs, and the latest version of this page, can be found\nat https://www.kernel.org/doc/man-pages/.\n\n\n\nLinux                                        2020-06-09                                   SYMLINK(7)",
            "subsections": []
        }
    },
    "summary": "symlink - symbolic link handling",
    "flags": [],
    "examples": [],
    "see_also": [
        {
            "name": "chgrp",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/chgrp/1/json"
        },
        {
            "name": "chmod",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/chmod/1/json"
        },
        {
            "name": "find",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/find/1/json"
        },
        {
            "name": "ln",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/ln/1/json"
        },
        {
            "name": "ls",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/ls/1/json"
        },
        {
            "name": "mv",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/mv/1/json"
        },
        {
            "name": "namei",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/namei/1/json"
        },
        {
            "name": "rm",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/rm/1/json"
        },
        {
            "name": "lchown",
            "section": "2",
            "url": "https://www.chedong.com/phpMan.php/man/lchown/2/json"
        },
        {
            "name": "link",
            "section": "2",
            "url": "https://www.chedong.com/phpMan.php/man/link/2/json"
        },
        {
            "name": "lstat",
            "section": "2",
            "url": "https://www.chedong.com/phpMan.php/man/lstat/2/json"
        },
        {
            "name": "readlink",
            "section": "2",
            "url": "https://www.chedong.com/phpMan.php/man/readlink/2/json"
        },
        {
            "name": "rename",
            "section": "2",
            "url": "https://www.chedong.com/phpMan.php/man/rename/2/json"
        },
        {
            "name": "symlink",
            "section": "2",
            "url": "https://www.chedong.com/phpMan.php/man/symlink/2/json"
        },
        {
            "name": "unlink",
            "section": "2",
            "url": "https://www.chedong.com/phpMan.php/man/unlink/2/json"
        },
        {
            "name": "utimensat",
            "section": "2",
            "url": "https://www.chedong.com/phpMan.php/man/utimensat/2/json"
        },
        {
            "name": "lutimes",
            "section": "3",
            "url": "https://www.chedong.com/phpMan.php/man/lutimes/3/json"
        },
        {
            "name": "lution",
            "section": "7",
            "url": "https://www.chedong.com/phpMan.php/man/lution/7/json"
        }
    ]
}