πŸ“‘Linux/Unix: Permissions & Ownership
Estimated reading: 3 minutes 28 views

πŸ›‘οΈ 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, and chgrp 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 TypeBase PermissionUmaskFinal Permission
File666022644
Directory777022755

πŸ§ͺ 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

πŸ”΅ Linux/Unix: chmod, chown, chgrp, umask

Or Copy Link

CONTENTS
Scroll to Top