This post was originally published on go2linux.org on July 29, 2007. The domain is no longer mine, but I am the original author. I am republishing it here on garron.me with corrections and improvements.

Introduction

chown changes the owner and/or group associated with a file or directory. Only root can change the owner of a file. A regular user can change the group only to a group they belong to.

Syntax

chown [options] owner file...
chown [options] owner:group file...
chown [options] :group file...
  • owner — the new owner (username or numeric UID)
  • group — the new group (group name or numeric GID)
  • :group with no owner — changes only the group

Basic examples

Change owner only:

chown jose file.txt

Change owner and group at once:

chown jose:accounting file.txt

Change group only:

chown :accounting file.txt

This is equivalent to chgrp accounting file.txt.

Use numeric UID and GID:

chown 1001:1001 file.txt

Useful in scripts or when working across systems where usernames may differ.

Recursive change

Apply the change to a directory and everything inside it:

chown -R jose:accounting /home/account/

Be careful with -R on system directories — changing ownership recursively on /etc or /var can break your system.

Useful options

| Option | Effect | |---|---| | -R | Recursive — apply to all files in subdirectories | | -v | Verbose — print each file as it is processed | | -c | Report only files whose ownership actually changes | | --from=owner:group | Only change files that currently have this owner:group | | --reference=file | Use the owner:group of another file as the target |

Only change files currently owned by a specific user:

chown --from=olduser:oldgroup newuser:newgroup /var/app/ -R

Copy ownership from another file:

chown --reference=/etc/passwd /tmp/myfile

Symbolic links

By default chown follows symbolic links and changes the ownership of the file the link points to. To change the ownership of the symlink itself instead:

chown -h jose symlink

Checking current ownership

Use ls -l to see owner and group:

ls -l file.txt
-rw-r--r-- 1 jose accounting 1024 Jun  8 10:00 file.txt

Or stat for more detail:

stat file.txt

chown vs chgrp vs chmod

| Command | Changes | |---|---| | chown | Owner (and optionally group) | | chgrp | Group only | | chmod | Permissions (read/write/execute) |