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.
Basic chown Example
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:
If we use the -v command line option for chown, it will confirm every action:
[[email protected] /home/greys]# chown -v greys:greys try
changed ownership of 'try' from root:root to greys:greys
chown Using Reference File
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?
I learn with Educative:
I'm also a fan of SetApp for macOS:
IT Consultancy
I'm a principal consultant with Tech Stack Solutions. I help with cloud architectrure, AWS deployments and automated management of Unix/Linux infrastructure. Get in touch!