Chown Command in Linux

Chown Command in Linux
Chown Command in Linux

Chown Command in Linux

Linux systems are designed in such a way that they can support multiple users. Because of this Linux systems should maintain the database for access permissions for different files and directories. So it will know that who can access these files and directories. Following are the two main types of permissions in Linux:

1.User Permissions: Permissions assigned only to the specific user in the Linux system.

2.Group Permissions: Permissions assigned only to the specific group in the Linux system.

To manage such file ownerships chown command is used in Linux. Chown command changes ownership of the files and directories. In this tutorial, we are going learn Chown command in Linux.

Basic Syntax

Following is the basic syntax for chown command:

chown [OPTIONS] [OWNER_USER:OWNER_GROUP] [FILE...]

Where,
OPTIONS = Different options provided for Chown command given here.
OWNER_USER:OWNER_GROUP = Username or Groupname otherwise you can also provide UID (UserID) or GID (GroupID) of specific user or group to whom ownership should be assigned.
FILE = File or multiple files.

How to change the owner of files and directories

You can change ownership of different files or directories by using the simple chown command. Following are the examples for different scenarios:

To change ownership of the files to a specific user linux4one you can run below command:

chown linux4one first_file.txt

You can also change ownership of multiple files at the same time using the following command:

chown linux4one first_file.txt second_file.txt

To change ownership of directories run below command:

chown linux4one first_directory second_directory

How to change the owner of files with UID

Get your UserID by using the following command:

echo $UID

The output should be:

1000

Now change ownership of the files running below command:

chmod 1000 first_file.txt
Change Owner and Group of the file

To change owner and group of the file you should provide User Name and Group Name. By using the chown command you can change the owner and group of a file in following way.

To change the owner of the file to user linux4one and group admins run following command:

chown linux4one:admins demo_file.txt

What if you dont want to provide Group Name at such time you can only provide username of the system in following way (remember :):

chown linux4one: demo_file.txt

In the above command as the group is not provided after : so it will by default takes the group of the given user linux4one

Change Group of the file

You can change the only group of the file using chown command. Here you should only provide specific Group Name.

Run the following command to change group of the file demo_file.php to httpd using the following command (Don’t forget colon : in below command):

chown :httpd demo_file.php

Change ownership of files recursively

Using -R option with the chown command you can change group or owner of all file and sub-directories in a specific directory.

You can use the following command to change group of a directory /var/www to httpd by running following command:

chown -R :httpd /var/www

View Owner of The Files

To view owner of the file you can use ls command with -l option in Linux system.

View owner of the file demo_file.txt running below command:

ls -l demo_file.txt

The output should be:

-rw-r--r-- 1 linux4one linux4oneusers 241 Mar 1 01:28 demo_file.txt

Conclusion

You have successfully learned Chown command in Linux. If you have any queries please don’t forget to comment out.

LEAVE A REPLY

Please enter your comment!
Please enter your name here