The chown command
The chown command changes who owns a file…
The chown command changes who owns a file: sudo chown sam report.txt gives the file to sam, and sudo chown sam:staff report.txt sets the group too.
Taught in Advanced, lesson 41
A practice shell in your browser that behaves like bash on Linux for these commands. Nothing you type leaves the page.
Usage
chown [-R] OWNER[:GROUP] FILE...
Change who owns a file. Gives files to another owner and, after a colon, another group. Only root can give a file away, so you usually need sudo. A web server can read files only if its user (www-data) is allowed to, so chown and chmod come up often on servers. Everything here is simulated in the page: no real accounts, passwords or software are involved.
In the practice terminal:The users are the practice machine’s own: root, www-data and you (learner); sudo useradd adds more. Nothing changes on your computer.
How to use chown
Give a file to another user
Changing the owner needs sudo, even for your own files. ls -l shows the owner and the group in its third and fourth columns.
sudo chown www-data report.txt && ls -l report.txt[sudo] password for learner: -rw-r--r-- 1 www-data www-data 35 Jan 12 09:00 report.txt
Set the owner and group: chown user:group
user:group sets both, and :group alone just the group, as chgrp does.
sudo chown root:adm report.txt && ls -l report.txt[sudo] password for learner: -rw-r--r-- 1 root adm 35 Jan 12 09:00 report.txt
A whole folder: chown -R
-R changes the folder and everything in it. Web servers often need sudo chown -R www-data:www-data /var/www/site.
sudo chown -R www-data:www-data website && ls -l website[sudo] password for learner: total 4 -rw-r--r-- 1 www-data www-data 14 Jan 12 09:00 index.html
Without sudo
chown root report.txtchown: changing ownership of 'report.txt': Operation not permitted
Options
| -R | everything inside folders too |
|---|---|
| -v | say what changed |
More examples
touch shared.txtsudo chown root:adm shared.txt[sudo] password for learner:
ls -l shared.txt-rw-rw-rw- 1 root adm 24 Jan 12 09:01 shared.txt
Questions about chown
What is the difference between chown and chmod?
chown changes who owns a file; chmod changes what the owner, the group and everyone else may do with it.
Why does chown say "Operation not permitted"?
Only root can give a file away, so run it with sudo. An ordinary user may only change a file’s group, to one of their own groups.
Why does chown say "invalid user"?
No account has that name. Check with id name, or create the account first with sudo useradd.
Learn it step by step
Related commands
Every everyday command on one page, to print or keep open: the Linux commands cheat sheet.