Changing file permissions in Linux
When working in a Linux environment, it is common to encounter situations where you need to change file permissions. This guide will help you understand how to modify file permissions using the chmod
command.
Understanding file permissions
Linux file permissions are broken down into three types:
- User (u): The owner of the file.
- Group (g): The group that owns the file.
- Others (o): Everyone else.
Permissions are also divided into three categories:
- Read (r): Permission to read the file.
- Write (w): Permission to modify the file.
- Execute (x): Permission to execute the file.
Viewing current permissions
To view the current permissions of a file, use the ls -l
command:
The output will look something like this
Here’s what each part means:
-
rwxr-xr-x
: The permissions for the file, broken down as follows:rwx
: The permissions for the user (owner) of the file.r
(read): The user can read the file.w
(write): The user can write to (modify) the file.x
(execute): The user can execute the file.
r-x
: The permissions for the group that owns the file.r
(read): The group members can read the file.-
(no write permission): The group members cannot write to (modify) the file.x
(execute): The group members can execute the file.
r-x
: The permissions for others (everyone else).r
(read): Others can read the file.-
(no write permission): Others cannot write to (modify) the file.x
(execute): Others can execute the file.
-
1
: Number of hard links. -
user
: The owner of the file. -
group
: The group that owns the file. -
4096
: The size of the file in bytes. -
Jun 17 12:34
: The last modification date and time. -
filename
: The name of the file.
Changing permissions
The chmod
command is used to change the permissions of a file or directory.
Using symbolic mode
Symbolic mode uses letters to represent the permissions. Here are some examples:
Add read permission for the user:
Remove write permission for the group:
Add execute permission for others:
Add read, write, and execute permissions for the user, and read and execute permissions for the group and others
Understanding numerical values for permissions
File permissions in Linux can also be represented using numerical (octal) values. Each permission type is assigned a specific value:
4
(read)2
(write)1
(execute)
These values are combined to form the total permission for the user, group, and others. The permissions are specified in the order: owner, group, and others.
Here is a detailed breakdown of the numerical values:
400
: Read by owner040
: Read by group004
: Read by anybody200
: Write by owner020
: Write by group002
: Write by anybody100
: Execute by owner010
: Execute by group001
: Execute by anybody
Example calculation
Summing up all the values above gives us 775
. Therefore, it grants:
- Read, write, and execute permissions to the owner (4+2+1=7)
- Read and execute permissions to the group (4+1=5)
- Read and execute permissions to others (4+1=5)
To set permissions, add the values together. For example, to set read, write, and execute permissions, use 7 (4+2+1). Here are some examples:
Set read, write, and execute permissions for the user, and read and execute permissions for the group and others (755):
Set read and write permissions for the user, and read permissions for the group and others (644):
Recursively changing permissions
To change permissions for a directory and all its contents, use the -R option: 755 grants all permissions (read, write, execute) to the user (owner) but only read and execute permissions to the group and others.
Conclusion
Understanding and managing file permissions is essential for maintaining security and proper access control in a Linux environment. With the chmod
command, you can easily modify permissions to suit your needs.