chown — change owner and group
💬 "chown command"
Even if permissions are correct, if the owner is wrong, access is denied. chown changes the owner/group.
The format is chown user:group path.
- Concept: Changes the owner and group of a file.
- Left argument: User or user:group format.
- Right argument: Target file/directory.
- Check: Verify the change with stat or ls -l.
Commonly used options
| Option | Description |
|---|---|
| -R | Recursively changes subitems |
| --reference=f | Matches the owner/group of another file |
| -h | Changes the owner of the symbolic link itself |
| -c | Outputs only changed items |
3 essential command combinations
chown -R www-data:www-data /var/www/html— changes the entire web root to be owned by the web server accountchown deploy:deploy /opt/app/current/public/index.php— specifies the owner of a deployment filechown --reference=/etc/nginx/nginx.conf /etc/nginx/conf.d/site.conf— unifies the ownership of config files
💬 "Show me an example of viewing time-based trends with this command"
Situations commonly encountered on production servers are organized in order.
Step 1 · Check current owner
See who owns it and what the web server account is.
stat -c '%n %U:%G' /var/www/html
Step 2 · Change owner
The web root is usually owned by www-data (or nginx).
chown -R www-data:www-data /var/www/html
Step 3 · Verify
Check the response and logs.
curl -sI https://example.com | head -1
How it's used in practice
1. Ownership mismatch after deployment
Situation — A deployment script ran as root, so files became owned by root.
chown -R deploy:www-data /opt/app/current
Result — The app can write files and permission errors disappear.
Point — Defining the deployment user, service user, and group structure in advance prevents recurring problems.
2. Upload write failure
Situation — Only uploads keep failing. Reading works.
chown -R www-data:www-data /var/www/html/uploads && chmod 755 /var/www/html/uploads
Result — The service account can write to the directory.
Point — Write failures are often an ownership issue. Don't just fiddle with permissions; check the owner.
Cautionary notes
- Running -R on a system path (/) by mistake can seriously damage the server. Double-check absolute paths.
- For symbolic links, the target's owner changes by default. To change the link itself, use -h.
- New files created by the app become owned by the running user, so use group inheritance (setgid) if needed.
Actual execution screen
This is the result of changing the web root owner to the web server account and then checking it.
!chown command execution result: checking owner and group
Production server
Source (HTML): https://games.aidebatehub.com/programs/linux/filesystem/chown.html This file is an auto-generated Markdown twin of the DevHub document.
AI Knowledge Hub
Visitor Comments