Here’s a structured guide to SELinux permission management in CentOS:
Verify if SELinux is enabled and its current mode (Enforcing/Permissive/Disabled):
getenforce
To check detailed status (including mode and policy type):
sestatus
setenforce 1 # Enable Enforcing mode
setenforce 0 # Enable Permissive mode
/etc/selinux/config and modify the SELINUX= line:sudo vi /etc/selinux/config
# Change to:
SELINUX=enforcing # or "permissive"
Reboot the system to apply changes.SELinux uses security contexts (format: user:role:type:level) to define access rules. Key commands:
View Context:
ls -Z /path/to/file_or_directory
Example output: -rw-r--r--. root root system_u:object_r:httpd_sys_content_t:s0
Temporary Modify Context (e.g., change a file to httpd_sys_content_t for Apache):
chcon -t httpd_sys_content_t /var/www/html/index.html
Permanent Modify Context:
Use semanage fcontext to add a new rule, then restorecon to apply it:
sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/html(/.*)?" # Recursive for directory
sudo restorecon -Rv /var/www/html # Restore default context recursively
Restore Default Context:
restorecon -Rv /path/to/file_or_directory
Booleans are toggleable options that enable/disable specific SELinux features (e.g., allowing Apache to connect to databases).
getsebool -a
-P):sudo setsebool -P httpd_can_network_connect_db 1
When you encounter “AVC denied” errors (e.g., a process can’t access a file):
sudo ausearch -m avc -ts recent # Filter for SELinux denials
audit2why to explain why access was denied:sudo ausearch -m avc -ts recent | audit2why
sudo grep httpd /var/log/audit/audit.log | audit2allow -M my_httpd_policy
sudo semodule -i my_httpd_policy.pp # Load the custom policy
Some services require specific SELinux ports (e.g., Apache needs port 80/tcp labeled as http_port_t).
sudo semanage port -l | grep http
sudo semanage port -a -t http_port_t -p tcp 8080
sudo semanage port -d -t http_port_t -p tcp 8080
For fine-grained control over user access (not commonly used for basic setups):
sudo semanage user -a -R user_u -r object_r -s system_u username
sudo semanage user -d username
/etc/selinux/config) before making changes.These steps cover essential SELinux permission management tasks in CentOS. For complex scenarios (e.g., MLS policies), refer to the official Red Hat SELinux documentation.