Mastering File Permissions in Linux: A Beginner’s Guide
When working with Linux, understanding and managing file permissions is essential for maintaining security and controlling access. This guide will break down file permissions, explain how to view them, and show you how to change them with simple commands.
Understanding Linux File Permissions
In Linux, every file and directory has an associated set of permissions that define who can read, write, or execute them. There are three types of users in Linux:
- Owner: The user who owns the file or directory.
- Group: A set of users with similar permissions.
- Others: All other users on the system.
File Permission Structure
File permissions in Linux are typically represented by a series of symbols. For example:
ls -ltr
You will see the result like this
drwxr-xr-x 3 shanikr shanikr 4096 Feb 7 2024 django_projects
_rwxr-xr-x 2 shanikr shanikr 4096 Feb 15 2024 docker-compose.yaml
drwxr-xr-x 2 shanikr shanikr 4096 Feb 15 2024 Documents
_rwx------ 3 shanikr shanikr 4096 Feb 18 2024 robots.txt
drwxr-xr-x 2 shanikr shanikr 4096 Jul 29 00:31 deploy_flask
Each section in this string represents permissions for a different type of user:
- The first character (-) indicates the file type (- for files, d for directories).
- The next three characters (rwx) indicate permissions for the owner.
- The following three (r-x) are permissions for the group.
- The last three (r--) represent permissions for others.
Here's a breakdown of permission types:
- r (read) – Allows viewing the contents of the file.
- w (write) – Allows modifying the file.
- x (execute) – Allows running the file as a program.
Viewing File Permissions
To check the permissions of a file or directory, use the ls -l command:
ls -l filename
This command displays detailed information about the file, including its permissions.
Changing Permissions with chmod
The chmod command is used to change permissions. You can specify permissions in two ways:
1. Symbolic Mode
In symbolic mode, you specify the type of users (u for owner, g for group, and o for others) and the permission changes. For example:
Add execute permission to the owner:
chmod u+x filename
Remove write permission from the group:
chmod g-w filename
Give all users read permission:
chmod a+r filename
Give recursively permission to a folder
chmod -R g+w folder
2. Octal Mode
In octal mode, permissions are represented by numbers. Each permission type has a numeric value:
- Read (r) = 4
- Write (w) = 2
- Execute (x) = 1
To set permissions, add up the values. For example, to give the owner full permissions and only read permission to group and others:
chmod 744 filename
This breakdown is:
- 7 (4+2+1) for owner – read, write, and execute
- 4 for group – read-only
- 4 for others – read-only
Here are some common chmod octal values:
- 777 – Full permissions for everyone.
- 755 – Full permissions for owner, read and execute for group and others.
- 644 – Read and write for owner, read-only for group and others.
Changing Ownership with chown
The chown command changes the ownership of files and directories. Use it to assign a new owner or group:
Change only owner of a file
chown newowner filename
Change owner and group of a file
chown newowner:newgroup file
Change only group of a file
chown :newgroup file
Change recursively ownership of a folder
chown -R newowner folder
Changing Group ownership with chgrp
Change Group Ownership of a File
chgrp newgroup file
Change Group Ownership of a Directory
chgrp newgroup directory
Change Group Ownership Recursively
chgrp -R newgroup directory
Keep learning keep growing!
Comments