1 November 2026 · One command a day
How do I run commands in a root-owned folder without changing ownership?
A folder owned by root, such as /etc/nginx, /opt/app or /srv/backups, refuses writes from your normal account with Permission denied. The tempting fix is chown, but there are better options that leave root as the owner. Which one fits depends on whether you need to do one job once, or whether a user needs to work in that folder every day.
I learn my automation tips in the AI Profit Boardroom. Have a look here. Affiliate link.
The commands
# one command, as root
sudo cp site.conf /etc/nginx/sites-available/
# one command, as the account the folder or app belongs to
sudo -u www-data php /var/www/example.com/cron.php
# write into a root-owned file (redirection needs tee)
echo "10.0.0.5 db" | sudo tee -a /etc/hosts >/dev/null
# give one user lasting access without touching the owner
sudo setfacl -R -m u:alice:rwX /srv/backups
sudo setfacl -R -d -m u:alice:rwX /srv/backupsOption 1: sudo for a one-off
sudo runs a single command as root and then drops back to your own account. For a one-off copy, edit or restart, that is all you need, and nothing about the folder changes. Anything sudo creates is owned by root, which is right inside a root-owned folder. For an edit, sudoedit /etc/nginx/nginx.conf opens a temporary copy in your own editor and writes it back as root when you save.
Option 2: sudo -u to act as the right user
If the folder or app belongs to a service account such as www-data or a site user, run the command as that account with sudo -u. Files it writes get that user as owner, so you never need to fix ownership afterwards. Running npm install or git pull with plain sudo is how root-owned files end up inside a site folder in the first place. For a shell as that user, see how to open a shell as another user with sudo.
Option 3: sudo tee when you need a redirect
This fails even with sudo:
sudo echo "10.0.0.5 db" >> /etc/hosts
bash: /etc/hosts: Permission deniedThe >> is handled by your own shell, as your own user, before sudo runs. Only echo got root. Pipe into sudo tee instead: tee is the program that opens the file, and it runs as root. Use tee -a to append and plain tee to overwrite. Adding >/dev/null stops tee printing the line back at you. The other way is to give the whole line to a root shell: sudo sh -c 'echo "10.0.0.5 db" >> /etc/hosts'.
Option 4: a shared group
When several people or a deploy account need to write in the folder every day, keep root as the owner and change only the group:
sudo groupadd deploy
sudo usermod -aG deploy alice
sudo chgrp -R deploy /srv/app
sudo chmod -R g+rwX /srv/app
sudo find /srv/app -type d -exec chmod g+s {} +The capital X adds execute only on folders, and on files that are already executable, so ordinary files do not become runnable. The g+s bit on folders makes new files inherit the deploy group. Group membership only takes effect at the next login, so alice has to log out and back in, or run newgrp deploy. The owner stays root, so ls -l shows root deploy. Note that each user's umask decides whether new files are group-writable; the common default of 022 makes them read-only for the group.
Option 5: ACLs with setfacl
An access control list adds permissions for one named user or group on top of the normal owner, group and other bits. Neither the owner nor the group changes. The first setfacl line in the commands above gives alice read and write on everything that exists now. The second, with -d, sets a default ACL on the folders, so files created later get the same access. Check the result with:
getfacl /srv/backups
# file: srv/backups
# owner: root
# group: root
user::rwx
user:alice:rwx
group::r-x
mask::rwx
other::r-x
default:user::rwx
default:user:alice:rwx
...ls -l shows a + at the end of the permissions when an ACL is set. If setfacl is missing, install it with sudo apt install acl. Watch the mask line: running chmod g-w on the folder lowers the mask, and that quietly limits alice's ACL entry too. Remove her access later with sudo setfacl -R -x u:alice /srv/backups and sudo setfacl -R -d -x u:alice /srv/backups.
When chown is the wrong fix
- System folders.
/etc,/usrand/var/libare owned by root on purpose. Some programs check it: sudo refuses to run if/etc/sudoersis not owned by root, and package upgrades expect their files to stay as they were installed. - App code the app runs as. If the web server user owns the code it serves, a bug that lets an attacker write files can rewrite your site. Give it write access only to the folders that need it, such as uploads or cache.
- Recursive chown on the wrong path. A stray space, as in
chown -R alice / srv/app, starts changing the whole system. If it has already happened inside a site folder, see how to fix file ownership after running something as root.
chown is the right tool when the files really belong to someone else, for example a site folder that should be owned by its site user, and root-owned files got in by mistake.
Questions people ask
Should I use chmod 777 instead?
No. 777 lets every account on the server, including every service, write there. A group or an ACL gives access to exactly the users who need it.
Group or ACL: which should I pick?
Use a group when a team or a deploy account needs the same access and you are happy to change the folder's group. Use an ACL when you want to leave owner and group exactly as they are, or need different access for different users.
You can only set up users, groups and ACLs like this on a server where you have root. 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.