In Linux, file and directory permissions are crucial for system security and user access control. Incorrect permissions can lead to unauthorized access, data loss, or serious security vulnerabilities.
1. Importance of File Permissions in Linux
Each file and directory in Linux has specific permissions and ownership. You can view a file’s permissions using the ls -l command:
GENEL
ls -l file.txt
Example Output:
GENEL
-rw-r--r-- 1 user group 1024 Feb 28 12:00 file.txt
-rw-r--r--→ File permissionsuser→ File ownergroup→ Group owner1024→ File size (bytes)Feb 28 12:00→ Last modified date
2. Setting File Permissions with chmod
Basic Permission Types
| Permission | Symbol | Value | Description |
|---|---|---|---|
| Read | r | 4 | Can read the file |
| Write | w | 2 | Can modify the file |
| Execute | x | 1 | Can execute the file |
Numeric Method
GENEL
chmod 755 file.txt
- 7 (rwx): Owner has all permissions
- 5 (r-x): Group can read/execute
- 5 (r-x): Others can read/execute
Common Settings
| Code | Description |
|---|---|
| 777 | Full access for everyone (not safe!) |
| 755 | Owner full access, others read/execute |
| 644 | Owner read/write, others read |
| 600 | Only owner can read/write |
Symbolic Method
GENEL
chmod u+rwx,g+rx,o-r file.txt
u+rwx: Add all permissions to userg+rx: Add read/execute to groupo-r: Remove read from others
GENEL
chmod o-w file.txt # Remove write from others
chmod u+x script.sh # Give execute to user
3. Changing Ownership with chown
Change File Owner
GENEL
chown ahmet file.txt
Change Owner and Group
GENEL
chown ahmet:www-data file.txt
Change Ownership Recursively
GENEL
chown -R apache:apache /var/www/html
4. Real-World Examples & Security Tips
Web Server Setup
GENEL
chown -R www-data:www-data /var/www/html
chmod -R 755 /var/www/html
SSH Key Permissions
GENEL
chmod 600 ~/.ssh/id_rsa
chmod 700 ~/.ssh
chmod 644 ~/.ssh/id_rsa.pub
Restrict User Home Access
GENEL
chmod 750 /home/username
Best Practices
- Avoid
777unless necessary - Use
444if write is not needed - Protect system files like
/etc/passwd - Run
ls -lbefore applying chmod/chown
Related Articles
Advanced File and User Management in Linux
0 Comments
Basic Terminal Commands and Usage in Linux
0 Comments
Comments ()
No comments yet. Be the first to comment!