ls -Zd /# system_u:object_r:root_t:s0 /
ls -Z /# system_u:object_r:bin_t:s0 bin# system_u:object_r:boot_t:s0 boot# system_u:object_r:default_t:s0 data
ps -Zaux | grep systemd-udevd# system_u:system_r:udev_t:s0-s0:c0.c1023 root 565 0.0 0.1 36964 15172 ? Ss Mar17 0:01 /usr/lib/systemd/systemd-udevd
:), and the meaning of each field is as follows:Field | Description |
system_u | SELinux user |
object_r | Role |
root_t | Type |
s0 | Sensitivity level |
object_r (representing a file or directory) and system_r (representing a process).lowlevel-highlevel. If the levels are the same, it is written as s0 (same as sensitivity). Each level is a sensitivity-category pair, and the category is optional. If a category is present, the level is written as sensitivity:category-set. If no category is present, it is written as sensitivity.# Install a packagesudo dnf install <package># View installed packagesrpm -q <package>
ls.sestatus command to view the SELinux policy type. The following is an example:sestatus# SELinux status: enabled# SELinuxfs mount: /sys/fs/selinux# SELinux root directory: /etc/selinux# Loaded policy name: targeted # Current policy type# Current mode: enforcing # The current running mode is enforcing mode
/etc/selinux/config file, which controls the SELinux working mode and policy type.selinux=0 on the kernel command line.getenforce command. If you need to obtain more information, you can run the sestatus command to view it.getenforce# Enforcing
setenforce command can conveniently switch between permissive and enforcing modes, but it cannot switch to disabled mode.# Switch to permissive modesetenforce 0# Switch to enforcing modesetenforce 1
/etc/selinux/config configuration file.fixfiles -F onbootreboot
seinfo command to query which specific rules are included.seinfo -b# Conditional Booleans: 247# allow_domain_fd_use# allow_ftpd_full_access# ...
getsebool/setsebool command to view or set the enablement status of the rule.# View all Boolean valuesgetsebool -a# View a specific Boolean valuegetsebool httpd_can_network_connect# Set a Boolean value (temporary)setsebool httpd_can_network_connect on# Set a Boolean value (permanent, written to policy)setsebool -P httpd_can_network_connect on
seinfo command to query the names of related policy rules. However, to view the specific content of a rule, you need to use the sesearch command.# View all allowed rulessesearch --allow# allow NetworkManager_dispatcher_ddclient_t init_t:service status;# allow NetworkManager_dispatcher_ddclient_t init_t:system status;# allow NetworkManager_dispatcher_ddclient_t init_t:unix_stream_socket sendto;
semanage is a powerful command that includes, but is not limited to, SELinux user query, user management, mapping query, rule query, context query, and more.# View the mapping between login accounts and SELinux userssudo semanage login -l# View the mapping between SELinux users and rolessudo semanage user -l# Query rules (Boolean values)sudo semanage boolean -l# Query security context.sudo semanage fcontext -l
getsebool command to view the current Boolean status:# View all Boolean values and their statusesgetsebool -a# View a specific Boolean valuegetsebool httpd_can_network_connect# httpd_can_network_connect --> off
semanage boolean -l to view detailed descriptions of Booleans, including their default and current values:sudo semanage boolean -l | grep httpd# httpd_can_network_connect (off , off) Allow httpd to network connect# httpd_can_network_connect_db (off , off) Allow httpd to network connect db# httpd_enable_cgi (on , on) Allow httpd to enable cgi
setsebool command to set Boolean values:# Temporary setting (invalid after restart)setsebool httpd_can_network_connect on# Permanent setting (written to the policy file and still effective after restart)setsebool -P httpd_can_network_connect on
-P option writes the modification to the policy file, which may take a few seconds. Without -P, the change takes effect only for the current runtime and reverts to the default after a restart.Boolean | Description |
httpd_can_network_connect | Allows httpd to initiate network connections. |
httpd_can_network_connect_db | Allows httpd to connect to database ports. |
httpd_enable_cgi | Allows httpd to execute CGI scripts. |
httpd_read_user_content | Allows httpd to read user home directory content. |
httpd_unified | Allows httpd to uniformly access all httpd-type files. |
ftp_home_dir | Allows vsftpd to access user home directories. |
nfs_export_all_rw | Allows read-write access to NFS shares. |
samba_enable_home_dirs | Allows Samba to share user home directories. |
ssh_sysadm_login | Allows sshd to directly log in as sysadm_t. |
use_nfs_home_dirs | Allows use of NFS-mounted home directories. |
chcon command is used to temporarily modify the security context of a file or directory. This modification is reverted to the default value after the file system is relabeled or restorecon is executed.# Change the file typechcon -t httpd_sys_content_t /var/www/html/index.html# Recursively change the type of a directory and its subfileschcon -R -t httpd_sys_content_t /var/www/html/# Change the complete context of a file (user:role:type)chcon system_u:object_r:httpd_sys_content_t:s0 /var/www/html/index.html# View the modification resultls -Z /var/www/html/index.html# system_u:object_r:httpd_sys_content_t:s0 /var/www/html/index.html
restorecon command is used to restore the security context of a file or directory to the default value defined in the policy.# Restore the default context of a single filerestorecon -v /var/www/html/index.html# Recursively restore the default context of all files in a directoryrestorecon -Rv /var/www/html/# Force relabeling (ignore existing labels)restorecon -R -F /var/www/html/
semanage fcontext command is used to permanently modify the default security context of a file or directory. After the modification, you need to run restorecon for it to take effect.# Add a new file context rulesudo semanage fcontext -a -t httpd_sys_content_t "/custom/web(/.*)?"# Modify an existing file context rulesudo semanage fcontext -m -t httpd_sys_content_t "/custom/web(/.*)?"# Make the rules take effectrestorecon -Rv /custom/web/# View custom file context rulessudo semanage fcontext -l | grep /custom/web# Delete a file context rulesudo semanage fcontext -d "/custom/web(/.*)?"
semanage fcontext are stored in the policy file under the /etc/selinux/targeted/contexts/files/ directory and remain effective after a restart. Path matching uses regular expressions, and (/.*)? matches the directory and all of its subcontent.semodule command.# List all installed modulessemodule -l# View the number of modulessemodule -l | wc -l
# Install a policy modulesudo semodule -i mymodule.pp
# Remove a policy modulesudo semodule -r mymodule
# Upgrade a policy modulesudo semodule -u mymodule.pp# Reload all modulessudo semodule -R# Build and install modules (with semodule_compile)# Typically, use checkmodule and semodule_package to compile .te files first.checkmodule -M -m -o mymodule.mod mymodule.tesemodule_package -o mymodule.pp -m mymodule.modsudo semodule -i mymodule.pp
.te (Type Enforcement) file, which is then compiled into a .mod file by checkmodule, packaged into a .pp file by semodule_package, and finally installed by using semodule -i.# View AVC denial messages in the audit logsudo ausearch -m AVC -ts recent# Monitor AVC denial messages in real timesudo tail -f /var/log/audit/audit.log | grep AVC# Use ausearch to view recent SELinux denialssudo ausearch -m AVC,USER_AVC -ts today
sealert command (provided by the setroubleshoot-server package) converts AVC denial messages into human-readable detailed descriptions and provides remediation suggestions:# Analyze the audit log and generate a detailed reportsudo sealert -a /var/log/audit/audit.log# Analyze a single AVC messagesudo sealert -l <message_id>
sealert:SELinux is preventing /usr/sbin/httpd from name_connect access on the tcp_socket.***** Plugin catchall_boolean (89.3 confidence) suggests ********************If you want to allow httpd to network connectThen you must tell SELinux about this by enabling the 'httpd_can_network_connect' boolean.Dosetsebool -P httpd_can_network_connect 1
setroubleshoot service continuously monitors the audit log, converts SELinux denial messages into detailed descriptions, and sends them to the system log and DBus:# Check the setroubleshoot service statussudo systemctl status setroubleshoot# Start the service and enable it to start on boot.sudo systemctl enable --now setroubleshoot# View messages generated by setroubleshootsudo journalctl -t setroubleshoot
# 1. Check the current SELinux statusgetenforcesestatus# 2. View recent AVC denial messagessudo ausearch -m AVC -ts recent# 3. Use sealert to analyze and obtain remediation suggestionssudo sealert -a /var/log/audit/audit.log# 4. If you determine that access needs to be allowed, follow the sealert recommendations.# Setting a Boolean valuesetsebool -P <boolean_name> on# Or modify the file contextsudo semanage fcontext -a -t <type> "<path>(/.*)?"restorecon -Rv <path>
Port 22 to Port 2222:sudo vi /etc/ssh/sshd_config
sudo semanage port -l | grep ssh
ssh_port_t tcp 22
sudo semanage port -a -t ssh_port_t -p tcp 2222
sudo semanage port -l | grep ssh
ssh_port_t tcp 2222, 22
sudo systemctl restart sshd
sudo ss -tlnp | grep sshd
-m (modify) instead of -a (add)./custom/web) as the website root directory, SELinux blocks the web service from accessing that directory. You need to set the correct security context for it.sudo mkdir -p /custom/web
ls -Zd /custom/web
unconfined_u:object_r:default_t:s0 /custom/web
sudo semanage fcontext -a -t httpd_sys_content_t "/custom/web(/.*)?"
sudo restorecon -Rv /custom/web/
ls -Zd /custom/web
system_u:object_r:httpd_sys_content_t:s0 /custom/web
DocumentRoot "/var/www/html" to DocumentRoot "/custom/web":sudo vi /etc/httpd/conf/httpd.conf
sudo systemctl restart httpd
curl http://localhost/
getsebool -a | grep httpd_can_network
-P option makes it persistent):setsebool -P httpd_can_network_connect on
setsebool -P httpd_can_network_connect_db on
getsebool httpd_can_network_connect
httpd_can_network_connect --> on
sudo systemctl restart httpd
getsebool -a | grep nfs
-P option makes it persistent):setsebool -P use_nfs_home_dirs on
getsebool use_nfs_home_dirs
use_nfs_home_dirs --> on
sudo mount -t nfs nfs-server:/home /home
ls -Z /home/user/
getenforce
Enforcing
setenforce 0
sudo ausearch -m AVC -ts recent
sudo sealert -a /var/log/audit/audit.log
setsebool -P <boolean_name> on
<type> and <path> with the actual type and path):sudo semanage fcontext -a -t <type> "<path>(/.*)?"sudo restorecon -Rv <path>
setenforce 1
<service> with the actual service name):sudo systemctl restart <service>
피드백