If you are managing a web server or sharing a computer with multiple users, securing your files is critical. Understanding Linux file permissions and knowing how to change them using the chmod command is a fundamental skill for any Ubuntu Linux user. In this guide, we will break down how permissions work and how you can easily manage them.
Understanding Read, Write, and Execute Permissions
In Linux, every file and directory has three specific types of permissions that can be assigned to three different types of users:
- Read (r): Allows the user to open and view the file’s contents.
- Write (w): Allows the user to modify, edit, or delete the file.
- Execute (x): Allows the user to run the file as a program or script.
These permissions are assigned to three user classes:
- Owner (u): The user who created or owns the file.
- Group (g): Other users who are in the file’s assigned group.
- Others (o): Everyone else on the system.
You can check the current permissions of any file by navigating to its directory in the terminal and running ls -l.
How to Change Permissions Using Chmod
The chmod (change mode) command allows you to alter these permissions. The easiest method for beginners is the “symbolic mode”, which uses letters to add (+) or remove (-) permissions.
- Open your Ubuntu terminal.
- To give the owner the ability to execute a script named script.sh, type:
chmod u+x script.sh - To remove write permissions from everyone else (“others”), type:
chmod o-w script.sh - To grant read and write permissions to the group, type:
chmod g+rw script.sh
Frequently Asked Questions
What are numeric permissions in chmod?
Numeric mode (also called absolute mode) uses a three-digit number to set permissions instantly. Read is assigned the value 4, Write is 2, and Execute is 1. You add these numbers together for each user class. For example, chmod 755 file.txt gives the owner full permissions (4+2+1=7), and gives the group and others read/execute permissions (4+1=5).
How do I change permissions for a folder and all its contents?
You can apply permissions recursively to a directory and everything inside it by using the -R flag. For example, chmod -R 755 /var/www/html/.