Changing File and Folder Ownership via SSH
Last Updated: 2025-01-01
3 min read
Overview
Every file and directory on a Linux system has an owner (user) and a group. Ownership determines which permission set (owner, group, or others) applies to a given user. The chown command changes ownership, and it’s essential when files need to be accessible by the web server, a specific user, or after migrating content.
Viewing Current Ownership
ls -la /home/username/public_html/
-rw-r--r-- 1 username groupname 1234 Jan 01 12:00 index.html
drwxr-xr-x 2 username groupname 4096 Jan 01 12:00 images/
The third and fourth columns show the owner and group respectively.
Basic Syntax
chown [options] user:group file_or_directory
You can also change just the user or just the group:
chown user file # Change owner only
chown :group file # Change group only
chown user:group file # Change both owner and group
Common Usage Examples
Change owner and group of a file
chown username:groupname index.html
Change ownership of a directory
chown username:groupname /home/username/public_html/
This changes only the directory itself, not its contents.
Recursive ownership change
Use -R to apply changes to a directory and all files inside it:
chown -R username:groupname /home/username/public_html/
Change only the group
chown :www-data /home/username/public_html/uploads/
Or use the chgrp command:
chgrp www-data /home/username/public_html/uploads/
Understanding User and Group in Hosting
On a typical web hosting server:
| Entity | Common Names | Role |
|---|---|---|
| Your user | Your cPanel/SSH username | Owns your files |
| Web server user | www-data, apache, nginx, nobody | Serves web content |
| Your group | Often same as your username | Group-level access control |
Why ownership matters
- If the web server doesn’t have read access to your files, your site won’t load (403 error).
- If the web server needs to write files (e.g., WordPress uploads), the upload directory must be owned by or accessible to the web server user.
- After restoring files from a backup or transferring from another server, ownership often needs to be corrected.
When to Change Ownership
| Scenario | Action |
|---|---|
| Restored backup shows wrong owner | chown -R username:username /home/username/public_html/ |
| WordPress can’t upload files | chown -R username:www-data /home/username/public_html/wp-content/uploads/ |
| Files created by root need to be accessible | chown -R username:username /path/to/files/ |
| Moved files from another user | chown -R newuser:newuser /path/to/files/ |
Useful Flags
| Flag | Description |
|---|---|
-R | Apply recursively to all files and subdirectories |
-v | Verbose — show each file as ownership is changed |
--reference=FILE | Set ownership to match another file |
-h | Change ownership of symbolic links (not the target) |
--from=OWNER:GROUP | Only change files with this current ownership |
Copy ownership from another file
chown --reference=/home/username/public_html/index.html newfile.html
Change ownership only for files with a specific current owner
chown --from=root:root -R username:username /home/username/public_html/
This changes ownership only for files currently owned by root:root.
Practical Hosting Examples
Fix ownership after restoring a backup
chown -R username:username /home/username/public_html/
Allow web server to write to a WordPress uploads directory
chown -R username:www-data /home/username/public_html/wp-content/uploads/
chmod -R 775 /home/username/public_html/wp-content/uploads/
Verify changes
ls -la /home/username/public_html/
Tips
- On shared hosting, you typically cannot use
chown— only root or users withsudoprivileges can change file ownership. Contact your hosting provider if you need ownership changes on shared hosting. - On VPS or dedicated servers, use
sudo chownfor files outside your own directory. - Always pair
chownwith appropriatechmodpermissions — ownership determines which permission set applies, but the permissions themselves control what access is granted. - After any migration, restoration, or manual file transfer, check both ownership and permissions.
Tags:
ssh
linux
ownership
chown
security