Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

See the Current Owner

ls -l index.html
-rw-r--r-- 1 root root 5120 Nov 30 09:12 index.html

The third column is the owner and the fourth is the group. The permission sets for owner and group, which the chmod command changes, apply to exactly these two. For a compact view, stat -c '%U:%G %n' index.html prints owner, group, and name.

Change the Owner

sudo chown deploy index.html

Only root can give a file to another user. A regular user who tries gets "Operation not permitted", which is why the examples use sudo.

Change Owner and Group Together

sudo chown deploy:www-data index.html
sudo chown deploy: index.html

The first line sets both. With a colon and nothing after it, the group becomes the user's login group. Older guides use a dot as the separator; use the colon.

Change Only the Group

sudo chown :www-data index.html
sudo chgrp www-data index.html

Both lines do the same. A regular user may change the group of a file they own to any group they belong to, without sudo.

Whole Directories with -R

A warning first: -R changes everything below a path, and there is no undo. On /, /etc, /usr, or a home directory it can break sudo, SSH key logins, and services. A stray space, as in / var/www, turns the command into one that runs on the whole system. Check the path first, then run the command:

ls -ld /var/www/example.com
sudo chown -R deploy:www-data /var/www/example.com

More patterns, such as changing only files or only directories with find, are in chown recursive.

Symbolic Links: -h

When you name a symbolic link, chown changes the file it points to, not the link. Add -h to change the link itself:

sudo chown -h deploy:deploy /var/www/current

With -R, chown does not follow symbolic links it meets inside the tree, so it stays within the directory you named.

Copy Ownership or Change Only Some Files

sudo chown --reference=index.html new.html
sudo chown -R --from=root:root deploy:www-data /var/www/example.com

The first copies owner and group from another file. The second changes only files that root currently owns and leaves the rest alone. find does the second job as well, and lets you add other tests such as file type or age:

sudo find /var/www/example.com -user root -exec chown deploy:www-data {} +

Numeric IDs

id deploy
sudo chown 1001:1001 data/

Files restored from a backup, copied from another server, or written by a container can show numbers instead of names in ls -l, because no local user has that ID. chown accepts numbers directly. For a Docker volume, the numbers must match the user the container runs as.

Typical Cases

  • Web root: the web server's user needs to read the site and to write only where the application stores uploads. It is www-data on Debian and Ubuntu, and nginx or apache on AlmaLinux and Rocky Linux. A common layout gives the files to a deploy user and the group to the web server.
  • Files copied as root: after sudo cp into a user's home, root owns the copy. Fix it with sudo chown deploy:deploy /home/deploy/file.txt.
  • Shared team directory: give the directory to a group, set the setgid bit, and add the members as shown in how to add a user to a group in Linux.
  • Verify: find /var/www/example.com ! -user deploy lists everything that deploy does not own.
Was this answer helpful?
Loading...