Raspberry Pi

How to Use chown Command to Change File Ownership on Raspberry Pi

The file system in Linux system including the Raspberry Pi has some privileges, which includes read, write, and execute. Mostly the owner of the file is allowed to overwrite the file, however in some cases you may need to perform some changes to the file and it doesn’t allow you to do any changes since it is associated with other user. In such cases, it is required to change the owner of the file so that they can get the owner privileges to the file.

In the Raspberry Pi system, the chown command is very handy in changing the owner of the file. So, in this article, you will learn how to change the ownership of the file on Raspberry Pi using the chown.

How to Use chown Command to Change File Ownership on Raspberry Pi?

The chown is a free software tool available in the Raspberry Pi system to change the ownership of any file. To use chown command in Raspberry Pi, verify that it’s already installed in your Raspberry Pi using the below-mentioned command:

$ chown --version

Though chown is pre-installed on Raspberry Pi system but if due to any reason it is not present in your system, you can install the it using the Coreutils package through the following command:

$ sudo apt install coreutils

Find Owners of File

To find the list of all the files along with the owner of each, you can use the below-mentioned command:

$ ls -l

The output of the command will display the list of all the files along with their owners.

And if you want to find out the owner of any particular file you can use the same list command along with the name of that file:

Syntax

$ ls -l <file_name>

Example

$ ls -l mydot.py

chown Command

The basic syntax of chown command is given below:

$ chown <options> <user> <:group> <file>

In the Raspberry Pi system, there are three categories to access file systems which includes user, group and others. “User” is the owner of a file and “group” is in which the file owner is present. So, in the above command the new username is used with the chown command to make it the owner of that file.

The group of files can also be changed by using the chown command but remember the group name is always used with a colon”:” to differentiate it from the user. <options> means that this command can be used with other options for files. Let’s discuss different ways to use the chown command, which are as follows:

Let’s discuss them one by one.

1: Changing File Owner for Single File

To change the owner of a particular file just use the chown command along with the username and name of file:

Syntax

$ sudo chown <new-user/owner-name> <file-name>

Example

$ sudo chown linuxhint mydot.py

Note: The ls -l command is only used to display the new owner.

2: Changing File Owner for Multiple Files

The chown can also be used to change the owner of multiple files at a time using the below-mentioned command:

Syntax

$ sudo chown <user-name> <file-1> <file-2>

Example

$ sudo chown linuxhint helloprogram.js raspbian

3: Changing Owner Through User ID

Instead of using the user’s name, user id can also be used to change the owner. To find the user ID, use the below-given command:

Syntax

$ id -u <user-name>

Example

$ id -u pi

Then further use that user id with chown command as shown below:

Syntax

$ sudo chown <user-ID> <file(s) name>

Example

$ sudo chown 1000 helloprogram.js raspbian

4: Changing Both Group & User Permission

Both user and group can also be changed at the same time by following the below-mentioned syntax:

Syntax

$ sudo chown <user-name/ID>:<group> <file(s)>

Example

$ sudo chown linuxhint:root index.html

5: Changing Only Group

If you want the original user to remain the same and only want to assign group privileges to a file, then below-mentioned syntax can be followed to only change the group:

Syntax

$ sudo chown <:group> <file(s)>

Example

$ sudo chown :pi index.html

6: Changing Owner with Reference Option

Reference option can be used with the chown command to copy the permission privileges from one file to another. By using reference, the privileges of the reference file are copied and transferred to the other file. So as an output both the files will have same owner privileges:

Syntax

$ chown --reference=<reference-file> <file>

Example

$ sudo chown --reference=mydot.py index.html

In the output, you can see that both the files have same user and group:

Conclusion

You can use the chown command in Raspberry Pi to change the owner of files. There are multiple ways to use the chown command in Raspberry Pi and each of them are discussed in the above-given guidelines. The basic syntax of chown includes <user>, <:group>, <file name>, and <options>. The user can follow the above command to change the ownership of the files on the Raspberry Pi system.

About the author

Zahra Zamir

An Electronics graduate who loves to learn and share the knowledge, my passion for my field has helped me grasp complex electronics concepts and now I am here to share them with others.