On Linux systems, each file is associated with an owner and group owner. When you don’t have the appropriate permission, you won’t be able to access or edit the files or directory. On a Linux system, there is a “change owner” (chown) tool that allows you to change the owner of a file/directory as well as the group owner. Let’s see how you can use the chown command in Linux to better manage your files and folders.
How to Use the chown Command in Linux
The command can be used according to the following syntax:
Check the Current Permissions of a File
Before changing the owner (or the group owner) of a file, you should first list the current permissions of a file by using the
ls -l. With this command, you will be able to see the owner and the group owner of the files you intend to operate on.
Changing Only the Owner of a File/Directory
It’s possible to change only the current owner of a file on the Linux system. As in the syntax of the command, you have something like two columns separated by the
:. The first column stands for the new owner, while the second column stands for the new group owner. Also, to change the owner of the file, you need superuser permission, which means prepending
sudo to all
Check the result of the command with
You can see that “maketech” is now the new owner of our file. By default, the command only works for a single file or directory. If you want to apply the changes to all the files and subdirectories within a folder, you have to use the
You can also change the owner of a file by using the UID of the user:
Change Only the Group Owner of a File or Directory
If you want to give other users permission to access the file, you can place them in a group, then change the group ownership of the file.
Check the result.
As you can see, we put the colon
: before the name of new group owner just to indicate that this is the group name and not the name of the new user.
Change the User and Group of a File
If you want to change the owner and the group owner of a file or directory, you will need to indicate the two values:
By doing this, you are assigning the new owner of the directory and also the group (meaning the users of the group) that can also operate.
The chown command in Linux is responsible for changing the file’s user and/or group ownership. It is often used together with the chmod command to better manage the permission of files in Linux.