Unix & Linux: Change permissions for a symbolic link (4 Solutions!!)
Channel: Roel Van de Paar
Date: 2020-08-09

Unix & Linux: Change permissions for a symbolic link


The Question: 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?

Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful

== This solution helped 63 people ==
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

== This solution helped 13 people ==
When you try to use chmod to set the link's permissions, the actually you do is
to set the permissions of the link's target.The link's permissions are
meaningless.

If you really need to change symlinks permission (usually meaningless, as
written in other answers), I was successful with using -R option of chown:
chown -R myuser:mygroup link
If -R was not used, the permissions were not changed.

With thanks & praise to God, and with thanks to the many people who have made this project possible! | Content (except music & images) licensed under cc by-sa 3.0 | Music: https://www.bensound.com/royalty-free-music | Images: https://stocksnap.io/license & others | With thanks to user u123 (https://unix.stackexchange.com/users/11740), user Stephane Chazelas (https://unix.stackexchange.com/users/22565), user Miroslav Mocek (https://unix.stackexchange.com/users/131494), user Just Do It (https://unix.stackexchange.com/users/118943), user JdeBP (https://unix.stackexchange.com/users/5132), user Anthon (https://unix.stackexchange.com/users/33055), user 200_success (https://unix.stackexchange.com/users/17535), and the Stack Exchange Network (http://unix.stackexchange.com/questions/87200). Trademarks are property of their respective owners. Disclaimer: All information is provided "AS IS" without warranty of any kind. You are responsible for your own actions. Please contact me if anything is amiss at Roel D.OT VandePaar A.T gmail.com.