Dump the code
Best ressources to improve your Python Skills

Chmod: Linux permissions basics

Created 11 months ago
Posted By admin
9min read
The chmod command in Unix-like systems is used to change the permissions of files and directories. There are two primary ways to specify the permissions: Numeric Mode and Symbolic Mode. in this course, we focus on numeric mode.

I.  Syntax and basic usage

Numeric mode allows you to represent permissions using a three-digit octal number. Each digit corresponds to a user category (owner, group, others), and the sum of the digits represents the permission set.

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

Numeric representation:
  • Owner: Read + Write + Execute = 4 + 2 + 1 = 7
  • Group: Read + Execute = 4 + 1 = 5
  • Others: Read only = 4

Example:

chmod 755 myfile.txt
This gives read, write, and execute permissions to the owner, and read and execute permissions to both the group and others.

Understanding and using both numeric and symbolic modes provides flexibility in managing file and directory permissions in Unix-like systems.

II. Changing Permissions for Files

To change permissions for files, you can use the `chmod` command. Here are some examples:

Example 1: Give read, write, and execute permissions to the owner, and read-only permissions to the group and others.

chmod 744 myfile.txt
In this example:
  • Owner: Read + Write + Execute = 4 + 2 + 1 = 7
  • Group: Read only = 4
  • Others: Read only = 4

Example 2: Give full permissions to the owner, and read-only permissions to the group and others.

chmod 744 myfile.txt
In this example:
  • Owner: Read + Write + Execute = 4 + 2 + 1 = 7
  • Group: Read only = 4
  • Others: Read only = 4

Tips:

- Always be cautious when changing permissions, especially when giving write or execute permissions to others.
- Use 'ls -l' to check the current permissions of files and directories.
- To change permissions for multiple files at once, you can specify multiple file names or use wildcard characters (e.g., '*.txt').

Remember to replace `myfile.txt` with the actual file name you want to modify. Adjust the permissions according to your specific needs and security requirements.

III. Changing permissions for directories

To change permissions for directories, you can use the chmod command, similar to changing permissions for files. However, there are some considerations and additional options you might want to be aware of. Here are examples using both numeric and symbolic modes:

Example 1: Give read, write, and execute permissions to the owner, and read-only permissions to the group and others.

chmod 755 mydirectory
In this example:
  • Owner: Read + Write + Execute = 4 + 2 + 1 = 7
  • Group: Read + Execute = 4 + 1 = 5
  • Others: Read + Execute = 4 + 1 = 5

Example 2: Give full permissions to the owner, and read-only permissions to the group and others.

chmod 744 mydirectory
In this example:
  • Owner: Read + Write + Execute = 4 + 2 + 1 = 7
  • Group: Read only = 4
  • Others: Read only = 4

Recursively changing permissions:

If you want to change permissions for a directory and all its subdirectories and files, you can use the '-R' (or '--recursive') option.

Example: Recursively give read and execute permissions to the owner and read-only permissions to the group and others.

chmod -R u=rx,go=r mydirectory

Tips:

- When changing permissions for directories, the execute permission is crucial. Without execute permission, users cannot enter the directory.
- Be cautious when using the -R option, especially with the chmod command, as it will recursively change permissions for all files and subdirectories.
- Always use the ls -l command to check the current permissions of directories and their contents.

Remember to replace mydirectory with the actual directory name you want to modify. Adjust the permissions according to your specific needs and security requirements.

VI. Principle of least privilege

The Principle of Least Privilege (PoLP), also known as the Principle of Minimal Privilege or the Principle of Least Authority, is a fundamental concept in computer security and access control. The principle is based on the idea that entities (such as users, processes, or systems) should be given the minimum level of access or permissions necessary to perform their job functions or tasks, and no more.

Users and processes should have only the minimum access rights and permissions required to perform their specific tasks or functions. Avoid assigning unnecessary privileges that could potentially be exploited by malicious actors.

By minimizing access, the attack surface—the potential points where a system can be exploited or compromised—is reduced. So limiting privileges helps to mitigate the impact of security vulnerabilities.

Provide permissions on a granular level rather than assigning broad, all-encompassing rights.
For example, if a user only needs to read a file, they should not be given write or execute permissions.

Regularly review and audit user accounts, processes, and systems to ensure that permissions are still appropriate. Remove or adjust permissions for users and processes when their roles change or when access is no longer needed.

Set default configurations and permissions to the minimum required for a system or application to function. Users and processes can then be granted additional permissions based on specific needs.

Reduce the use of shared mechanisms, such as shared user accounts or shared resources, to limit potential points of compromise.

V. Handling permission issues in Web Servers

Handling permission issues in web servers is crucial for ensuring the security and proper functioning of web applications. Permissions determine which users or processes can access and manipulate files and directories on the server. Here are common scenarios and solutions for handling permission issues in web servers.

Web Server user and group:
   - Issue: The web server needs the appropriate permissions to read and execute files.
   - Solution: Identify the user and group that the web server runs as (commonly `www-data` for Apache on Ubuntu) and ensure that files and directories are owned by or accessible to this user and group.

# Example: Change ownership to www-data
chown -R www-data:www-data /var/www/html

File and directory permissions:
   - Issue: Incorrect file and directory permissions can lead to access errors.
   - Solution: Use appropriate permissions based on the needs of your web server. For example, web directories often need the execute permission for the server to traverse them.

# Example: Set directory permissions
chmod 755 /var/www/html

Permission for configuration files:
   - Issue: Configuration files containing sensitive information should be restricted to authorized users.
   - Solution: Set strict permissions for configuration files, allowing only the necessary users or groups to read or modify them.

# Example: Set permissions for a configuration file
chmod 600 /etc/apache2/apache.conf

Upload directories:
   - Issue: Web applications may need to write to specific directories (e.g., for file uploads).
   - Solution: Identify the directories where uploads occur and ensure that the web server has write permissions to those directories.

# Example: Set write permissions for an uploads directory
chmod 755 /var/www/html/uploads

SELinux or AppArmor issues:
   - Issue: Security Enhanced Linux (SELinux) or AppArmor might be preventing access.
   - Solution: Adjust the SELinux context or AppArmor profile to allow the necessary actions.

# Example: Set SELinux context
chmod -Rv --type=httpd_sys_content_t /var/www/html

CGI Scripts:
   - Issue: CGI scripts may require execute permissions.
   - Solution: Ensure that CGI scripts have the execute permission and that the directory containing them allows CGI execution.

# Example: Set execute permissions for a CGI script
chmod +x /var/www/html/cgi-bin/script.cgi

Logs and temporary directories:
   - Issue: Web applications often need to write logs or use temporary directories.
   - Solution: Allow write access to directories where logs and temporary files are stored.

# Example: Set write permissions for a logs directory
chmod 755 /var/log/myapp

Regular auditing:
   - Issue: Not regularly auditing and reviewing file permissions.
   - Solution: Periodically audit and review file permissions to ensure they align with security best practices.

# Example: List permissions for files in a directory
ls -l /var/www/html
 
By addressing these common permission issues, you can enhance the security and reliability of your web server. Always be cautious about granting excessive permissions and regularly review and audit permissions to ensure they align with the Principle of Least Privilege.

Topics

Mastering Nginx

27 articles

Bash script

2 articles

Crontab

2 articles