How to Change File Owner User and Group Permissions
In the previous tutorial we learned how to set file permissions and add and remove file permissions on Linux. A file permissions on Linux are set based on three recipient objects, namely user, group and other.
The user in question is the user who is the owner of the file, default the user who owns the file is the user who created the file. And group what is meant is group user of the file, by default group user a file is primary group of the user who created the file.
Access rights granted when setting access rights are access rights to user owner and group of the file. As already mentioned above, default user and group are set during the file creation process. But you can change the user owner of a file and group of the file.
In this Learn Terminal tutorial, we will learn how to change the user owner of a file and group user who has group access rights for the file, this is also known as file ownership.
How to Change the User Owner of a File on Linux
You can see the user who owns a file and group user of a file using the command ls -l followed by the name of the file you want to view, as shown in the following image:
After the file access rights mark, writing with the name “yaumil” is the user who owns the file. While “admin” is group user set to have access rights group to the file.
You can change the ownership of a file in Linux by using the command chowns, provided you have to do it with the “root” user or a user who has access rights to run sudo, for security reasons.
How to change the user owner of a file is very easy, you only need to run the command chown with the following format:
chown nama_user nama_file
Don’t forget to use the command sudo if you’re not login with the “root” user. For example, if you want to change the ownership of the “script.js” file above to the user “ikhsan”, you can do this by using a command like the following:
sudo chown ikhsan script.js
Then the “script.js” file will belong to the user named “ikhsan”, not to the user “yaumil” as before. To prove whether the command was successful you can check it by using the command ls -l as in the image below:
How to Change the Owner Group of a File
Before going into how to change group a file. For information, you can look at group anywhere a user is joined by using the command groups and followed by the name of the user you want to see in front of it. For example you want to see group from the “ikhsan” user, you can do this by using a command like the following:
groups ikhsan
Then a list will appear group whichever has the user “ikhsan” in it, as shown in the image below:
To change group owner of a file, you can do this by using the command chgrp. For example, you want to change the “script.js” file that previously belonged to group “admin” belongs to group “group1” which is group from the user “ikhsan” above, you can do this by using a command like the following:
sudo chgrp group1 script.js
Then group from the “script.js” file it will change to group “group1”, to prove it you can check it by using the command ls -l as in the picture below, where group the user of the file has changed to group “group1”:
In addition to using the “chgrp” command you can also change the group from a file using the command chown. For example you want to change group from the “script.js” file to the group “group1” as in the example above, you can also do this by using the following command:
sudo chwon :group1 script.js
How to Change Group and User File Owner at the same time
As mentioned above, you can also change group from a file using the command chwonusing the command chown you can change too group and the user who owns a file at the same time.
For example, with the same example as before, you want to change the user who owns the file “script.js” to user “ikhsan”, and you also want to change group the file becomes group “group1”, you can do this using just one of the following commands:
sudo chown ikhsan:admin script.js
Then the user who owns the file will belong to “ikhsan” and group the file becomes group “group1”. Although you can change the owner user and group of a file simultaneously, it’s still important for you to know how to do it one by one as before.