Changing File Permissions with chmod
Learn how to use the chmod command to change file permissions in Linux. Covers both symbolic and numeric modes with practical examples for securing files and directories.
File permissions are one of the fundamental security features in Linux. Every file and directory has specific permissions that control who can read, write, or execute them. The chmod command is your primary tool for changing these permissions, and understanding how to use it properly is essential for any Linux user.
Understanding Linux File Permissions
Before diving into chmod, let's understand what we're actually changing. When you run ls -l in a directory, you'll see something like this:
-rw-r--r-- 1 user group 1024 Dec 15 10:30 example.txt
drwxr-xr-x 2 user group 4096 Dec 15 10:25 documents/The first column shows the permissions. The first character indicates the file type (- for files, d for directories). The next nine characters represent three sets of permissions:
- Owner permissions (characters 2-4): What the file owner can do
- Group permissions (characters 5-7): What members of the file's group can do
- Other permissions (characters 8-10): What everyone else can do
Each set uses three characters: r (read), w (write), and x (execute). A dash (-) means that permission is not granted.
Using chmod with Symbolic Mode
The symbolic mode uses letters to represent permissions and is often easier for beginners to understand. The basic syntax is:
chmod [who][operator][permissions] filenameHere's what each part means:
- Who:
u(user/owner),g(group),o(others),a(all) - Operator:
+(add),-(remove),=(set exactly) - Permissions:
r(read),w(write),x(execute)
Let's see some practical examples:
# Give the owner execute permission
chmod u+x script.sh
# Remove write permission for group and others
chmod go-w document.txt
# Set read and write for owner, read-only for group and others
chmod u=rw,go=r data.txt
# Make a file executable for everyone
chmod a+x programUsing chmod with Numeric Mode
Numeric mode uses three-digit numbers where each digit represents the permissions for owner, group, and others respectively. Each permission has a value:
r(read) = 4w(write) = 2x(execute) = 1
You add these values together to get the permission number for each category. For example:
7(4+2+1) = read, write, and execute6(4+2) = read and write5(4+1) = read and execute4= read only
Common permission combinations:
# 755: Owner can do everything, group and others can read and execute
chmod 755 script.sh
# 644: Owner can read and write, group and others can read only
chmod 644 document.txt
# 600: Owner can read and write, no permissions for group or others
chmod 600 private.txtWorking with Directories
Directory permissions work slightly differently. For directories:
- Read (r): Can list directory contents
- Write (w): Can create, delete, or rename files in the directory
- Execute (x): Can enter the directory (required to access files inside)
Use the -R flag to change permissions recursively for directories and all their contents:
# Change permissions for directory and everything inside it
chmod -R 755 my_project/Common chmod Examples
Here are some everyday scenarios you'll encounter:
# Make a script executable
chmod +x backup.sh
# Secure a private file (owner read/write only)
chmod 600 ~/.ssh/id_rsa
# Set standard permissions for a web directory
chmod 755 /var/www/html/
# Remove all permissions for group and others
chmod go-rwx sensitive_data.txtWhat's Next
Now that you understand how to change file permissions, the next step is learning about file ownership. Understanding how to use the chown command to change file and directory ownership will complete your knowledge of Linux file security fundamentals.