chown
Give a file or a whole folder to another user and group with chown, and why it needs sudo.
>_ Try it: a simulated Linux, right herenothing runs on your computer
Loading the terminal…
Examples
-
sudo chown deploy /srv/site/index.html -
sudo chown -R deploy:deploy /srv/site -
ls -l /srv/site -
touch /srv/site/about.html -
sudo chown www-data:www-data /srv/site/about.html -
ls -l /srv/site
How it works
Every file has an owner and a group: ls -l shows them in the third and fourth columns. The owner decides who may change it (with chmod).
chown USER FILE gives the file to USER; chown USER:GROUP sets both; -R does a folder and everything inside. Only root may give files away, so on a server it is sudo chown.
Syntax
sudo chown [-R] USER[:GROUP] FILE...
Options
| Option | What it does |
|---|---|
USER | the new owner |
USER:GROUP | the new owner and group at once |
-R | recursive: the folder and everything inside it |
Good to know
- "Permission denied" in a folder usually means it belongs to someone else: check with
ls -ld FOLDERbefore reaching forsudoevery time. - Be careful with
-Ron system folders:sudo chown -Ron the wrong path can break a server.