How to change the file owner and group in Linux?
Tagged: linux
- AuthorPosts
- September 7, 2020 at 9:46 AM #3823Santhosh Kumar DKeymaster@santhosh
In Linux, every file and directory is managed by a specific user and a specific group. File system ownership is fundamental to the Linux operating system. Please note that only the root user and members of the sudo group can change the ownership of a file in Linux for security purposes.
How to change the file owner and group in Linux?
For instance, let’s say we have a file named
file.pdf
. We want to change the ownership of this file to a user namedsanthosh
and also change the group ownership of this file to a group calleddigitash
. Let’s see how to do just that.Find the file owner and group
You can find the current owner of the file and the group using the following command.
sudo ls -l file.pdf
You will get an output like this.
-rw-rw-r-- 1 root www-data 1847 Oct 9 2019 file.pdf
This means that the file is currently owned by the
root
user and belongs to thewww-data
group.Change the file owner of a specific file
You can change the file owner using the
chown
command.sudo chown santhosh file.pdf
Check if the file owner has been changed.
sudo ls -l file.pdf -rw-rw-r-- 1 santhosh www-data 1847 Oct 9 2019 file.pdf
You will now see that the file ownership has been changed. The file
file.pdf
is now owned by the userSanthosh
.Change the ownership of all files inside a directory
To change the ownership of all files inside a directory, you can use the -R option as follows.
sudo chown -R user directory/
Change the group ownership of specific a file
You can change the group ownership of a specific file using the
chgrp
command.sudo chgrp digitash file.pdf
Check if the group ownership of the file has been changed.
sudo ls -l file.pdf -rw-rw-r-- 1 santhosh digitash 1847 Oct 9 2019 file.pdf
You will now see that the group ownership of the file has been changed. The file
file.pdf
now belongs to thedigitash
group.Change the group ownership of all files inside a directory
To change the group ownership of all files inside a directory, you can use the -R option as follows.
sudo chgrp -R group directory/
Change both the file owner and the group
You can change both the file owner and the group using just the
chown
command as follows.sudo chown santhosh:digitash file.pdf
Check if the file owner and the group has been changed.
sudo ls -l file.pdf -rw-rw-r-- 1 santhosh digitash 1847 Oct 9 2019 file.pdf
The file
file.pdf
is now owned bysanthosh
and belongs to thedigitash
group.Learn more Linux commands.
- AuthorPosts
- You must be logged in to reply to this topic.