The Linux permission system is based on the principle of least privilege. This means that users and processes should have access strictly to what they need to work, reducing the attack surface in case of vulnerabilities.
In this practical guide, we cover how to create users, group them, and control access to files and directories using essential terminal commands.
1. Creating and Managing Users
Never run your application or share accounts using the `root` user directly. Create individual accounts with limited privileges.
Useful commands:
2. Creating and Managing Groups
Groups facilitate directory sharing among multiple users (for example, developers who need to edit the same files of a website in WordPress).
How to structure:
1. Create the work group: `sudo groupadd devteam` 2. Add users to the group: `sudo usermod -aG devteam user1` `sudo usermod -aG devteam user2`3. Changing File Owners with chown
Each file and folder in Linux belongs to a user and a group. The `chown` (change owner) command changes this ownership.
Practical use:
To set the `www-data` user and the `devteam` group as owners of a web application folder recursively (all subfolders): `sudo chown -R www-data:devteam /var/www/mysite`4. Managing Permissions with chmod
The `chmod` (change mode) command defines who can read (r), write (w), or execute (x) files and directories.
Permissions are distributed to three classes:
1. Owner (User - u)
2. Group (Group - g)
3. Others (Others - o)
Numerical Representation (Octal):
By summing the values, we generate the permission. For example:
Common chmod examples:
Conclusion
Maintaining proper control of owners and permissions is one of the pillars of security for any Linux server. Applying the correct chown and chmod prevents security breaches of sensitive files without hindering the functioning of your applications.