In Linux, every file and directory has a strict set of permissions that dictates who can read it, write to it, or execute it. Understanding how to view and change these permissions using the chmod (change mode) command is a fundamental skill for any Linux user or server administrator.
Understanding the Permission Structure
Before you change permissions, you need to know what they currently are. Run the following command in your terminal:
ls -l filename.txt
The output will begin with a 10-character string that looks something like this: -rwxr-xr--. The first character indicates the file type (a hyphen means it’s a regular file, a ‘d’ means directory). The next nine characters are broken down into three sets of three:
- Owner (User – u): The first three characters (e.g.,
rwx) show what the file’s owner can do. - Group (g): The next three (e.g.,
r-x) show what users in the file’s assigned group can do. - Others (o): The final three (e.g.,
r--) show what everyone else on the system can do.
The letters stand for Read, Write, and eXecute.
Method 1: The Symbolic Mode (Letters)
The easiest way to use chmod is with letters. You specify who you are targeting, what action to take, and which permission to change.
- Targets:
u(user/owner),g(group),o(others),a(all) - Action:
+(add),-(remove),=(set exactly) - Permissions:
r(read),w(write),x(execute)
Examples:
- Make a script executable for the owner:
chmod u+x script.sh - Remove write access for the group:
chmod g-w document.txt - Give everyone read permission:
chmod a+r public_file.html - Set exact permissions for others to read-only (removing any write/execute rights):
chmod o=r config.yaml
Method 2: The Numeric (Octal) Mode
Professionals often prefer the numeric method because it allows you to set permissions for the User, Group, and Others simultaneously using a three-digit number. Each permission has a number value:
- Read (r) = 4
- Write (w) = 2
- Execute (x) = 1
- No Permission = 0
To set the permission, you add the numbers together. For example, Read + Write = 6 (4+2). Read + Write + Execute = 7 (4+2+1). You write three digits in a row corresponding to User, Group, and Others.
Examples:
chmod 755 filename: The owner gets everything (7), the Group and Others get Read and Execute (5). This is standard for executable scripts and web directories.chmod 644 filename: The owner can Read and Write (6), the Group and Others can only Read (4). This is standard for regular text files or HTML files.chmod 600 filename: The owner can Read and Write (6), and absolutely no one else has any access (0). This is highly secure and used for private keys like SSH files.