9 October 2026 · One command a day
How do I fix file ownership on Linux after running something as root?
The chown -R command changes who owns a file or folder, and everything inside it. You reach for it when you have run a command as root, such as a git pull, an npm install or a build, and your normal user or app can no longer write to its own files.
I learn my automation tips in the AI Profit Boardroom. Have a look here. Affiliate link.
The command
sudo chown -R user:user /var/www/appnamesudo is needed because only root can give a file to another user. chown means change owner. -R makes it recursive, so it works through every file and subfolder under the path. user:user is the new owner, then a colon, then the new group. On Ubuntu each user usually has a group with the same name. The last part is the folder you want to fix.
What you will see
On success chown prints nothing. To see the change, list the folder before and after with ls -l.
$ ls -l /var/www/appname
drwxr-xr-x 2 root root 4096 Oct 9 09:12 dist
-rw-r--r-- 1 root root 812 Oct 9 09:12 package.json
drwxr-xr-x 98 user user 4096 Oct 2 14:40 node_modules
$ sudo chown -R user:user /var/www/appname
$ ls -l /var/www/appname
drwxr-xr-x 2 user user 4096 Oct 9 09:12 dist
-rw-r--r-- 1 user user 812 Oct 9 09:12 package.json
drwxr-xr-x 98 user user 4096 Oct 2 14:40 node_modulesThe third and fourth columns are the owner and group. Before the fix, dist and package.json belonged to root, so the app user could not change them. Afterwards every entry shows user user. Add -c to the command if you want it to print a line for each file it actually changes.
When to use it
- A deploy fails with
EACCES: permission deniedbecause an earliersudo npm installleft root-owned files in the project. - You copied or unpacked a site into
/var/www/sitenameas root and now need your deploy user to own it. - Git refuses to pull because some files in
.gitare owned by root after asudo git pull.
Watch out for
Check the path before you press Enter. A stray space, as in sudo chown -R user:user / var/www/appname, tells chown to start at / and hand the whole server to one user. Unlike rm, chown does not protect the root folder by default, and sudo, SSH and many services will stop working. Adding --preserve-root makes chown refuse to run on /, which is a cheap safety net.
Questions people ask
What is the difference between user and user: and user:user?
user changes only the owner and leaves the group alone. user: with a trailing colon sets the group to that user's login group, and user:user names the group explicitly.
How do I avoid the problem in the first place?
Run project commands as the user who owns the project rather than with sudo. You can switch to that user first, as shown in how to open a shell as another user with sudo.
Changing who owns files across your web folders is the kind of thing you can only do on a server you control. You can get the same VPS with 20% off at https://asksteves.co.uk/vps. Affiliate link.
Want more automation tips? The AI Profit Boardroom on Skool is where I get mine. Have a look here. Affiliate link.
Everything on this blog runs on one Hostinger KVM 8. This link takes 20% off the same box.
Affiliate link.