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:
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.
To check chown Version: To check chown version on your system, we need to follow below
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:
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
|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|
|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
|Recursively changes the ownership of directories and their contents in it.|
|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.
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( :).
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:
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 :
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 below 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 :
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 out is given below.
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
chown owner_name /directory_name
2. Change the Group Name of a Directory
chown :grp_nme /directory_name
3. To Change Owner and Group Name
chown owner_name:group_name /directory_name
4. To Change Ownership/Groupname for Multiple Files or Directories
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.
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,
chown user symblink
To see the changes made to the ownership, do ls –lrt command and the output will be shown as below:
-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
This is a guide to Chown Command in Linux. Here we discuss the syntax and examples of chown command in Linux along with chown commands for directories and links. You can also go through our other suggested articles to learn more –