One of the most useful and powerful basic Unix commands, chown command allows you to change ownership of specified files and directories – change user or group owner.
chown Must be Run as root chown is one of these commands that must be run as root. Running it as a regular user will not work: one user can’t change even its own files so that they belong to another user.
I’ll start in my home directory /home/greys. Let’s use touch command to create a file named try and then use sudo command to become root:
As you can see, the file rightfully belongs to me and my group: greys:greys.
Let’s change the owner and owner group to root:
If we use the -v command line option for chown, it will confirm every action:
A really cool way of using chown and also a great gateway to shell scripting is making chown inspect a given (reference) file and then apply its ownership information to other specified files. So you’re making chown command confirm owner and group of a file and then apply this to lots of other files – all without really knowing or specifying the actual ownership info. That’s while such a file is called reference file.
For instance, look at the chrony config files in /etc directory. See how /etc/chrony.keys file belongs to root:chrony?
Here’s how you can make chown apply the same ownership details to my /home/greys/try file:
Pretty cool, huh?