Why SELinux Is Blocking Your Service (and How to Fix It)
Troubleshooting SELinux-Related Issues in Linux
Running services with SELinux enabled can sometimes lead to unexpected denials or blocked actions—especially if SELinux was previously disabled or the service is configured in a non-standard way.
In most cases, SELinux issues are the result of misconfigured contexts, policies, or port bindings. Here's a step-by-step approach to diagnose and fix SELinux-related problems while keeping your system secure.
Check SELinux Status and Mode
Start by verifying that SELinux is active on your system:
# sestatus
Look for:
- SELinux status: enabled
- Current mode: enforcing or permissive
To test if SELinux is causing a problem, temporarily switch to permissive mode:
# setenforce 0 # Logs denials but doesn’t block actions
# setenforce 1 # Reverts to enforcing mode
If the issue disappears in permissive mode, SELinux is likely the root cause.
Look for SELinux Denials in Logs
Use the audit logs to find detailed denial messages:
# grep "avc: denied" /var/log/audit/audit.log
To generate a human-readable report from the logs:
# sealert -a /var/log/audit/audit.log
Review the AVC denial messages related to your service or application.
Note:
On RHEL 8 (and newer systems), the sealert tool is not installed by default. Install setroubleshoot-server to get sealertyum install setroubleshoot-server -y
Fix File and Directory Contexts
Incorrect file contexts are a common issue. Check them with:
# ls -Z /path/to/your/file_or_directory
To restore default contexts:
# restorecon -Rv /path/to/your/directory
To permanently set custom contexts (example: for a custom web directory):
# semanage fcontext -a -t httpd_sys_content_t "/custom_web(/.*)?"
# restorecon -Rv /custom_web
Adjust SELinux Policies
Allow Non-Default Ports
# semanage port -l | grep http
# semanage port -a -t http_port_t -p tcp 8080
Enable Required Booleans
# getsebool -a | grep httpd_can_network_connect
# setsebool -P httpd_can_network_connect on
Create Custom SELinux Policy Modules (Advanced)
If there's no existing fix, you can generate custom policy modules based on denial logs:
# grep "avc: denied" /var/log/audit/audit.log | audit2allow -M mypolicy
# semodule -i mypolicy.pp
To understand the cause of denials:
# audit2why < /var/log/audit/audit.log
Make Changes Persistent
To ensure changes survive a reboot:
- Use
-P
withsetsebool
to make boolean changes permanent. - Use
semanage
for ports and file context changes. - Edit
/etc/selinux/config
to setSELINUX=enforcing
orpermissive
.
Common Scenarios & Fixes
Apache/Nginx Access Denied
# semanage fcontext -a -t httpd_sys_content_t "/custom_web_dir(/.*)?"
# restorecon -Rv /custom_web_dir
# setsebool -P httpd_can_network_connect on
MySQL/PostgreSQL Access Issues
# restorecon -Rv /var/lib/mysql
# semanage port -a -t mysqld_port_t -p tcp 3307
Samba/NFS Share Denials
# setsebool -P samba_export_all_rw on
# chcon -t samba_share_t /shared_directory
SSH Binding to Non-Default Port
# semanage port -a -t ssh_port_t -p tcp 2222
# restorecon -Rv /etc/ssh
Final Tips
- Avoid disabling SELinux: Use policies and adjustments to preserve security.
- Use permissive mode for testing: But always revert to enforcing when done.
- Run
restorecon
after updates: Especially if new files were added. - Document your changes: Helps you (and others) debug future issues faster.
By following these steps and understanding SELinux’s role, you can troubleshoot most access issues without compromising system security.
Comments
Post a Comment