Updated April 18, 2023
Introduction to Chown Command in Linux
In Linux, when we create a file, folder or link, each file or folder is associated with a group and an owner which is assigned with access right permissions for the file owner, group members, and others.
To create a file in Linux, we can use the below syntax:
Syntax:
touch file_name
Output:
When a file is created, the user who has created the file becomes the owner and the group the owner owns becomes the user’s current group.
As there might be many people using Linux at the same time, we need to increase the security that each user accesses their file. The permissions to access a file is given to:
- User: Only the owner of the file is given permission to access the file.
- Group: A group of users is given permission to access the file.
- Others: Every other user trying to access the file can have access.
CHOWN means CHange OWNership for a file /folder/link.
How to Use Chown Command in Linux?
The basic syntax for CHOWN command is:
chown [OPTIONS].. USER[:GROUP] FILE(s)..
- [OPTIONS]: the above command can be used with or without additional options.
- [USER]: the username or the numeric user ID (UID) is the new owner of a file.
- [:]: to change a group of a file/folder/link, use the colon
- [GROUP]: to change the group ownership of a file is optional.
- FILE: the target file name to which the chown command is to be applied.
Output:
To check chown Version: To check chown version on your system, we need to follow below
Syntax:
chown –version
Examples
Few examples listed to use chown command in Linux:
1. To Change the Ownership of a File: below is the syntax for changing the owner of the file:
Syntax:
chown owner_name file_name
There are different options to use for chown as below:
- –c: Results when a file change is made. Eg: chown –c owner_name file_name
- –v: Results gives you the verbose information of each file. Eg: chown –v owner_name file_name
- –f: Results a forceful change of the ownership of the file. It hides most of the error messages when you are not permitted to change the ownership.
- –R: Results on recursive files and directories. Eg: chown –R owner_name file_name1 file_name2
Option | Description |
-c
–changes |
Verbosely describe the action for a file whose ownership gets changed |
–dereference | It doesn’t act on symbolic links themselves instead act on what they are pointed to |
-f
–silent |
Do not print or it hides the error messages for the files whose ownership cannot be changed
|
–reference=file_name | Use the user and group information of the reference file instead of an explicit new owner value
|
-R
–recursive |
Recursively changes the ownership of directories and their contents in it. |
-v
–verbose |
Verbosely describes the action (or non-action) taken for each file. |
2. To Change the Group of a File: Below is the syntax for changing the group of a file.
Syntax:
chown :group_name file_name
Another way to change the group name is by using the command chgrp.
3. To Change Owner and Group of a File: Below is the syntax for changing the owner and group at the same time. We do so by using chown command followed with the owner_name and group_name separated by a colon( :).
Syntax:
chown owner_name:group_name file_name
4. To Change the Ownership from a Particular Owner: if we want to change a particular owner and give them access to root, use the below syntax:
Syntax:
sudo chown --from=owner_name root file_name
5. To Change the Group from a Particular Group Only: If we want to remove a particular group for a file and assign to a new group, use below syntax :
Syntax:
chown –from:group-name :new_group_name file_name
6. To Change the Ownership/Group Info from a Reference File: If we want to change the information of owner/group from other files instead of manually updating to the file, we can use the below syntax :
Syntax:
chown –reference=ref_file_name file_name
7. To Change the Ownership and Group Name to Uid and Gid: we can use User ID and Group ID to change the ownership and group for a file. For this, we can use the below syntax :
Syntax:
chown 1000:1001 file_name
Chown Commands for Directories
Chown commands are applied to directories also as we have done it for the files. To see the permissions for a directory, use ls-l command and output is given below.
Output:
drwxr-xr-x 2 user group 4096 Mar 20 17:39 directory_name
Here, the user is the owner and group is the group_name assigned to the directory name
1. To Change the Ownership of a Directory
Syntax:
chown owner_name /directory_name
2. Change the Group Name of a Directory
Syntax:
chown :grp_nme /directory_name
3. To Change Owner and Group Name
Syntax:
chown owner_name:group_name /directory_name
4. To Change Ownership/Groupname for Multiple Files or Directories
Syntax:
chown owner:grpname file1 file2 file3..
Chown Command for Links
A symbolic link is a link that is a reference to an existing file. To create a symbolic link, we can do it by “ln”. let us suppose, we will create a symbolic link as symblink for a file test.
Syntax:
ln –s test.txt symblink
To check the owner and group access, we can use ls –lrt command as below:
lrwxr-xr-x 1 root root 5 Feb 29 22:11 symblink -> test.txt
-rw-r--r-- 1 root root 0 Feb 29 22:11 test.txt
we can see here, there are two entries available, one the test file and the other is symblink linked to test file.
Let us now try to change the ownership of the symblink and see what happens,
Syntax:
chown user symblink
To see the changes made to the ownership, do ls –lrt command and the output will be shown below:
Output:
-rw-r–r– 1 user root 0 Feb 29 22:11 test.txt
lrwxr-xr-x 1 root root 5 Feb 29 7 22:11 symblink -> test.txt
Recommended Articles
We hope that this EDUCBA information on “Chown Command in Linux” was beneficial to you. You can view EDUCBA’s recommended articles for more information.