Linux File Permissions
Linux File Permissions
What Are Linux File Permissions?
In Linux, every file and directory has permissions that control who can read, write, or execute it.
This ensures security and controlled access in a multi-user environment.
Basic Permission Types
| Symbol | Permission | Description | Example |
|---|---|---|---|
| r | Read | View file contents or list directory | cat file.txt |
| w | Write | Modify or delete the file | echo "Hi" > file.txt |
| x | Execute | Run file as a program/script or access directory | ./script.sh |
Permission Groups
| Group | Description | Example |
|---|---|---|
| User (u) | The file owner | Usually the person who created it |
| Group (g) | Other users in the same group | e.g., developers group |
| Others (o) | All other users | Not owner or group members |
Common Permission Examples
Command | Meaning |
chmod 644 file | Owner can read/write, others read |
chmod 600 file | Owner only can read/write |
chmod 755 dir | Owner full access, others read/execute (good for scripts) |
chmod 700 file | Only owner can read/write/execute |
Add them up for each group:
- 7 = 4+2+1 = rwx
- 6 = 4+2 = rw-
- 5 = 4+1 = r-x
- 4 = 4 = r–
- 0 = no permissions
Useful Options
- -R, –recursive: Changes files and directories recursively.
- -v, –verbose: Outputs a Diagnostic when a file is processed.
- -c, –change: Reports only when a change is made.
- -f, –silent, –quiet: Suppresses most error massages.
- –version: Outputs version information and exit.
- –help: Displays this help and exit.
File Ownership Change
FAQs
1. How do you set permissions in Linux?
To set permissions in Linux, you use the chmod command. This command allows you to change the permissions of a file or directory. For example, to set the permissions of a file to allow the owner to read, write, and execute, the group to read and execute, and others to read and execute, you would use the following command:
chmod 755 filename
This sets the permissions of filename to rwxr-xr-x, which is equivalent to the numeric value 755.
2. What is chmod 755 or 777?
chmod 755 sets the permissions of a file or directory to allow the owner to read, write, and execute, the group to read and execute, and others to read and execute. This is a common permission setting for directories and executable files.
chmod 777, on the other hand, sets the permissions of a file or directory to allow everyone (owner, group, and others) to read, write, and execute. This is not recommended for security reasons, as it allows anyone to modify or execute the file or directory.
Here’s an example of how to set permissions using chmod 777:
chmod 777 filename
This sets the permissions of filename to rwxrwxrwx, which is equivalent to the numeric value 777.
3. What is chmod 666 or 777?
chmod 666 sets the permissions of a file or directory to allow everyone (owner, group, and others) to read and write, but not execute. This is not a common permission setting, as it does not allow the execution of files.
chmod 777, as mentioned earlier, sets the permissions of a file or directory to allow everyone to read, write, and execute.
Here’s an example of how to set permissions using chmod 666:
chmod 666 filename
This sets the permissions of filename to rw-rw-rw-, which is equivalent to the numeric value 666.
4. What is the meaning of chmod 400?
chmod 400 sets the permissions of a file or directory to allow only the owner to read, and denies all permissions to the group and others. This is a restrictive permission setting, suitable for sensitive files that should only be accessible by the owner.
Here’s an example of how to set permissions using chmod 400:
chmod 400 filename
This sets the permissions of filename to r--------, which is equivalent to the numeric value 400.
The chown Command
The chown command is used to change the ownership of a file or directory.
It allows you to specify a new owner and group for the file or directory,
ensuring that the correct user or group has access to it.
The basic syntax of the chown command is as follows:
....................................................
sudo chown username file.txt
In this example, username is the new owner of the file file.txt.
You can also specify a group along with the owner using the following syntax:
sudo chown username:groupname file.txt
Examples of Recursive Permissions
..................................
chmod -R 755 /var/www/html
This command sets the permissions of the /var/www/html directory and all
its contents to 755. This means the owner has read, write,
and execute permissions, the group has read and execute permissions,
and others have read and execute permissions.
chown -R user:group /var/www/html
This command changes the ownership of the /var/www/html directory and all
its contents to user and sets the group ownership to group.
This ensures that the specified user and group have the appropriate
access to the directory and its contents.
......................................................................
chown -R www-data:www-data /var/www/html
chmod -R 755 /var/www/html
How to Use chown and chgrp
..........................
sudo chown username file.txt