
Linux is designed to support a large number of users. Because of this, it should maintain access permissions to files and directories. Also need how users can access these files and directories. The chown command is used to change the user and group ownership of a given file, directory or link. In this tutorial we will show you how to use chown command in Linux.
Chown Command Syntax
Before going into how to use the chown command, let’s start by reviewing the basic syntax.
chown [OPTIONS] OWNER[:GROUP] FILE(s)
Where,
- OPTIONS = Different options provided for Chown command given here.
- OWNER =
OWNER
is the username or you can also provide UID (UserID) of specific user. - GROUP =
GROUP
is the name of the new group or the group ID. - FILE =
FILE
is the name of one file or multiple files.
Make sure your user account have sudo privileges then only you can change the ownership of a file.
How to Change the Owner of a File and Directory
You can use simply chown
command to change the ownership of files or directories. Following are the few example with different scenarios:
For example, below command will change the ownership of a file to a specific user tecnstuff:
chown tecnstuff test.txt
You also can change the ownership of multiple files or directories by specify space separate list. Following command will change the ownership of multiple files test.txt and test1.txt
chown tecnstuff test.txt test1.txt dir1
There is another way to define owner using user id (UID) instead of username. Below command will change the ownership of file to user tecnstuff:
chown 7002 test1.txt
How to Change the Owner and Group of a File
You can change the both owner and the group of a file using chown
command. Chown command followed by the new owner and group separated by colon with no intervening spaces.
Below command will change the ownership of a file test.txt
to a new owner tecnstuff
and group users.
chown tecnstuff:users test.txt
If you have not specified the group name then by default it will assign user’s login group.
chown tecnstuff: test.txt
Change Group of the file
You can change the only group of the file using chown
command. You just need to provide only specific group name.
For example, if you want to change group of test.txt
file from users to www-data
then run below command:
chown :www-data test.txt
Change Ownership of Files Recursively
You can use -R
option along with chown
command to change group or owner of all file and sub-directories in a specific directory.
Run below command if you wants to change group of all directories inside /var/www
to a new owner and group named www-data
:
chown www-data: /var/www
Conclusion
Finally, you successfully learned how to use the chown command in Linux. If you want to learn more about the chown command visit the chown man page.
Leave a Reply