Change permissions for a symbolic link

  • I have a symlink with these permissions:

    lrwxrwxrwx 1 myuser myuser       38 Aug 18 00:36 npm -> ../lib/node_modules/npm/bin/npm-cli.js*
    

    The symlink is located in a .tar.gz archive. Now when I unpack the tar.gz archive using maven the symlink is no longer valid. I'm therefore trying to reconstruct the symlink. First I create the symlink using ln but how do I set the same permissions as the original symlink?

  • Correct answer

    7 years ago

    You can make a new symlink and move it to the location of the old link.

    ln -s <new_location> npm2
    mv -f npm2 npm
    

    That will preserve the link ownership. Alternatively, you can use chown to set the link's ownership manually.

    chown -h myuser:myuser npm
    

    On most systems, symlink permissions don't matter. When using the symlink, the permissions of the components of symlink's target will be checked. However, on some systems they do matter. MacOS requires read permission on the link for readlink, and NetBSD's symperm mount option forces link permissions checks on read and traversal. On those systems (and their relatives, including FreeBSD and OpenBSD) there is a equivalent -h option to chmod.

    chmod -h 777 npm
    

    I just ran into a permission problem with symlinks on a CentOS 6.8 server. The symlinks had owner:group of root:root. When the owner and group were changed to the user that owned the directory they were in, as shown in this answer, the permission problem went away.

    Just providing a clarification. To change ownership or rights on a symlink, the -h flag will affect the symlink file instead of the dereferenced file.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM