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