File permissions define who can read, write, or execute a file. In Linux/Unix-like systems, file permissions are set for three different groups of users: the owner of the file, the group associated with the file, and others.
- Read (r): Allows the file to be opened and read.
- Write (w): Allows modifying or deleting the file.
- Execute (x): Allows executing the file as a program.
- User (u): The owner of the file.
- Group (g): A set of users who share the same group.
- Others (o): All other users on the system.
File permissions are often displayed as a string of characters such as: --rwxr-xr--
This string consists of 10 characters, with the following breakdown:
-
The first character represents the file type:
-for a regular filedfor a directorylfor a symbolic linkbBlock device (e.g., hard drives, USB drives)cCharacter device (e.g., keyboards, mice)
-
The remaining 9 characters represent the permissions for user (owner), group, and others in the following order:
- User (Owner): The first three characters
- Group: The next three characters
- Others: The last three characters
For example, rwxr-xr-- means:
- User: rwx (read, write, and execute permissions)
- Group: r-x (read and execute permissions)
- Others: r-- (read permission)
Permissions can also be represented by numeric values:
| Permission | Numeric Value |
|---|---|
| Read (r) | 4 |
| Write (w) | 2 |
| Execute (x) | 1 |
Each permission type can be assigned a numeric value, and the sum of those values determines the permission setting. For example:
rwx= 4 + 2 + 1 = 7r-x= 4 + 0 + 1 = 5r--= 4 + 0 + 0 = 4
Thus, the permission rwxr-xr-- corresponds to 755 in numeric form.
You can use the chmod command to change file permissions.
- Add execute permission for user:
- Set permissions to `rwxr-xr--` (755):
- Set permission `rw-r--r--` (644):
- Set permission `rwx------` (700) (Only the owner can read, write, and execute):
- Set permission `rwxrwxrwx` (777) (Everyone has full access):
-
Symbolic:
chmod u+x file.txt(adds execute permission to the user). -
Numeric:
chmod 755 file.txt(sets permissions torwxr-xr--).
To check file permissions, use the ls -l command:
ls -l /path/to/fileExample output:
drwxr-xr-x 2 user user 4096 Feb 9 10:30 my_folder
-rw-r--r-- 1 user user 1024 Feb 9 10:30 file.txt
lrwxrwxrwx 1 user user 10 Feb 9 10:30 link -> file.txtUse the chmod command to modify file permissions:
Symbolic Method
chmod u+x file.txt # Add execute permission for userchmod g-w file.txt # Remove write permission for groupchmod o+r file.txt # Add read permission for otherschmod a+x file.txt # Add execute permission for everyoneNumeric Method
chmod 644 file.txt # rw-r--r--chmod 755 script.sh # rwxr-xr-xchmod 777 public.sh # rwxrwxrwx (full permissions)Use the chown command to change the owner and group of a file:
chown username file.txt # Change ownerchown username:groupname file.txt # Change owner and groupTo check the current default permissions:
umask
To set a new default permission (e.g., 022 for 755 permissions):umask 022