How to Use the Linux chmod and chown Commands to Manage File Permissions

The Wall of “Permission Denied”

One of the most intimidating hurdles for beginners migrating to a Linux environment is the strict file permission system. In macOS or Windows (for a single home user), if you create a file, you can generally run it, modify it, or delete it without a second thought. Linux, however, was designed from day one as a multi-user server environment. Its default stance is paranoia: files are locked down unless explicit permission is granted.

If you download a script from GitHub and attempt to execute it by typing ./script.sh, you will almost certainly be met with a frustrating error: bash: ./script.sh: Permission denied. If you try to edit a configuration file in the /etc/ directory, your text editor will refuse to save the changes.

To overcome this, you must understand how Linux assigns ownership and permissions, and master the two commands used to control them: chown (Change Owner) and chmod (Change Mode).

Understanding Linux Permissions

Before using the commands, you must understand what you are changing. If you type ls -l in a terminal, you will see a detailed list of files. Look at the confusing string of letters on the far left side, which looks something like this: -rw-r--r--.

This 10-character string is the permission matrix. It is divided into four parts:

  1. Character 1: The file type (a - means a regular file, a d means a directory).
  2. Characters 2-4: The permissions for the User (the specific person who owns the file).
  3. Characters 5-7: The permissions for the Group (a specific group of users).
  4. Characters 8-10: The permissions for Others (everyone else on the system).

Within those three categories (User, Group, Others), the permissions are defined by three letters:

  • r (Read): Permission to open and view the file’s contents.
  • w (Write): Permission to edit or delete the file.
  • x (Execute): Permission to run the file as a program or script. (If it is a directory, ‘x’ means permission to enter the directory).

So, -rw-r--r-- translates to: It is a file (-). The owner can read and write (rw-). The group can only read (r--). Everyone else can only read (r--). No one can execute it.

Changing Ownership with chown

The chown command changes who owns the file. This is frequently necessary when moving files into a web directory (like /var/www/html), where the files must be owned by the web server application (usually a user named www-data or apache) rather than your personal user account.

Because changing ownership affects system security, you almost always need to run this command with sudo (superuser do).

Basic Syntax

sudo chown [new_owner]:[new_group] filename

Examples

To change the owner of report.txt to a user named alice:

sudo chown alice report.txt

To change both the owner to alice and the group to developers simultaneously:

sudo chown alice:developers report.txt

Crucial Flag: If you want to change the ownership of a directory and everything inside it (all subfolders and files), use the recursive flag -R (capital R).

sudo chown -R www-data:www-data /var/www/html/mywebsite/

Changing Permissions with chmod

The chmod command changes what the owner, group, and others are allowed to do with the file. There are two ways to use chmod: Symbolic mode (easier to read) and Numeric mode (faster to type).

Symbolic Mode (+ and -)

You define who you are changing (u for user/owner, g for group, o for others, a for all) and then add (+) or remove (-) the specific permission (r, w, x).

To fix the “Permission Denied” error on our downloaded script, we need to grant the user (owner) the ability to execute (x) it:

chmod u+x script.sh

To completely lock down a private file so that only you (the user) can read it, and no one else (group or others) can even look at it, you remove read permissions:

chmod go-r private_key.pem

Numeric Mode (The 777 System)

Linux administrators rarely use symbolic mode. They use a three-digit number representing the exact permission state. Each permission has a numerical value:

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

You add the numbers together for each category (User, Group, Others) to create a three-digit code.

  • If you want Read + Write (4 + 2), the number is 6.
  • If you want Read + Write + Execute (4 + 2 + 1), the number is 7.
  • If you want Read Only (4), the number is 4.

Therefore, a standard file permission where the owner can do anything (7), but everyone else can only read (4), is written as 744.

chmod 744 script.sh

Warning: The code 777 means the Owner, Group, and literally anyone else on the system has full permission to read, write, and execute the file. While chmod 777 will often “fix” an annoying permission error during troubleshooting, it creates a massive security vulnerability and should never be used on a production server.

Conclusion

Understanding chown and chmod is the gateway to Linux mastery. Once you learn how to read the -rw-r--r-- matrix and understand that chmod 755 grants execute permissions to a script without compromising security, you stop fighting against the Linux operating system and begin working securely alongside it.

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.