Linux change link owner

But it’s not changing. I’m logged in as root. The current user/group is set to root:root. What went wrong?

Which operating system do you use?Acoording to the manaul page,-h option takes affect only on systems that can change the ownership of symbolic link.

Anything that ends with / is a directory. You mean mysymbolic , which is the symbolic link, not mysymbolic/ which is probably the directory it points to.

10 Answers 10

I was putting a slash in the end of target:

chown -h myuser:mygroup mysymbolic/ 

just removed the slash in the end and works. Here’s the correct way:

 chown -h myuser:mygroup mysymbolic 

I can’t believe after 4 years, I have bumped into my past self suffereing the same problem, the missin ‘-h’!

I’ve tried this myself and it works for me. If you have the -h it changes the owner of the symbolic link, but if you dont then it changes the owner of the file itself and not the link.

But it doesnt seem to work of the symbolic link is linked to a directory

For what it’s worth, the man page on OS X is a lot clearer on the -h option than the one on (Arch) Linux. “-h If the file is a symbolic link, change the user ID and/or the group ID of the link itself.” vs. “-h, —no-dereference affect symbolic links instead of any referenced file (useful only on systems that can change the ownership of a symlink)”

I was unable to chown a directory even with -h but using the full path worked.

# ls -al drwxr-xr-x 2 deploy deploy 4096 Dec 30 10:29 . drwxr-xr-x 3 deploy deploy 4096 Dec 30 08:59 .. lrwxrwxrwx 1 root root 32 Dec 30 09:02 apps -> /u/apps/ # chown -h deploy:deploy apps # ls -al drwxr-xr-x 2 deploy deploy 4096 Dec 30 10:29 . drwxr-xr-x 3 deploy deploy 4096 Dec 30 08:59 .. lrwxrwxrwx 1 root root 32 Dec 30 09:02 apps -> /u/apps/ # chown -h deploy:deploy apps/ # ls -al drwxr-xr-x 2 deploy deploy 4096 Dec 30 10:29 . drwxr-xr-x 3 deploy deploy 4096 Dec 30 08:59 .. lrwxrwxrwx 1 root root 32 Dec 30 09:02 apps -> /u/apps/ # pwd /var/www/html # chown -h deploy:deploy /var/www/html/apps # ls -al drwxr-xr-x 2 deploy deploy 4096 Dec 30 10:29 . drwxr-xr-x 3 deploy deploy 4096 Dec 30 08:59 .. lrwxrwxrwx 1 deploy deploy 32 Dec 30 09:02 apps -> /u/apps/ 

Is the target a file or a directory?

Читайте также:  Bluetooth kde arch linux

If it is a directory then try -H (upper case H)

Sorry for the thread necromancy, but I’d like to point out that the correct syntax is with the lowercase ‘h’.

chown -h myuser:mygroup [without trailing slash] 

should be enough and work!

Recreate that link by myuser at myuser’s home, and mv this link to the target location by sudo.

For example: (as myuser), ln -s somedir/ linkname (will be a broken link if somedir/ doesn’t exist in user’s directory)

Then, sudo mv linkname targetlocation (will become a valid link provided targetlocation/somedir/ exists)

Your answer is without detail and hard to fully understand. Please consider revising your answer to provide more detail.

I had a similar problem. For me, I could not chmod the symbolic link even as root regardless how I called chmod. To add confusion to this, nautilus was showing the owner/group as nothing. The owner was just blank. So I tried to change the symbolic link using nautilus running as root since chmod wasn’t working and nautilus crashed!!

But I think I figured out the problem. The directory the symbolic link was pointing to had different permissions than the symbolic link. So I chmod’ed the target directory (using -h) to my user/group name. Then chmod’ed the symbolic link to the same and it worked! And viewing the symbolic link’s details in nautilus (with root permissions) now no longer crashes.

So for others having a similar problem, check the permissions of the target directory/file and make sure it is compatible with the permissions you are setting the symbolic link to.

Источник

15 most used chown command in Linux [Cheat Sheet]

chown , which stands for change owner, is a command in Linux to change user or group ownership of a file, directory, or symbolic link. Every file or directory has a user or group ownership in the Linux systems. The Linux system can have multiple users and groups. They all have unique names and IDs. However, the name and ID of a user and a group can be the same.

Different examples to use chown command

chown command requires root permissions for its execution. You have to log in as a root user or use sudo in front of chown command.

To view the owner and group , you can use ls-l command. Here, test is the name of a file. The first home is owner and the second home is group.

$ ls -l test -rw-rw-r-- 1 home home 42 Aug 29 21:58 test 

In this tutorial, you will learn the different ways to change owner and group of a file, directory, or symbolic link using chown command.

1. Change the owner of a file using chown command

This is a simple chown command that changes the owner/user of a file. It requires a new owner name and file name. The new owner must be a valid user.

$ sudo chown new_owner file_name

Sample Output:

chown command to change owner of a file

2. Change the group of a file using chown command

chown command allows you to change the group of a file. You must use a colon in front of a new group name. Otherwise, it will be considered as a new owner.

$ sudo chown :new_group file_name

Sample Output:

Читайте также:  Postgresql linux посмотреть пользователей

chown command to change group of a file

3. chown command to change the owner using user ID

chown command also changes the owner of a file with numeric user ID. But there should not be any other user having the same name as userID. You can use id -u user_name to print the user ID of a user.

$ sudo chown user_ID file_name

Sample Output:

chown command to change owner owner using user id

4. chown command to change the group using group ID

You can change the group of a file using group ID instead of group name. id -g group_name prints the group ID.

$ sudo chown :group_id file_name 

Sample Output:

chown command to change group using group id

5. chown command to change owner of multiple files

This is a useful command that allows you to change owner of multiple files simultaneously. You have to separate file names with a space. It will assign same owner to all mentioned files.

$ sudo chown new_owner file_name1 file_name2 file_name3

Sample Output:

chown command to change owner of multiple files

6. Change owner and group name at the same time with chown command

chown command allows you to change owner and group name at the same time. You have to provide both new owner and group name in order to assign them in a file.

$ sudo chown new_owner:new_group file_name

Sample Output:

chown command to change owner and group at the same time

7. chown command to copy owner and group name from one file to another

This command copies the owner and group name from one file and assigns the same ownership to another file in the same directory.

$ sudo chown --reference=souce_file destination_file

Sample Output:

chown command to copy names settings from one file to another

8. Print the changes made by chown command

This command prints the changes made by chown command in detail. By default, chown command prints nothing.

Sample Output:

print the changes made by chown command

It also prints the information when the owner or group name is not changed.

chown command to print changes indetail

9. chown command to print the detail only if changes are made

Unlike the previous command, it prints the information only if the changes are made in the owner or group name. It does not print anything if the changes are not made in the owner or group name.

Sample Output:

print only changes made by chown command

10. chown command to change owner or group of a directory

Using the directory name instead of the file name, you can change the owner or group of a directory using chown command. You can also use directory path.

For changing the owner of a directory:

$ sudo chown new_owner directory_name

Sample Output:

chown command to change owner of a directory

For changing the group of a directory:

$ sudo chown :new_group directory_name

Sample Output:

chown command to change group of a directory

11. Change owner of a file using chown command if only the current owner matches

This command assigns the new owner to a file if only its current owner matches. —from option verifies the current owner.

$ sudo chown --from=current_owner new_owner file_name

Sample Output:

chown command to change owner if current owner is a specific user

If the current owner does not match, then the file owner will remain unchanged.

Sample Output:

chown command to change owner only if the current owner is a specific user

12. chown command to change group of a file only if the current group matches

Similar to the previous command, it only allows you to change the group of a file if the current group matches. —from option also verifies the current group of a file.

$ sudo chown --from=:current_group :new_group file_name

Sample Output:

chown command to change group if current group matches

You can also use —from option to verify the current owner and group and assign a new owner and group at the same time.

$ sudo chown --from current_owner:current_group new_owner:new_group file_name

Sample Output:

Читайте также:  Can open nvidia linux

chown command to verify user and group to change ownership

13. Change the owner or group of files using chown command

-R option allows you to change the owner or group of files and sub-directories recursively present in the same directory. It is very useful when there are large number of files that need same ownership.

sudo chown -R new_owner:new_group directory_name

Sample Output:

chown command to change ownership of a file recursively

14. chown command to change ownership of a symbolic link file

A symbolic link file is a type of file that points to another file. You can create a symbolic link by using:

$ ln -s source_file sym_file

To change the ownership of a symbolic link file using chown command, you have to use -h option to apply the changes. Otherwise, the changes will apply to the main file.

$ sudo -h new_owner:new_group sym_file

Sample Output:

chown command to change owner of symbolic link file

chown command without using h

As you can see in the above output, the ownership of ‘test’ is changed instead of ‘symtest’.

15. Hide error message in the output with chown command

This command hides the error message in the output when the file or directory is not found.

$ sudo chown -f new_owner file_name

Sample Output:

chown command to hide error message in the output

It does not hide the error message printed due to invalid user, group, or syntax.

chown command error message not hide in the output

Conclusion

These are the most important chown commands in Linux. Now, you can easily change the user and group ownership of a file, directory, or symbolic link in the Linux systems.

What’s Next

Further Reading

Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Источник

But the permission of the soft link is not getting changed. What am I missing here to change the permission of the link?

3 Answers 3

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)

$ 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.

Источник

Оцените статью
Adblock
detector