Oct 23, 2023 6 min read

Chown Command in Linux (File Ownership)

Use the chown command in linux with our step-by-step tutorial. It allows you to change the owner of a file, directory, by changing the user/group

Chown Command in Linux
Table of Contents

Introduction

Before we discuss chown command in Linux (File Ownership), let's briefly understand-What is chown command ?

The chown command allows you to change the owner of a file, directory, or symbolic link by changing the user and/or group.

All files in Linux have an owner and a group, and authorization access privileges are assigned to the file owner, group members, and others.

In this tutorial, you will use the chown command using realistic examples. We will also address a few FAQs on chown command in Linux.

How to Use chown

Let's go through the basic syntax of the chown command before we get into how to utilize it.

The expressions for the chown command is as follows:

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

The username or user ID (UID) of the new owner is USER. The name of the new group or the group ID is GROUP (GID). The name of one or more files, folders, or URLs is FILE(s). The + sign should be used to prefix numeric IDs.

  • USER - If only the user is specified, that user will become the owner of the given files and the group ownership will remain unchanged.
  • USER: -  When a username is followed by a colon : and no group name is specified, the user becomes the owner of the files, and the ownership of the files is transferred to the user's login group.
  • USER:GROUP - If both the user and the group are specified (with no space between them), the files' user ownership is changed to the specified user, and the group ownership is changed to the specified group.
  • :GROUP - Only the group ownership of the files is transferred to the supplied group if the User is omitted and the group is prefixed with a colon :.
  • : - There is no change if only a colon : is specified without specifying the user or group.

chown does not produce any output and returns 0 by default when it succeeds.

To find out who owns a file or which group it belongs to, use the ls -l command:

ls -l filename.txt
Output

-rw-r--r-- 12 linuxize users 12.0K Apr  8 20:51 filename.txt
|[-------]   [------] [---]
     |          |       |
     |          |       +-----------> Group
     |          +-------------------> Owner
     +------------------------------> Permissions

Normal users can only modify the file's group if they own it and only to a group they are a part of. All files can have their group ownership changed by administrative users.

How to Change the Owner of a File

Use the chown command with the new owner's username and the target file as an argument to change the owner of a file:

chown USER FILE

The following command, for example, will transfer ownership of a file named file1 to a new owner named vega:

chown vega file1

Specify numerous files or directories as a space-separated list to alter their ownership. The following command transfers ownership of a file called file1 and a directory named dir1 to vega:

chown vega file1 dir1

Instead of a username, a numeric user ID (UID) might be used. The following example will transfer ownership of file2 to a new owner with the UID 1000:

chown 1000 file2

If a username with a numeric owner already exists, ownership will be transferred to the username. Prefix the ID with + to avoid this:

chown 1000 file2

How to Change the Owner and Group of a File

Use the chown command followed by the new owner and group separated by a colon (:) with no intervening spaces and the target file to change both the owner and the group of a file.

chown USER:GROUP FILE

The command below will transfer ownership of a file named file1 to a new owner named vega, as well as group users:

chown vega:users file1

If the group name is omitted after the colon(:), the file's group is changed to the provided user's login group:

chown vega: file1

How to Move a File to a Different Group

Use the chown command, followed by a colon (:), the new group name (without a space between them), and the target file as an argument to change only the group of a file:

chown :GROUP FILE

The owner group of a file named file1 will be changed to www-data with the following command:

chown :www-data file1

chgrp is another command that can be used to change the group ownership of files.

When the recursive option is not specified, the chown command only affects the group ownership of the files that the symbolic links point to, not the symbolic links themselves.

If you try to modify the owner and group of the symbolic link symlink1 that points to /var/www/file1, for example, chown will change the ownership of the file or directory to which the symlink points:

chown www-data: symlink1

You'll probably get a "cannot dereference'symlink1': Permission refused" error instead of changing the target ownership.

The problem occurs because symlinks are protected by default on most Linux versions, and you cannot operate on target files. /proc/sys/fs/protected symlinks specify this option. 1 indicates that the item is enabled, whereas 0 indicates that it is disabled. We do not advocate disabling the symlink protection.

Use the -h option to modify the group ownership of the symlink:

chown -h www-data symlink1

How to Change File Ownership in a Recursive Way

Use the -R (--recursive)  option to recursively act on all files and folders beneath the supplied directory:

chown -R USER:GROUP DIRECTORY

The following example will assign www-data to a new owner and group for all files and subdirectories in the /var/www directory:

chown -R www-data: /var/www

Pass the -h option if the directory contains symbolic links:

chown -hR www-data: /var/www

The -H and -L arguments can also be used to recursively change directory ownership.

The -H option causes the command to traverse a symbolic link that links to a directory if the argument supplied to chown is a symbolic link that points to a directory. The -L option instructs chown to follow each symbolic link to a directory it encounters. Generally, you should avoid using these choices because they may cause your system to malfunction or pose a security risk.

Making Use of a Reference File

You can use the --reference=ref file option to modify the user and group ownership of given files to match that of the specified reference file (ref_file). If the reference file is a symbolic link, chown will utilize the target file's user and group.

chown --reference=REF_FILE FILE

The following command, for example, will give file2 the user and group ownership of file1.

chown --reference=file1 file2

FAQs on Chown Command in Linux

How does the chown command work?

The chown command modifies the owner and group associated with a file or directory. It requires the user to specify the new owner and, optionally, the new group.

How do I use the chown command to change ownership?

To change ownership using chown, provide the new owner and, if desired, the new group, followed by the file or directory you wish to modify. For example, chown user:group file.txt will change the ownership of file.txt to the specified user and group.

Can I change the owner of multiple files using chown?

Yes, chown accepts multiple file or directory names as arguments, allowing you to change the ownership of multiple items simultaneously. For example, chown user:group file1 file2 dir1 will change the ownership of file1, file2, and dir1.

What are some common use cases for the chown command?

The chown command is commonly used to change the ownership of files after copying them to another user's directory, when transferring files between users, or when updating ownership after a system migration.

Can I recursively change ownership of directories and their contents?

Yes, you can use the -R or --recursive option with chown to recursively change ownership for directories and their contents. This option ensures that ownership changes are applied to all subdirectories and files within the specified directory.

Can I change only the group ownership without modifying the owner?

Yes, you can change only the group ownership of a file or directory without modifying the owner. To achieve this, use the :group syntax without specifying a new owner. For example, chown :group file.txt will change the group ownership of file.txt without affecting the file's owner.

What happens if I use chown with root privileges?

chown requires root privileges (sudo) when changing ownership of files or directories that belong to other users. With root privileges, you can modify ownership without any restrictions.

Conclusion

chown is a command-line tool for changing the owner of a file on a Linux/UNIX system.

Visit the chown man page or type man chown in your terminal to learn more about the chown command.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.