Linux File Permissions

Before we dive into File Permissions, let’s first understand the Linux File System. It’s like a big map that shows where all your files are stored. In Linux, everything is organized into a directory tree, starting from the root directory, which is the base of the tree and represented by /.

The Linux system has three types of files:

  • General files - These include images, videos, programs, and text files in ASCII or binary format.
  • Directory files - These are used to store other file types and can be nested within directories.
  • Device files - Represent devices in the Linux system, using names like /dev/sda1 or /dev/sda2, instead of drive letters like F: or G: in Windows.

In Windows-like operating systems, files are organized into separate folders on different data drives (e.g., C:, D:, E:). In contrast, Linux/Unix systems store files in a hierarchical structure starting from the root directory.

Here are some common top-level directories you’ll see in the Linux file system:

  • /home - Where all your personal files are stored. Each user has their own folder here.
  • /bin - Contains important programs needed to run your system.
  • /etc - Stores system configuration files (like settings for your computer).
  • /var - Stores files that change frequently, such as logs.
  • /tmp - Temporary files are stored here and are cleared on reboot.
  • /opt - Contains optional or third-party software.
  • /usr - Stores user-related programs and data.

By exploring these directories, you can understand how Linux stores and organizes all the files and programs on your system!


Linux File Permissions

Linux file permissions are a way to control who can access or make changes to files and directories. By setting file permissions, we ensure only the right people can read, modify, or run certain files.

Understanding File Permissions

In Linux, each file and directory has a set of permissions. These permissions decide who can read (see the contents), write (make changes), and execute (run) the file.

Permissions are divided among three groups:

  • Owner - The person who owns the file
  • Group - A group of users who can access the file
  • Others - Everyone else

How Permissions Are Represented

File permissions in Linux are shown in two ways:

1. Symbolic Notation

This uses letters to represent the permissions:

  • r = read
  • w = write
  • x = execute

Example: rwxr-xr-- means:

  • rwx (read, write, execute) for the owner
  • r-x (read, execute) for the group
  • r-- (read only) for others

2. Numeric Notation

Numeric notation uses numbers to represent permissions:

  • 4 = read
  • 2 = write
  • 1 = execute

Example: 755 means:

  • 7 (read, write, execute) for the owner
  • 5 (read, execute) for the group
  • 5 (read, execute) for others

Security and File Permissions

Setting the wrong permissions can be dangerous. For example, if you allow anyone to write (modify) important system files, it can break the system or allow hackers to take control.

Always make sure sensitive files have the right permissions. For instance, files containing passwords should only be readable by their owner.

Common Scenarios

  • Files with 777 (read, write, execute for everyone) are a security risk.
  • Files with 600 (read and write for the owner) are much safer for sensitive files.