Chown Command In Linux (Change File Ownership)
Chown Command in Linux

Chown Command In Linux (Change File Ownership)

In Linux and other Linux distributions, the chown (Change Ownership) command is used to change the ownership of files, directories, and symbolic links. In Linux, all files and directories are associated with an owner and group, and different permissions are assigned to the owner, group, and others to access the file.

But the root user can change the ownership of any file. In this article, you will know in detail about chown command in Linux.

Check Ownership of a File in linux

Before changing the ownership of a file and directory, or a symbolic link file, verify its original owner and group. To change the ownership of any file, run the following command in Terminal.

If you run the simple ls command then you will get to see only a list of files in the output so use the ls -l to see file listing as well as permissions, file directory, create date, UID, etc information.

#ls -l
check Ownership of a File in Linux

How to Change the Owner of a File

To change the owner of a file with the chown command, you need to type in the name of the new owner and the name of the file whose ownership you want to change.

In the below screen you can see that the first owner of the mail file is root but the ownership was changed to siya with the chown command

#chown NewUser FILE
#chown siya mail 
How to Change the Owner of a File

Change File Ownership from UID

To change the ownership of a file or directory, you can use a UID instead of a username. In the example below, you can change the new owner of the WWW file with the help of user 1000 UID.

chown 1000 www
Change File Ownership from UID

How to Change the Owner and Group of a File in linux

in Linux To change both the owner and group of a file, the chown command is used. To change the new owner and new group of any file, use a colon (:) between the New Owner and New Group without any space.

chown USER:GROUP FILE

From the below example you can understand that file test1 has been changed to new owner Siya and new group tech. In Linux when you change the new owner and new group of any file, your system’s group name, owner name, and file name may be different.

How to Change both the Owner and Group of a File in linux

How to Change the Group of a File

If you want to change the group of a file, you must use a colon (:) before the group name. Type the name of the new group after the colon, and then type the name of the file whose group you want to change.

From the below example, you can understand that the file test1 was exist in the tech group, but with the chown command, we changed the test1 file to the new group (simi)

chown :New-Group File-name
How to Change the Group of a File

How to Change Symbolic Links Ownership

there are many types of files available in the Linux systems such as Block Files, Character device files, Pipe files, Symbol link files, Socket files, etc. But sometimes we need to change the ownership of the symbolic link file, for this, you can change the group ownership of any symbolic link file or directory with chown command.

To change the ownership of any symbolic link file in the Linux system, use the -h option after chown command.

How to Change Symbolic Links Ownership

How to Recursively Change the File Ownership

If you want to change the ownership of the directory as well as subdirectories and all the files under the main directory, then for that you have to use the -R (–recursive) option.

In the example below you can check that we have changed all the subdirectories and files under the data directory to a new owner and group named siya.

chown -R USER: GROUP DIRECTORY
How to Recursively Change the File Ownership

How To Transfer setting From One File To Other File

If you want to transfer the ownership of a specific file to another file, then the –reference option should be used. With the chown command, you can copy the ownership of a specific file to another file. In the below example we have transferred all the ownership of www file to daily-tech file with chown command .

chown --reference=ReferenceFILE FILE
How To Transfer setting From  One File To Other File

Conclusion

chown command used in Linux and linux distributions, the full form of chown is change ownership. With this command, you can change the ownership of any directory and file. If you want more information about chown, visit the chown man page or run man chown command in terminal.
Comment for any type of feedback related to this article and blog

Relatd article : How To Monitor cpu utilization in linux

Rate this post