We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Learn how to – How To use Linux chown command with examples

Learn how to – How To use Linux chown command with examples.

Introduction

The man page of chown says that chown changes the user and/or group ownership of each given file. When only an owner (a user name or numeric user ID) is given, that user is made the owner of each given file, and the files’ group is not changed. If the owner is followed by a colon and a group name (or numeric group ID), with no spaces between them, the group ownership of the files is changed as well.

When you use a colon but no group name following the user name, that user is made the owner of the files and the group of the files is changed to that user’s login group. If the colon and group are given, but the owner is omitted, only the group of the files is changed; in this case, chown performs the same function as chgrp. If only a colon is given, or if the entire operand is empty, neither the owner nor the group is changed.

Common chown options used

  • -c, –changes like verbose but report only when a change is made
  • -f, –silent, –quiet suppress most error messages
  • -v, –verbose output a diagnostic for every file processed
  • -R, –recursive operate on files and directories recursively

To view permissions set for the files you are about to change, use ls utility.

Run a long listing of the files or directories

$ ls -l 

-rw-rw-r-- 1 tech    tech    0 Jul 26 15:07 backend.php
-rw-rw-r-- 1 jmwang jmwang   0 Jul 26 15:05 configs.txt
-rw-rw-r-- 1 tech    tech    0 Jul 26 15:07 frontend.php

As you can see, the above files are owned by user tech and group tech and user jmwang and group jmwang.

To change user and group for configs.txt to tech, do

# sudo chown tech:tech configs.txt

List the files to show the new ownership

$ ls -l

-rw-rw-r-- 1 tech tech  0 Jul 26 15:07 backend.php
-rw-rw-r-- 1 tech tech  0 Jul 26 15:05 configs.txt
-rw-rw-r-- 1 tech tech  0 Jul 26 15:07 frontend.php

To change the user for frontend.php to jmwang and retain the group to tech

$ sudo chown jmwang:tech frontend.php

List the files to confirm the change

$ ls -l

-rw-rw-r-- 1 tech   tech  0 Jul 26 15:07 backend.php
-rw-rw-r-- 1 tech   tech  0 Jul 26 15:05 configs.txt
-rw-rw-r-- 1 jmwang tech  0 Jul 26 15:07 frontend.php

To change group for backend.php to jmwang, do

$ sudo chown :jmwang backend.php
$ ls -l

-rw-rw-r-- 1 tech   jmwang  0 Jul 26 15:07 backend.php
-rw-rw-r-- 1 tech   tech    0 Jul 26 15:05 configs.txt
-rw-rw-r-- 1 jmwang tech    0 Jul 26 15:07 frontend.php

You can use the permissions of one file and super-impose them on another using the –reference option. Let us try and see if it works.

$ sudo chown --reference=backend.php frontend.php
$ ls -l

-rw-rw-r-- 1 tech jmwang  0 Jul 26 15:07 backend.php
-rw-rw-r-- 1 tech tech    0 Jul 26 15:05 configs.txt
-rw-rw-r-- 1 tech jmwang  0 Jul 26 15:07 frontend.php

So far, we have been dealing with the files.What if you needed to change the ownership of a directory together will all of the files and sub-directories within it? chown comes with a recursive functionality.

To change user and group ownership on a directory recursively, just use the -R option and you will be good to go. Use the same commands as the one for files only that you add -R option.

$ sudo chown -R http:httpd /var/www/html/

To change group ownership only

$ sudo chown -R :httpd /var/www/html/

To change user ownership only

$ sudo chown -R httpd /var/www/html/  ## Notice there is no leading full colon.

So far, we have interacted with files and directories. What happens when we change the ownership of a symbolic link? Let us investigate and solve it. Let us create a symbolic link for configs.txt

$ sudo ln -s configs.txt test.txt

List them to see the soft link

$ ls -l

-rw-rw-r-- 1 tech jmwang  0 Jul 26 15:07 backend.php
-rw-rw-r-- 1 tech tech    0 Jul 26 15:05 configs.txt
-rw-rw-r-- 1 tech jmwang  0 Jul 26 15:07 frontend.php
lrwxrwxrwx 1 tech tech   11 Jul 26 15:33 test.txt -> configs.txt

Let us try to change the user and group ownership of test.txt the way we know it to jmwang

$ sudo chown jmwang:jmwang test.txt

Long list to see the changes

$ ls -l

-rw-rw-r-- 1 tech jmwang  0 Jul 26 15:07 backend.php
-rw-rw-r-- 1 tech tech    0 Jul 26 15:05 configs.txt
-rw-rw-r-- 1 tech jmwang  0 Jul 26 15:07 frontend.php
lrwxrwxrwx 1 tech tech   11 Jul 26 15:33 test.txt -> configs.txt

You will notice that the ownership of test.txt did not change.

In order to force change to the ownership of the symbolic link, issue the -h option as shown:

$ sudo chown -h jmwang:jmwang test.txt

Long list to see the changes

$ ls -l

-rw-rw-r-- 1 tech   jmwang  0 Jul 26 15:07 backend.php
-rw-rw-r-- 1 tech   tech    0 Jul 26 15:05 configs.txt
-rw-rw-r-- 1 tech   jmwang  0 Jul 26 15:07 frontend.php
lrwxrwxrwx 1 jmwang jmwang  11 Jul 26 15:33 test.txt -> configs.txt

Conclusion

Changing file and directory ownership is as simple as using chown command which in itself is quite intuitive as you have discovered. We hope the article was comprehensive and helpful.

Also Read:

How to extract .xz files on Linux – CenOS / Ubuntu / Debian

How to use scp command to securely transfer files with examples

Top 10 Free Backup software for Linux

netstat vs ss usage guide on Linux

How to use rsync command on Linux/Unix with examples

How to Check TCP connections States in Linux with Netstat

Cart

Your Cart is Empty

Back To Shop