Knowledgebase

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

Read This First

chown -R rewrites the owner of every file below a path, and there is no undo other than a backup or snapshot taken before. Many system files must belong to root; if /, /etc, /usr, or a whole home tree gets a new owner by mistake, sudo and SSH can stop working.

  • Look at the path first: run ls -ld on it and type the full path.
  • Add a guard: --preserve-root makes chown refuse to work recursively on /; without it, chown does not refuse.
  • Take a snapshot: before large changes, take one in your provider's control panel.

The Command

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

The part before the colon is the new owner and the part after it the new group. Both must exist. Only root can give files to another user, so the command needs sudo. Check the result:

ls -l /var/www/example.com

Owner Only, Group Only

CommandChanges
chown -R deploy dirOwner only
chown -R :www-data dirGroup only
chgrp -R www-data dirGroup only
chown -R deploy: dirOwner, and group to the owner's login group

Symbolic Links

With -R, chown does not follow symbolic links into other folders; it changes the links themselves, which is the safe default. Without -R, chown on a symbolic link changes the file it points to. To change the link itself in that case, add -h:

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

Avoid -L together with -R unless you mean it: it follows every link, and a link to a system folder would hand that folder to the new owner as well.

Copy Ownership from Another File

sudo chown -R --reference=/var/www/example.com/index.php /var/www/example.com/new-module

--reference gives the target the same owner and group as the reference file, so you do not need to know the names.

Web Server Owners

  • Debian and Ubuntu: Nginx and Apache run as www-data.
  • AlmaLinux and Rocky Linux: Nginx runs as nginx, Apache as apache.
  • A sound layout: the files belong to your deploy user, the group is the web server group, and only upload or cache folders are group-writable. Making the web server the owner of all files lets a compromised site rewrite its own code.

To let your own account edit those files, add it to the web server group as shown in how to add a user to a group.

Change Only Some Files with find

find selects files by their current owner, type, or name, so you change only what needs changing:

sudo find /var/www/example.com -user olduser -exec chown deploy {} +
sudo find /var/www/example.com -type d -name cache -exec chown -R deploy:www-data {} +

Run the find part without -exec first to see the list. To find files whose owner no longer exists, for example after a user was deleted:

sudo find /var/www -nouser

Permissions Come Next

Ownership decides who the owner and group are; the mode decides what each may do. After a chown, check the modes with chmod recursively. A site that returns 403 after an ownership change usually has a folder the web server can no longer enter; see 403 Forbidden in Nginx.

Common Errors

  • Operation not permitted: the command ran without sudo.
  • Invalid user or invalid group: the name is misspelled or does not exist; check with id deploy and getent group www-data.
  • Wrong path changed: do not close the session. Restore from the snapshot or backup, as explained in snapshot vs backup.
Was this answer helpful?
Loading...