Unix & Linux: How to change ownership from symbolic links? (3 Solutions!!)
Channel: Roel Van de Paar
Date: 2020-08-13

Unix & Linux: How to change ownership from symbolic links?


The Question: I am facing some issue with creating soft links. Following is the original
file.
$ ls -l /etc/init.d/jboss
-rwxr-xr-x 1 askar admin 4972 Mar 11 2014 /etc/init.d/jboss
Link creation is failing with a permission issue for the owner of the file:
ln -sv jboss /etc/init.d/jboss1
ln: creating symbolic link `/etc/init.d/jboss1': Permission denied

$ id
uid=689(askar) gid=500(admin) groups=500(admin)
So, I created the link with sudo privileges:
$ sudo ln -sv jboss /etc/init.d/jboss1
`/etc/init.d/jboss1' -＞ `jboss'

$ ls -l /etc/init.d/jboss1
 lrwxrwxrwx 1 root root 11 Jul 27 17:24 /etc/init.d/jboss1 -＞ jboss
Next I tried to change the ownership of the soft link to the original user.
$ sudo chown askar.admin /etc/init.d/jboss1

$ ls -l /etc/init.d/jboss1
lrwxrwxrwx 1 root root 11 Jul 27 17:24 /etc/init.d/jboss1 -＞ jboss
But the permission of the soft link is not getting changed.
What am I missing here to change the permission of the link?

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

== This solution helped 15 people ==
When acting on symlinks, you must tell most of the tools (chown, chmod, ls...)
not to dereference the link: you must add the -h parameter, as stated in the
manpage :
-h, --no-dereference
affect symbolic links instead of any referenced file (useful only on systems
that can change the ownership of a symlink)
So try : sudo chown -h askar.admin /etc/init.d/jboss1

== This solution helped 101 people ==
On a Linux system, when changing the ownership of a symbolic link using chown,
by default it changes the target of the symbolic link (ie, whatever the
symbolic link is pointing to).
If you'd like to change ownership of the link itself, you need to use the -
h option to chown:
 -h, --no-dereference affect each symbolic link instead of any
 referenced file (useful only on systems that can change the ownership
 of a symlink)
For example:
$ touch test
$ ls -l test*
-rw-r--r-- 1 mj mj 0 Jul 27 08:47 test
$ sudo ln -s test test1
$ ls -l test*
-rw-r--r-- 1 mj mj 0 Jul 27 08:47 test
lrwxrwxrwx 1 root root 4 Jul 27 08:47 test1 -＞ test
$ sudo chown root:root test1
$ ls -l test*
-rw-r--r-- 1 root root 0 Jul 27 08:47 test
lrwxrwxrwx 1 root root 4 Jul 27 08:47 test1 -＞ test
Note that the target of the link is now owned by root.
$ sudo chown mj:mj test1
$ ls -l test*
-rw-r--r-- 1 mj mj 0 Jul 27 08:47 test
lrwxrwxrwx 1 root root 4 Jul 27 08:47 test1 -＞ test
And again, the link test1 is still owned by root, even though test has changed.
$ sudo chown -h mj:mj test1
$ ls -l test*
-rw-r--r-- 1 mj mj 0 Jul 27 08:47 test
lrwxrwxrwx 1 mj mj 4 Jul 27 08:47 test1 -＞ test
And finally we change the ownership of the link using the -h option.

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 Zama Ques (https://unix.stackexchange.com/users/37444), user Stefan Schmiedl (https://unix.stackexchange.com/users/67770), user mjturner (https://unix.stackexchange.com/users/5186), user ivanleoncz (https://unix.stackexchange.com/users/164969), user Adrien M. (https://unix.stackexchange.com/users/50851), and the Stack Exchange Network (http://unix.stackexchange.com/questions/218557). 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.