Change File Ownership with chown
If you want to change the owner of a file or directory, you can use the chown
command. The syntax is as follows:
It is important to understand that as a regular user, you cannot change ownership of files or directories to have them belong to another user. However, the root account can. Consider the following example.
Let's suppose that you created a file called memo.txt
in the user chris’s home directory while you were root user. Here’s how you, as root, could change it to be owned by chris:
This will change ownership of memo.txt
to chris. However, this does not change the group owner, merely the user owner. If you wanted to change the group owner at the same time, you could have entered:
Now, the file is also owned by the group joe.
When a new user is created, it is typical for a group to be created of the same name. This is because Linux requires users to be a member of at least one group, referred to as their primary group, as part of its security and access control model.
The chown
command can also be employed recursively with the help of the -R
option. This is particularly useful when you need to modify the ownership of an entire directory structure to be owned by a specific user. For instance, if you've connected a USB drive, mounted it on the /media/myusb
directory, and wish to grant complete ownership of the drive's contents to the user chris, you can execute the following command:
Last updated
Was this helpful?