13

In OSX I can set the permissions of the symlink itself (instead of the thing it points to using the -h). From the man page:

 -h      If the file is a symbolic link, change the mode of the link itself rather than the file that the link points to.

In Ubuntu 14.04 I try to set permissions of a symlink, but it's only set on the symlink target.

It's regarding a symlink from /home/nagios/.ssh/someprivatekey to /somewhere/else/privatekey, so permissions are important for ssh. How can I achieve this?

4
  • So, why do you need this symlink? – muru May 22 '15 at 13:35
  • 1
    are you sure ssh cares about the permissions on the symbolic link and not the permissions on /somewhere/else/privatekey? – Random832 May 22 '15 at 16:55
  • @Random832 Yes, I may have drawn the wrong conclusion, I was also a bit confused about how the permissions were displayed in the ls -l output (lrwxrwxrwx). – Ray Burgemeestre May 22 '15 at 21:26
  • @muru I was creating a (buildserver) Docker image which uses an ssh key to access all kinds of servers. I don't want to put the private key inside because of different reasons (i.e. if I want to host it on docker hub). So I symlinked it and have it point to a volume that's only mounted once the image is ran. Although, come to think of it, I could have also changed the key location using .ssh/config :) – Ray Burgemeestre May 22 '15 at 21:30
18

Not possible. There is no way since permissions on a symlink are meaningless (a symlink is not a file; it only points to a file). The way to do this with Linux is through ACL though.

symlink are explained as...

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.


The difference is here: chmod and chmod... it is BSD versus Linux.


Not sure if it is important but regarding SSH: it uses stat(2), not lstat(2) to get the permissions.

  • stat() stats the file pointed to by path and fills in buf.
  • lstat() is identical to stat(), except that if path is a symbolic link, then the link itself is stat-ed, not the file that it refers to.
6
  • 2
    The ability to set permissions on symlinks is not specified in even the latest version, it's a BSD extension. – Random832 May 22 '15 at 16:54
  • 1
    @Random832 hmm that might be more acurate yes. I read that bit today on SO but did not do a check on this :D – Rinzwind May 22 '15 at 16:58
  • 4
    Incidentally POSIX does specify the fchmodat function, which can be used to change the mode of symbolic links (optional feature - systems that do not support it, including Linux, return error EOPNOTSUPP), but nothing in the chmod command to use this. Only -R is defined in POSIX. – Random832 May 22 '15 at 17:08
  • 2
    symlink permissions are meaningless on Linux. On systems where you can change them (with lchmod(2) or fchmodat(2)...), they do have a meaning (allow/deny readlink() (read) or path resolution through them (execute)). Write permissions usually don't though as you can't change the target of a symlink. – Stéphane Chazelas May 23 '15 at 7:12
  • Unix would disagree with you that "a symlink is not a file". A symlink is a special file. – hobbs May 23 '15 at 7:47
9

You can't. The underlying chmod system call simply doesn't support this in Linux, and for that matter, Linux doesn't care about the link's permissions either. From man chmod:

chmod never changes the permissions of symbolic links; the chmod system
call cannot change their permissions.  This is not a problem since  the
permissions  of  symbolic  links  are  never  used.   However, for each
symbolic link listed on the command line, chmod changes the permissions
of  the  pointed-to  file.   In  contrast, chmod ignores symbolic links
encountered during recursive directory traversals.

As for hard links or bind mounts, the source's permissions are used, so none of the three standard ways to reflect a file's contents elsewhere can help you in this.

1
  • This answer was also very clear to me, sorry I could only accept one as the answer! – Ray Burgemeestre May 22 '15 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.