keepcalculator

06 Numbers and Powers

Chown Command Examples: Change File Owner

Card No. 06·47238 · Updated · Standard formula, shown below

The chown command changes the owner and group of files and directories on Linux and Unix-like systems. You use it by typing chown, then the new owner, optionally a colon and group, then the file path, for example chown alice:staff report.pdf. This article explains the syntax, common examples, and pitfalls so you can use it correctly.

What chown Does

Every file and directory on a Linux system has an owner (a user) and a group. The owner is usually the user who created the file, and the group is usually the user’s primary group. The chown command changes these two attributes. It is a fundamental tool for managing permissions, especially in multi-user systems, servers, and shared directories.

The name stands for “change owner.” It is part of the GNU Core Utilities, so it is available on virtually every Linux distribution, as well as on macOS and other BSD systems. The basic syntax is:

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

If you specify only a user, the group is left unchanged. If you specify a user followed by a colon and a group, both are changed. If you specify only a colon and a group (e.g., :staff), only the group changes. You can also use a dot instead of a colon (e.g., alice.staff), but the colon is the standard and recommended form.

Basic Examples

Here are the most common uses you will encounter:

  • Change owner only: chown alice file.txt makes Alice the owner, leaves the group unchanged.
  • Change owner and group: chown alice:staff file.txt makes Alice the owner and the group staff.
  • Change group only: chown :staff file.txt changes only the group to staff.
  • Change owner and set group to their primary group: chown alice: file.txt changes the owner to Alice and the group to Alice’s primary group.
  • Change multiple files: chown alice file1.txt file2.txt file3.txt changes the owner of all three files.

You can also use wildcards. For example, chown alice *.txt changes the owner of all .txt files in the current directory.

Working with Directories and Recursion

When you run chown on a directory, it only changes the directory itself, not the files inside. To change the owner of a directory and everything inside it, you need the recursive option -R (or --recursive).

For example, to give Alice ownership of the entire /var/www directory tree:

chown -R alice:staff /var/www

This is one of the most common uses of chown in web server administration. However, be careful: -R follows symbolic links by default, which can change the owner of the file the link points to, not the link itself. To avoid this, use -h to change the link itself, or -P to not traverse symbolic links. The safe combination is chown -hR or chown -RP if you want to avoid unexpected changes.

Using Numeric IDs and References

You can specify the owner or group by their numeric ID instead of a name. This is useful in scripts or when the user does not exist on the system. For example:

chown 1000:1000 file.txt

This sets the owner to UID 1000 and the group to GID 1000. You can also mix: chown 1000:staff file.txt.

Another powerful feature is the --reference option. It copies the owner and group from another file. For example, to make newfile.txt have the same owner and group as template.txt:

chown --reference=template.txt newfile.txt

This is handy when you need to match permissions across many files.

Common Pitfalls and Mistakes

Several mistakes are common when using chown:

  • Forgetting the colon: chown alice staff file.txt is invalid. It tries to set the owner to a user named “alice staff” (which will fail). Always use a colon or dot between user and group.
  • Using a dot when the group name contains a dot: If the group name has a dot (e.g., staff.local), using a dot separator will confuse the command. Use a colon instead.
  • Not using -R for directories: New users often run chown alice /var/www and wonder why the files inside are still owned by root.
  • Following symbolic links unintentionally: As mentioned, -R follows symlinks. If you have a symlink pointing to a system file, you could accidentally change its owner. Use -h to change the link itself.
  • Using chown when you only need group change: If the owner is correct but the group is wrong, use chgrp or chown :group instead of changing the owner too.

Permissions and Who Can Run chown

Only the root user (or a user with sudo privileges) can change the owner of a file. A regular user cannot give away ownership of a file they own, nor can they change the group to a group they are not a member of. This is a security measure to prevent users from making files owned by others to bypass quotas or permissions.

If you try to run chown without sufficient privileges, you will get an error like Operation not permitted. In that case, prefix the command with sudo, for example:

sudo chown alice:staff file.txt

When using sudo, the command runs as root, so it can change any file.

chown vs. chmod vs. chgrp

It is easy to confuse these three commands:

  • chown changes the owner and optionally the group.
  • chgrp changes only the group.
  • chmod changes the permission bits (read, write, execute) for the owner, group, and others.

In practice, you often use chown to set the owner and group, then chmod to set the permissions. For example, to give a web server write access to a directory:

sudo chown -R www-data:www-data /var/www
sudo chmod -R 755 /var/www

This sets the owner and group to the web server user, then gives the owner full access and others read/execute access.

Frequently Asked Questions

What is the difference between chown alice:staff and chown alice: staff?

The first is correct: it sets the owner to alice and the group to staff. The second has a space after the colon, which makes the command interpret staff as a separate file argument. It will try to change the owner of a file named staff to alice, which is probably not what you want.

How do I change the owner of a file to the current user?

You can use the special name $(whoami) or just your username. For example: sudo chown $(whoami) file.txt. This is useful when you have root-owned files in your home directory.

Can I use chown with a numeric UID that doesn’t exist?

Yes, you can. The system will store the numeric UID even if no user has that ID. When you list the file with ls -l, it will show the number instead of a name. This is sometimes used for special system files.

What does chown -R do to symbolic links?

By default, -R follows symbolic links and changes the owner of the target file, not the link itself. To change the link itself, add the -h option: chown -hR alice link. The -P option also avoids following links but does not change the link’s owner either.

Why do I get “Operation not permitted” even with sudo?

If you are using sudo and still get this error, it could be due to filesystem mount options. For example, a filesystem mounted with the nosuid or nodev option may restrict ownership changes. Also, some filesystems like FAT or NTFS do not support Unix ownership and will always show the same owner. In that case, chown will not work.

How do I change the owner of all files in a directory but not the directory itself?

You can use a glob pattern: chown alice /path/to/dir/* (without -R) changes all files and subdirectories directly inside, but not the directory itself. To include hidden files, you need to use chown alice /path/to/dir/.* as well, because * does not match dotfiles by default.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →