Understanding File Permissions
File Permissions in Linux
In Linux, every file and directory has a set of permissions that determine who can access, modify, or execute the file. These permissions are defined by three main categories: owner, group, and others.
The permissions for a file or directory are represented by a sequence of 10 characters, such as -rw-r--r--
. The first character indicates the file type (-
for regular file, d
for directory, l
for symbolic link, etc.). The remaining 9 characters represent the read (r
), write (w
), and execute (x
) permissions for the owner, group, and others, respectively.
graph TD
A[File Permissions] --> B(Owner)
A --> C(Group)
A --> D(Others)
B --> B1[Read]
B --> B2[Write]
B --> B3[Execute]
C --> C1[Read]
C --> C2[Write]
C --> C3[Execute]
D --> D1[Read]
D --> D2[Write]
D --> D3[Execute]
Modifying File Permissions
You can use the chmod
command to change the permissions of a file or directory. The command takes an octal or symbolic representation of the permissions you want to set.
Octal representation:
chmod 755 file.txt
sets the permissions to rwxr-xr-x
.
chmod 644 file.txt
sets the permissions to rw-r--r--
.
Symbolic representation:
chmod u+x file.txt
adds execute permission for the owner.
chmod g-w file.txt
removes write permission for the group.
chmod o=r file.txt
sets the permission for others to read-only.
Inheriting Permissions
When you create a new file or directory, the permissions are determined by the parent directory's permissions and the user's default umask value. The umask is a four-digit octal number that represents the permissions that should be removed from the default permissions.
For example, if the default permissions for a new file are 0666
(read and write for owner, group, and others) and the umask is 0022
, the resulting permissions for the new file will be 0644
(read and write for owner, read-only for group and others).