화. 8월 12th, 2025

Introduction

In Linux/Unix systems, file permissions and ownership are critical for security and collaboration. Two fundamental commands for managing these are chmod (change mode) and chown (change owner). This guide explains their usage, syntax, and practical applications in detail.


1. Understanding Ownership & Permissions

  • Ownership: Every file/directory has an owner (user) and a group.
  • Permissions: Define access levels for:
    • User (u): Owner of the file.
    • Group (g): Members of the file’s group.
    • Others (o): All other users.
  • Permission Types:
    • r (read): View file/directory contents.
    • w (write): Modify or delete files.
    • x (execute): Run files or traverse directories.

2. chown: Changing Ownership

Purpose: Transfer file/directory ownership to another user or group.

Syntax

chown [options] :   

Key Options

  • -R: Apply changes recursively (for directories).
  • --reference=file.txt: Copy ownership from another file.

Examples

  • Change owner to alex:
    chown alex document.txt  
  • Change owner to alex and group to dev-team:
    chown alex:dev-team document.txt  
  • Recursively change ownership for a directory:
    chown -R alex:dev-team /project/  

3. chmod: Modifying Permissions

Purpose: Change read/write/execute permissions for users, groups, or others.

Two Permission Modes

A. Symbolic Mode
Uses letters (u, g, o, a) and operators (+, -, =).

Examples:

  • Add execute permission for the owner:
    chmod u+x script.sh  
  • Remove read/write from group and others:
    chmod go-rw document.txt  
  • Reset permissions to rwxr-xr-- (user: rwx, group: r-x, others: r–):
    chmod u=rwx,g=rx,o=r app  

B. Numeric Mode
Uses octal numbers (0–7) representing permission bits:

  • 4 = read (r)
  • 2 = write (w)
  • 1 = execute (x)

Examples:

  • rwxr-xr-- becomes:
    • User: 4+2+1 = 7
    • Group: 4+0+1 = 5
    • Others: 4+0+0 = 4
      chmod 754 app  
  • Set read/write for user, read for others (no group access):
    chmod 604 config.yml  

Key Options

  • -R: Apply recursively.
  • -v: Verbose output (show changes).

4. Common Use Cases

  • Restrict Confidential Files:
    chmod 600 secret.txt  # Only owner can read/write.  
  • Share Files with a Group:
    chown :team-project report.md && chmod 770 report.md  
  • Make a Script Executable:
    chmod +x backup_script.sh  
  • Fix Web Server Permissions:
    chown -R www-data:www-data /var/www/ && chmod -R 755 /var/www/  

5. Pro Tips

  • Safety First: Use ls -l to verify permissions before changing them.
  • Recursive Caution: Avoid broad chmod -R 777 /—it’s a security risk!
  • Default Permissions: Use umask to set default permissions for new files.
  • Special Permissions:
    • setuid (chmod u+s): Execute as owner.
    • sticky bit (chmod +t /dir): Restrict file deletion in shared directories.

Conclusion

chmod and chown are indispensable tools for managing Linux security. By mastering symbolic/numeric modes and recursive options, you’ll ensure precise access control for users and groups. Always test commands in a safe environment before applying them to critical systems!

> Practice Safely: Experiment with permissions in a sandbox directory first!

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다