sudo chown

Unlike chmod command, chown only becomes useful if run with elevated (root) privileges. In Linux, it is most commonly used with the help of sudo command.

Why sudo is needed for chown

Basic security model in Unix is around users and groups and their ownership of various files and directories. This means that without elevated privileges (becoming root or running commands via sudo) no regular user is meant to have enough privileges to act on behalf of another user.

Example of sudo chown

Here’s how we would run chown to change ownership of my own files (belonging to user greys) to root and back:

[greys@rhel8~]$ touch file1 file2 file
[greys@rhel8~]$ ls -la file*
-rw-rw-r--. 1 greys greys 0 Jun 19 12:43 file1
-rw-rw-r--. 1 greys greys 0 Jun 19 12:43 file2
-rw-rw-r--. 1 greys greys 0 Jun 19 12:43 file3

Without sudo our chown attempt will fail:

[greys@rhel8 ~]$ chown root file*
chown: changing ownership of 'file1': Operation not permitted
chown: changing ownership of 'file2': Operation not permitted
chown: changing ownership of 'file3': Operation not permitted

…but with sudo it does the trick:

[greys@rhel8 ~]$ sudo chown root file*
[sudo] password for greys: 
[greys@rhel8 ~]$ ls -la file*
-rw-rw-r--. 1 root greys 0 Jun 19 12:43 file1
-rw-rw-r--. 1 root greys 0 Jun 19 12:43 file2
-rw-rw-r--. 1 root greys 0 Jun 19 12:43 file3

That’s all I wanted to share today. Have fun!

See Also




Keep Learning with Me

Follow me on Facebook and Twitter or jump into Telegram chat!:
I'm using Brave browser
Give it a try, it's awesome: Brave Browser
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!

Recent Tweets