]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/symlink.7
Many pages: Fix style issues reported by `make lint-groff`
[thirdparty/man-pages.git] / man7 / symlink.7
CommitLineData
eae3689b
MK
1.\" Copyright (c) 1992, 1993, 1994
2.\" The Regents of the University of California. All rights reserved.
828b6e3d 3.\" and Copyright (C) 2008, 2014 Michael Kerrisk <mtk.manpages@gmail.com>
eae3689b 4.\"
40fa0ff4 5.\" SPDX-License-Identifier: BSD-3-Clause
eae3689b
MK
6.\"
7.\" @(#)symlink.7 8.3 (Berkeley) 3/31/94
8.\" $FreeBSD: src/bin/ln/symlink.7,v 1.30 2005/02/13 22:25:09 ru Exp $
9.\"
10.\" 2008-06-11, mtk, Taken from FreeBSD 6.2 and heavily edited for
11.\" specific Linux details, improved readability, and man-pages style.
12.\"
1d767b55 13.TH SYMLINK 7 2021-03-22 "Linux" "Linux Programmer's Manual"
eae3689b
MK
14.SH NAME
15symlink \- symbolic link handling
7aa6eb89 16.SH DESCRIPTION
eae3689b
MK
17Symbolic links are files that act as pointers to other files.
18To understand their behavior, you must first understand how hard links
19work.
a721e8b2 20.PP
eae3689b
MK
21A hard link to a file is indistinguishable from the original file because
22it is a reference to the object underlying the original filename.
7eb11a9f
MK
23(To be precise: each of the hard links to a file is a reference to
24the same
e7e006f2
MK
25.IR "inode number" ,
26where an inode number is an index into the inode table,
9ee4a2b6 27which contains metadata about all files on a filesystem.
99d8a684 28See
7eb11a9f 29.BR stat (2).)
eae3689b
MK
30Changes to a file are independent of the name used to reference the file.
31Hard links may not refer to directories
9ee4a2b6 32(to prevent the possibility of loops within the filesystem tree,
eae3689b 33which would confuse many programs)
9ee4a2b6 34and may not refer to files on different filesystems
e7e006f2 35(because inode numbers are not unique across filesystems).
a721e8b2 36.PP
eae3689b 37A symbolic link is a special type of file whose contents are a string
39c56b84 38that is the pathname of another file, the file to which the link refers.
ed171400
MK
39(The contents of a symbolic link can be read using
40.BR readlink (2).)
eae3689b
MK
41In other words, a symbolic link is a pointer to another name,
42and not to an underlying object.
43For this reason, symbolic links may refer to directories and may cross
9ee4a2b6 44filesystem boundaries.
a721e8b2 45.PP
eae3689b
MK
46There is no requirement that the pathname referred to by a symbolic link
47should exist.
48A symbolic link that refers to a pathname that does not exist is said
7eb11a9f
MK
49to be a
50.IR "dangling link" .
a721e8b2 51.PP
9ee4a2b6 52Because a symbolic link and its referenced object coexist in the filesystem
eae3689b
MK
53name space, confusion can arise in distinguishing between the link itself
54and the referenced object.
55On historical systems,
7eb11a9f
MK
56commands and system calls adopted their own link-following
57conventions in a somewhat ad-hoc fashion.
eae3689b
MK
58Rules for a more uniform approach,
59as they are implemented on Linux and other systems,
60are outlined here.
61It is important that site-local applications also conform to these rules,
62so that the user interface can be as consistent as possible.
04ce17bd
MK
63.\"
64.SS Magic links
65There is a special class of symbolic-link-like objects
66known as "magic links", which
67can be found in certain pseudofilesystems such as
41e24055
AS
68.BR proc (5)
69(examples include
04ce17bd
MK
70.IR /proc/[pid]/exe " and " /proc/[pid]/fd/* ).
71Unlike normal symbolic links, magic links are not resolved through
41e24055 72pathname-expansion, but instead act as direct references to the kernel's own
04ce17bd
MK
73representation of a file handle.
74As such, these magic links allow users to
41e24055 75access files which cannot be referenced with normal paths (such as unlinked
04ce17bd 76files still referenced by a running program ).
41e24055
AS
77.PP
78Because they can bypass ordinary
79.BR mount_namespaces (7)-based
04ce17bd
MK
80restrictions,
81magic links have been used as attack vectors in various exploits.
82.\"
eae3689b
MK
83.SS Symbolic link ownership, permissions, and timestamps
84The owner and group of an existing symbolic link can be changed
85using
86.BR lchown (2).
87The only time that the ownership of a symbolic link matters is
88when the link is being removed or renamed in a directory that
89has the sticky bit set (see
90.BR stat (2)).
a721e8b2 91.PP
eae3689b
MK
92The last access and last modification timestamps
93of a symbolic link can be changed using
94.BR utimensat (2)
95or
96.BR lutimes (3).
a721e8b2 97.PP
eae3689b 98.\" Linux does not currently implement an lchmod(2).
41e24055
AS
99On Linux, the permissions of an ordinary symbolic link are not used in any
100operations; the permissions are always 0777 (read, write, and execute for all
101user categories), and can't be changed.
102.PP
04ce17bd
MK
103However, magic links do not follow this rule.
104They can have a non-0777 mode,
41e24055 105though this mode is not currently used in any permission checks.
eaa18d3c 106.\" .PP
eae3689b
MK
107.\" The
108.\" 4.4BSD
109.\" system differs from historical
110.\" 4BSD
111.\" systems in that the system call
112.\" .BR chown (2)
113.\" has been changed to follow symbolic links.
114.\" The
115.\" .BR lchown (2)
116.\" system call was added later when the limitations of the new
117.\" .BR chown (2)
118.\" became apparent.
d33b6ed2
MK
119.SS Obtaining a file descriptor that refers to a symbolic link
120Using the combination of the
121.B O_PATH
122and
1ae6b2c7 123.B O_NOFOLLOW
d33b6ed2
MK
124flags to
125.BR open (2)
126yields a file descriptor that can be passed as the
1ae6b2c7 127.I dirfd
d33b6ed2
MK
128argument in system calls such as
129.BR fstatat (2),
130.BR fchownat (2),
131.BR fchmodat (2),
132.BR linkat (2),
133and
134.BR readlinkat (2),
135in order to operate on the symbolic link itself
136(rather than the file to which it refers).
a721e8b2 137.PP
d33b6ed2
MK
138By default
139(i.e., if the
1ae6b2c7 140.B AT_SYMLINK_FOLLOW
d33b6ed2
MK
141flag is not specified), if
142.BR name_to_handle_at (2)
143is applied to a symbolic link, it yields a handle for the symbolic link
144(rather than the file to which it refers).
145One can then obtain a file descriptor for the symbolic link
146(rather than the file to which it refers)
147by specifying the
148.B O_PATH
58be64a4 149flag in a subsequent call to
d33b6ed2
MK
150.BR open_by_handle_at (2).
151Again, that file descriptor can be used in the
152aforementioned system calls to operate on the symbolic link itself.
eae3689b
MK
153.SS Handling of symbolic links by system calls and commands
154Symbolic links are handled either by operating on the link itself,
155or by operating on the object referred to by the link.
156In the latter case,
7eb11a9f
MK
157an application or system call is said to
158.I follow
159the link.
eae3689b
MK
160Symbolic links may refer to other symbolic links,
161in which case the links are dereferenced until an object that is
162not a symbolic link is found,
163a symbolic link that refers to a file which does not exist is found,
164or a loop is detected.
165(Loop detection is done by placing an upper limit on the number of
166links that may be followed, and an error results if this limit is
167exceeded.)
a721e8b2 168.PP
eae3689b
MK
169There are three separate areas that need to be discussed.
170They are as follows:
171.IP 1. 3
172Symbolic links used as filename arguments for system calls.
173.IP 2.
174Symbolic links specified as command-line arguments to utilities that
175are not traversing a file tree.
176.IP 3.
177Symbolic links encountered by utilities that are traversing a file tree
178(either specified on the command line or encountered as part of the
179file hierarchy walk).
7200e796
MK
180.PP
181Before describing the treatment of symbolic links by system calls and commands,
182we require some terminology.
183Given a pathname of the form
184.IR a/b/c ,
185the part preceding the final slash (i.e.,
186.IR a/b )
187is called the
188.I dirname
189component, and the part following the final slash (i.e.,
190.IR c )
191is called the
1ae6b2c7 192.I basename
7200e796
MK
193component.
194.\"
195.SS Treatment of symbolic links in system calls
eae3689b
MK
196The first area is symbolic links used as filename arguments for
197system calls.
a721e8b2 198.PP
7200e796
MK
199The treatment of symbolic links within a pathname passed to
200a system call is as follows:
201.IP 1. 3
202Within the dirname component of a pathname,
203symbolic links are always followed in nearly every system call.
204(This is also true for commands.)
205The one exception is
206.BR openat2 (2),
207which provides flags that can be used to explicitly
208prevent following of symbolic links in the dirname component.
209.IP 2.
210Except as noted below,
211all system calls follow symbolic links
212in the basename component of a pathname.
eae3689b 213For example, if there were a symbolic link
7eb11a9f 214.I slink
eae3689b 215which pointed to a file named
7eb11a9f 216.IR afile ,
eae3689b 217the system call
e6e731d1 218.I "open(""slink"" ...\&)"
eae3689b 219would return a file descriptor referring to the file
7eb11a9f 220.IR afile .
a721e8b2 221.PP
7200e796
MK
222Various system calls do not follow links in
223the basename component of a pathname,
224and operate on the symbolic link itself.
eae3689b
MK
225They are:
226.BR lchown (2),
227.BR lgetxattr (2),
228.BR llistxattr (2),
229.BR lremovexattr (2),
230.BR lsetxattr (2),
231.BR lstat (2),
eae3689b
MK
232.BR readlink (2),
233.BR rename (2),
234.BR rmdir (2),
235and
236.BR unlink (2).
a721e8b2 237.PP
7200e796
MK
238Certain other system calls optionally follow symbolic links
239in the basename component of a pathname.
eae3689b
MK
240They are:
241.BR faccessat (2),
242.\" Maybe one day: .BR fchownat (2)
243.BR fchownat (2),
244.BR fstatat (2),
245.BR linkat (2),
df63d3e8 246.BR name_to_handle_at (2),
eae3689b
MK
247.BR open (2),
248.BR openat (2),
df63d3e8 249.BR open_by_handle_at (2),
eae3689b
MK
250and
251.BR utimensat (2);
252see their manual pages for details.
253Because
254.BR remove (3)
255is an alias for
256.BR unlink (2),
257that library function also does not follow symbolic links.
258When
259.BR rmdir (2)
260is applied to a symbolic link, it fails with the error
261.BR ENOTDIR .
a721e8b2 262.PP
eae3689b
MK
263.BR link (2)
264warrants special discussion.
265POSIX.1-2001 specifies that
266.BR link (2)
267should dereference
268.I oldpath
269if it is a symbolic link.
270However, Linux does not do this.
a902a39f 271(By default, Solaris is the same,
eae3689b
MK
272but the POSIX.1-2001 specified behavior can be obtained with
273suitable compiler options.)
cad7ef53 274POSIX.1-2008 changed the specification to allow
eae3689b
MK
275either behavior in an implementation.
276.SS Commands not traversing a file tree
277The second area is symbolic links, specified as command-line
278filename arguments, to commands which are not traversing a file tree.
a721e8b2 279.PP
eae3689b
MK
280Except as noted below, commands follow symbolic links named as
281command-line arguments.
282For example, if there were a symbolic link
7eb11a9f 283.I slink
eae3689b 284which pointed to a file named
7eb11a9f 285.IR afile ,
eae3689b
MK
286the command
287.I "cat slink"
288would display the contents of the file
7eb11a9f 289.IR afile .
a721e8b2 290.PP
eae3689b 291It is important to realize that this rule includes commands which may
10850212 292optionally traverse file trees; for example, the command
eae3689b
MK
293.I "chown file"
294is included in this rule, while the command
2f6b8245 295.IR "chown\ \-R file" ,
eae3689b
MK
296which performs a tree traversal, is not.
297(The latter is described in the third area, below.)
a721e8b2 298.PP
eae3689b 299If it is explicitly intended that the command operate on the symbolic
10850212 300link instead of following the symbolic link\(emfor example, it is desired that
eae3689b
MK
301.I "chown slink"
302change the ownership of the file that
7eb11a9f 303.I slink
9660f3ba 304is, whether it is a symbolic link or not\(emthen the
90850708 305.I \-h
eae3689b
MK
306option should be used.
307In the above example,
308.I "chown root slink"
309would change the ownership of the file referred to by
7eb11a9f 310.IR slink ,
eae3689b 311while
2f6b8245 312.I "chown\ \-h root slink"
eae3689b 313would change the ownership of
7eb11a9f 314.I slink
eae3689b 315itself.
a721e8b2 316.PP
eae3689b
MK
317There are some exceptions to this rule:
318.IP * 2
319The
320.BR mv (1)
321and
322.BR rm (1)
323commands do not follow symbolic links named as arguments,
324but respectively attempt to rename and delete them.
325(Note, if the symbolic link references a file via a relative path,
326moving it to another directory may very well cause it to stop working,
327since the path may no longer be correct.)
328.IP *
329The
330.BR ls (1)
331command is also an exception to this rule.
332For compatibility with historic systems (when
333.BR ls (1)
88879aeb 334is not doing a tree walk\(emthat is,
90850708 335.I \-R
eae3689b
MK
336option is not specified),
337the
338.BR ls (1)
339command follows symbolic links named as arguments if the
90850708 340.I \-H
eae3689b 341or
90850708 342.I \-L
eae3689b
MK
343option is specified,
344or if the
90850708
MK
345.IR \-F ,
346.IR \-d ,
eae3689b 347or
90850708 348.I \-l
eae3689b
MK
349options are not specified.
350(The
351.BR ls (1)
352command is the only command where the
90850708 353.I \-H
eae3689b 354and
90850708 355.I \-L
eae3689b
MK
356options affect its behavior even though it is not doing a walk of
357a file tree.)
358.IP *
359The
360.BR file (1)
361command is also an exception to this rule.
362The
363.BR file (1)
364command does not follow symbolic links named as argument by default.
365The
366.BR file (1)
367command does follow symbolic links named as argument if the
90850708 368.I \-L
eae3689b
MK
369option is specified.
370.\"
371.\"The 4.4BSD system differs from historical 4BSD systems in that the
372.\".BR chown (1)
373.\"and
374.\".BR chgrp (1)
375.\"commands follow symbolic links specified on the command line.
376.SS Commands traversing a file tree
377The following commands either optionally or always traverse file trees:
378.BR chgrp (1),
379.BR chmod (1),
380.BR chown (1),
381.BR cp (1),
382.BR du (1),
383.BR find (1),
384.BR ls (1),
385.BR pax (1),
386.BR rm (1),
387and
388.BR tar (1).
a721e8b2 389.PP
eae3689b
MK
390It is important to realize that the following rules apply equally to
391symbolic links encountered during the file tree traversal and symbolic
392links listed as command-line arguments.
a721e8b2 393.PP
eae3689b
MK
394The \fIfirst rule\fP applies to symbolic links that reference files other
395than directories.
396Operations that apply to symbolic links are performed on the links
397themselves, but otherwise the links are ignored.
a721e8b2 398.PP
eae3689b 399The command
2f6b8245 400.I "rm\ \-r slink directory"
eae3689b 401will remove
7eb11a9f 402.IR slink ,
eae3689b 403as well as any symbolic links encountered in the tree traversal of
7eb11a9f 404.IR directory ,
eae3689b
MK
405because symbolic links may be removed.
406In no case will
407.BR rm (1)
408affect the file referred to by
7eb11a9f 409.IR slink .
a721e8b2 410.PP
eae3689b
MK
411The \fIsecond rule\fP applies to symbolic links that refer to directories.
412Symbolic links that refer to directories are never followed by default.
413This is often referred to as a "physical" walk, as opposed to a "logical"
d5a31e0c 414walk (where symbolic links that refer to directories are followed).
a721e8b2 415.PP
eae3689b
MK
416Certain conventions are (should be) followed as consistently as
417possible by commands that perform file tree walks:
418.IP * 2
a1d5601b 419A command can be made to follow
eae3689b
MK
420any symbolic links named on the command line,
421regardless of the type of file they reference, by specifying the
90850708
MK
422.I \-H
423(for "half-logical") flag.
eae3689b
MK
424This flag is intended to make the command-line name space look
425like the logical name space.
426(Note, for commands that do not always do file tree traversals, the
90850708 427.I \-H
eae3689b 428flag will be ignored if the
90850708 429.I \-R
eae3689b 430flag is not also specified.)
a721e8b2 431.IP
eae3689b 432For example, the command
2f6b8245 433.I "chown\ \-HR user slink"
eae3689b 434will traverse the file hierarchy rooted in the file pointed to by
7eb11a9f 435.IR slink .
eae3689b 436Note, the
90850708 437.I \-H
eae3689b 438is not the same as the previously discussed
90850708 439.I \-h
eae3689b
MK
440flag.
441The
90850708 442.I \-H
eae3689b
MK
443flag causes symbolic links specified on the command line to be
444dereferenced for the purposes of both the action to be performed
445and the tree walk, and it is as if the user had specified the
446name of the file to which the symbolic link pointed.
447.IP *
448A command can be made to
449follow any symbolic links named on the command line,
450as well as any symbolic links encountered during the traversal,
451regardless of the type of file they reference, by specifying the
90850708 452.I \-L
eae3689b
MK
453(for "logical") flag.
454This flag is intended to make the entire name space look like
455the logical name space.
456(Note, for commands that do not always do file tree traversals, the
90850708 457.I \-L
eae3689b 458flag will be ignored if the
90850708 459.I \-R
eae3689b 460flag is not also specified.)
a721e8b2 461.IP
eae3689b 462For example, the command
2f6b8245 463.I "chown\ \-LR user slink"
7eb11a9f
MK
464will change the owner of the file referred to by
465.IR slink .
466If
467.I slink
468refers to a directory,
469.B chown
470will traverse the file hierarchy rooted in the directory that it
eae3689b
MK
471references.
472In addition, if any symbolic links are encountered in any file tree that
7eb11a9f
MK
473.B chown
474traverses, they will be treated in the same fashion as
475.IR slink .
eae3689b
MK
476.IP *
477A command can be made to
478provide the default behavior by specifying the
90850708 479.I \-P
7eb11a9f 480(for "physical") flag.
eae3689b
MK
481This flag is intended to make the entire name space look like the
482physical name space.
483.PP
484For commands that do not by default do file tree traversals, the
90850708
MK
485.IR \-H ,
486.IR \-L ,
eae3689b 487and
90850708 488.I \-P
eae3689b 489flags are ignored if the
90850708 490.I \-R
eae3689b
MK
491flag is not also specified.
492In addition, you may specify the
90850708
MK
493.IR \-H ,
494.IR \-L ,
eae3689b 495and
90850708 496.I \-P
7eb11a9f
MK
497options more than once;
498the last one specified determines the command's behavior.
eae3689b
MK
499This is intended to permit you to alias commands to behave one way
500or the other, and then override that behavior on the command line.
a721e8b2 501.PP
eae3689b
MK
502The
503.BR ls (1)
504and
505.BR rm (1)
506commands have exceptions to these rules:
507.IP * 2
508The
509.BR rm (1)
510command operates on the symbolic link, and not the file it references,
511and therefore never follows a symbolic link.
512The
513.BR rm (1)
514command does not support the
90850708
MK
515.IR \-H ,
516.IR \-L ,
eae3689b 517or
90850708 518.I \-P
eae3689b
MK
519options.
520.IP *
521To maintain compatibility with historic systems,
522the
523.BR ls (1)
524command acts a little differently.
525If you do not specify the
90850708 526.IR \-F ,
aa0da2b9 527.IR \-d ,
eae3689b 528or
90850708 529.I \-l
eae3689b
MK
530options,
531.BR ls (1)
532will follow symbolic links specified on the command line.
533If the
90850708 534.I \-L
eae3689b
MK
535flag is specified,
536.BR ls (1)
537follows all symbolic links,
538regardless of their type,
539whether specified on the command line or encountered in the tree walk.
540.SH SEE ALSO
eae3689b
MK
541.BR chgrp (1),
542.BR chmod (1),
543.BR find (1),
544.BR ln (1),
545.BR ls (1),
546.BR mv (1),
2f3db2a5 547.BR namei (1),
eae3689b
MK
548.BR rm (1),
549.BR lchown (2),
550.BR link (2),
551.BR lstat (2),
552.BR readlink (2),
553.BR rename (2),
554.BR symlink (2),
555.BR unlink (2),
556.BR utimensat (2),
7eb11a9f
MK
557.BR lutimes (3),
558.BR path_resolution (7)