π‘οΈ Linux/Unix: chmod
, chown
, chgrp
, umask
β Control File Permissions and Ownership
π§² Introduction β Why Learn chmod, chown, chgrp, and umask?
Linux/Unix systems are multi-user environments. Managing file access securely and effectively requires a solid understanding of permissions and ownership commands. Tools like chmod
, chown
, chgrp
, and umask
let you define who can read, write, or execute filesβand under what defaults.
π― In this guide, youβll learn:
- How to use
chmod
,chown
, andchgrp
to set permissions and ownership - What
umask
is and how it sets default permissions - Examples of how to control access in real-world scenarios
π οΈ chmod
β Change File/Directory Permissions
β Syntax:
chmod [options] mode filename
π Description:
Modifies permissions for user (u
), group (g
), and others (o
) using symbolic or numeric modes.
π’ Numeric Examples:
chmod 755 script.sh # rwxr-xr-x
chmod 644 file.txt # rw-r--r--
chmod 700 private.sh # rwx------
π§Ύ Symbolic Examples:
chmod u+x filename # Add execute to user
chmod go-w file.txt # Remove write from group and others
chmod a+r notes.md # Add read permission for everyone
π€ chown
β Change File Ownership
β Syntax:
chown [options] user[:group] filename
π Description:
Changes the owner and optionally the group of a file.
π§ͺ Examples:
chown john file.txt # Change owner to john
chown john:devteam report.csv # Change owner and group
sudo chown -R root:admin /opt/app # Change recursively
β Commonly used by admins for managing system/user-owned files.
π₯ chgrp
β Change Group Ownership
β Syntax:
chgrp [options] group filename
π Description:
Changes the group assigned to a file without changing the owner.
π§ͺ Examples:
chgrp developers code.c
sudo chgrp -R www-data /var/www/
π Use ls -l
to confirm changes.
π§° umask
β Set Default File Permissions
β Syntax:
umask [value]
π Description:
Defines default permission “masks” for new files and directories created by a user.
π Default Behavior:
File Type | Base Permission | Umask | Final Permission |
---|---|---|---|
File | 666 | 022 | 644 |
Directory | 777 | 022 | 755 |
π§ͺ View and Set Umask:
umask # Show current value
umask 027 # Set new umask
π§ umask
subtracts permissionsβso 022 removes write from group/others.
π Real-World Use Case
Scenario:
You want to ensure scripts in /scripts
are:
- Owned by user
admin
- Executable by everyone
- Group-owned by
developers
π§ Commands:
sudo chown admin scripts/myscript.sh
sudo chgrp developers scripts/myscript.sh
chmod 755 scripts/myscript.sh
π Summary β Recap & Next Steps
These commands give you full control over who owns a file and who can access it. Whether setting strict security or collaborating in a team, understanding chmod
, chown
, chgrp
, and umask
ensures safe and organized permission management.
π Key Takeaways:
chmod
sets who can read, write, or execute files.chown
changes the file owner;chgrp
sets group ownership.umask
controls the default permissions when new files are created.- Use numeric (
755
) or symbolic (u+x
) permission formats.
β FAQs
β What is the difference between chown
and chgrp
?
β
chown
changes the owner (and optionally the group). chgrp
changes only the group.
β What are safe permission values for scripts?
β
Use:
chmod 755 script.sh
This makes it executable for all, editable only by the owner.
β How do I change permissions recursively?
β
Use:
chmod -R 755 /var/www
β What does a umask
of 0022 mean?
β
It means new files will get 644
and directories 755
. Write is removed for group and others.
β Can I set permanent umask
?
β
Yes, add umask 027
to your shellβs config file like ~/.bashrc
.
Share Now :