Author: Madhan Gopalakrishnan | Published on : 13-02-2025

SELinux (Security-Enhanced Linux) is a security architecture that enforces mandatory access controls (MAC) on Linux systems. It adds an extra layer of security by defining and enforcing policies that restrict the actions of users and applications. The semanage
command is a crucial tool in managing SELinux policies, making it easier to configure and maintain security contexts. This guide provides an in-depth and beginner-friendly explanation of SELinux and the semanage
tool. š
š”ļø Understanding SELinux
š Feature | š Description |
---|---|
Enforcement Mode | Defines how SELinux policies are applied (Enforcing, Permissive, or Disabled). |
Security Contexts | Labels processes and files with a security context to control access. |
Booleans | Toggle SELinux policy features dynamically without modifying policies. |
Targeted Policy | The default SELinux policy that enforces restrictions on specific processes. |
SELinux Modes:
š Mode | š Description |
---|---|
Enforcing | SELinux actively enforces security policies and blocks violations. |
Permissive | SELinux logs policy violations but does not enforce them. Useful for debugging. |
Disabled | SELinux is completely turned off. This is not recommended for security. |
š Checking and Setting SELinux Mode
- To check the current SELinux mode:
sestatus
- To temporarily set SELinux to permissive mode:
sudo setenforce 0
- To permanently change SELinux mode, edit
/etc/selinux/config
:sudo nano /etc/selinux/config
Change the line:SELINUX=enforcing # Change to permissive or disabled if needed
Save and reboot for changes to take effect.
š Managing SELinux Policies with Semanage
semanage
is used to modify SELinux policies, manage file contexts, port labeling, booleans, and user mappings.
š Common semanage
Commands
š Command | š Description |
---|---|
semanage fcontext | Manage file security contexts. |
semanage port | Modify SELinux rules for network ports. |
semanage boolean | Enable or disable SELinux booleans. |
semanage login | Map Linux users to SELinux roles. |
semanage permissive | Mark domains as permissive for debugging. |
š Changing File Security Contexts
- List file contexts:
semanage fcontext -l
- Add a new file context:
semanage fcontext -a -t httpd_sys_content_t '/myweb(/.*)?'
- Apply changes using
restorecon
:restorecon -Rv /myweb
š Managing Network Ports with SELinux
- List current SELinux port mappings:
semanage port -l
- Allow a service to use a custom port (e.g., Apache on port 8080):
semanage port -a -t http_port_t -p tcp 8080
- Modify an existing port label:
semanage port -m -t http_port_t -p tcp 9090
š Enabling and Disabling SELinux Booleans
- List all SELinux booleans:
getsebool -a
- Enable or disable a boolean (e.g., allow Apache to connect to a database):
setsebool -P httpd_can_network_connect_db on
š Assigning SELinux Roles to Users
- List current user mappings:
semanage login -l
- Map a Linux user to an SELinux role:
semanage login -a -s user_u myuser
š Auditing SELinux Logs with Ausearch
The ausearch
tool is used to analyze SELinux logs and troubleshoot security issues effectively.
š Using ausearch
for SELinux Events
- Search for SELinux AVC (Access Vector Cache) denials:
ausearch -m avc
- Filter logs based on a specific process (e.g., Apache):
ausearch -c httpd
- Search logs for a specific time range:
ausearch -m avc -ts yesterday
- View logs for a particular user:
ausearch -m USER_LOGIN -ua 1000
Using ausearch
effectively helps administrators identify policy violations and security issues that need to be addressed to ensure a hardened Linux environment.
š„ Best Practices for SELinux Security Hardening
- Always keep SELinux in enforcing mode unless troubleshooting.
- Regularly audit SELinux logs for security violations:
ausearch -m avc
- Use
restorecon
to fix mislabelled files instead of disabling SELinux. - Enable relevant SELinux booleans based on system requirements.
- Keep SELinux policies updated to prevent vulnerabilities.
- Use
semanage
to properly configure ports, file contexts, and user mappings. - Implement log monitoring and alerting systems for SELinux-related issues.
š Conclusion
SELinux provides an advanced security mechanism for Linux systems by enforcing strict access controls. The semanage
command simplifies policy management, making it easier to configure security settings. The ausearch
tool is invaluable for analyzing logs and detecting policy violations. By understanding and properly configuring SELinux, administrators can significantly enhance system security and reduce risks from unauthorized access.
By implementing these practices, you can achieve a robust and secure Linux system. šš§ Happy Securing! š