Managing SELinux
Theory Questions
1. Set SELinux to disabled mode in RHEL 9.
- Command(s):
grubby --update-kernel=ALL --args="selinux=0"
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
- Why: In RHEL 9, setting
SELINUX=disabledsolely in/etc/selinux/configis deprecated and ignored during early kernel initialization. Completely disabling SELinux requires passingselinux=0as a kernel argument viagrubby. - Gotchas: Re-enabling SELinux after disabling it requires creating
/.autorelabelbefore rebooting; otherwise, unlabeled files will cause system boot failures. - Verify:
reboot
sestatus
(Returns SELinux status: disabled)
2. Show the current SELinux mode.
- Command(s):
getenforce
# OR
sestatus
- Why:
getenforcedisplays the active runtime enforcement mode (Enforcing,Permissive, orDisabled), whilesestatusprovides comprehensive details including policy type and configuration file settings. - Gotchas: Confusing the runtime mode (
getenforce) with the mode defined on disk in/etc/selinux/config. - Verify:
sestatus | grep "Current mode"
3. Identify the most significant item in the SELinux context label for system administration.
- Command / Context Syntax:
user:role:TYPE:level (e.g., system_u:object_r:httpd_sys_content_t:s0)
- Why: The Type attribute (
*_t) is the primary component evaluated under SELinux Targeted Policy. Access decisions between process domains and target resource types rely almost entirely on type enforcement rules. - Gotchas: Attempting to modify user or role fields when troubleshooting permission denials; administrators almost exclusively modify the context type.
- Verify:
ls -Z /var/www/html
4. Display SELinux-related information using command-line switches.
- Command(s):
ls -Z /var/www/html
ps -Z aux | grep httpd
id -Z
ss -Ztulpn
- Why: Capital
-Z(or--context) appends SELinux security context information (user:role:type:level) to standard diagnostic command outputs. - Gotchas: Using lowercase
-zon commands that require uppercase-Z. - Verify:
ls -Zd /var/www/html
5. Set the context type of a directory (e.g., /web to httpd_sys_content_t).
- Command(s):
semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?"
restorecon -Rv /web
- Why:
semanage fcontextwrites persistent pattern-matching rules to the policy file database.restoreconapplies those persistent rules to actual files and subdirectories on disk. - Gotchas: Omitting the regex suffix
(/.*)?. Without it, subdirectories and files created inside/webwill not inherit the context rule persistently. - Verify:
ls -Zd /web
semanage fcontext -l | grep "^/web"
6. Restore the appropriate SELinux context after moving a file.
- Command(s):
restorecon -v /path/to/moved_file
- Why: The
mvcommand preserves a file’s original SELinux attributes from its source path. Moving files into service directories (such as from/rootto/etcor/var/www/html) leaves them with incompatible contexts, triggering AVC denials. - Gotchas: Assuming
mvbehaves likecp.cpgenerates a new target file that inherits destination folder context labels, whereasmvretains original source labels. - Verify:
ls -Z /path/to/moved_file
7. Handle a “port already defined” error when setting a port context with semanage.
- Command(s):
semanage port -m -t ssh_port_t -p tcp 2022
- Why: Using
-a(--add) returns an error if the port number is already assigned within the default SELinux policy store. Using-m(--modify) updates existing port type assignments. - Gotchas: Retrying
semanage port -arepeatedly instead of switching tosemanage port -m. - Verify:
semanage port -l | grep 2022
8. Change a SELinux Boolean persistently (survives reboot).
- Command(s):
setsebool -P httpd_enable_homedirs on
- Why: The
-Pflag writes Boolean changes to default policy files on disk, ensuring settings persist across reboots. - Gotchas: Omitting
-P. Runningsetsebool httpd_enable_homedirs onwithout-Pmodifies runtime memory only; changes reset upon reboot. - Verify:
getsebool httpd_enable_homedirs
(Output displays httpd_enable_homedirs --> on)
9. Identify the file for troubleshooting SELinux messages.
- Path / Command:
/var/log/audit/audit.log
# OR (if setroubleshoot-server is installed)
/var/log/messages
- Why: The Linux kernel audit subsystem writes all raw Access Vector Cache (AVC) denial events directly to
/var/log/audit/audit.log. - Gotchas: Searching
/var/log/securefor SELinux denial events. - Verify:
ausearch -m avc -ts recent
10. Grep the audit log for SELinux denial messages.
- Command(s):
ausearch -m avc -ts recent
# OR
grep "AVC" /var/log/audit/audit.log
- Why:
ausearch -m avcparses raw audit log records specifically for AVC denial events, converting system timestamps into human-readable strings. - Gotchas: Relying solely on
grep, which leaves timestamps in unparsed epoch format. - Verify:
ausearch -m avc -ts today
11. Put SELinux temporarily in permissive mode.
- Command(s):
setenforce 0
- Why:
setenforce 0switches runtime enforcement to Permissive mode, where SELinux logs policy violations toaudit.logbut does not block operations. - Gotchas: Expecting
setenforce 0to persist across reboots. On system restart, SELinux re-reads/etc/selinux/configand reverts to Enforcing mode. - Verify:
getenforce
(Returns Permissive)
12. List all available SELinux Booleans.
- Command(s):
getsebool -a
# OR for detailed descriptions:
semanage boolean -l
- Why: Displays all system Booleans along with their active runtime states and default persistent configurations.
- Gotchas: Scrolling through unformatted lists; pipe output to
grep(e.g.,getsebool -a | grep httpd) to isolate service-specific Booleans. - Verify:
getsebool -a | head -n 10
13. Resolve missing service-specific SELinux man pages.
- Command(s):
dnf install -y selinux-policy-doc
sepolicy manpage -a
- Why: The
selinux-policy-docpackage generates manual pages detailing file contexts, types, and Booleans associated with individual service domains (e.g.,httpd_selinux(8)). - Gotchas: Searching standard manual pages (e.g.,
man httpd) for SELinux context definitions. Useman <service>_selinux. - Verify:
man httpd_selinux
14. Install the package for human-readable SELinux log messages.
- Command(s):
dnf install -y setroubleshoot-server
- Why:
setroubleshoot-serverruns background analysis tools that translate raw AVC denials into human-readable diagnostic reports and plain-text resolution steps inside/var/log/messages. - Gotchas: Assuming
sealertis installed by default on minimal RHEL installations. - Verify:
rpm -q setroubleshoot-server
15. Apply a context type (e.g., httpd_sys_content_t) to a directory.
- Command(s):
semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?"
restorecon -Rv /web
- Why: Adds policy mapping to
/etc/selinux/targeted/contexts/files/file_contexts.localand applies labels across active directory structures. - Gotchas: Using
chcon, which applies temporary context changes that are lost whenrestoreconruns or system relabeling occurs. - Verify:
ls -Zd /web
16. Determine when to use the chcon command.
- Command(s):
chcon -t httpd_sys_content_t /path/to/file
- Why:
chconmodifies file contexts temporarily in runtime memory without updating persistent policy databases. It is useful strictly for rapid testing and troubleshooting. - Gotchas: Using
chconto solve RHCSA exam tasks. Automated grading scripts executerestorecon -R, which immediately wipes out changes made viachcon. Always usesemanage fcontextfollowed byrestorecon. - Verify:
semanage fcontext -l | grep "/path/to/file"
(Returns no output if changed only via chcon)
17. Completely disable SELinux by editing the correct file.
- Command(s):
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
grubby --update-kernel=ALL --args="selinux=0"
- Why: Disables SELinux policy loading at both kernel boot initialization and systemd user space initialization.
- Gotchas: Failing to pass
selinux=0to kernel command line parameters on RHEL 9 systems. - Verify:
cat /etc/selinux/config | grep "^SELINUX="
grubby --info=DEFAULT | grep selinux=0
18. Identify where SELinux logs all of its messages.
- Path / Command:
/var/log/audit/audit.log
- Why: The
auditddaemon writes all kernel AVC denial records directly to this audit log file. - Gotchas: Expecting logs to populate
/var/log/audit/audit.logwhenauditd.serviceis disabled. - Verify:
systemctl is-active auditd
19. Find available context types for a specific service (e.g., FTP).
- Command(s):
sepolicy manpage -t ftpd_t
# OR
man ftpd_selinux
- Why: Displays all context types, file label patterns, and Booleans associated with the
ftpd_tsecurity domain. - Gotchas: Requiring
selinux-policy-docpackage installation prior to querying service SELinux man pages. - Verify:
man ftpd_selinux | grep -A 10 "FILE CONTEXTS"
20. Determine whether a service issue is caused by SELinux.
- Command(s):
setenforce 0
# Test the service, then check for audit logs:
ausearch -m avc -ts recent
setenforce 1
- Why: Temporarily switching to Permissive mode isolates SELinux restrictions. If the service succeeds under Permissive mode and logs an AVC denial, SELinux policies are the cause.
- Gotchas: Leaving SELinux in Permissive mode after testing instead of restoring Enforcing mode (
setenforce 1). - Verify:
getenforce
(Confirms Enforcing mode is restored post-testing)
Lab Tasks
SELinux Practice
Task 1 & 2: Change Apache document root to /web, configure index file, diagnose SELinux failure, and apply persistent fix.
- Command(s):
# Step 1: Install httpd if missing
dnf install -y httpd
# Step 2: Create directory and index file
mkdir -p /web
echo "welcome to my web server" > /web/index.html
# Step 3: Update httpd configuration DocumentRoot
sed -i 's|^DocumentRoot "/var/www/html"|DocumentRoot "/web"|' /etc/httpd/conf/httpd.conf
sed -i 's|<Directory "/var/www/html">|<Directory "/web">|' /etc/httpd/conf/httpd.conf
# Step 4: Restart httpd service
systemctl restart httpd
# Step 5: Test service (Fails with 433 Forbidden due to SELinux default default_t context on /web)
curl http://localhost/
# Step 6: Diagnose denial
ausearch -m avc -ts recent
# Step 7: Apply persistent SELinux context type fix
semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?"
restorecon -Rv /web
# Step 8: Test access again
curl http://localhost/
-
Why:
-
semanage fcontext: Adds a persistent file context rule mapping/weband all its contents tohttpd_sys_content_t. -
restorecon -Rv /web: Relabels the directory structure based on the updated policy database. -
Gotchas: Using
chcon -R -t httpd_sys_content_t /web. Whilecurlworks temporarily, the configuration will fail verification ifrestoreconis run during automated grading. -
Verify:
ls -Zd /web
curl http://localhost/
(Output returns welcome to my web server)
Task 3 & 4: Create /root/hosts, move it to /etc, and restore correct SELinux context.
- Command(s):
# Step 1: Create file in root's home directory
cat << 'EOF' > /root/hosts
192.168.4.200 labipa.example.com
192.168.4.210 server1.example.com
192.168.4.220 server2.example.com
EOF
# Step 2: Move file to /etc (preserves admin_home_t context from /root)
mv /root/hosts /etc/hosts
# Step 3: Verify misconfiguration context
ls -Z /etc/hosts
# Step 4: Restore correct system SELinux context (net_conf_t)
restorecon -v /etc/hosts
- Why: Moving files preserves original context labels (
admin_home_t). Runningrestorecon -v /etc/hostsmatches/etc/hostsagainst system default policy definitions and relabels it tonet_conf_t. - Gotchas: Omitting
restoreconaftermv. Processes restricted by SELinux domains that requirenet_conf_twill be blocked from reading/etc/hosts. - Verify & Reboot Persistence Test:
# Verify SELinux context:
ls -Z /etc/hosts
# Reboot system to verify configuration persistence:
reboot
# Post-reboot checks:
sestatus
ls -Zd /web
ls -Z /etc/hosts
curl http://localhost/
(Confirms web server returns index content and /etc/hosts maintains net_conf_t persistently across reboots)