Linux is fundamentally designed as a multi-user operating system. To ensure that one user cannot accidentally delete another user’s work, or worse, modify core system files, Linux employs a strict permissions system. When a “Permission denied” error halts your work, you must use the terminal to resolve it.
The two most critical commands for managing file security are chown (change owner) and chmod (change mode/permissions). In this guide, you will learn how to use these commands to secure files and directories in Linux.
Understanding Linux Permissions
Before you can change permissions, you must know how to read them. If you run the command ls -l (list long format) in a directory, you will see output resembling this:
-rwxr-xr-- 1 alice developers 4096 Aug 12 10:00 script.sh
Let’s break down the two critical parts of this output:
- Ownership (alice developers): The file is owned by the user alice, and belongs to the group developers.
- Permissions (-rwxr-xr–): This 10-character string dictates exactly who can do what.
- The first character (
-) indicates it is a file (adwould mean directory). - The next three (
rwx) are the Owner’s permissions (Read, Write, eXecute). Alice has full control. - The next three (
r-x) are the Group’s permissions. Anyone in the developers group can read and execute the script, but cannot modify it. - The final three (
r--) are the Others permissions (everyone else). They can only read the file.
- The first character (
How to Use chown (Change Ownership)
If you create a file as the `root` user, standard users will likely be locked out of it. You must change the ownership using chown. Because you are transferring ownership, you almost always need to run this command with sudo.
Syntax: sudo chown [user]:[group] [filename]
Examples:
- To change the owner to “bob”:
sudo chown bob script.sh - To change the owner to “bob” and the group to “admins”:
sudo chown bob:admins script.sh - To change the ownership of an entire directory and every file inside it, use the recursive (-R) flag:
sudo chown -R bob:admins /var/www/html/
How to Use chmod (Change Permissions)
Once the correct person owns the file, you use chmod to define what they are allowed to do with it. There are two ways to use chmod: Symbolic mode and Numeric mode. Numeric mode is faster and preferred by professionals.
Numeric Mode (Octal Format)
Permissions are assigned a numerical value:
- Read (r) = 4
- Write (w) = 2
- Execute (x) = 1
You add these numbers together to create a single digit for the Owner, the Group, and Others. For example, Read + Write (4+2) = 6. Full control (4+2+1) = 7.
Syntax: chmod [Owner][Group][Others] [filename]
Examples:
- chmod 755 script.sh : The Owner gets full control (7). The Group gets read and execute (5). Others get read and execute (5). This is the standard permission for executable scripts.
- chmod 644 document.txt : The Owner can read and write (6). The Group and Others can only read (4). This is the standard permission for standard files.
- chmod 777 public_folder/ : Everyone has full control to read, write, and execute. Warning: This is highly insecure and should only be used for temporary troubleshooting.
By mastering chown and chmod, you can resolve access errors instantly and ensure your Linux server remains secure against unauthorized modifications.