May 10, 2025 - 08:22
File and Directory Permissions in Linux: Using chmod and chown Image
Linux

File and Directory Permissions in Linux: Using chmod and chown

Comments

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 permissions
  • userFile owner
  • groupGroup owner
  • 1024File size (bytes)
  • Feb 28 12:00Last modified date

2. Setting File Permissions with chmod

Basic Permission Types

Permission Symbol Value Description
Readr4Can read the file
Writew2Can modify the file
Executex1Can 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

CodeDescription
777Full access for everyone (not safe!)
755Owner full access, others read/execute
644Owner read/write, others read
600Only owner can read/write

Symbolic Method

GENEL
chmod u+rwx,g+rx,o-r file.txt
  • u+rwx: Add all permissions to user
  • g+rx: Add read/execute to group
  • o-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 777 unless necessary
  • Use 444 if write is not needed
  • Protect system files like /etc/passwd
  • Run ls -l before applying chmod/chown

Related Articles

Comments ()

No comments yet. Be the first to comment!

Leave a Comment