AI Hints
Home›Linux Tutorial›Running and Owning
Linux · Lesson 68 of 120

Ownership with chown

Who owns a file.

⏱ 2 min read▶ Video at 1:29:12🎓 Free
Watch this lesson in the Linux courseWatch on YouTube ↗Full Linux course

Every file has an owner, and a group. The owner is usually whoever created it. But sometimes you need to hand a file over to someone else, or to a program. That's what CH own does.

Ownership with chown
From the video: Ownership with chown
  • Every file has an owner
  • Usually its creator
  • chown hands it over

Changing the owner

Changing ownership needs admin rights, so we put sudo in front. We'll explain sudo properly in the next lesson. Here's a report, owned by Sam. Sudo CH own alex report dot TXT makes Alex the owner. LS dash L shows alex as the owner, but the group is still sam.

Terminal
ls -l report.txt
-rw-r--r-- 1 sam sam 51 Jan  5 09:00 report.txt
sudo chown alex report.txt
ls -l report.txt
-rw-r--r-- 1 alex sam 51 Jan  5 09:00 report.txt

Owner and group together

You can change the owner and the group in one go, with a colon between them. Here we give the file back to Sam, but put it in the group called users. And CHGRP changes only the group.

Terminal
sudo chown sam:users report.txt
ls -l report.txt
-rw-r--r-- 1 sam users 51 Jan  5 09:00 report.txt
sudo chgrp sam report.txt
ls -l report.txt
-rw-r--r-- 1 sam sam 51 Jan  5 09:00 report.txt

Where you will use it

On your own laptop, you won't change owners very often. But on servers, it's common. For example, a web server runs as its own user, called www data, and it needs to own the website's files. Dash R changes a whole folder at once.

keyvalue
chown user filenew owner
chown user:groupboth at once
chgrp group filegroup only
-Ra whole folder

Summary

CH own changes a file's owner, and the group too with a colon. It needs sudo. Next, what sudo really is, and the root user.

Key takeaways
  • sudo chown alex file
  • chown user:group
  • chgrp
Next up: sudo and root — admin powers