Change File Ownership with chown

A practical guide to changing file ownership on Unix systems

Every file on a Unix system has an owner and a group. This matters because permissions are based on these—who can read, write, or execute depends on whether you’re the owner, in the group, or neither.

The chown command lets you change ownership. It’s one of those commands I use often enough to remember the basics, but rarely enough that I forget the edge cases.

The Basics

The syntax looks like this:

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

A few variations:

  • USER — Changes the owner, leaves the group alone
  • USER: — Changes the owner and sets the group to that user’s default group
  • USER:GROUP — Changes both owner and group
  • :GROUP — Changes only the group (owner stays the same)

Regular users can only change group ownership, and only if they own the file and belong to the target group. Root can change anything.

Changing the Owner

The most common case—you want a different user to own a file:

chown newuser file1

Multiple files work the same way:

chown newuser file1 dir1 file2

You can use a numeric UID instead of a username:

chown 1000 file2

One gotcha: if a username happens to be numeric (weird, but possible), chown will match the username first. Prefix with + to force UID interpretation:

chown +1000 file2

Changing the Group

To change only the group, prefix with a colon:

chown :newgroup file1

There’s also chgrp which does the same thing, but I find myself just using chown for everything.

Changing Both

Separate user and group with a colon, no spaces:

chown newuser:users file1

If you leave out the group name after the colon, chown uses the user’s default login group:

chown newuser: file1

Recursive Changes

For directories and everything inside them, add -R:

chown -R newuser:newgroup /path/to/directory

If there are symbolic links in the mix and you want to change the links themselves (not what they point to), add -h:

chown -hR newuser:newgroup /path/to/directory

There are also -H and -L options that follow symlinks in various ways, but I’d avoid these unless you really know what you’re doing. Following symlinks during recursive ownership changes can lead to unexpected results.

Matching Another File’s Ownership

Sometimes you want to copy ownership from one file to another:

chown --reference=file1 file2

This sets file2 to have the same owner and group as file1. Handy when you’re trying to match permissions across files without remembering the exact user/group names.