What chown
does
· Changes the owner (user) and/or group of files/directories.
· Only root (or a process with the right capability) can change the owner.
· The owner of a file can usually change its group to any group they’re a member of.
Check current ownership:
ls -l file
stat -c '%U %G %n' file # shows owner (%U) and group (%G)
Basic syntax
chown [OPTIONS] OWNER[:GROUP] FILE...
chown [OPTIONS] :GROUP FILE... # change group only
chown [OPTIONS] --reference=REF FILE... # copy owner:group from REF
Examples
sudo chown alice file.txt # set owner to alice
sudo chown alice:devs file.txt # owner=alice, group=devs
sudo chown :devs file.txt # only group → devs
sudo chown -R alice:devs project/ # recursive on a tree
sudo chown --reference=/etc/hosts a.txt # match owner:group of hosts
Options you’ll actually use (GNU chown
)
·
-R
: recursive (directories and their contents).
·
-v
/ -c
:
verbose / report changes only.
·
--from=CUR:GRP
: change only if current owner:group
matches (safe in scripts).
·
--reference=FILE
: copy owner:group from another file.
·
--preserve-root
(default): refuse to act on /
(safety).
· Symlink handling (important):
o
-h
/ --no-dereference
: act on the symlink itself.
o Recursive traversal control:
§ -H
: follow symlinks listed on command line.
§ -L
: follow all symlinks during
recursion.
§ -P
(default): do not follow
symlinks during recursion.
o
If unsure, stick
to the default (-P
) to avoid unexpected changes via links.
·
--numeric-ids
: interpret OWNER/GROUP as numeric IDs (UID/GID), not
names.
Owner vs Group (quick refresher)
· Every file has one owner (user) and one group.
·
Permissions are
applied to owner / group / others (see chmod
notes).
· For team folders, combine group ownership with setgid on the directory so new files inherit the group:
sudo chgrp devs /srv/app
sudo chmod 2775 /srv/app # 's' on group → inherit group
sudo chown -R :devs /srv/app/*
Common patterns
# Make a whole project owned by a service account:
sudo chown -R www-data:www-data /var/www/myapp
# Give your user ownership of a copied dataset:
sudo chown -R $USER:$USER ~/datasets
# Only switch group (you are a member of 'devs'):
chown :devs script.sh # (no sudo needed if you own the file)
Typical errors & fixes
·
“Operation
not permitted”: you’re not root
and trying to change owner; use sudo
.
·
“invalid
group” / “invalid user”: the
name doesn’t exist; check /etc/passwd
& /etc/group
or getent passwd/group
.
·
On WSL/Windows
drives (/mnt/c
):
ownership may not behave as expected (NTFS). Mount with metadata support or
work inside your Linux home (~
) for proper UNIX
ownership.
Good scripting habits
# Change only if currently owned by 'root:root'
sudo chown --from=root:root -R alice:devs /data/shared
# Copy ownership from a template file (idempotent & readable)
sudo chown --reference=/var/www . -R
Mini-lab (10–15 min)
mkdir -p ~/lab/chown && cd ~/lab/chown
echo "data" > a.txt
ls -l a.txt
# make a group folder (simulate with your user + group)
sudo groupadd devs 2>/dev/null || true
sudo chgrp devs a.txt
ls -l a.txt # owner stays you; group → devs
# recursive change on a tree
mkdir -p proj/{src,logs}
touch proj/src/app.py proj/logs/app.log
sudo chown -R $USER:devs proj
stat -c '%U %G %n' proj proj/src/app.py proj/logs/app.log
Exam-ready bullets
·
Purpose: change owner and/or group:
chown OWNER[:GROUP] file
.
· Root required to change owner; owners can usually change group to one they belong to.
·
Recursive: -R
; symlink behavior: default don’t follow
(-P
), use -h
, -H
, -L
intentionally.
·
Reference
& conditional: --reference=FILE
, --from=OLD:OLDG
.
·
Numeric
IDs: --numeric-ids
to force UID/GID.
·
Use with team
dirs + setgid (chmod 2775 dir
) so new
files inherit the group.