SELinux (Security-Enhanced Linux) is a powerful security framework that provides an additional layer of protection by enforcing mandatory access control (MAC) policies. If you need to fix SELinux issues, it’s important to understand how it regulates process access to files and other system resources.
However, SELinux can sometimes cause problems with certain services or applications, especially when it blocks legitimate access or mistakenly flags operations as malicious.

In this guide, we will walk you through the process of troubleshooting and fixing common SELinux issues on a Linux server, ensuring that your system remains both secure and functional.
Preliminary Steps Before Fixing SELinux Issues
Before diving into fixing SELinux issues, ensure that SELinux is properly installed and running on your Linux server.
Check SELinux Status
To check if SELinux is enabled and its current mode, use the following command:
sestatus
You should see output like this:
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
Current mode: enforcing
- Enforcing mode: SELinux is actively enforcing security policies.
- Permissive mode: SELinux will log actions that would be denied in enforcing mode, but will not block them.
- Disabled: SELinux is turned off completely.
Check the SELinux Mode
If SELinux is causing issues, you might want to temporarily set it to permissive mode to allow all actions and log them, without enforcing the policies. To check or change the mode, run:
getenforce
If it’s set to enforcing, and you want to temporarily switch to permissive mode:
sudo setenforce 0
To revert to enforcing mode:
sudo setenforce 1
Note: Changing the SELinux mode with setenforce
is temporary and will reset to the default mode after a reboot. If you want to make the change permanent, you’ll need to modify the SELinux configuration file.
Review SELinux Logs
SELinux logs are essential for troubleshooting. They can provide detailed information about blocked processes and access violations. The log files are usually located in /var/log/audit/audit.log
on most systems.
To review the logs, use:
sudo tail -f /var/log/audit/audit.log
Look for entries with the AVC
(Access Vector Cache) label, as these are the most common SELinux denials.
Identifying Common SELinux Issues
Here are some common issues that may arise with SELinux on a Linux server:
- SELinux Denials (AVC Denials)
If SELinux denies access to a service, you might see AVC denials in the logs. These denials can occur when an application tries to access resources it doesn’t have permissions for, according to the SELinux policy.
- Services Not Starting Due to SELinux Policies
Sometimes, services (like web servers or databases) fail to start because of SELinux restrictions. This could be caused by incorrect contexts on files, directories, or ports.
- SELinux Blocking Access to Files or Directories
SELinux might block file access, even if the files or directories have the correct permissions. This could be due to SELinux labels not being set correctly.
- Incorrect SELinux Labels
Files and processes must have the correct SELinux context (labels) to be allowed to access each other. Incorrect labeling can prevent applications from working properly.
Fixing SELinux on Linux Server: Step-by-Step Solutions
Let’s go through the solutions for fixing common SELinux issues.
Check and Fix SELinux Denials (AVC Denials)
If you see repeated AVC denials in your SELinux logs, it means that SELinux is preventing certain actions from occurring. To resolve this, you can either:
- Adjust the SELinux policies to allow the action.
- Create custom rules to allow the action without compromising the security model.
- Identify the Denied Action:
First, identify the process and action that was blocked by reviewing the log entries. A typical log entry will look like this:
type=AVC msg=audit(1609459201.123:12345): avc: denied { write } for pid=1234 comm="httpd" name="index.html" dev="sda1" ino=12345678 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:httpd_sys_content_t:s0 tclass=file
This shows that the Apache HTTPD service (httpd
) was denied write access to the file index.html
.
- Generate a SELinux Policy Module:
To generate and apply a custom policy module that will allow the denied action, run:
sudo audit2allow -a -M mycustompolicy
This will create a module called mycustompolicy.pp
. You can apply this policy with:
sudo semodule -i mycustompolicy.pp
- Test the Change:
After applying the policy module, test if the action is now allowed by the application. Check the logs again to confirm that the denial no longer occurs.
Fix Services Not Starting Due to SELinux Policies
If services like Apache or MySQL fail to start due to SELinux, it’s often caused by incorrect contexts or missing policies.
- Check SELinux Contexts:
Use the ls -Z
command to check the SELinux context of files and directories:
ls -Z /var/www/html
This will show the SELinux context of the files. For example, web files should have a httpd_sys_content_t
label.
- Restore SELinux Contexts:
If the SELinux context is incorrect, you can restore the correct contexts using the restorecon
command:
sudo restorecon -Rv /var/www/html
This will recursively restore the correct context for all files in the specified directory.
- Check if the Correct Port is allowed for the Service:
If your service (e.g., a web server) is trying to use a non-standard port, SELinux may block it. To allow a non-standard port for the HTTP service (e.g., port 8080), use the following:
sudo semanage port -a -t http_port_t -p tcp 8080
This will allow HTTP traffic on port 8080.
Fix SELinux Blocking Access to Files or Directories
If SELinux is blocking file access even though the file permissions are correct, it could be due to incorrect file contexts.
- Check the Context of Files:
You can check the SELinux context of a file or directory with the ls -Z
command:
ls -Z /path/to/file
Ensure that the file has the correct context for its application. For example, web content should have the httpd_sys_content_t
context.
- Restore the Correct SELinux Contexts:
To restore the correct contexts, use the restorecon
command:
sudo restorecon -v /path/to/file
This will restore the default SELinux context based on the file’s location and type.
Fix Incorrect SELinux Labels
SELinux uses labels to enforce policies on files and processes. If labels are incorrect, it can prevent access to files or services.
- Check SELinux Labels:
To view the SELinux label of a file, use the ls -Z
command:
ls -Z /path/to/file
For example, files in web directories should have a httpd_sys_content_t
label. If they don’t, you’ll need to correct them.
- Apply Correct Labels:
Use the chcon
command to apply the correct SELinux label manually. For example, to label a file for web server access:
sudo chcon -t httpd_sys_content_t /path/to/file
- Use
restorecon
to Correct Labels Automatically:
To fix labels for all files in a directory, use restorecon
:
sudo restorecon -Rv /path/to/directory
This will recursively restore the correct SELinux labels for all files in the specified directory.
Set SELinux to Permissive Mode for Troubleshooting
If you need to disable SELinux enforcement for troubleshooting temporarily, you can set it to permissive mode, where SELinux logs violations but doesn’t block them.
To set SELinux to permissive mode:
sudo setenforce 0
To set SELinux back to enforcing mode:
sudo setenforce 1
For a permanent change, edit the SELinux configuration file /etc/selinux/config
:
sudo nano /etc/selinux/config
Change the line:
SELINUX=enforcing
To:
SELINUX=permissive
After making changes, reboot the system for the changes to take effect.
Conclusion
Fixing SELinux issues on a Linux server involves troubleshooting common problems such as SELinux denials, misconfigured contexts, and service failures. By following the steps in this guide, you can resolve most SELinux-related issues, ensuring that your server is both secure and functional. Regularly monitor SELinux logs, check file contexts, and apply the correct policies to ensure that your server operates smoothly with SELinux enforcement.