How the Chown Command Works on Linux
Let's look at how the chown command works on Linux, and how to use it.
Join the DZone community and get the full member experience.Join For Free
In previous articles, we've covered how chmod works. In this article, we'll be covering how to use
chown is closely connected to chmod since it affects who owns a file, and which group it belongs to.
In our article on
chmod we cover how different user types (i.e. owners, groups, and all other users) have different permission sets. That means who owns or in which group a file belongs, can affect who can run it, and might even affect if a system process can run or open a file.
The syntax for chown is shown below, where
[OPTIONS] are optional settings,
[USER[:GROUP]] are the user and group, and
[FILE] is the file, files, directory or directories we wish to affect with the command.
chown [OPTIONS] [USER[:GROUP]] [FILE]
If we specify only one
[USER[:GROUP]], then it is assumed that we are trying to change the owner only, whereas if we only
type: GROUP with a colon followed by the group name, then it is assumed that only the group is changing.
At its most basic, we can change the owner of a particular file with
chown. For example, the following will change the owner for
chown someOwner file.txt
If we want to change the group to
someGroup, and owner to
someOwner, then the following command will do that:
chown someOwner:someGroup file.txt
And if we want to change just the group to
someGroup, then this will do it:
chown :someGroup file.txt
This code will also work for directories.
If we wish to change the owner or group for files and directories within a directory, we have to use the -R option.
For example, the below code will change the owner and group for all files in the
./test directory to
chown -R someOwner:someGroup ./test
Symbolic Links and Chown
There are three options that affect how symbolic links are affected by chown, and whether they are followed. These are
-L- follow every symbolic link.
-H- if the command line contains a reference to a specific symbolic link, then follow it.
-P- do not follow symbolic links (default). For example, the below will change all owners and groups within the
./testdirectory, and will traverse every symbolic link:
chown -RL someOwner:someGroup ./test
Using a Reference File To Change Owners and Groups
To use a reference file as a basis for the ownership and group of a file, use
--reference. For example, below the file new-file.txt will use the owner and group from file.txt. This will not work by default on MacOS.
chown --reference=file.txt new-file.txt
Change Owner and Group if It Matches a Specific Owner and Group
If we want to change only owners or groups which match a specific pair, we can use
--from. For example, the below will only change the user and group
newOwner: newGroup if the file was originally set to
someOwner: someGroup. This will not work by default on MacOS.
chown --from=someOwner:someGroup newOwner:newGroup new-file.txt
This follows the same rules as before, so if we only want to match on
someGroup, we'd write --from=:someGroup. If we only wanted to match on
someUser, we'd write --from=someUser.
As well to the already mentioned options, there are a few others that may be useful:
-v- verbose - will show a message after each file change.
-f- suppresses most error messages.
-h- changes the owner and group of a symbolic link, rather than the file or directory it links to.
-c- similar to verbose, but only reports when a change is made.
Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.