What is the Chown command in Linux and how to use it?

On Linux operating systems, each file is associated with ownership by a group and an owner. A Linux chown command is shorthand for “change owner”. The superuser can use it on any Unix-like system. Here you will learn how it can benefit you and how to start using it.

With the chown options, you can change ownership of files, directories, and links. If a normal user wants to make certain changes to a file, a superuser can use chown to change ownership and allow them.

Viewing property information

First, you will need to login to your VPS using SSH. If you need a reminder, it 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 example, if the file path is /tmp/TestUnixgo there with the following command:

cd /tmp/TestUnix

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

ls -l

For this tutorial we create a file called chownSample.txt In the address book. The result of the above command would be:

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

here the first part -rw-r–r–, represents the permissions of the file. The first root represents the property information and the second root represents the group information. In the example above, chownSample.txt is owned by root, 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’s change ownership so that it is no longer root but another user named whales. Below is a sample of this command:

See also  Registrar Information -

chown whales chownSample.txt

To verify the property change, you can again use the 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 for changing ownership and group is:

chown user filename(s)

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

chown whales:aquatic chownSample.txt

To check ownership and group change, 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 needs to be changed, then we can omit the owner. As an example, you could type this on the command line:

chown :aquatic chownSample.txt

Chown performs functions similar to chgrp when 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 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 named TestUnix, we can use the command ls -l to see the permissions. A sample of this output will be:

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

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

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

See also  Padding vs Margin: what's the difference in CSS?

chown whales /TestUnix

To change only the group, you can use:

chown :aquatic /TestUnix

To change the 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 has the format:

chown file1 file2

A sample of such a command is:

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

chown for links

The 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.txtyou can create a symbolic link like:

ln -s chownSample.txt symlink

To check ownership and group information, we can use the command ls -l. This will produce results 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 tickets available. One is for the physical file and the other is for the symbolic link. To do this, if we try to change the property using the following command:

chown whales symlink

The above command changes the ownership of the file chownSample.txt. So the output of the command ls -l for this it will be as seen 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 property of the symbolic link, we must use the option -h. The command would be:

chown -h whales symlink

Here, if we use the command ls -lthe output will be the following, where the symbolic link property has changed:

See also  email archives

-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

The chown command can be used on directories; however, we might have a recursive directory structure and might want to change ownership of all files and directories.

Recursive use of the chown command ensures that all directories and subdirectories can have a property or group change.

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

chown -R Directory

If we have a directory like TestUnix with multiple subdirectories, then the following command will change ownership of all directories and subdirectories to the whales user.

chown -R whales /TestUnix

To end

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

Gustavo is passionate about creating websites. He focuses on the application of SEO strategies at for Spain and Latin America, as well as the creation of high-level content. When he is not applying new WordPress tricks you can find him playing the guitar, traveling or taking an online course.

Loading Facebook Comments ...
Loading Disqus Comments ...