How to Configure SELinux on AlmaLinux 9
SELinux (Security-Enhanced Linux) adds an extra layer of security to your system by enforcing mandatory access controls.
To configure SELinux on AlmaLinux 9, use the following steps:
Step 1: Check SELinux Status
By default, SELinux is enabled on AlmaLinux. To confirm its status, run:
Step 2: Display Detailed SELinux Information
For a more comprehensive view of SELinux settings, use:
Step 3: Understand SELinux Modes
SELinux operates in three modes:
- Enforcing: Strict policy enforcement.
- Permissive: Logs policy violations but does not enforce them.
- Disabled: SELinux is turned off.
Step 4: Disable SELinux
If you need to disable SELinux for any reason, follow these steps.
Temporarily Disable SELinux
Permanently Disable SELinux
Edit /etc/selinux/config
and set:
Reboot the System
After disabling SELinux permanently, reboot your system for the changes to take effect.
Step 5: Verify SELinux Status
Ensure that SELinux is properly configured by checking its status again:
Step 6: Example Setup with Apache
As a practical example, we will configure Apache to listen on port 8001 and host a website in a custom directory.
Install the Apache
Update Apache Configuration
Edit /etc/httpd/conf/httpd.conf
and add:
Create a Custom Virtual Host
Create a new Apache configuration file:
Add the following content:
Prepare the Web Root
Create the document root and set proper permissions:
Open Firewall for the New Port
If the firewall is active, allow traffic on port 8001:
Install Required SELinux Utilities
Ensure you have the necessary tools for SELinux management:
Manage SELinux Port Definitions
Check the current SELinux port settings:
Add a Custom Port Definition
If the port is not allowed, run:
Verify the New Port Definition
Check if the port has been added successfully:
Restart Apache
Apply the changes and restart Apache:
Adjust SELinux Context for the Custom Directory
Step 7: Access Your Website
You can now access your Apache server at:
If everything is set up correctly, you should see "Hello, SELinux!"
Conclusion
By following these steps, you can configure and optimize SELinux for enhanced security on AlmaLinux 9.