+5 votes
207 views
Change the permissions of a symbolic link without modifying the linked resource

in Guides by (371k points)
reopened | 207 views

1 Answer

+3 votes
Best answer

What is a symbolic link in Linux environments?
Important considerations to keep in mind.
How to change the permissions of a symbolic link without modifying the permissions of the link's target resource.

We are going to place ourselves in a device with Linux operating system and we will be in front of a terminal session to learn something important about symbolic links and how to change their permissions . Specifically, in this guide we will learn to change the permissions of a symbolic link but without modifying the permissions of the resource to which it links .

image

For the change of permissions through the console, in Linux we have the chmod command, but we will also talk about the chown command that allows us to change the owner and group of the files.

The disadvantage of changing the permissions of a symbolic link in Linux, is that the permissions of the resource linked by the symbolic link are also changed. For example, when we install an Apache server , the active websites are stored in the sites-enabled folder creating symbolic links to the corresponding configuration files, which are located in the sites-available folder.

What is a symbolic link in Linux environments?

A symbolic link in Linux environments is also known by the name "soft link" and is nothing more than a special file, whose function is to link to another resource. We can match it with what we call shortcuts in the Windows operating system.

Symbolic links can point to another file, such as a folder, a network resource, etc. These symbolic links, being simple files, are also affected by the Linux permission system , hence we must take certain precautions when using commands such as chmod or chown .

How to change the permissions of a symbolic link without modifying the permissions of the link's target resource.

In order to explain this section correctly, we will assume that we have a symbolic link called ejemplosim that points to the file /home/zeokat/ejemplo.txt .

image

If I want to change the symbolic link permissions ejemplosim with the chmod command, but without modifying the permissions of the destination file, the command to execute is as follows:
chmod -h 644 ejemplosim

Once this command is executed, we will see that the permissions of the symbolic link have been changed but not those of the resource to which it points.

The same modifier -h , we can use it with the chown command as follows:
chown -h www-data:www-data ejemplosim

image

We see that this is something simple, but in many occasions it can compromise the security of the system. We must pay special attention when we change permissions recursively in different directories, since we can ignore some symbolic link that ends up changing the permissions of the target resource in an unwanted way.

Important considerations to keep in mind.

The -h chmod is not supported on all Linux operating systems . For example in FreeBSD if it is supported, while in Ubuntu it is not. In contrast, the chown command does support this switch on most systems.

When a Linux system does not support the -h switch with chmod , it is because it does not support changing permissions on symbolic links . Therefore, if we change the permissions to the symbolic link, they will only be changed in the linked resource . As this can be a problem, in these systems, when chmod is applied recursively, symbolic links are ignored .

Extracted from the original text of the Linux man help pages:

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.


by (2.3m points)

Related questions

+3 votes
1 answer
+3 votes
1 answer
+5 votes
1 answer
+4 votes
1 answer
+5 votes
1 answer
asked Aug 9, 2019 in Guides by backtothefuture (371k points) | 165 views
7,114 questions
7,211 answers
319 comments
2 users