]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man7/symlink.7
All pages: Remove the 5th argument to .TH
[thirdparty/man-pages.git] / man7 / symlink.7
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 2021-03-22 "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.
20 .PP
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).
36 .PP
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.
45 .PP
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" .
51 .PP
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 " and " /proc/[pid]/fd/* ).
71 Unlike normal symbolic links, magic links are not resolved through
72 pathname-expansion, but instead act as direct references to the kernel's own
73 representation of a file handle.
74 As such, these magic links allow users to
75 access files which cannot be referenced with normal paths (such as unlinked
76 files still referenced by a running program ).
77 .PP
78 Because they can bypass ordinary
79 .BR mount_namespaces (7)-based
80 restrictions,
81 magic links have been used as attack vectors in various exploits.
82 .\"
83 .SS Symbolic link ownership, permissions, and timestamps
84 The owner and group of an existing symbolic link can be changed
85 using
86 .BR lchown (2).
87 The only time that the ownership of a symbolic link matters is
88 when the link is being removed or renamed in a directory that
89 has the sticky bit set (see
90 .BR stat (2)).
91 .PP
92 The last access and last modification timestamps
93 of a symbolic link can be changed using
94 .BR utimensat (2)
95 or
96 .BR lutimes (3).
97 .PP
98 .\" Linux does not currently implement an lchmod(2).
99 On Linux, the permissions of an ordinary symbolic link are not used in any
100 operations; the permissions are always 0777 (read, write, and execute for all
101 user categories), and can't be changed.
102 .PP
103 However, magic links do not follow this rule.
104 They can have a non-0777 mode,
105 though this mode is not currently used in any permission checks.
106 .\" .PP
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.
119 .SS Obtaining a file descriptor that refers to a symbolic link
120 Using the combination of the
121 .B O_PATH
122 and
123 .B O_NOFOLLOW
124 flags to
125 .BR open (2)
126 yields a file descriptor that can be passed as the
127 .I dirfd
128 argument in system calls such as
129 .BR fstatat (2),
130 .BR fchownat (2),
131 .BR fchmodat (2),
132 .BR linkat (2),
133 and
134 .BR readlinkat (2),
135 in order to operate on the symbolic link itself
136 (rather than the file to which it refers).
137 .PP
138 By default
139 (i.e., if the
140 .B AT_SYMLINK_FOLLOW
141 flag is not specified), if
142 .BR name_to_handle_at (2)
143 is applied to a symbolic link, it yields a handle for the symbolic link
144 (rather than the file to which it refers).
145 One can then obtain a file descriptor for the symbolic link
146 (rather than the file to which it refers)
147 by specifying the
148 .B O_PATH
149 flag in a subsequent call to
150 .BR open_by_handle_at (2).
151 Again, that file descriptor can be used in the
152 aforementioned system calls to operate on the symbolic link itself.
153 .SS Handling of symbolic links by system calls and commands
154 Symbolic links are handled either by operating on the link itself,
155 or by operating on the object referred to by the link.
156 In the latter case,
157 an application or system call is said to
158 .I follow
159 the link.
160 Symbolic links may refer to other symbolic links,
161 in which case the links are dereferenced until an object that is
162 not a symbolic link is found,
163 a symbolic link that refers to a file which does not exist is found,
164 or a loop is detected.
165 (Loop detection is done by placing an upper limit on the number of
166 links that may be followed, and an error results if this limit is
167 exceeded.)
168 .PP
169 There are three separate areas that need to be discussed.
170 They are as follows:
171 .IP 1. 3
172 Symbolic links used as filename arguments for system calls.
173 .IP 2.
174 Symbolic links specified as command-line arguments to utilities that
175 are not traversing a file tree.
176 .IP 3.
177 Symbolic links encountered by utilities that are traversing a file tree
178 (either specified on the command line or encountered as part of the
179 file hierarchy walk).
180 .PP
181 Before describing the treatment of symbolic links by system calls and commands,
182 we require some terminology.
183 Given a pathname of the form
184 .IR a/b/c ,
185 the part preceding the final slash (i.e.,
186 .IR a/b )
187 is called the
188 .I dirname
189 component, and the part following the final slash (i.e.,
190 .IR c )
191 is called the
192 .I basename
193 component.
194 .\"
195 .SS Treatment of symbolic links in system calls
196 The first area is symbolic links used as filename arguments for
197 system calls.
198 .PP
199 The treatment of symbolic links within a pathname passed to
200 a system call is as follows:
201 .IP 1. 3
202 Within the dirname component of a pathname,
203 symbolic links are always followed in nearly every system call.
204 (This is also true for commands.)
205 The one exception is
206 .BR openat2 (2),
207 which provides flags that can be used to explicitly
208 prevent following of symbolic links in the dirname component.
209 .IP 2.
210 Except as noted below,
211 all system calls follow symbolic links
212 in the basename component of a pathname.
213 For example, if there were a symbolic link
214 .I slink
215 which pointed to a file named
216 .IR afile ,
217 the system call
218 .I "open(""slink"" ...\&)"
219 would return a file descriptor referring to the file
220 .IR afile .
221 .PP
222 Various system calls do not follow links in
223 the basename component of a pathname,
224 and operate on the symbolic link itself.
225 They 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),
232 .BR readlink (2),
233 .BR rename (2),
234 .BR rmdir (2),
235 and
236 .BR unlink (2).
237 .PP
238 Certain other system calls optionally follow symbolic links
239 in the basename component of a pathname.
240 They are:
241 .BR faccessat (2),
242 .\" Maybe one day: .BR fchownat (2)
243 .BR fchownat (2),
244 .BR fstatat (2),
245 .BR linkat (2),
246 .BR name_to_handle_at (2),
247 .BR open (2),
248 .BR openat (2),
249 .BR open_by_handle_at (2),
250 and
251 .BR utimensat (2);
252 see their manual pages for details.
253 Because
254 .BR remove (3)
255 is an alias for
256 .BR unlink (2),
257 that library function also does not follow symbolic links.
258 When
259 .BR rmdir (2)
260 is applied to a symbolic link, it fails with the error
261 .BR ENOTDIR .
262 .PP
263 .BR link (2)
264 warrants special discussion.
265 POSIX.1-2001 specifies that
266 .BR link (2)
267 should dereference
268 .I oldpath
269 if it is a symbolic link.
270 However, Linux does not do this.
271 (By default, Solaris is the same,
272 but the POSIX.1-2001 specified behavior can be obtained with
273 suitable compiler options.)
274 POSIX.1-2008 changed the specification to allow
275 either behavior in an implementation.
276 .SS Commands not traversing a file tree
277 The second area is symbolic links, specified as command-line
278 filename arguments, to commands which are not traversing a file tree.
279 .PP
280 Except as noted below, commands follow symbolic links named as
281 command-line arguments.
282 For example, if there were a symbolic link
283 .I slink
284 which pointed to a file named
285 .IR afile ,
286 the command
287 .I "cat slink"
288 would display the contents of the file
289 .IR afile .
290 .PP
291 It is important to realize that this rule includes commands which may
292 optionally traverse file trees; for example, the command
293 .I "chown file"
294 is included in this rule, while the command
295 .IR "chown\ \-R file" ,
296 which performs a tree traversal, is not.
297 (The latter is described in the third area, below.)
298 .PP
299 If it is explicitly intended that the command operate on the symbolic
300 link instead of following the symbolic link\(emfor example, it is desired that
301 .I "chown slink"
302 change the ownership of the file that
303 .I slink
304 is, whether it is a symbolic link or not\(emthen the
305 .I \-h
306 option should be used.
307 In the above example,
308 .I "chown root slink"
309 would change the ownership of the file referred to by
310 .IR slink ,
311 while
312 .I "chown\ \-h root slink"
313 would change the ownership of
314 .I slink
315 itself.
316 .PP
317 There are some exceptions to this rule:
318 .IP * 2
319 The
320 .BR mv (1)
321 and
322 .BR rm (1)
323 commands do not follow symbolic links named as arguments,
324 but respectively attempt to rename and delete them.
325 (Note, if the symbolic link references a file via a relative path,
326 moving it to another directory may very well cause it to stop working,
327 since the path may no longer be correct.)
328 .IP *
329 The
330 .BR ls (1)
331 command is also an exception to this rule.
332 For compatibility with historic systems (when
333 .BR ls (1)
334 is not doing a tree walk\(emthat is,
335 .I \-R
336 option is not specified),
337 the
338 .BR ls (1)
339 command follows symbolic links named as arguments if the
340 .I \-H
341 or
342 .I \-L
343 option is specified,
344 or if the
345 .IR \-F ,
346 .IR \-d ,
347 or
348 .I \-l
349 options are not specified.
350 (The
351 .BR ls (1)
352 command is the only command where the
353 .I \-H
354 and
355 .I \-L
356 options affect its behavior even though it is not doing a walk of
357 a file tree.)
358 .IP *
359 The
360 .BR file (1)
361 command is also an exception to this rule.
362 The
363 .BR file (1)
364 command does not follow symbolic links named as argument by default.
365 The
366 .BR file (1)
367 command does follow symbolic links named as argument if the
368 .I \-L
369 option 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
377 The 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),
387 and
388 .BR tar (1).
389 .PP
390 It is important to realize that the following rules apply equally to
391 symbolic links encountered during the file tree traversal and symbolic
392 links listed as command-line arguments.
393 .PP
394 The \fIfirst rule\fP applies to symbolic links that reference files other
395 than directories.
396 Operations that apply to symbolic links are performed on the links
397 themselves, but otherwise the links are ignored.
398 .PP
399 The command
400 .I "rm\ \-r slink directory"
401 will remove
402 .IR slink ,
403 as well as any symbolic links encountered in the tree traversal of
404 .IR directory ,
405 because symbolic links may be removed.
406 In no case will
407 .BR rm (1)
408 affect the file referred to by
409 .IR slink .
410 .PP
411 The \fIsecond rule\fP applies to symbolic links that refer to directories.
412 Symbolic links that refer to directories are never followed by default.
413 This is often referred to as a "physical" walk, as opposed to a "logical"
414 walk (where symbolic links that refer to directories are followed).
415 .PP
416 Certain conventions are (should be) followed as consistently as
417 possible by commands that perform file tree walks:
418 .IP * 2
419 A command can be made to follow
420 any symbolic links named on the command line,
421 regardless of the type of file they reference, by specifying the
422 .I \-H
423 (for "half-logical") flag.
424 This flag is intended to make the command-line name space look
425 like the logical name space.
426 (Note, for commands that do not always do file tree traversals, the
427 .I \-H
428 flag will be ignored if the
429 .I \-R
430 flag is not also specified.)
431 .IP
432 For example, the command
433 .I "chown\ \-HR user slink"
434 will traverse the file hierarchy rooted in the file pointed to by
435 .IR slink .
436 Note, the
437 .I \-H
438 is not the same as the previously discussed
439 .I \-h
440 flag.
441 The
442 .I \-H
443 flag causes symbolic links specified on the command line to be
444 dereferenced for the purposes of both the action to be performed
445 and the tree walk, and it is as if the user had specified the
446 name of the file to which the symbolic link pointed.
447 .IP *
448 A command can be made to
449 follow any symbolic links named on the command line,
450 as well as any symbolic links encountered during the traversal,
451 regardless of the type of file they reference, by specifying the
452 .I \-L
453 (for "logical") flag.
454 This flag is intended to make the entire name space look like
455 the logical name space.
456 (Note, for commands that do not always do file tree traversals, the
457 .I \-L
458 flag will be ignored if the
459 .I \-R
460 flag is not also specified.)
461 .IP
462 For example, the command
463 .I "chown\ \-LR user slink"
464 will change the owner of the file referred to by
465 .IR slink .
466 If
467 .I slink
468 refers to a directory,
469 .B chown
470 will traverse the file hierarchy rooted in the directory that it
471 references.
472 In addition, if any symbolic links are encountered in any file tree that
473 .B chown
474 traverses, they will be treated in the same fashion as
475 .IR slink .
476 .IP *
477 A command can be made to
478 provide the default behavior by specifying the
479 .I \-P
480 (for "physical") flag.
481 This flag is intended to make the entire name space look like the
482 physical name space.
483 .PP
484 For commands that do not by default do file tree traversals, the
485 .IR \-H ,
486 .IR \-L ,
487 and
488 .I \-P
489 flags are ignored if the
490 .I \-R
491 flag is not also specified.
492 In addition, you may specify the
493 .IR \-H ,
494 .IR \-L ,
495 and
496 .I \-P
497 options more than once;
498 the last one specified determines the command's behavior.
499 This is intended to permit you to alias commands to behave one way
500 or the other, and then override that behavior on the command line.
501 .PP
502 The
503 .BR ls (1)
504 and
505 .BR rm (1)
506 commands have exceptions to these rules:
507 .IP * 2
508 The
509 .BR rm (1)
510 command operates on the symbolic link, and not the file it references,
511 and therefore never follows a symbolic link.
512 The
513 .BR rm (1)
514 command does not support the
515 .IR \-H ,
516 .IR \-L ,
517 or
518 .I \-P
519 options.
520 .IP *
521 To maintain compatibility with historic systems,
522 the
523 .BR ls (1)
524 command acts a little differently.
525 If you do not specify the
526 .IR \-F ,
527 .IR \-d ,
528 or
529 .I \-l
530 options,
531 .BR ls (1)
532 will follow symbolic links specified on the command line.
533 If the
534 .I \-L
535 flag is specified,
536 .BR ls (1)
537 follows all symbolic links,
538 regardless of their type,
539 whether specified on the command line or encountered in the tree walk.
540 .SH SEE ALSO
541 .BR chgrp (1),
542 .BR chmod (1),
543 .BR find (1),
544 .BR ln (1),
545 .BR ls (1),
546 .BR mv (1),
547 .BR namei (1),
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),
557 .BR lutimes (3),
558 .BR path_resolution (7)