Working with SELinux
1. Checking SELinux Status
| Command | Description | Output Example |
|---|---|---|
| sestatus | Displays the current SELinux mode and policy version | |
| getenforce | Shows the current SELinux mode (enforcing, permissive, or disabled) | Enforcing |
| sestatus -v | Verbose output including detailed policy information | |
Example: Check SELinux status in a script
#!/bin/bash
if [[ "$(getenforce)" == "Enforcing" ]]; then
echo "SELinux is active and enforcing policies."
else
echo "SELinux is not in enforcing mode."
fi
2. Setting SELinux Mode
| Mode | Description | Command | Notes |
|---|---|---|---|
| enforcing | SELinux is active and enforcing policies | setenforce 1 |
Default mode in most distributions |
| permissive | SELinux is active but logging only | setenforce 0 |
Useful for debugging policy issues |
| disabled | SELinux is completely disabled | setenforce 0 |
Not recommended for production systems |
3. Viewing Security Contexts
| Command | Description | Example Output |
|---|---|---|
| ls -Z | Lists files with their security contexts | unconfined_u:object_r:var_log_t:s0 /var/log |
| ps -eo pid,comm,sestatus | Displays processes with their security contexts | 1234 firefox unconfined_t |
| getenforce | Shows current SELinux mode (Enforcing, Permissive, or Disabled) | Enforcing |
Example: Check security context of a file
$ ls -Z /etc/passwd
system_u:object_r:etc_t:s0 /etc/passwd
Example: View process security context
$ ps -eo pid,comm,sestatus | grep firefox
1234 firefox unconfined_t
Note: The security context format is
user:role:type:level. For more details on each component, refer to the SELinux documentation.
4. Changing File Context
| Command | Description | Example |
|---|---|---|
| ls -Z | Lists files with their SELinux context | ls -Z /var/www/html/index.html |
| chcon | Changes the SELinux context of files or directories | chcon -t httpd_sys_content_t /var/www/html/index.html |
| restorecon | Restores default SELinux context from policy | restorecon -R /var/www/html |
Example: Change context for a web file
# View current context
ls -Z /var/www/html/index.html
# Change to httpd context
chcon -t httpd_sys_content_t /var/www/html/index.html
# Verify change
ls -Z /var/www/html/index.html
Note: The
-R flag for restorecon recursively resets contexts in directories.Warning: Incorrect context changes may prevent applications from accessing files.
5. Restoring Default Context
SELinux context can be restored to default values using the restorecon command. This is useful when file contexts have been manually modified or become inconsistent.
| Command | Description | Example |
|---|---|---|
| restorecon | Restores default SELinux context to files | restorecon -R /var/www/html |
| restorecon -R | Recursively restores context for directories and subdirectories | restorecon -R /etc/httpd |
| restorecon -v | Verbose mode to show which files were restored | restorecon -v /etc/passwd |
Example: Restoring Context to Web Content
# Restore default context to web directory
restorecon -R /var/www/html
# Verify the context was restored
ls -Z /var/www/html/index.html
Note: The
-R flag ensures all files and subdirectories are processed recursively.
Warning: Be cautious when running
restorecon on system directories as it may reset contexts to default values that differ from custom policies.
6. Managing Policy Booleans
| Boolean Name | Current Value | Description | State |
|---|---|---|---|
| httpd_can_network_connect | off |
Allows HTTPD to connect to network | DEPRECATED |
| sshd_can_network_connect | off |
Allows SSHD to connect to network | NEW |
| authlogin_nsswitch_support | on |
Enables NSS switch support for authentication | STABLE |
Note: The
-P flag makes the change persistent across reboots.
Warning: Setting policy booleans can affect system security. Ensure you understand the implications before changing them.
7. Viewing SELinux Denials
Example: Check SELinux denials using auditctl
# View recent SELinux denials
auditctl -l | grep avc
# Monitor real-time denials
ausearch -m avc --start recent
| Command | Description | Notes |
|---|---|---|
| ausearch | Searches audit logs for specific events | Use -m avc to filter SELinux denials |
| auditctl -l | Lists active audit rules | Shows current audit configuration including SELinux rules |
| sealert | SELinux alert analyzer | Interprets SELinux denials and suggests fixes |
Note: SELinux denials are logged to the audit log and can be viewed using
ausearch or sealert. These tools help in diagnosing and resolving permission issues.
Example: Using sealert to analyze denials
# Analyze recent SELinux denials
sealert -a /var/log/audit/audit.log
# Show only the most recent denials
sealert -a /var/log/audit/audit.log | tail -20
8. Creating Custom Policies
Custom SELinux policies allow you to define specific rules for your applications or services that are not covered by default policies. These policies can be created using the semanage command or by writing policy modules in the Policy Language.
| Command | Description | Usage Example |
|---|---|---|
| semanage permissive | Set a domain to permissive mode | semanage permissive -a myapp_t |
| semanage port | Add or modify port contexts | semanage port -a -t http_port_t -p tcp 8080 |
| semanage fcontext | Define file contexts for labeling files | semanage fcontext -a -t httpd_sys_content_t "/var/www/custom(/.*)?" |
Example: Define a Custom Policy Module
# Create a policy module file named myapp.te
module myapp 1.0;
require {
type myapp_t;
type http_port_t;
class tcp_socket name_connect;
}
# Allow myapp to connect to HTTP ports
allow myapp_t http_port_t:tcp_socket name_connect;
Example: Compile and Install Custom Policy
# Compile the policy module
checkmodule -M -m -o myapp.mod myapp.te
# Convert to binary policy
semodule_package -o myapp.pp -m myapp.mod
# Install the policy module
sudo semodule -i myapp.pp
Note: Always test custom policies in permissive mode before enforcing them to avoid system lockouts.
Warning: Incorrect policy rules can lead to system instability or security vulnerabilities. Ensure thorough testing in a development environment.
9. Managing SELinux Users
| User Type | Description | Commands |
|---|---|---|
| sysadm_u | System administrator user with broad administrative privileges | semanage login -a -s sysadm_u -r s0-s0:c0.c255 user |
| staff_u | Staff user with standard system access and some administrative capabilities | semanage login -a -s staff_u -r s0-s0:c0.c255 user |
| user_u | Standard user with minimal system access | semanage login -a -s user_u -r s0-s0:c0.c255 user |
| unconfined_u | Unconfined user with no SELinux restrictions | semanage login -a -s unconfined_u -r s0-s0:c0.c255 user |
Note: The
-r s0-s0:c0.c255 sets the default security range for the user.
Warning: Modifying SELinux user mappings can affect system security. Ensure proper testing in a non-production environment.
10. Configuring Port Labels
| Port Range | Label | Description |
|---|---|---|
| 80/tcp | http_port_t |
Standard HTTP port for web traffic |
| 443/tcp | http_port_t |
Standard HTTPS port for secure web traffic |
| 22/tcp | ssh_port_t |
Standard SSH port for secure shell access |
| 25/tcp | smtp_port_t |
Standard SMTP port for email services |
| 53/udp | dns_port_t |
Standard DNS port for domain name resolution |
| 123/udp | ntp_port_t |
Standard NTP port for network time protocol |
Note: Changes to port labels take effect immediately but require a reboot to persist across system updates.
11. Troubleshooting SELinux Issues
| Issue | Description | Solution |
|---|---|---|
| SELinux preventing access | SELinux blocks access to files or services based on policies | Use sealert -a /var/log/audit/audit.log to analyze logs and get suggestions |
| Denial messages in logs | Logs show avc: denied entries indicating policy violations |
Check /var/log/audit/audit.log or /var/log/messages for details |
| Context mismatch | Files or directories have incorrect SELinux contexts | Use ls -Z to check context and chcon or restorecon to fix |
Note: Setting
setenforce 0 disables SELinux temporarily. For permanent changes, modify /etc/selinux/config.
Warning: Disabling SELinux reduces system security. Re-enable it after troubleshooting.