chown Command in Linux



Permissions and ownership plays a critical role in securing files and directories and this helps you control who can read, write or execute files and directories on your system. For this purpose, the chown command is introduced.

The chown command is a widely used Linux command that makes it easy for you to change the owner or group of a specified file or directory. It is pretty useful in scenarios when you want to gain or revoke access to specific resources on your system.

Table of Contents

Here is a comprehensive guide to the options available with the chown command −

How to Install chown Command in Linux?

The chown command usually comes pre-installed on most Linux distributions and you can verify its presence by executing the below-given command −

chown --version

If, for some reason, you have mistakenly removed it, you can add it again by installing the coreutils package on your system.

The coreutils package can be installed on Debian-based systems like Ubuntu using the following command −

sudo apt install coreutils

For ReadHat-based systems like Fedora and CentOS, you can run the below-given command to install the coreutils package −

sudo yum install coreutils

Syntax for chown Command in Linux

The basic syntax for chown command in Linux is as follows −

chown [OPTIONS] USER[:GROUP] File/Directory

Where,

  • OPTIONS are additional command flags or options you can use with the chown command.
  • USER is the new owner of the specified file or directory.
  • GROUP is the new group owner of the specified file.
  • FILE/Directory is the target file or directory

Different Options Available for chown Command

With the chown command, you can use different options and change the behavior of the command. These options are discussed in the table below −

Option Description
-f, --silent, --quiet Forces the command, suppressing most error messages.
-R, --recursive All the command to work on files and directories recursively.
-c, --changes Display a result in case only if the ownership has been changed.
-v, --verbose, --dereference Provides a detailed output for each processed file.
-h, --no-dereference Prevents chown from following symbolic links.
-H, Changes the ownership of symbolic links in case only if they point to a directory.
-L Follows symbolic links and changes the target file or directory ownership.
-P Follows symbolic links and changes the symbolic link ownership itself.
--help Displays a help message that includes information about the command and its different flags or options.
--version Displays the version information for chown.

Examples of chown Command in Linux

Here are some common examples of chown command in Linux −

Change User Ownership of a File

One of the basic functions of the chown command on Linux is to change the ownership of files for a specified user. For example, let’s say we have a file called myfile.txt and we want the regular user account to own it. For this process, the following command will be used −

sudo chown myuser myfile.txt
Change User Ownership of a File

Ensure to replace ubuntu and myfile.txt with your desired username and filename.

Change User and Group Ownership

If you want to change both the user and group ownership of a file on your Linux system, you can use the following command −

sudo chown ubuntu:linux myfile.txt
Change User and Group Ownership

Remember to replace ubuntu, linux, and myfile.txt with the actual values relevant to your system.

Change Ownership of Group to Default Group of a User

To change the group ownership of a file to the default group of a user, you can use the following chown command on Linux −

sudo chown :linux myfile.txt
Change Ownership of Group to Default Group of a User

Replace linux with the desired group name and myfile.txt with the actual filename. This command will set the group ownership to the default group associated with the user.

Change Ownership of Group Only

If you want to change only the group ownership without affecting the user ownership, simply consider the following chown command −

sudo chown :linux myfile.txt
Change Ownership of Group Only

The above command will modify the group ownership while leaving the user ownership unchanged.

Change Ownership of a Directory Recursively

To recursively change the ownership of a directory and all its contents including files and subdirectories, you can use the below-given chown command −

sudo chown -R new_owner_name directory_name
Change Ownership of a Directory Recursively

Don’t forget to replace new_owner_name with the desired owner’s username and directory_name with the actual directory path. The above command will modify the ownership for everything that is inside the specified directory, not just the directory itself.

Change Ownership of Hidden Files

With the chown command, you can also change the ownership of hidden files on your system. For example, to change ownership of hidden files those that are starting with a dot, like .hiddenfile, simply use the following command −

sudo chown ubuntu:linux .newfile.txt
Change Ownership of Hidden Files

You must replace ubuntu, linux, and .hiddenfile with the actual values that are relevant to your system.

Symbolic links are special types of files that point to other files or directories and you can use the chmod command to change the ownership of the symbolic link. For example, if you have a symbolic link and want to change its ownership, you can use use the below-given command −

sudo chown ubuntu:linux mylink
Change Ownership of a Symbolic Link

Again, replace ubuntu, linux, and mylink with the appropriate values.

This way you can use the chown command on your Linux system.

Alternative Commands to chown in Linux

There are some alternative commands that you can use in place of the chown command. These commands are chgrp and chmod. The chgrp command is useful for changing the group ownership of a file or directory similar to the chown command. Here is an example that illustrates the use of chgrp command that is specially designed for changing the group ownership −

sudo chgrp newgroup filename

In this example, the group ownership of filename is changed to newgroup via the chgrp command on Linux.

The chmod command on the other hand is used for changing the file or directory permission on Linux. It is a more granular utility compared to chgrp or chown since it allows you to control who can read, write and execute a file on the system. The basic example for chmod command is provided below −

sudo chmod 755 filename

In this example, the permission of filename is changed to 755. It means that owners have the authority to read, write and execute the file, while the groups and other users can only be able to read and execute the file.

Conclusion

The chown is a versatile Linux command used for changing the owner or group of a file or directory. This command will be helpful in changing the ownership of a file, user and group, directory, hidden files and symbolic links.

In this tutorial, we covered in detail the syntax of chown command, followed by different options that can be used with the command. Further, examples are provided to understand the working of the command and help them decide when to use the command with different options.

Advertisements