RHCSA Commands: Security and Firewall
SELinux Mode Management
| Command | Description | Example |
|---|---|---|
getenforce |
Display the current SELinux operating mode (Enforcing, Permissive, or Disabled) | getenforce |
setenforce |
Temporarily switch SELinux between enforcing (1) and permissive (0) modes | setenforce 0 (Permissive) / setenforce 1 (Enforcing) |
sestatus |
Display comprehensive SELinux runtime status, policy name, mode, and Boolean values | sestatus |
sestatus -v |
Display detailed SELinux status including process and file contexts | sestatus -v |
sestatus -b |
Display SELinux Boolean values | sestatus -b | grep nfs_export_all_rw |
grubby |
Permanently disable/enable SELinux via GRUB kernel boot arguments (RHEL 9) | grubby --update-kernel ALL --args selinux=0 (disable) / grubby --update-kernel ALL --remove-args selinux (re-enable) |
SELinux Context Viewing
| Command | Description | Example |
|---|---|---|
ls -Z |
List files and directories with their SELinux context labels | ls -Z /var/www/html |
ps -Z |
List processes with their SELinux domain context | ps -Z |
ps -eZ |
List all processes with their SELinux domain context | ps -eZ |
ps Zaux |
List all processes with full details including SELinux context | ps Zaux |
id -Z |
Display the SELinux context of the current user | id -Z |
ss -Ztul |
Show all network ports and their associated SELinux context labels | ss -Ztul |
semanage port -l |
List all SELinux port labels defined in the policy database | semanage port -l |
semanage login -l |
List the mapping between Linux users and SELinux users | semanage login -l |
seinfo |
Provide information on SELinux policy components (requires setools-console) | seinfo -u (list SELinux users) |
sesearch |
Search rules in the SELinux policy database | sesearch -s httpd_t |
SELinux File Context Management
| Command | Description | Example |
|---|---|---|
chcon |
Change SELinux context on files temporarily (does NOT survive filesystem relabeling) | chcon -t httpd_sys_content_t /web |
chcon -R |
Change SELinux context recursively on a directory tree | chcon -R -u user_u -t public_content_t /tmp/sedir1 |
semanage fcontext -a |
Add a file context rule to the SELinux policy database (persistent) | semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?" |
semanage fcontext -m |
Modify an existing file context rule in the policy database | semanage fcontext -m -t httpd_sys_content_t "/web(/.*)?" |
semanage fcontext -d |
Delete a file context rule from the policy database | semanage fcontext -d "/web(/.*)?" |
semanage fcontext -l |
List file context rules in the policy database | semanage fcontext -l |
semanage fcontext -C -l |
List recent changes to the policy database | semanage fcontext -C -l |
semanage fcontext -a -e |
Add an equivalence rule (map one path to another for context purposes) | semanage fcontext -a -e /home /home1 |
restorecon |
Restore default SELinux contexts on files from the policy database | restorecon /etc/hosts |
restorecon -v |
Restore default context with verbose output | restorecon -v /etc/hosts |
restorecon -R -v |
Restore default contexts recursively with verbose output | restorecon -R -v /web |
restorecon -Rv / |
Relabel the entire filesystem from policy | restorecon -Rv / |
touch /.autorelabel |
Create a file to trigger automatic filesystem relabeling on next reboot | touch /.autorelabel |
SELinux Port Management
| Command | Description | Example |
|---|---|---|
semanage port -l |
List all SELinux port labels in the policy database | semanage port -l |
semanage port -l | grep http |
List port labels for a specific service | semanage port -l | grep http |
semanage port -a |
Add a new port label to the SELinux policy database | semanage port -a -t ssh_port_t -p tcp 2022 |
semanage port -m |
Modify an existing port label (use when port already exists in policy) | semanage port -m -t ssh_port_t -p tcp 443 |
semanage port -d |
Delete a port label from the SELinux policy database | semanage port -d -t http_port_t -p tcp 8010 |
SELinux Boolean Management
| Command | Description | Example |
|---|---|---|
getsebool -a |
Display all SELinux Booleans and their current settings | getsebool -a |
getsebool -a | grep ftp |
Filter Booleans for a specific service | getsebool -a | grep ftp |
getsebool <name> |
Display the current setting of a specific Boolean | getsebool nfs_export_all_rw |
setsebool |
Modify a Boolean value temporarily (lost on reboot) | setsebool ftpd_anon_write on |
setsebool -P |
Modify a Boolean value persistently (survives reboot) | setsebool -P httpd_can_network_connect on |
semanage boolean -l |
List all Booleans with current and default settings | semanage boolean -l |
semanage boolean -l | grep ftpd_anon |
Filter Booleans for a specific service | semanage boolean -l | grep ftpd_anon |
man -K <boolean> |
View manual pages for a specific Boolean (requires selinux-policy-doc) | man -K abrt_anon_write |
SELinux Login/User Mapping
| Command | Description | Example |
|---|---|---|
semanage login -l |
List Linux-to-SELinux user mappings | semanage login -l |
semanage login -a |
Map a Linux user to an SELinux user | semanage login -a -s user_u user10 |
SELinux Troubleshooting
| Command | Description | Example |
|---|---|---|
sealert -a |
Analyze all AVC records in the audit.log and produce a formatted report | sealert -a /var/log/audit/audit.log |
sealert -l <id> |
View details of a specific SELinux alert by its ID | sealert -l fde99ca7-d84d-4956-beec-aa55d0a68044 |
grep AVC |
Search the audit log for SELinux denial messages | grep AVC /var/log/audit/audit.log |
journalctl | grep sealert |
View SELinux alert messages from the systemd journal | journalctl | grep sealert |
ausearch |
Search audit logs for specific commands or events | ausearch -c 'httpd' --raw |
audit2allow |
Generate SELinux policy modules from audit log entries | ausearch -c 'httpd' --raw | audit2allow -M my-httpd |
semodule |
Install custom SELinux policy modules | semodule -X 300 -i my-httpd.pp |
sepolicy generate |
Generate a custom SELinux policy module to allow an application to run | sepolicy generate |
man -k _selinux |
Search for SELinux-specific man pages for a service (requires selinux-policy-doc) | man -k _selinux | grep http |
mandb |
Update the man page database | mandb |
Firewall (firewalld)
| Command | Description | Example |
|---|---|---|
firewall-cmd --add-port |
Add a port to the firewall (runtime, lost on reload/reboot) | firewall-cmd --add-port=2022/tcp |
firewall-cmd --add-port --permanent |
Add a port permanently (survives reboot) | firewall-cmd --add-port=8080/tcp --permanent |
firewall-cmd --reload |
Reload the firewall to apply permanent changes | firewall-cmd --reload |
SSH Server Management
| Command | Description | Example |
|---|---|---|
ssh |
Connect to a remote server via SSH | ssh student@remoteserver |
ssh -p |
Connect to a remote server on a non-default port | ssh -p 2022 student@server1 |
ssh-keygen |
Generate SSH public/private key pairs | ssh-keygen -t rsa |
ssh-copy-id |
Copy local public key to a remote host’s authorized_keys | ssh-copy-id user@remote_host |
ssh-agent |
Start the SSH agent to cache private key passphrases | ssh-agent /bin/bash |
ssh-add |
Add a private key identity to the ssh-agent | ssh-add ~/.ssh/id_rsa |
sshd -t |
Test SSH daemon configuration syntax for errors before restarting | sshd -t |
systemctl restart sshd |
Restart the SSH daemon to apply configuration changes | systemctl restart sshd |
systemctl status -l sshd |
Check the status of the SSH daemon with full log output | systemctl status -l sshd |
systemctl enable --now httpd |
Start and enable a service (used in SSH port change exercises) | systemctl enable --now httpd |
File Ownership Management
| Command | Description | Example |
|---|---|---|
ls -l |
Display file ownership and permissions | ls -l |
chown |
Change file/directory user ownership | chown linda files |
chown -R |
Change ownership recursively | chown -R linda /files |
chown user:group |
Change both user and group ownership | chown lisa.sales myfile |
chown user.group |
Change both user and group ownership (dot notation) | chown lisa:sales myfile |
chown .group |
Change only group ownership (dot notation) | chown .sales myfile |
chown :group |
Change only group ownership (colon notation) | chown :sales myfile |
chgrp |
Change group ownership of a file/directory | chgrp account /home/account |
chgrp -R |
Change group ownership recursively | chgrp -R sales /data/sales |
groups |
Display the groups a user belongs to | groups lisa |
newgrp |
Temporarily change the effective primary group for new files | newgrp sales |
find -user |
Find all files owned by a specific user | find / -user linda |
find -group |
Find all files owned by a specific group | find / -group users |
Basic Permissions Management
| Command | Description | Example |
|---|---|---|
chmod (absolute) |
Set permissions using numeric mode (replaces all current permissions) | chmod 755 /somefile |
chmod (relative) |
Modify permissions relative to current settings | chmod g+w,o-r somefile |
chmod +x |
Add execute permission for all users | chmod +x somefile |
chmod -R a+X |
Add execute permission recursively (directories only, not files) | chmod -R a+X files |
chmod 770 |
Set rwx for user and group, no permissions for others | chmod 770 /data/sales |
chmod 660 |
Set rw for user and group, no permissions for others | chmod 660 filename |
Advanced Permissions (SUID, SGID, Sticky Bit)
| Command | Description | Example |
|---|---|---|
chmod u+s |
Set SUID (Set User ID) on a file | chmod u+s /usr/bin/passwd |
chmod g+s |
Set SGID (Set Group ID) on a directory (new files inherit group) | chmod g+s /data/sales |
chmod +t |
Set sticky bit on a directory (users can only delete their own files) | chmod +t /data/sales |
chmod 2755 |
Set SGID with numeric mode (rwxr-xr-x + SGID) | chmod 2755 /somedir |
chmod 2770 |
Set SGID with rwx for user and group, no access for others | chmod 2770 /shared_dir |
chmod g+s,o+t |
Set both SGID and sticky bit in relative mode | chmod g+s,o+t /data/sales |
Default Permissions (umask)
| Command | Description | Example |
|---|---|---|
umask |
Display or set the default file creation mask | umask 027 |
umask 022 |
Default umask: files get 644, directories get 755 | umask 022 |
umask 027 |
Restrictive umask: files get 640, directories get 750 | umask 027 |
File Attributes (Extended)
| Command | Description | Example |
|---|---|---|
chattr +i |
Set immutable attribute (file cannot be modified, deleted, or renamed) | chattr +i /etc/critical.conf |
chattr +a |
Set append-only attribute (data can be added but not deleted/modified) | chattr +a /var/log/app.log |
chattr +s |
Set secure deletion attribute (overwrites blocks with zeros after deletion) | chattr +s somefile |
chattr -i |
Remove immutable attribute | chattr -i /root/myfile |
chattr -s |
Remove secure deletion attribute | chattr -s somefile |
lsattr |
List extended file attributes | lsattr /etc/critical.conf |
File Operations with SELinux Context Awareness
| Command | Description | Example |
|---|---|---|
cp --preserve=context |
Copy a file while preserving its SELinux context | cp --preserve=context /tmp/sefile2 /etc/default/ |
cp -a |
Copy a file preserving all attributes including SELinux context | cp -a source dest |
mv |
Move a file (SELinux context remains intact after move) | mv ~/hosts /etc |
tar --selinux |
Archive files while preserving SELinux contexts | tar --selinux -cvf archive.tar /path |
System Service Management (used in security exercises)
| Command | Description | Example |
|---|---|---|
systemctl restart |
Restart a system service | systemctl restart sshd |
systemctl status -l |
Check service status with full log output | systemctl status -l sshd |
systemctl enable --now |
Start and enable a service at boot | systemctl enable --now httpd |
Package Management (for SELinux tools)
| Command | Description | Example |
|---|---|---|
dnf install |
Install packages (SELinux tools, setroubleshoot, etc.) | dnf install selinux-policy-doc |
dnf -y install |
Install packages without confirmation prompt | dnf -y install setroubleshoot-server |
dnf search |
Search for available packages | dnf search selinux |
dnf whatprovides |
Find which RPM package provides a specific command | dnf whatprovides */semanage |
Key Configuration Files
| File | Purpose |
|---|---|
/etc/selinux/config |
SELinux configuration file (SELINUX and SELINUXTYPE directives) |
/etc/sysconfig/selinux |
SELinux configuration file (RHEL 9 symlink to /etc/selinux/config) |
/etc/ssh/sshd_config |
SSH daemon configuration file |
/etc/ssh/ssh_config |
SSH client configuration file (system-wide) |
~/.ssh/config |
SSH client configuration file (per-user) |
~/.ssh/authorized_keys |
Public keys authorized for SSH key-based authentication |
/etc/selinux/targeted/contexts/files/file_contexts |
Default SELinux file context definitions |
/etc/selinux/targeted/contexts/files/file_contexts.local |
Custom SELinux file context definitions |
/var/log/audit/audit.log |
SELinux audit log (AVC denial messages) |
/var/log/messages |
System log (SELinux messages when auditd is not running) |
/var/log/secure |
SSH authentication log (failed login attempts) |
/etc/profile.d/umask.sh |
System-wide umask configuration script |
~/.bash_profile or ~/.profile |
User-specific umask configuration |
/.autorelabel |
Trigger file for automatic filesystem relabeling on reboot |
SSH Configuration Directives (sshd_config)
| Directive | Purpose | Example |
|---|---|---|
Port |
Define the TCP listening port for SSH | Port 2022 |
PermitRootLogin |
Control whether root can log in via SSH | PermitRootLogin prohibit-password |
PasswordAuthentication |
Enable or disable password-based authentication | PasswordAuthentication no |
AllowUsers |
Restrict SSH login to specific users (space-separated) | AllowUsers student |
MaxAuthTries |
Maximum number of authentication attempts per connection | MaxAuthTries 6 |
MaxSessions |
Maximum number of open sessions per connection | MaxSessions 10 |
UseDNS |
Enable/disable DNS reverse lookup for clients | UseDNS no |
TCPKeepAlive |
Monitor whether the client is still available | TCPKeepAlive yes |
ClientAliveInterval |
Interval in seconds to send keepalive packets to client | ClientAliveInterval 300 |
ClientAliveCountMax |
Number of keepalive packets before disconnecting | ClientAliveCountMax 3 |
ServerAliveInterval |
Client-side: interval to send keepalive to server | ServerAliveInterval 300 |
ServerAliveCountMax |
Client-side: max keepalive packets before disconnecting | ServerAliveCountMax 3 |
GSSAPIAuthentication |
Enable/disable GSSAPI authentication | GSSAPIAuthentication no |
SELinux Troubleshooting Workflow
- Service fails →
setenforce 0→ Test service - If it works in permissive → It is an SELinux issue
- Check logs:
grep AVC /var/log/audit/audit.log - Analyze:
sealert -a /var/log/audit/audit.log - Fix context:
semanage fcontext -a -t <type> "<path>(/.*)?"thenrestorecon -R -v <path> - Fix port:
semanage port -a -t <type> -p tcp <port> - Fix boolean:
setsebool -P <boolean> on - Re-enable:
setenforce 1