sodcalculator

10 Tech How-To

Chown Command in Linux: How to Use It

Guide 10-50934 · Updated · Step by step

The chown command in Linux changes the owner and group of files and directories. You use it by typing chown [options] new_owner[:new_group] file, and it is essential for managing permissions and access control on a Linux system.

What Is chown and Why Does It Matter?

Every file and directory on a Linux system has an owner and an associated group. The owner is typically the user who created the file, and the group is a collection of users who share access rights. The chown (change owner) command lets you modify these attributes. This is crucial for system administration, especially when you need to transfer files between users, set up shared directories, or fix permission issues that prevent a service from reading or writing files.

Without the ability to change ownership, you would be stuck with the original owner forever, which would make multi-user systems impractical. For example, if you copy a file from another user’s home directory, you might need to change its owner to yourself to edit it. Or if you install software that runs under a dedicated service account, you must ensure the relevant files are owned by that account.

Basic Syntax and Common Options

The basic syntax of chown is:

chown [OPTIONS] USER[:GROUP] FILE...

You can specify the new owner, the new group, or both. Here are the most common forms:

  • chown alice file.txt – changes the owner of file.txt to user alice.
  • chown alice:developers file.txt – changes the owner to alice and the group to developers.
  • chown :developers file.txt – changes only the group to developers (note the colon before the group name).
  • chown alice: file.txt – changes the owner to alice and sets the group to alice’s login group (the colon with an empty group).

Common options include:

  • -R (recursive) – applies the change to all files and subdirectories within the given directory.
  • -v (verbose) – shows a message for each file processed.
  • -c (changes) – like verbose, but only reports files where a change actually occurs.
  • --reference=RFILE – copies the owner and group from a reference file instead of specifying them directly.
  • -h – affects symbolic links themselves, not the files they point to (by default, chown follows symlinks).

How to Choose the Right Owner and Group

Choosing the correct owner and group depends on what you want to achieve. For personal files, the owner should be your own username and the group can be your primary group (usually the same as your username on many distributions). For system files, the owner is often root and the group might be root or a service-specific group like www-data for web servers.

When setting up a shared directory, you might want a group that includes all users who need access. For example, if you have a project directory used by the team members alice, bob, and carol, you could create a group called project, add all three users to it, then run:

chown :project /srv/project

This ensures that the group ownership is correct, but you also need to set appropriate permissions (e.g., chmod 2770) so that group members can read and write.

Common Use Cases and Examples

Here are practical scenarios where you would use chown:

  • Fixing permission errors after copying files: If you copy a file from a USB drive or another user, it may be owned by a UID that doesn’t match your user. Use chown yourname:yourgroup file to take ownership.
  • Preparing a web directory: If your web server runs as www-data, you might need to change the owner of your website files to that user: chown -R www-data:www-data /var/www/html.
  • Transferring files to another user: To give a file to a colleague, you can change the owner to them: chown alice report.pdf.
  • Changing ownership of a mounted drive: When you mount an external drive, files might be owned by root. To allow a regular user to write, change the owner recursively: chown -R yourname:yourgroup /mnt/usb.

Common Mistakes and How to Avoid Them

One of the most common mistakes is forgetting the -R flag when you intend to change ownership of an entire directory tree. Without it, only the directory itself is changed, not its contents.

Another mistake is using the wrong syntax for group-only changes. If you write chown developers file (without a colon), chown will interpret developers as a username, not a group. Always use the colon (:developers) to specify a group.

Be careful with symbolic links. By default, chown follows symlinks and changes the target’s ownership. If you want to change the link itself, use the -h option. This is a subtle but important distinction, especially in directories with many symlinks.

Also, avoid using chown -R on system directories like /usr or /etc unless you are absolutely sure. A single typo can make your system unbootable or break critical services.

Permissions and Security Considerations

Only the root user (or a user with sudo privileges) can change the owner of a file. A regular user can change the group of a file they own, but only to a group they belong to. This restriction prevents users from giving away files to others or taking ownership of files they shouldn’t.

Changing ownership can have security implications. For example, if you change a setuid executable’s owner to root, it will run with root privileges, which could be a security risk if the program has vulnerabilities. Always think about who should be able to read, write, or execute a file after you change its ownership.

Also, be aware that ownership is tied to user and group IDs (UID and GID), not usernames. If you delete a user and later create a new user with the same UID, that new user will automatically own all files previously owned by the deleted user. This is why it’s important to manage UIDs carefully on systems with many users.

Alternatives and Related Commands

While chown is the primary command for changing ownership, there are related tools:

  • chgrp – changes only the group of a file. It is equivalent to chown :group file.
  • chmod – changes the permission bits (read, write, execute) of a file. It does not affect ownership, but it works together with chown to control access.
  • usermod – modifies user account properties, such as the user’s primary group or supplementary groups. This is useful when you need to add a user to a group before using chown to set group ownership.

In some cases, you might want to use setfacl (Access Control Lists) to grant more granular permissions to specific users or groups without changing the file’s owner. ACLs are more flexible but also more complex.

Frequently Asked Questions

How do I change the owner of a file to root?

Use sudo chown root filename. This changes the owner to the root user. You will need sudo privileges to do this, as only root can change ownership.

What is the difference between chown and chmod?

chown changes who owns a file (the owner and group), while chmod changes what permissions the owner, group, and others have (read, write, execute). They are complementary: chown sets the identity, chmod sets the access rights.

Can I change the owner of multiple files at once?

Yes, you can list multiple files after the owner specification, like chown alice file1 file2 file3. You can also use wildcards, e.g., chown alice *.txt, to match multiple files.

How do I see the current owner and group of a file?

Use ls -l to show detailed information, including the owner and group in the third and fourth columns. For example, ls -l file.txt outputs something like -rw-r--r-- 1 alice developers 1234 Jan 1 12:00 file.txt, where alice is the owner and developers is the group.

What happens if I specify a non-existent user or group?

chown will fail with an error message like “invalid user: ‘nonexistent'”. The command will not change anything. You must use a valid username or numeric UID.

Can I use a numeric UID instead of a username?

Yes, you can use a numeric UID, e.g., chown 1000 file. This is useful when the user does not exist on the system (e.g., files from an external drive). However, be careful: if the UID is not mapped to a user, the file will show that number instead of a name in ls -l.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →