Sobes.tech
Junior

How do permissions work in Linux?

sobes.tech AI

Answer from AI

In Linux, permissions determine who (user, group, or others) can read, write, or execute a file or directory.

Each file and directory has a set of permissions consisting of three categories:

  • Owner: The user who created the file or directory.
  • Group: A group of users who have additional rights.
  • Others: All other users of the system.

For each category, three types of permissions are defined:

  • Read (r): Allows viewing the contents of a file or listing files in a directory.
  • Write (w): Allows modifying the contents of a file or creating/deleting files in a directory.
  • Execute (x): Allows running a file as a program or entering a directory and accessing its contents.

Permissions can be represented in symbolic or numeric form.

Symbolic representation:

Usually displayed as a string of 9 characters: rwxrwxrwx. The first three characters relate to the owner, the next three to the group, and the last three to others. A hyphen - indicates the absence of the corresponding permission.

Example: rwx-rw-r--

  • Owner: Read, Write, Execute (rwx)
  • Group: Read, Write (rw-)
  • Others: Read (r--)

Numeric (octal) representation:

Each permission is assigned a number:

  • Read (r): 4
  • Write (w): 2
  • Execute (x): 1
  • No permissions: 0

The values for each category (owner, group, others) are summed.

Example: rwx-rw-r-- corresponds to the numeric value 764.

  • Owner: r (4) + w (2) + x (1) = 7
  • Group: r (4) + w (2) + - (0) = 6
  • Others: r (4) + - (0) + - (0) = 4

The command ls -l is used to view permissions of files and directories.

# View permissions for the file example.txt
ls -l example.txt

The chmod command is used to change permissions.

# Add execute permission for the owner
chmod u+x example.sh

# Remove write permission for group and others
chmod go-w example.txt

# Set permissions in numeric form (owner: rwx, group: rw-, others: r--)
chmod 764 example.txt

The chown command is used to change the owner of a file or directory.

# Change owner to user1
chown user1 example.txt

The chgrp command is used to change the group of a file or directory.

# Change group to group1
chgrp group1 example.txt

Besides standard rwx permissions, there are special bits:

  • SUID (Set User ID): When executing a file with the SUID bit, the process runs with the permissions of the file owner, not the user who executed it. It applies only to executable files. It is displayed as s instead of x for the owner.
  • SGID (Set Group ID): When executing a file with the SGID bit, the process runs with the permissions of the file's group. For directories, new files created inside inherit the directory's group rather than the caller's. It is displayed as s instead of x for the group.
  • Sticky Bit: Applied to directories. It allows users to create files in the directory but only delete them if they own the file or have appropriate rights. Commonly used in /tmp. It is displayed as t instead of x for others.

Special bits also have numeric representations:

  • SUID: 4000
  • SGID: 2000
  • Sticky Bit: 1000

The numeric representation including special bits will consist of four digits (e.g., 4755 for SUID with rwxr-xr-x) or three, where the first digit represents the sum of the special bits.

# Set the SUID bit on an executable file (example: with permissions 755)
chmod 4755 script.sh

# Set the Sticky Bit on a directory (example: for all with permissions 777)
chmod +t /mnt/shared_dir