How To Chmod

How To Chmod

Reading time1 min
#Linux#Security#Sysadmin#chmod#Permissions#LinuxPermissions

Mastering chmod: Setting Precise Permissions for Secure Linux Systems

File and directory permissions are the first line of defense in Linux multi-user environments. One careless chmod and you either expose private application secrets or break your build pipeline by locking out legitimate processes. No alert, just headaches: Permission denied at 2 a.m., or worse, an unnoticed data leak.

Understanding the Permission Model

Three sets: user (owner), group, others. Each may have:

  • r (read)
  • w (write)
  • x (execute)

Permissions impact not just file access, but also software behavior:

$ ls -l /usr/local/bin/deploy.sh
-rwxr-x--- 1 deploy ops 2387 Jun 11 00:06 /usr/local/bin/deploy.sh

Above: owner can read/write/execute, group can read/execute, others blocked. A least-privilege default.

Why Not 777?

It’s tempting:

chmod 777 /srv/tmp/*

Suddenly, nobody is blocked. Unfortunately, nobody is protected, either. Even 777 on a harmless temp folder can create escalation vectors for exploits (see CVE-2021-3156 for a notorious example). Critically, permissions are both a convenience and a responsibility.

Symbolic vs Numeric Modes

Both approaches are valid; pick based on clarity.

Symbolic

Set or alter permissions in a human-readable way:

  • u (user), g (group), o (others), a (all)
  • Operators: + (add), - (remove), = (set exactly)
chmod g+w,o-r config.yaml   # Grant group write, remove others' read
chmod u=rw,go= /etc/secret  # Owner can read/write, nobody else allowed

For complex updates with frequent tweaks, symbolic is less error-prone.

Numeric (Octal)

Direct and compact, preferred for automation and scripts.

rwxValue
r--4
-w-2
--x1

Calculate per entity:

  • 7: rwx
  • 6: rw-
  • 5: r-x
  • 4: r--
chmod 750 /opt/data
# Owner: all, group: read/execute, others: none

Numeric is concise for bulk updates — but easy to miscalculate under stress.

Real-World Scenarios and Gotchas

1. Protecting Configuration Files

# PostgreSQL 14 expects config mode 600 on secrets
chmod 600 /etc/postgresql/14/main/server.key

File with looser permissions triggers:

FATAL: private key file has group or world access; permissions should be u=rw (0600) or less

Always verify:

ls -l /etc/postgresql/14/main/server.key

2. Collaborative Code Directories

Shared dev folder:

  • Owners and group (devteam) get all access.
  • Others: none.
chown -R alice:devteam /srv/devproject
chmod -R 2770 /srv/devproject

SetGID (2xxx): new files inherit group automatically — consistency for collaboration.

3. Scripts That Only the Owner Should Run

You want a cron-deployed script, unreadable to others. The mistake:

chmod 700 /usr/local/bin/deploy.sh

That’s readable to owner, not only executable. If you genuinely want "owner execute only" (rare):

chmod 100 /usr/local/bin/deploy.sh

But consider whether this breaks text editors that require read for syntax highlighting.

Special Permission Bits (Advanced Use)

  • SetUID (chmod 4750 bin): runs as file owner, not caller — needed rarely. Only if you trust the binary.
  • SetGID (chmod 2755 dir): files created in dir inherit group. Essential for shared workspaces.
  • Sticky bit (chmod +t /tmp or chmod 1777 /tmp): users can remove only their files, typical on /tmp.

Quick diagnostic:

ls -ld /tmp
# drwxrwxrwt 10 root root 4096 Jun 11 03:55 /tmp

t indicates sticky bit.

Recursive Permissions: Use With Extreme Caution

One wrong chmod -R and you inherit hours of rework. Not all files and dirs require the same permissions (.ssh keys, executable binaries, config dirs). Script granular updates or use find:

find /opt/project -type d -exec chmod 2750 {} \;
find /opt/project -type f -exec chmod 0640 {} \;

Only directories get setgid, files get restrictive read/write.

Subtle Tips from the Trenches

  • Files created by users mirror umask. Check umask defaults before scripting permission fixes.
  • Many editors (e.g., Vim) create temporary swap files with -rw-------. This can break sharing if not accounted for.
  • Backups can change permissions (especially with rsync --perms). Validate after restores.
  • For CI/CD, check that container build systems (like Dockerfiles with COPY) do not weaken intended file modes.

Key Takeaways

  • Avoid global 777; grant only what is needed.
  • Symbolic modes offer clarity; numeric fits scripts.
  • Test permission sets with all relevant accounts, not just root.
  • Special bits (setuid, setgid, sticky) are powerful — and easy to misuse.

Occasionally, you’ll have to trade between slightly broader permissions and system usability—but never make that trade blindly.


Encountered a tough chmod situation with layered containers, package managers, or legacy code? Share your example below.