Atlantic.Net Blog

How to Change Ownership of Files and Directory with chown Command

In Linux, only root or superusers have access to all files and directories. If you are a normal user, then you can not access files and directories created by other users. In this case, you can use the chmod and chown command to change the permissions or ownership of those files and directories.

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

Prerequisites

  • A Linux server on the Atlantic.Net Cloud Platform
  • A root password configured on your server

Step 1 – Create Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server. Create a new server, choosing Ubuntu, CentOS, or Rocky Linux as the operating system with at least 2GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Once you are 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

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 name of the file in which you want to change the ownership.

To check the version of 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 the ownership of any file or directory, 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 login.html and admin.html file:

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 use the –reference option to copy ownership settings from one file to another.

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

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

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G3.2GB Cloud VPS Server Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year