Securing RHEL with SELinux
Nov 18, 2025
SELinux principle: If it isn’t specifically allowed, it is denied.
SELinux Labels
The security context label consists of four components of which only the third component is relevant to RHCSA. Furthermore, the vast majority of systems that use SELinux only use the third component, the context type.
user:role:type:level
ps Zauxview labels of running processesls -Zlview labels of files and directories
SELinux Modes
-
SELinux can either be enabled or disabled and a reboot is required to switch
-
When enabled, SELinux can either be in enforcing or permissive mode
getenforceshows the current modesetenforcesets mode- modify
/etc/sysconfig/selinuxto set the default SELinux mode
-
To switch to disabled, boot parameters must be used
- There really should never be a reason to disable
-
SELinux kernel parameters
- enforcing=0
- enforcing=1
- selinux=0
-
Modify the GRUB boot command to change the settings while booting or the GRUB configuration files to make the settings persistent
SELinux Components
- SELinux context labels are applied to source and target objects
- Source objects:
- Users
- Processes
- Target objects:
- Files and directories
- Ports
- Source objects:
- SELinux policies define source access to targets
SELinux Context Labels
- In most SELinux configurations, only context type matters
- Ignore user, role, and level for RHCSA
- Most services won’t need additional configuration if default settings are used
- New and copied files typically inherit the context of the parent directory
- Moved files retain their original context
- Use
restoreconto restore a file context - Use
semanage fcontextto set a file context- This will write the context to the SELinux policy, but not to the filesystem
- Use
semanage fcontext -ato set a new context label - Use
semanage fcontext -mto modify context label
- Use
restoreconto enforce the policy setting on the system - Alternatively, use
touch /.autorelabelto relabel all files to the context that is specified in the policy man semanage-fcontext- Use
semanage fcontext -l -Cto show only settings that have changed in the current policy
Finding the Right Context
- If a non default configuration is used, simply checking the context of the default configuration will work much of the time and this is how it should be done on the exam
sealert- Read man pages for
selinux-policy-doc, but there’s no time for that on the RHCSA exam
SELinux Port Access
- For any non-default port access, use
semanage portto apply the right label - Use
semanage port -lto print a list all the currently defined port contexts - Use the examples section in
man semanage-port
Using Booleans
semanage boolean -lto list all booleanssemanage boolean --modify {boolean} --[on|off]to set boolean valuesemanage boolean -l -Cto see all booleans that have a non-default value setting
Troubleshooting SELinux
- Determine if SELinux is blocking access by switching to permissive mode
- Check the audit log
grep AVC /var/log/audit/audit.log
- Consult the selinux policy doc
dnf install selinux-policy-docman -k _selinux
Lab Exercise
- httpd
- Configure httpd to bind to port 82
- Configure httpd to serve content from /myweb
- Create a new index.html file in /myweb with the contents “hello from myweb”
- Attempt to restart httpd, inspect the journalctl and SELinux logs
- Make the appropriate SELinux policy modifications to allow the non-default httpd configuration
- Verify that you can view the new content with curl
- SSH
- Bind sshd to port 2022
- Attempt to restart httpd, inspect the systemd journal and SELinux logs
- Make the appropriate SELinux policy modifications to allow the non-default sshd configuration
- Ensure that you are able to use ssh to login via the new port
- Clint Jordan