Changing Soft Link Ownership

Soft link [1] is a special file that points to another file or folder, as in, an alias to another file or folder. Let's illustrate this in Ubuntu 13.04.

1. Go to the /tmp folder and create a sample dummy file called a.
$ cd /tmp
$ touch a
$ ls -l a
-rw-rw-r-- 1 kianmeng kianmeng 0 Oct 12 12:37 a

/tmp is the global temporary folder in most Unix or GNU/Linux. As the name implied, most applications use this folder to store temporary files and these files are removed after each reboot. The touch command, which is supposed to update the timestamp of a file can be used to create a empty file quickly.

2. Great. Now, let's create a soft link using the ln command, call b, to the file a we've just created.
$ ln -s a b                                     
$ ls -l a b
-rw-rw-r-- 1 kianmeng kianmeng 0 Oct 17 02:12 a
lrwxrwxrwx 1 kianmeng kianmeng 1 Oct 17 02:12 b -> a

I always have a hard time remember the exact order of the link command especially when creating a soft link. The best way is to
$ ln -s filename linkname

3. To change the ownership of the soft link, we will use the chown command. Type these commands at the console as shown below.
$ sudo chown root.root b
chown: cannot dereference ‘b’: Permission denied

4. Solve this using the -h option which only affect the soft link instead of the actual file.
$ sudo chown root.root -h b
$ ls -l a b
-rw-rw-r-- 1 kianmeng kianmeng 0 Oct 17 02:12 a
lrwxrwxrwx 1 root  root  1 Oct 17 02:12 b -> a

No comments:

Post a Comment