Limited Time Offer!
For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!
The chown
(Change Owner) command in Linux is used to change the owner and group ownership of files and directories. It is particularly useful when managing permissions for multiple users on a system.
Every file in Linux has an owner and a group, as seen in the ls -l
output:
-rw-r--r-- 1 user1 group1 1234 Jan 30 12:00 file.txt
- user1 โ Owner
- group1 โ Group
Now, let’s explore 10 powerful chown
commands with practical explanations.
1. Change the Owner of a File
chown newuser file.txt
โ What it does:
- Changes the owner of
file.txt
tonewuser
. - The group remains unchanged.
๐ Example Before (ls -l file.txt
):
-rw-r--r-- 1 olduser group1 1234 Jan 30 12:00 file.txt
๐ Example After:
-rw-r--r-- 1 newuser group1 1234 Jan 30 12:00 file.txt
๐ก Useful when transferring file ownership to another user.
2. Change the Owner and Group of a File
chown newuser:newgroup file.txt
โ What it does:
- Changes owner to
newuser
and group tonewgroup
.
๐ Before:
-rw-r--r-- 1 olduser oldgroup file.txt
๐ After:
-rw-r--r-- 1 newuser newgroup file.txt
๐ก Ensures both owner and group are modified in one command.
3. Change Only the Group of a File
chown :newgroup file.txt
โ What it does:
- The owner remains unchanged, but the group is changed.
๐ Before:
-rw-r--r-- 1 user1 oldgroup file.txt
๐ After:
-rw-r--r-- 1 user1 newgroup file.txt
๐ก Equivalent to the chgrp
command.
4. Change Owner and Group for Multiple Files
chown newuser:newgroup file1.txt file2.txt
โ What it does:
- Modifies both file1.txt and file2.txt.
๐ Before:
-rw-r--r-- 1 user1 group1 file1.txt
-rw-r--r-- 1 user1 group1 file2.txt
๐ After:
-rw-r--r-- 1 newuser newgroup file1.txt
-rw-r--r-- 1 newuser newgroup file2.txt
๐ก Saves time when modifying multiple files at once.
5. Change Owner for a Directory and Its Contents
chown -R newuser:newgroup mydir/
โ What it does:
-R
โ Applies changes recursively to all files and subdirectories inmydir/
.
๐ Before:
drwxr-xr-x 1 user1 group1 mydir/
-rw-r--r-- 1 user1 group1 file1.txt
-rw-r--r-- 1 user1 group1 file2.txt
๐ After:
drwxr-xr-x 1 newuser newgroup mydir/
-rw-r--r-- 1 newuser newgroup file1.txt
-rw-r--r-- 1 newuser newgroup file2.txt
๐ก Useful when transferring ownership of a project directory.
6. Change Ownership Based on Another Fileโs Ownership
chown --reference=referencefile targetfile
โ What it does:
- Sets
targetfile
ownership to matchreferencefile
.
๐ Example:
chown --reference=existing.txt newfile.txt
newfile.txt
gets the same owner and group asexisting.txt
.
๐ก Quick way to match file ownership settings!
7. Change Ownership Using User ID (UID) and Group ID (GID)
chown 1001:1002 file.txt
โ What it does:
1001
โ User ID (UID)1002
โ Group ID (GID)
๐ Before:
-rw-r--r-- 1 user1 group1 file.txt
๐ After (ls -n file.txt
to check UIDs/GIDs):
-rw-r--r-- 1 1001 1002 file.txt
๐ก Helpful in scripts where UID/GID is known but not the username/group name.
8. Change Ownership for Files Owned by a Specific User
chown newuser:newgroup $(find /path -user olduser)
โ What it does:
- Finds all files owned by
olduser
and transfers them tonewuser:newgroup
.
๐ Example: Change all files owned by john
under /home/projects
:
chown newuser:newgroup $(find /home/projects -user john)
๐ก Useful when deleting a user and reassigning files.
9. Prevent Symbolic Link Issues with -h
chown -h newuser:newgroup symlink
โ What it does:
- Changes the owner of a symbolic link, not the target file.
๐ Without -h
(ls -l symlink
output):
lrwxrwxrwx 1 olduser oldgroup symlink -> targetfile
๐ After using -h
:
lrwxrwxrwx 1 newuser newgroup symlink -> targetfile
๐ก Useful when dealing with symbolic links in /etc
or /usr/local/bin/
.
10. Restrict Ownership Transfer to Superuser Only
chown root file.txt
โ What it does:
- Transfers file ownership to root.
- Can only be executed by a superuser (root).
๐ Before:
-rw-r--r-- 1 user1 group1 file.txt
๐ After (ls -l
output):
-rw-r--r-- 1 root group1 file.txt
๐ก Important for securing critical system files.
Final Thoughts
chown
is essential for managing file ownership and permissions.- Always use
-R
carefully, especially on/etc/
,/home/
, or/var/
directories. - Changing ownership to
root
can restrict user access.