In this tutorial, We will understand and use the chown command in Linux through various examples.
The chown
the command is used to change the ownership of the user and group on any directory, file, or symbolic link.
In Linux, all the files are related to an owner and a group and assigned with permission. Which shows the accessibility on file for the file owner, group member, and others.
How to Use chown
Before moving to examples, let’s review the syntax of the command.
chown [OPTIONS] [USER:GROUP] FILE(s)
USER
represent the username or the user ID (UID) of the new owner. GROUP
represent the name of the new group or the group ID (GID). FILE(s)
is the name of one or more files (For multiple files use space to separate them), directories, or links. Numeric IDs should be prefixed with the +
symbol.
USER
– at the place of this if we only specify the user then that specified user will become the owner of the given files. The ownership of the group will not changed.USER:
– If we only specify the username with:
, and the group name is not given, then the user will become the owner and the group ownership also changed to user’s group.USER:GROUP
– here the user specified both user and group with:
without any space then the ownership of the user and group changed as per the details.:GROUP
– If the User is omitted and only group name is provided after the:
then only group ownership will be changed.:
If only a colon:
is given, without specifying the user and the group, then no change will take place in the ownership.
To check chown
work successfully or not :
Use ls -l
command to check the user and group ownership of any file, directory:
ls -l testfile.txt
-rw-r--r-- 12 linuxpanda users 12.0K Apr 8 20:51 testfile.txt |[-][-][-]- [------] [---] | | | +-----------> Group +-------------------> Owner
Normal users can change the group only if they own the file. The root user can change the group and user ownership of all the files.
How to Change the Ownership of a File
To change the user ownership of a file using chown
command then specify the new owner with the target file name:
chown USER FILE
For example, this command will change the ownership of a file Testfile
to a new owner linuxpanda
:
chown linuxpanda Testfile
To change the ownership of multiple files or directories, specify the username with file names and separate the names of the files with space. In the below example we will change the ownership of a file and directory. For example, a file is Testfile
and a directory Testdir
to a new owner linuxpanda
.
chown linuxpanda Testfile Testdir
In the command below, we will change the ownership of a file named Test file to a new owner. We will use UID 1001 (UID below 1001 are system reserved).
We can also specify the ownership of a file and Directory by using the UID. In the below example we will change the ownership of a file named Testfile
to a new owner with UID 1001
(UID below 1001 are reserved by the system) :
chown 1001 Testfile
How to Change the Ownership and Group of a File
To change both the owner and the group of a file at the same time, use the chown
command with a new owner and new group, separate them by a colon :
without using the space in between them.
chown USER:GROUP FILENAME
In the below example we will change the ownership of a file named Testfile
to a new owner linuxpanda
and the new group panda
:
The following command will change the ownership of a file named file1
to a new owner named linuxpanda
and group users
:
chown linuxpanda:panda Testfile
If we omit the group name after the colon :
then the group name will be the same as the user’s login group name :
chown linuxpanda: file1
How to Change the Group of a File
We can change the group ownership of a file, by using chown
command with the colon :
then specify the group name and the file name:
chown :GROUP FILE
In the below example we will change the group of a file named as Testfile
to Newgroup
:
chown :Newgroup Testfile
chgrp
also changes the group ownership of a file.
How to Change Symbolic Links Ownership
Whenever we try to change the ownership of the Symbolic Links, it will change the ownership of the directory or file which is pointed by the link, not the symbolic links themselves.
In the below example, we will try to change the owner and group of a symbolic_link
which point to /home/user1/Downloads
by using chown
the command, but in this case chown
command will change the ownership of the directory pointed by the symbolic link.
chown user1: symbolic_link
It may show some errors while changing the ownership. The error will be “cannot dereference ‘symbolic_link’: Permission denied”
Most of the Linux distribution symbolic links are protected, we can’t perform modification on the links
. Due to this, we have to face such errors. The rules regarding protection are specified in /proc/sys/fs/protected_symlinks
. Symbolic link protection must be enabled (1 means enabled and 0 means disabled).
To change the ownership of the symbolic, use chown -h
command
To change the group ownership of the symlink itself, use the -h
option:
chown -h user1 Link
How to Recursively Change the File Ownership
To change the ownership of the files and directories with subdirectories, Use recursive -R (-recursive)
option with the chown command in Linux
chown -R USER:GROUP DIRECTORY
In the below example, we will change the ownership of directories including subdirectories.
chown -R linuxpanda: /home/user/Downloads
To change the ownership of directories that include symbolic links, then we have to use chown -hR
:
chown -hR linuxpanda: /home/user/Downloads
Using a Reference File
We can also change ownership of user and group a file by using a reference file, then chown command with --reference=ref_file
will change the ownership same as those of the specified in the reference_file
.
chown --reference=reference_file FILE
In the below example, we will assign the user and group ownership of Testfile1
to Testfile2
.
chown --reference=Testfile1 Testfile2
Conclusion
chown
is a Linux command for changing the ownership of the user and group of a file.
For the complete detailed information about chown command, visit man chown
or chown --help
in the Linux terminal.
If you guys have any queries related to this, let me know in the comments.
Leave a Reply