0
0
Linux-cliHow-ToBeginner · 3 min read

How to Use chown Command in Linux: Change File Ownership

The chown command in Linux changes the owner and optionally the group of a file or directory. Use chown owner[:group] filename to set ownership, where owner is the new user and group is optional.
📐

Syntax

The basic syntax of the chown command is:

  • chown [OPTIONS] OWNER[:GROUP] FILE...

OWNER is the new user who will own the file.

GROUP is optional and sets the group ownership.

FILE is the file or directory to change ownership of.

Options like -R apply changes recursively to directories.

bash
chown [OPTIONS] OWNER[:GROUP] FILE...
💻

Example

This example changes the owner of example.txt to user alice and the group to staff. It shows how to use chown with both owner and group.

bash
touch example.txt
ls -l example.txt
sudo chown alice:staff example.txt
ls -l example.txt
Output
-rw-r--r-- 1 currentuser currentgroup 0 date example.txt -rw-r--r-- 1 alice staff 0 date example.txt
⚠️

Common Pitfalls

Common mistakes include:

  • Not using sudo when you lack permission to change ownership.
  • Forgetting to specify the group if you want to change it, or using a colon : incorrectly.
  • Using chown on symbolic links without -h option, which changes the target file instead.

Always check ownership with ls -l after running chown.

bash
sudo chown bob example.txt
# Changes owner only
sudo chown bob: example.txt
# Changes owner and sets group to bob's default group
📊

Quick Reference

CommandDescription
chown alice file.txtChange owner to alice
chown alice:staff file.txtChange owner to alice and group to staff
chown -R alice:staff /folderChange owner and group recursively in /folder
chown -h bob symlinkChange owner of the symlink itself, not the target

Key Takeaways

Use chown owner[:group] file to change file ownership and optionally group.
You need proper permissions or sudo to change ownership.
Use -R to apply changes recursively to directories.
Check ownership changes with ls -l after running chown.
Be careful with symbolic links; use -h to change the link itself.