Back to Blog
Lesson 17 of the Linux: Linux Command Line for Developers course
LinuxAugust 4, 20264 min read

Modifying Permissions with Chmod: A Developer's Linux Guide

Learn how to use chmod to modify Linux file permissions. Master symbolic and numeric modes to secure your server project files with precision.

linuxcommand-linesecuritypermissionschmod
A close-up shot of a hand holding a penguin sticker against a blurred outdoor background.

Previously in this course, we covered File Permissions Fundamentals: Decoding Linux Access Controls, where you learned how to interpret the rwx strings returned by ls -l. Now, we move from observation to action: you’ll learn how to modify those permissions using chmod to harden your server environment.

The chmod (change mode) command is your primary tool for defining who can read, write, or execute files. Without it, you cannot restrict access to sensitive configuration files or enable scripts to run as executables.

Changing Permissions with Symbolic Mode

Symbolic mode uses letters to represent the user (u), group (g), others (o), and all (a), combined with operators to add (+), remove (-), or set (=) permissions. This is often more readable than numbers.

To change permissions for the owner, group, or others, follow this syntax: chmod [who][operator][permission] [file].

For example, if you have a shell script in your web server project that isn't currently executable, you can grant execute permissions to the owner like this:

Bash
# Grant execute permission to the user (u)
chmod u+x deploy.sh

# Remove write permissions from everyone else
chmod o-w config.json

Mastering Numeric Mode for Precise Control

Macro shot of camera mode and control dials with various settings.

Numeric mode is the "pro" way to set permissions. It uses a three-digit octal value (e.g., 755), where each digit represents the sum of permissions for the owner, group, and others respectively.

  • 4 = Read (r)
  • 2 = Write (w)
  • 1 = Execute (x)
  • 0 = No permission

By adding these numbers, you create the permission set. 7 (4+2+1) means full access, while 5 (4+0+1) means read and execute.

Numeric ModeMeaningUse Case
600Owner read/write onlySensitive config/SSH keys
644Owner RW, others readStandard web content files
755Owner RWX, others RXDirectories and executable scripts

To apply this to our project's configuration file:

Bash
# Sets owner to RW, group to R, others to R
chmod 644 config.json

Applying Recursive Permissions

When managing a web server, you often need to set permissions for an entire directory tree. Using the -R (recursive) flag allows you to apply changes to a directory and everything inside it.

Warning: Use this carefully. You rarely want to make every file in a directory executable.

Bash
# Make all directories within /var/www/html searchable (755)
# This is a common requirement for web server access
chmod -R 755 /var/www/html/public

Hands-on Exercise: Securing Your Web Project

  1. Navigate to your project directory.
  2. Create a dummy secret file: touch secret_config.env.
  3. Set the permissions so only you can read and write it (600).
  4. Verify the change with ls -l secret_config.env. The string should look like -rw-------.
  5. Create a scripts directory and ensure your owner has full control while others have none (700).

Common Pitfalls

Close-up of a triangular warning sign indicating a slippery surface, fixed to a wooden post.

  • The "Everything is 777" Trap: Beginners often run chmod -R 777 to fix "Permission Denied" errors. This is a massive security risk. It allows any user on the system to modify or delete your files. Always use the principle of least privilege (give only the access required).
  • Directory Execution: Directories require the "execute" (x) permission to be traversed. If you remove x from a directory, you cannot cd into it, even if you have read access.
  • Recursive Overkill: Be careful running chmod -R on files. If you change a file's permission to 777 because you meant to change a directory, you've left that file vulnerable to modification by any malicious actor.

FAQ

Q: What is the default permission for new files? A: This is controlled by the umask setting, which subtracts permissions from the system default. We will cover this in detail in a future lesson on environment configurations.

Q: Can I change permissions for a file I don't own? A: Only the owner of the file or the root user can change permissions using chmod. If you are not the owner, you will need sudo.

Q: Is 755 always the best for directories? A: It is standard for web servers because it allows the server process to enter the directory and read the files. However, if a directory contains sensitive user data, you should use 700 or 750.

Recap

Team members presenting a project in a modern office setting with a focus on collaboration.

You’ve mastered chmod by using symbolic modes for quick changes and numeric modes for precision. You also learned how to handle directory structures recursively and, most importantly, why you should avoid 777 at all costs. Keeping your config files set to 600 is your first real step toward a hardened production environment.

Up next: We will look at changing file ownership with chown to ensure your web server process only has access to the files it absolutely needs.

Similar Posts