How to change the permissions of a file or directory in Linux
In this manual, you will learn how to manage file and directory permissions in GNU/Linux systems using the command line. Understanding this system is essential to ensure the security and correct operation of the system.
Prerequisites
Before starting, make sure to:
- Have access to the system terminal.
- Have sufficient permissions (file owner or a user with privileges such as
root).
?? If you do not yet have a Linux server, you can easily deploy one with SW Hosting in a matter of seconds.
1. Permission types
Each file or directory in Linux can have three types of permissions:
- Read (
r): Allows viewing the contents of a file or listing a directory. - Write (
w): Allows modifying a file or adding/removing elements in a directory. - Execute (
x): Allows executing files (scripts or binaries) or accessing a directory.
2. User types
Permissions are assigned to three categories of users:
- User (
u) ? File owner - Group (
g) ? Group associated with the file - Others (
o) ? Other system users
You can also use:
- All (
a) ? Applies to user, group, and others
3. How combinations work
Each user type can have any combination of permissions (r, w, x).
For example:
- The owner can have full control (
rwx) - The group only read access (
r) - Others without permissions (
---)
This allows for very flexible management of resource access.
4. Changing permissions with chmod
The main command for modifying permissions is:
chmod [changes] file_or_directory
?? Only the owner or a user with administrative privileges can modify permissions.
4.1 Symbolic syntax
Letters are used to define users and permissions:
Permissions
r? readw? writex? execute
Users
u? ownerg? groupo? othersa? all
4.2 Practical examples
Give execute permission to the owner:
chmod u+x file
Give full permissions to the owner and group:
chmod ug+rwx file
Remove execute permission for everyone:
chmod a-x file
Give execute permission to other users:
chmod o+x file
Remove write permission for the group:
chmod g-w file
4.3 Applying permissions recursively
To apply changes to a directory and all its contents:
chmod -R u+wx directory
This will modify permissions for all included files and subdirectories.
5. Important considerations
- Changing permissions incorrectly can compromise system security.
- Avoid giving excessive permissions (such as
777) unless strictly necessary. - Perform tests in controlled environments if you are unsure.
Note on octal mode
In addition to symbolic mode, chmod allows the use of numerical values (octal mode) to define permissions. This method is faster in advanced environments and is explained in specific guides.