Linux File Permissions β chown and chgrp Commands Explained
Managing file permissions in Linux is essential for system security, collaboration, and efficient administration. Understanding how to use the chown and chgrp commands ensures data integrity and proper access control. Here we will learn concepts like file ownership, user and group management, and provide practical chown and chgrp command examples .
So Why File Permissions Matter
In Linux, every file and directory has an owner, group, and others, each with specific read (r), write (w), and execute (x) permissions. Proper configuration ensures :
- Security β Prevent unintended access
- Collaboration β Enable group workflows
- Automation β Ensure smoother CI/CD and scripts
So Why File Permissions Matter
In Linux, every file and directory has an owner, group, and others, each with specific read (r), write (w), and execute (x) permissions. Proper configuration ensures :
- Security β Prevent unintended access
- Collaboration β Enable group workflows
- Automation β Ensure smoother CI/CD and scripts
Now let's understand User and Groups which are foundational knowledge .
What Is a User in Linux?
A user in Linux represents an individual account that can log into the system and run processes. Each user has a unique user ID (UID) and typically has a home directory, a shell, and specific permissions.
There are three types of users in Linux :
- Root β The superuser with full system privileges (UID = 0).
- System Users β Used for running system services (like mysql, www-data).
- Regular Users β Created by administrators or during system setup.
Example:
$ whoami
john
This shows the current user is john.
What Is a Group in Linux?
A group is a collection of users. It allows you to assign permissions to a set of users all at once, rather than individually.
Every user belongs to at least one group:
- A primary group (set when the user is created)
- Optional secondary groups (for additional access control)
Why groups matter:
If you want multiple users to read/write to the same directory (e.g., /shared/devops/), putting them in the same group and assigning group permissions to the directory is more efficient.
File Permissions with Users & Groups
When you run ls -l, you'll see output like :
-rw-rw-r-- 1 alice devs 1200 Jul 21 report.txt
- alice: Owner (user)
- devs: Group
-
Permissions:
- Owner: rw- β read/write
- Group: rw- β read/write
- Others: r-- β read-only
This means alice and members of the devs group can edit the file; others can only read it.
How to Change the Owner of a File or Directory
The chown command (short for "change owner") alters the user who owns a file or directory.
Syntax of chown command :
sudo chown newowner filename
Example of chown command :
sudo chown emma report.txt
Now, emma is the owner of report.txt. Use sudo if youβre not the current owner or donβt have permissions.
How to Change the Group Ownership
If you want to keep the current user as owner but switch the group , perhaps to grant access to a broader team - use the chgrp command.
Syntax of chgrp command :
sudo chgrp newgroup filename
Example of chgrp command :
sudo chgrp developers script.sh
This command gives the developers group access to script.sh.
Changing Both Owner and Group at the Same Time
For streamlined ownership management (especially during handovers), you can assign both owner and group in a single command using chown :
Syntax :
sudo chown newowner:newgroup filename
Example:
sudo chown alice:managers presentation.pdf
Here, alice owns the file and managers is the group.
How to Change Ownership Recursively
When working with directories containing many files and subdirectories like a web project or shared folder , changing ownership on each item individually is tedious. Instead, use the -R (recursive) option.
Recursively Change Owner :
sudo chown -R bob /srv/projectx
Assigns bob as the owner for /srv/projectx and everything inside it.
Recursively Change Both Owner and Group :
sudo chown -R emma:designers /home/shared/designs/
Now, emma is the owner and group access is set for designers across all files and subfolders.
Recursively Change Group Only :
sudo chgrp -R admins /opt/teamdocs/
This sets the group to admins for all relevant content in /opt/teamdocs/.
- Use ls -l to view current ownership and permissions before and after changes :
βls -l filename
chown vs chgrp
Both chown and chgrp manage file and directory ownership, but they serve distinct purposes :
-
chown (Change Owner) :
- Alters the user (owner) of a file or directory.
- Can also (optionally) change the group at the same time.
- Useful for transferring files from one user to another or assigning both owner and group in a single step.
Example :
sudo chown alice file.txt
# owner becomes 'alice'
sudo chown alice:developers file.txt
# owner is 'alice', group is 'developers'
- chgrp (Change Group) :
- Changes only the group ownership - the user (owner) stays the same.
- Perfect when you want to grant access to a new group of users without affecting the actual file owner.
Example :
sudo chgrp testers results.csv
# group set to 'testers'
Command | Changes | Typical Use |
---|---|---|
chown | User (owner) and optionally group | Transferring file ownership or handling both at once |
chgrp | Group only | Assigning files to a new or existing group |
Using ls -l to View Ownership and Permissions
One of the best ways to see who owns what and what permissions are set is with the long listing form of ls :
ls -l filename
Letβs break down what you see :
-rw-r--r-- 1 alice devs 1042 Jul 25 09:30 report.txt
How to read this :
- -rw-r--r-- β Permission string (owner: read/write, group: read, others: read)
- 1 β Number of hard links
- alice β File owner (user)
- devs β File group
- 1042 β File size in bytes
- Jul 25 09:30 β Last modified date
- report.txt β Filename
Want to see info on an entire folder ?
ls -l /path/to/folder/
Common Use Cases for chown and chgrp
- Sharing files with a team
-
Migrating projects across accounts
- Bulk permission fixes
Using sudo with Ownership Commands
When to use sudo with chown and chgrp :
- When you need to change the owner of files you donβt own.
- When managing files outside your home directory or system-level files.
- When performing administrative tasks, server setups, or bulk changes.
Example of sudo command :
sudo chown bob:staff project/*
sudo chgrp editors shared-docs/
Learning Linux file permissions and understanding how to use the chown and chgrp commands is crucial for both security and productivity in any multi-user environment. With these tools, we gain fine-grained control over who owns and can access files and directories , making collaborative work safer and system administration more efficient.
In IT administration , it's a routine checking of ownership with ls -l for ensuring that the right people always have the right access at the right time.