baraboom's and peth's answers are both correct: Permission bits on the symbolic links themselves are irrelevant (except on macOS; see below), and changing permission on a symbolic link – by the chmod
command-line tool or by the chmod()
system call – will simply act as if it was performed against the target of the symbolic link.
To quote the SUSv4/POSIX.1-2008 description of symlink() system call:
The values of the file mode bits for the created symbolic link are unspecified. All interfaces specified by POSIX.1-2008 shall behave as if the contents of symbolic links can always be read, except that the value of the file mode bits returned in the st_mode field of the stat structure is unspecified.
Here, “unspecified” leaves room of interpretation for each implementation. Specifics:
- On Linux (tested using ext4fs),
stat()
returns st_mode=0777
, no matter what the umask was when the symlink was created; ls -l
therefore always displays lrwxrwxrwx
for symbolic links.
- On macOS (HFS) and FreeBSD (both UFS and ZFS), a symbolic link does have its own permission: The
chmod -h
command noted above can change this link permission (which internally uses a non-POSIX lchown()
system call to achieve this), and the stat()
system call returns this value for st_mode
.
Symbolic links on Linux and FreeBSD can always be followed, as specified by POSIX. In particular, on FreeBSD, this means that the file mode of a symbolic link has no effect at all on access control.
On the other hand, macOS slightly breaks POSIX. Although a symbolic link can be followed regardless its read permission, readlink()
fails with EACCES
(Permission denied) if the user does not have read permission:
$ sudo ln -shf target symlink
$ sudo chmod -h 444 symlink
$ ls -l symlink
lr--r--r-- 1 root staff 1 Mar 14 13:05 symlink -> target
$ sudo chmod -h 000 symlink
$ ls -l symlink
ls: symlink: Permission denied
l--------- 1 root staff 1 Mar 14 13:05 symlink
$ echo kthxbye > target
$ cat symlink
kthxbye
(Note that the -> target
portion is missing in the output from the second ls -l
command, and that cat symlink
still succeeded and printed the contents of the target
file even though the user did not have read permission on symlink
.)
NetBSD apparently offers a special mount option named symperm
which, if set, causes symbolic link read/execute permissions to control readlink()
and link traversal.
Permissions only affect the file, not the symlink. – baraboom – 2011-06-27T21:12:00.767