Only root or superusers access all files and directories in Linux. If you are a regular user, you can not access files and directories created by other users. In this case, you can use the chmod and chown commands to change the permissions or ownership of those files and directories.

In this post, we will show you how to use the chown command to change the ownership of files and directories.

Step 1 – Update the OS

Once logged in to your server, run the following command to update your base system with the latest available packages.

apt-get update -y

Or

dnf update -y

Step 2 – Basic Syntax of the chown Command

The basic syntax of the chown command is shown below:

chown [OPTIONS] USER:GROUP FILE

Where:

  • USER – Define the new owner of the file.
  • GROUP – Define the new group owner of the file.
  • FILE – Define the file name you want to change the ownership.

To check the version of the chown command, run:

chown --version

Sample output:

chown (GNU coreutils) 8.21
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David MacKenzie and Jim Meyering.

Step 3 – Check the Ownership of a File and Directory

Before changing any file or directory ownership, you will need to know the original file owner or group.

To check the ownership of files and directories in /opt directory, run the following command:

ls -l /opt

You should see the user and group owner of all files in the second and third columns:

-rw-r--r-- 1 root root    0 Dec  4  2020 admin.html
-rw-r--r-- 1 root root    0 Dec  4  2020 ajax.html
drwxr-xr-x 2 root root 4096 Aug 26 14:30 backup
-rw-r--r-- 1 root root    0 Dec  4  2020 composer.html
drwxr-xr-x 6 root root 4096 Oct 17  2020 ffmpeg
-rw-r--r-- 1 root root  891 Nov  3  2020 file2.txt
-rw-rw-r-- 1 root root    0 Jan 29  2021 file3.txt
-rw-r--r-- 1 root root   16 Oct 23  2020 -filename
drwxr-xr-x 4 root root 4096 Jun 28  2020 google
-rw-r--r-- 1 root root    0 Dec  4  2020 index.html
-rw-r--r-- 1 root root    0 Dec  4  2020 login.html
drwxr-xr-x 2 root root 4096 Jul  4  2020 mount
-rw-r--r-- 1 root root    0 Oct 22  2020 myfile.txt
drwxr-xr-x 3 root root 4096 Jul  2  2020 nginx-static-etags

Step 4 – Change the Owner of a Single File

As you can see, all files and directories are owned by the root user.

Now, pick a file named index.html and change the owner from root to vyom:

chown vyom /opt/index.html

Now, verify the ownership of index.html using the following command:

ls -l /opt/index.html

You should see the following output:

-rw-r--r-- 1 vyom root 0 Dec  4  2020 /opt/index.html

Step 5 – Change the Owner of Multiple Files

You can also change the ownership of multiple files and directories by using a single space between multiple files.

For example, run the following command to change the ownership of the login.html and admin.html files:

chown vyom /opt/login.html /opt/admin.html

If you want to change the ownership of all files and directories recursively, run the following command:

chown -R vyom /opt/*

Step 6 – Change the Owner and Group of a File

To change the group of a specific file, use the following syntax:

chown :NewGroupname Filename

For example, to change the group of a file index.html to www-data, run the following command:

chown :www-data /opt/index.html

If you want to change the user and group ownership of any file, run the following command:

chown vyom:www-data /opt/index.html

Where: vyom is the user owner, and www-data is the group owner.

Step 7 – Transfer User and Group Ownership from One File to Another

You can copy ownership settings from one file to another using the –reference option.

For example, to transfer user and group ownership settings from index.html file to admin.html, run the following command:

chown --reference=/opt/index.html /opt/admin.html

Conclusion

This guide explained how to change the file and directory ownership using the chown command. The chown command is a powerful tool that helps you manage file and directory ownership. Get started on your virtual private server from Atlantic.Net.