access_time
hourglass_empty
person_outline

What is a Chown Command in Linux and How to Use it

In Linux operating systems, every file is associated with a group ownership and an owner. A Linux chown command is an abbreviation to “change owner.” It can be used on any Unix like systems by the superuser. Here you’ll learn how it can benefit you, and how to start using it.

With chown options, one can change the ownership of files, directories, and links. If a regular user wants to make certain changes to a file, a superuser can use chown commands to change the ownership and permit them.

Viewing Ownership Information

First, you’ll need to log into your VPS using SSH. If you need a reminder, here’s a tutorial that covers everything you need to know.

Before using the chown command, we would need to confirm the user and group information. You can get this information, by using cd and navigating to the required directory.

For instance, if the path of the file is /tmp/TestUnix, go there using the following command:

cd /tmp/TestUnix

Here you can list the files within the directory with the following command:

ls -l

For this tutorial, we created a file called chownSample.txt in the directory. The output of the above command would be:

-rw-r--r-- 1 root root 0 Feb 20 17:35 chownSample.txt

Over here the first part -rw-r–r–, represents the file permissions. The first root represents the ownership information and the second root represents the group information. In the above sample, chownSample.txt has root ownership and root belongs to the root group.

Chown for Files

To change the owner of a file, the basic format of the command is:

chown user filename(s)

For the same file chownSample.txt, let us change the ownership from root to another user with name whales. A sample of this command is as highlighted below:

chown whales chownSample.txt

To verify the change of ownership, you can again use command ls -l. This will give the output as shown below:

-rw-r--r-- 1 whales root 0 Feb 20 17:45 chownSample.txt

The command can be modified to change the group. The basic format to change ownership and group is:

chown user[:group] filename(s)

If we want to change the same file chownSample.txt to owner whales and group aquatic, then the command will be:

chown whales:aquatic chownSample.txt

To verify the change of ownership and group, you can use ls -l. The output of this command is:

-rw-r--r-- 1 whales aquatic 0 Feb 20 17:50 chownSample.txt

If only the group should be changed, then we can skip the owner. As an example, you could type this into the command line:

chown :aquatic chownSample.txt

Chown performs functions similar to chgrp when the owner information is not provided. This command can also be used with multiple options.

A generic structure of the chown command with options would be:

chown [OPTIONS] [USER] [:GROUP] filename(s)

Chown for Directories

Chown can also be applied for directories. This can contain only files or only directories or a combination of both.

Let’s say we have a directory with the name TestUnix, we can use ls -l command to view the permissions. A sample of this output will be:

drwxr-xr-x 2 root root 4096 Feb 20 17:35 TestUnix

Over here the first part drwxr-xr-x, represents the permission for the folder. The first root is the ownership information and the second root is the group information. In this example, TestUnix has root ownership and root group.

Similar to files, we can change ownership and group for directories. A sample of this command will be:

chown whales /TestUnix

To change the group alone, you can use:

chown :aquatic /TestUnix

To change owner and group of the file you can use:

chown whales:aquatic /TestUnix

The same command can also be used to provide multiple files or directories. A sample of this command is in the format:

chown [OPTIONS] [USER][:GROUP] file1 file2

A sample of such command is:

chown whales:aquatic /tmp/TestUnix/chownSample.txt /tmp/TestUnix

Chown for Links

Chown command can be used on symbolic links or soft links. A symbolic link is a reference to an existing physical file. The ln command is used to create soft links. For a file chownSample.txt, a symbolic link can be created as:

ln -s chownSample.txt symlink

To verify the ownership and group information, we can use the ls -l command. This will produce output as shown below:

-rw-r--r--  1 root root  0 Feb 19 22:01 chownSample.txt
lrwxr-xr-x  1 root root 5 Feb 19  7 22:01 symlink -> chownSample.txt

There are two entries available. One is for the physical file and the other is for the symbolic link. To do this, if we try to change the ownership using the below command:

chown whales symlink

The above command changes the ownership for the file chownSample.txt. So, the ls -l command output for this will be as highlighted below:

-rw-r--r--  1 whales root  0 Feb 19 22:01 chownSample.txt
lrwxr-xr-x  1 root root 5 Feb 19  7 22:01 symlink -> chownSample.txt

If we want to change the ownership for the symbolic link then we need to use the -h option. The command would be:

chown -h whales symlink

Here if we use ls -l command then the output will be as highlighted below, where the ownership of symbolic link has changed:

-rw-r--r--  1 whales root  0 Feb 19 22:01 chownSample.txt
lrwxr-xr-x  1 whales root  5 Feb 19 7 22:01 symlink -> chownSample.txt

Recursive Use of Chown

Chown command can be used over directories, however, we could have a recursive directory structure and might want to change ownership for all the files and directories.

Recursive use of chown command ensures all directories and sub-directories can have a change in ownership or group.

For a recursive operation, we need to use the -R option. A sample of this command would be:

chown -R [USER][:GROUP] Directory

If we have a directory as TestUnix with several sub-directories, then the below command will change the ownership of all directories and sub-directories to user whales.

chown -R whales /TestUnix

Wrapping Up

That’s it, now you know the basics of the chown command. Unix systems provide a manual page for each command. This will help you fully master the command, and it’s possibilities. You can find the manual by executing man chown. We hope this helps you manage your VPS files in a safe and efficient way. Happy developing!

The Author

Author

Edward S. / @edvardasstabinskas

Edward is Hostinger's copywriter. He's an expert communicator with years of experience in IT as a writer, marketer, and Linux enthusiast. IT is a core pillar of his life, personal and professional. Edward's goal is to encourage millions to achieve an impactful online presence. He also really loves dogs, guitars, and everything related to space.

Related tutorials

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Become a part of Hostinger now!

More in VPS
What is a Curl Command And How to Use It?
Close