SELinux Commands
SELinux security context management, booleans, and policy commands.
Status & Mode
getenforceShow current SELinux mode
sestatusShow detailed SELinux status
sestatus -vShow verbose SELinux status
cat /etc/selinux/configView SELinux configuration
sudo setenforce 1Set SELinux to enforcing mode
sudo setenforce 0Set SELinux to permissive mode
sudo setenforce EnforcingAlternative enforcing command
sudo setenforce PermissiveAlternative permissive command
File Contexts
ls -Z /path/to/fileShow SELinux context of file
ls -laZ /path/to/dirShow contexts of directory contents
sudo chcon -t httpd_sys_content_t /var/www/html/fileChange file type context
sudo chcon -R -t httpd_sys_content_t /var/www/html/Recursively change context
sudo chcon --reference=/var/www/html /path/to/fileCopy context from reference
sudo restorecon -v /path/to/fileRestore default file context
sudo restorecon -Rv /path/to/dirRecursively restore contexts
matchpathcon /path/to/fileShow expected context for path
Policy Management
sudo semanage fcontext -lList all file context mappings
sudo semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?"Add file context rule
sudo semanage fcontext -d "/web(/.*)?"Delete file context rule
sudo semanage fcontext -m -t httpd_sys_rw_content_t "/web(/.*)?"Modify file context rule
sudo semanage permissive -a httpd_tMake domain permissive
sudo semanage permissive -d httpd_tRemove domain from permissive
sudo semanage permissive -lList permissive domains
Boolean Management
getsebool -aList all SELinux booleans
getsebool httpd_can_network_connectGet specific boolean value
sudo setsebool httpd_can_network_connect onEnable boolean (temporary)
sudo setsebool -P httpd_can_network_connect onEnable boolean (permanent)
sudo setsebool -P httpd_can_network_connect offDisable boolean (permanent)
semanage boolean -lList booleans with descriptions
semanage boolean -l | grep httpdSearch for httpd booleans
Port Management
sudo semanage port -lList all port type definitions
sudo semanage port -l | grep httpSearch for http port types
sudo semanage port -a -t http_port_t -p tcp 8080Add port to http_port_t
sudo semanage port -d -t http_port_t -p tcp 8080Delete port from type
sudo semanage port -m -t http_port_t -p tcp 8080Modify port type
seinfo --portcon=80Show port context for port 80
Process & Domain
ps auxZShow all processes with SELinux context
ps -eZ | grep httpdShow httpd process context
id -ZShow current user SELinux context
sudo runcon -t unconfined_t commandRun command in specific context
sudo semanage login -lList login mappings
sudo semanage user -lList SELinux users
Troubleshooting
sudo ausearch -m avc -ts recentSearch recent AVC denials
sudo ausearch -m avc -ts todaySearch today's AVC denials
sudo ausearch -m avc -c httpdSearch denials for httpd
sudo audit2why < /var/log/audit/audit.logExplain audit denials
sudo sealert -a /var/log/audit/audit.logAnalyze audit log (GUI tool)
sudo cat /var/log/audit/audit.log | audit2allow -m mymoduleGenerate policy module
sudo grep "denied" /var/log/audit/audit.logFind denied operations
journalctl -t setroubleshootView setroubleshoot journal
Module Management
sudo semodule -lList installed policy modules
sudo semodule -i mymodule.ppInstall policy module
sudo semodule -r mymoduleRemove policy module
sudo semodule -BRebuild and reload policy
sudo semodule -d mymoduleDisable policy module
sudo semodule -e mymoduleEnable policy module
checkmodule -M -m -o mymodule.mod mymodule.teCompile type enforcement
semodule_package -o mymodule.pp -m mymodule.modCreate policy package
Policy Generation
sudo audit2allow -a -M mymoduleGenerate module from all denials
sudo audit2allow -i /var/log/audit/audit.log -M mymoduleGenerate from audit log
sudo ausearch -m avc | audit2allow -M mymoduleGenerate from recent denials
sudo semodule -i mymodule.ppInstall generated module
audit2allow -w -aShow why denials occurred
audit2allow -R -aGenerate with reference policy
Network & Services
sudo semanage port -l | grep sshCheck SSH port label
sudo setsebool -P httpd_can_network_connect_db onAllow httpd to connect to DB
sudo setsebool -P httpd_can_sendmail onAllow httpd to send mail
sudo setsebool -P httpd_use_nfs onAllow httpd to use NFS
sudo setsebool -P allow_ftpd_full_access onAllow FTP full access
sudo setsebool -P samba_enable_home_dirs onAllow Samba home dirs
Configuration Files
sudo nano /etc/selinux/configEdit SELinux config (requires reboot)
ls /etc/selinux/List SELinux config directory
cat /etc/selinux/targeted/contexts/files/file_contextsView file contexts
ls /var/log/audit/List audit logs
sudo touch /.autorelabelForce relabel on next boot
Common Booleans
sudo setsebool -P httpd_can_network_connect 1Allow httpd outbound connections
sudo setsebool -P httpd_read_user_content 1Allow httpd to read user content
sudo setsebool -P named_write_master_zones 1Allow named zone writes
sudo setsebool -P mysql_connect_any 1Allow MySQL any connections
sudo setsebool -P virt_use_nfs 1Allow VMs to use NFS
sudo setsebool -P container_manage_cgroup 1Allow containers cgroup access