Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Linux File Permissions

1. Introduction

Linux file permissions are a critical part of system security that control who can read, write, or execute a file. Understanding these permissions is essential for any Linux administrator.

2. Understanding Permissions

2.1 Key Concepts

Every file and directory in Linux has associated permissions that determine how they can be accessed. These permissions are represented as three sets of attributes:

  • Owner (User)
  • Group
  • Others

2.2 Permission Types

Each of the above sets has three types of permissions:

  • Read (r): Permission to read the file or list the directory.
  • Write (w): Permission to modify the file or directory.
  • Execute (x): Permission to execute the file (if it’s a script or binary) or access the directory.

These permissions can be viewed using the command:

ls -l filename

The output will show permissions in the format: -rwxr-xr--, where:

  • - indicates it's a file; d indicates a directory.
  • Next three characters are for the owner.
  • Next three for the group.
  • Last three for others.

3. Changing Permissions

Permissions can be modified using the chmod command. There are two methods to change permissions: symbolic and numeric.

3.1 Symbolic Method

The symbolic method uses letters to represent users and permissions:

To add a permission:

chmod u+x filename

To remove a permission:

chmod g-w filename

3.2 Numeric Method

In the numeric method, permissions are represented by numbers:

  • Read = 4
  • Write = 2
  • Execute = 1

Thus, to set permissions:

chmod 755 filename

This means: Owner can read, write and execute; Group and Others can read and execute.

4. Best Practices

Always use the principle of least privilege when setting permissions.
  • Regularly review permissions for sensitive files.
  • Use groups to manage access instead of individual user permissions.
  • Use chmod 700 for private scripts.
  • Ensure log files are not writable by unprivileged users.

5. FAQ

What is the default permission for new files?

The default permission is typically determined by the umask value, which subtracts permissions from the default (usually 666 for files and 777 for directories).

How can I view the current umask value?

You can view the current umask value by simply typing umask in the terminal.

Can I set permissions recursively?

Yes, you can set permissions recursively using the -R option with the chmod command, e.g., chmod -R 755 directory_name.