The Core Components: Users and Groups

At its heart, Linux, like any operating system, manages access through users and groups. Every entity that interacts with the system, whether a human user or a background service, is assigned a unique User ID (UID). These UIDs are the fundamental identifiers. While you and I are typically associated with standard UIDs, the system also employs special 'pseudo' users for isolating services like daemons, graphics drivers, or system processes. You can inspect all user accounts and their UIDs by examining the /etc/passwd file.

For most day-to-day operations, the primary users of concern are the regular human users and the all-powerful 'root' user, which possesses superuser privileges.

Understanding Group Membership

Users are not isolated entities; they are organized into groups, which are critical for how the Linux kernel enforces permissions. Each group has its own Group ID (GID), and a comprehensive list can be found in the /etc/group file. A user is always a member of at least one group, known as their primary group. Additionally, users can belong to multiple supplementary groups, granting them access and permissions associated with those groups.

By default, when a new user account is created, it is assigned a primary group with the same name as the username. For example, a user named 'human' would typically have 'human' as their primary group. This default can be altered, allowing for more flexible group management and access control strategies.

File Permissions Explained: Read, Write, Execute

File permissions in Linux are typically represented by a sequence of characters, often displayed in a format like -rwxr-xr--. This string provides a detailed breakdown of who can do what with a specific file or directory. Let's dissect this:

  • The First Character: File Type The very first character indicates the type of file. A hyphen (-) signifies a regular file. Other common indicators include d for a directory, l for a symbolic link, and others for special file types.
  • The Next Nine Characters: Permissions Breakdown The subsequent nine characters are divided into three sets of three, each representing a different category of user and their associated permissions:
    • User (Owner) Permissions (rwx): The first set of three characters (rwx in our example) defines the permissions for the owner of the file.
    • Group Permissions (r-x): The second set (r-x) specifies the permissions for members of the group associated with the file.
    • Others Permissions (r--): The final set (r--) denotes the permissions for all other users on the system who are not the owner and not in the associated group.
  • Permission Meanings: Within each set of three characters, the individual permissions are:
    • Read (r): Allows viewing the contents of a file or listing the contents of a directory.
    • Write (w): Permits modifying the contents of a file or creating, deleting, and renaming files within a directory.
    • Execute (x): Enables running a file as a program or script, or entering (accessing) a directory.

If a permission is not granted, it is represented by a hyphen (-). For instance, -rwxr-xr-- translates to: the owner can read, write, and execute; members of the group can read and execute; and all others can only read.

Manipulating Permissions: The `chmod` Command

The primary tool for changing file permissions is the chmod command. It can be used in two main modes: symbolic and octal.

Symbolic Mode

Symbolic mode uses letters to represent users and permissions. The syntax is generally chmod [ugoa][+-=][rwx] file...:

  • User Categories:
    • u: User (owner)
    • g: Group
    • o: Others
    • a: All (equivalent to ugo)
  • Operators:
    • +: Add permission
    • -: Remove permission
    • =: Set permission exactly (removes others not specified)
  • Permissions:
    • r: Read
    • w: Write
    • x: Execute

Examples:

  • chmod u+x script.sh: Adds execute permission for the owner of script.sh.
  • chmod go-w data.txt: Removes write permission for the group and others for data.txt.
  • chmod a=r config.conf: Sets read permission for everyone and removes any other permissions for config.conf.
  • chmod ug+rw,o-rwx sensitive_file: Grants read/write to user and group, and removes all permissions for others on sensitive_file.

Octal Mode

Octal mode is a more concise way to set permissions using numerical values. Each permission type is assigned a value:

  • Read (r) = 4
  • Write (w) = 2
  • Execute (x) = 1

These values are summed for each permission set (user, group, others). For example:

  • rwx = 4 + 2 + 1 = 7
  • rw- = 4 + 2 + 0 = 6
  • r-x = 4 + 0 + 1 = 5
  • r-- = 4 + 0 + 0 = 4
  • --- = 0 + 0 + 0 = 0

The chmod command in octal mode takes a three-digit number, where the first digit represents the owner's permissions, the second the group's, and the third others'.

Examples:

  • chmod 755 script.sh: Sets owner to rwx (7), group to r-x (5), and others to r-x (5). This is common for executable scripts and directories.
  • chmod 644 data.txt: Sets owner to rw- (6), group to r-- (4), and others to r-- (4). This is typical for regular data files.
  • chmod 700 private_dir: Sets owner to rwx (7), and removes all permissions for group and others (0). Useful for private directories.

Referenced Sources

Share this intelligence