File and directory permissions are a big part of Linux. Without proper access, users can be locked out of essential files and not do much about the situation.
Well, from the many useful terminal utilities Linux offers, there’s chown. This command is used to change the user and/or group ownership of a given file, folder or symbolic link.
In this article, we’re going over how you can use the chown command.
Also read: How to remove a Directory in Linux?
Basic Chown syntax
The basic syntax for the chown command is as follows.
chown [flags] user[:group} file
User here is the username or the UID of the new owner, Group is the name of the new group or GID, and file is the name or the path of the file. Keep in mind that numeric IDs should be prefixed with the + symbol.
Here’s a detailed breakdown of the user and group syntax.
- User – If only the user is mentioned, that particular user will become the file owner. Group ownership remains unchanged.
- User: – If the username is followed by a colon (:) and no group name is specified, the user assumes ownership, and the group ownership goes to the user’s group.
- User:Group – In this case, the specified user and group become the file owner.
- :Group – Only changes the group ownership of the file.
No change in ownership is made if only a colon is given without any group or user names. By default, the chown command doesn’t give an output and returns zero.
You can use use the ls -l command to find the owners of a file.
Now normal users can only change a file’s ownership if they own a file and only to a group of which they’re a member. Admin or root users can change the user and group ownership of all files.
Also read: Mint vs Ubuntu: Linux distro comparison
How to change the ownership of a file using Chown?
Here’s the basic syntax we’ll be using to change a file’s owner.
chown User File
The below command will give file1’s ownership to the CTroot user.
chown CTroot file1
To change the ownership of multiple files or directories, mention the file/directory names one by one after a space.
chown CTrool file1 file2 dir1 dir2
Aforementioned, you can also specify the UID of the user you want to give the ownership to. However, if a user account has the same name as the UID, ownership will be given to them instead. To avoid this, prefix the UID with a + sign.
chown +1234 file1 file2
How to change the ownership and group of a file using Chown?
To change both the user and the group ownership of the file, we’re going to have to mention the new user and the group separated by a colon.
chown user:group file
For example, the following command will give file1‘s ownership to the demon user from the daemons group.
chown demon:daemons file1
Note that if you omit the group name after the colon, the group ownership is changed to that of the owner’s.
chown demon: file1
Changing the group ownership of a file
To change only the group ownership of a file, use the following syntax.
chown :group file
chown :daemons file1
Alternatively, you can also use the chgrp command.
chgrp daemons file1
Also read: How to rename a file in Linux?
How to change symbolic link ownerships?
When you don’t use the recursive flag, the chown command changes the group ownership of the files to which the symlink points, no the symlink itself.
Assuming if you were trying to change the owner and group of a symlink that points to /var/www/file1, chown will change the ownership of file1 instead of the symlink itself.
chown CTroot: symlink1
You’ll probably get the following error message — cannot dereference ‘symlink1’: Permission denied.
This error occurs because, by default, most Linux distros protect their symlinks and don’t allow users to operate on target files. You can disable this protection, but we advise against this.
To change the group ownership of a symlink itself, use the -h flag.
chown -h CTroot symlink1
Also read: How to check the list of users in Linux?
How to change file ownership recursively?
You can recursively work on all files in a given directory using the -R flag.
chown -R user:group directory
The example below will change the user and group ownership of all files in the /etc/bin directory.
chown -R CTroot: /etc/bin
If the directory contains symlinks, you can add the -h flag as well.
chown -hR CTroot: /etc/bin
Two more flags that you can use when recursively changing file ownerships are as follows.
- -H: this causes the chown command to traverse a symlink that points to a directory.
- -L: tells chown to traverse each symlink to an encountered directory.
Using these options isn’t recommended as it can cause a security risk or mess something up in your system.
Also read: How to change password in Linux?
How to change permissions using a reference file?
The –reference flag lets you assign permissions to a file that are the same as the specified reference file. If the reference is a symlink, the ownership of the target file will be used.
chown --reference=ref_file file1
Also read: How to shutdown Linux from Command Line?
Someone who writes/edits/shoots/hosts all things tech and when he’s not, streams himself racing virtual cars. You can reach out to Yadullah at [email protected], or follow him on Instagram or Twitter.