Linux Permissions: The Ultimate Cheat Sheet

Updated: January 29, 2024 By: Guest Contributor Post a comment

Understanding Linux permissions is critical for anyone looking to control access to files and directories on their system. This guide aims to provide you with a handy cheat sheet to decode the mysteries of Linux file permissions.

Introduction to Linux Permissions

In Linux, access to files and directories is controlled through sets of permissions. These permissions determine who can read, write, or execute a file. There are three types of users that these permissions apply to: the file owner, the group, and others.

The permissions are represented by either a symbolic notation (e.g., rw-r--r--) or a numeric notation (e.g., 644). Symbolic notation is made up of 10 characters: the first character indicates the type of file, the next three are the permissions for the owner, followed by the permissions for the group, and finally, the permissions for others.

Understanding Symbols

  • - indicates a regular file
  • d indicates a directory
  • r means read permission
  • w stands for write permission
  • x signifies execute permission
  • - (in the position of permission) means permission is not granted

Reading Symbolic Permissions

Take the symbolic notation drwxr-xr-x. The first character d means it’s a directory. The next three rwx signifies that the user can read, write, and execute. The following three, r-x, indicates read and execute permissions for the group, and the last three, r-x, are for others.

Reading Numeric Permissions

The numeric notation is a three-digit number where each digit can be from 0 to 7. The first digit indicates the user’s permission, the second the group’s, and the third for others. Each number is the sum of 4 (read), 2 (write), and 1 (execute). For example, 6 is read (4) plus write (2), thus rw-.

Changing Permissions with chmod

The chmod command is used to change a file’s permissions. To add permissions:

chmod u+r file.txt

To remove permissions:

chmod o-r file.txt

To set permissions explicitly:

chmod 755 file.txt

You can also change permissions recursively for all files and directories within a directory:

chmod -R 755 /path/to/directory

Ownership and Groups

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

chown username file.txt

To change both the owner and the group:

chown username:groupname file.txt

Use the chgrp command to change the group:

chgrp groupname file.txt

Special Permissions

There are also special permissions such as the setuid, setgid, and sticky bit.

  • setuid (s in user’s execute position): Allows users to run an executable with the permissions of the executable’s owner.
  • setgid (s in group’s execute position): Similar to setuid but for the group. Directories with setgid will have new files inherit the group ID set on the directory.
  • Sticky bit (t): Typically set on directories to indicate that only the owner of a file can delete or rename files within the directory, regardless of the directory’s other permissions.

Examples of Permission Usage

Here are some practical examples of how to use permissions:

To give full permissions to the owner and restrict all permissions for group and others:

chmod 700 file.txt

To ensure a script can be executed by everyone:

chmod a+x script.sh

To set the setgid bit on a directory:

chmod g+s /path/to/directory

To secure a directory with the sticky bit:

chmod +t /path/to/world_writable_directory

Conclusion

Linux file permissions are a foundational concept for system security and administration. By using this cheat sheet, you can effectively manage permissions and keep your system secure.

Remember that altering permissions should be done with care, as incorrect settings can lead to security vulnerabilities or system errors.

For further exploration, consider reading the man pages for chmod, chown, and ls to deepen your understanding of these commands and their various options.

With the knowledge of these permissions in hand, you are well on your way to expertly managing your Linux system’s security and file access.