How to Use the chmod Command to Secure File Permissions

When you create a bash script or attempt to run a newly downloaded binary on a Linux server, you will frequently encounter a frustrating error message: “Permission Denied.” This occurs because Linux is a fundamentally secure, multi-user operating system. Merely downloading a file does not grant you the right to execute it. To tell the operating system who is allowed to read, write, or execute a file, you must use the chmod (Change Mode) command.

The chmod command modifies file permissions by altering three distinct categories of users. While it can appear mathematically complex at first glance, understanding its logic is the foundational key to Linux system administration.

Understanding the Three Users and Three Permissions

Every single file and directory in Linux has three categories of users attached to it:

  1. User (u): The specific person who owns the file.
  2. Group (g): A specific group of users who share access (e.g., developers).
  3. Others (o): Everyone else on the server; the general public.

For each of these three categories, you can assign three types of permissions:

  • Read (r): The ability to view the contents of a file.
  • Write (w): The ability to modify or delete the file.
  • Execute (x): The ability to run the file as a program or script.

The Symbolic Method: Changing Permissions with Letters

The easiest way to use chmod is the symbolic method, which uses letters to add (+) or remove (-) specific permissions.

Example 1: Making a script executable for yourself
You wrote a script named backup.sh, but you get a “Permission Denied” error when you try to run it. You need to add Execute (x) permissions for the User (u).

chmod u+x backup.sh

This command translates to: Change mode -> for the User -> add Execute -> to backup.sh.

Example 2: Removing read access from the public
You have a file named passwords.txt and you want to ensure that “Others” cannot read it.

chmod o-r passwords.txt

This translates to: Change mode -> for Others -> remove Read -> from passwords.txt.

The Absolute Method: Changing Permissions with Numbers (Octal)

While the symbolic method is easy for quick fixes, system administrators usually use numbers to set all permissions for all three categories simultaneously. This is called the Octal method. Each permission is assigned a numerical value:

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

To grant multiple permissions to a user, you simply add the numbers together. (e.g., Read + Write = 4 + 2 = 6).

A standard chmod command using numbers requires three digits (one for the User, one for the Group, and one for Others).

Example 1: chmod 755 script.sh
Let’s break down this incredibly common permission setting:

  • 7 (User): 4 (Read) + 2 (Write) + 1 (Execute). The owner has total control.
  • 5 (Group): 4 (Read) + 1 (Execute). The group can read and run the script, but cannot modify it.
  • 5 (Others): 4 (Read) + 1 (Execute). Everyone else can also read and run the script, but cannot modify it.

Example 2: chmod 600 private_key.pem
This is the required security setting for SSH private keys:

  • 6 (User): 4 (Read) + 2 (Write). The owner can read and modify the key.
  • 0 (Group): No access whatsoever.
  • 0 (Others): No access whatsoever.

By mastering both the symbolic and octal methods of the chmod command, you guarantee that your sensitive files remain private, while ensuring your critical scripts actually execute when commanded.

Related posts

  1. How to Show the Calendar in Linux Using the cal Command
  2. How to Clear the Bash History in Linux
  3. How to Rename a File in Linux Using the mv Command

Leave a Reply

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

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.

Receive our best articles and tips delivered straight to your inbox.