Site logo
Authors
  • avatar Nguyễn Đức Xinh
    Name
    Nguyễn Đức Xinh
    Twitter
Published on
Published on

Understanding Linux File Permissions

Introduction to Linux Permissions

In Linux systems, file and directory permissions are crucial for maintaining security and controlling access to resources. Linux permission determines who can access, modify, and execute files and directories, ensuring proper access control and protecting sensitive information.
Understanding and setting permissions effectively is essential for security and system integrity, particularly in multi-user environments. By understanding and effectively managing permissions, you can safeguard your system and data from unauthorized access.
This section will guide you through viewing, understanding, and modifying Linux permissions, ensuring proper access control across your server.

Basic Concepts

Linux permissions are based on three types of access:

  • Read (r): Ability to view file contents or list directory contents
  • Write (w): Ability to modify file contents or create/delete files in a directory
  • Execute (x): Ability to run a file as a program or access a directory

Permission Categories

Permissions are assigned to three categories of users:

  • Owner: The user who owns the file or directory
  • Group: A group of users with shared permissions
  • Others: All other users on the system

How to Check Permissions in Linux

To view permissions for files and directories, use the ls -l command. The output will display permissions in the following format:

ls -l
total 16
drwxrwxr-x 2 ubuntu ubuntu 4096 Oct 31 10:23 sample-folder
drwxr-xr-x 2 root   root   4096 Oct 31 10:28 sample-folder-2
-rw-rw-r-- 1 ubuntu ubuntu   10 Oct 31 10:42 sample.md
-rw-r--r-- 1 root   root    574 Oct 31 10:43 sample2.md

Alt text

Modifying Permissions with chmod

Use the chmod command to change permissions for files and directories. Permissions can be set in symbolic or numeric (octal) formats:
Symbolic format: chmod u+rwx,g+rx,o-r filename

  • u: User (owner)
  • g: Group
  • o: Others
  • +: Add permission
  • -: Remove permission
# Add permissions
chmod +rwx filename

# Remove permissions
chmod -rwx directoryname

# Allow executable permissions
chmod +x filename

Numeric format: Permissions are represented with three octal numbers, such as chmod 755 filename:

  • 7 (Owner): Read, write, and execute
  • 5 (Group): Read and execute
  • 5 (Others): Read and execute

Permission Table: Binary, Octal, and Access Rights

Binary Octal Permission Meaning
000 0 --- No permissions
001 1 --x Execute only
010 2 -w- Write only
011 3 -wx Write and execute
100 4 r-- Read only
101 5 r-x Read and execute
110 6 rw- Read and write
111 7 rwx Read, write, and execute

# Owner, Group, Others: read, write, execute
chmod 777 ~/example.txt

# Owner: read, write, Group: read, execute Others: No permissions
chmod 750 ~/example.txt

Changing Ownership with chown

The chown command is used to change ownership of a file or directory, which can be crucial for shared access environments:

chown new_owner:new_group filename

Common Permission Scenarios

  • 644: rw-r--r-- (Owner can read/write, others can read)
  • 755: rwxr-xr-x (Owner can read/write/execute, others can read/execute)
  • 600: rw------- (Owner can read/write, no access for others)
  • 777: rwxrwxrwx (Owner,Group, Other can read/write/execute)

Common Permission Configurations

  • Private file: chmod 600 filename (read and write only for the owner)
  • Executable by the owner: chmod 700 script.sh (full access for the owner)
  • Publicly readable file: chmod 644 publicfile
  • Grant permissions to everyone to read, write, and execute: chmod 777 example

Best Practices

  • Use the principle of least privilege
  • Regularly audit and review permissions
  • Be cautious when changing permissions on system files
  • Use groups effectively to manage permissions for multiple users

Conclusion

Understanding and effectively managing Linux permissions is crucial for maintaining a secure and well-organized system. By mastering these concepts, you can ensure that files and directories are accessible only to authorized users, protecting sensitive information and maintaining the integrity of your Linux environment.

Vietnamese version
English version