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