Intermediate · SELinux and SSH · topic 31 of 57 · 25 min
SELinux booleans and port labels
Explanation
Booleans: switches for optional behaviour
The policy authors knew some setups need extra access that most servers should not have. Instead of making you write policy, they added booleans: named switches, off by default for safety. For example, a web app that connects to a remote database needs httpd_can_network_connect_db on.
$ getsebool -a | grep httpd_can_network
httpd_can_network_connect --> off
httpd_can_network_connect_db --> off
httpd_can_network_memcache --> off
httpd_can_network_relay --> off
# semanage boolean -l | grep httpd_enable_homedirs
httpd_enable_homedirs (off , off) Allow httpd to read home directories
# setsebool -P httpd_enable_homedirs on
# getsebool httpd_enable_homedirs
httpd_enable_homedirs --> on| Boolean | Turn on when… |
|---|---|
| httpd_can_network_connect_db | A PHP or Python web app talks to a database on another server |
| httpd_can_network_connect | The web server must connect to any network port (e.g. a reverse proxy to an app) |
| httpd_enable_homedirs | Users publish pages from ~/public_html |
| httpd_use_nfs | Web content lives on an NFS share |
| samba_enable_home_dirs | Samba shares users' home directories |
| ftpd_anon_write | Anonymous FTP uploads are allowed (rarely wise) |
Read the list with semanage boolean -l | grep <service>. The descriptions are short and clear. The man pages named after services (for example man httpd_selinux, after installing selinux-policy-doc) explain each one.
Port labels
Network ports have SELinux types too. The policy allows httpd_t to listen only on ports labelled http_port_t; sshd_t only on ssh_port_t. So if you move a web server to port 82, SELinux blocks it even with the firewall open.
$ semanage port -l | grep -w http_port_t
http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
# (Listen 82 in httpd.conf)
# systemctl restart httpd
Job for httpd.service failed because the control process exited with error code.
# journalctl -u httpd -n 3 --no-pager
... (13)Permission denied: AH00072: make_sock: could not bind to address [::]:82
# semanage port -a -t http_port_t -p tcp 82
# systemctl restart httpd # works now
# firewall-cmd --add-port=82/tcp --permanent && firewall-cmd --reloadIf the port already has another label (for example 8080 is http_cache_port_t), semanage port -a says it is already defined; use -m (modify) instead of -a. semanage port -d removes a custom entry.
Common mistakes
- Forgetting -P with setsebool.
- Opening the firewall for a new port but forgetting the SELinux port label, or the reverse.
- Turning on broad booleans like httpd_can_network_connect when a narrower one (httpd_can_network_connect_db) is enough.
- Guessing boolean names. Search with getsebool -a | grep word.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
SELinux booleans and port labels · 1/6
Booleans are on and off switches for optional behaviour in the policy.
Prefer a video by a teacher? Find free videos on “selinux booleans setsebool semanage port tutorial” on YouTube ↗
In real life: Moving SSH to port 2222 at a hosting provider
A hosting provider in Bengaluru moves SSH on its jump servers from port 22 to 2222 to cut down noise from automated scanners. The engineer changes Port in a sshd drop-in file, adds the SELinux label for 2222, opens the firewall, and tests a NEW SSH session before closing the old one. Without the semanage step, sshd would have failed to start and locked everyone out.
# semanage port -a -t ssh_port_t -p tcp 2222
# semanage port -l | grep ssh_port_t
ssh_port_t tcp 2222, 22
# firewall-cmd --add-port=2222/tcp --permanent && firewall-cmd --reload
# sshd -t && systemctl restart sshdPractice (0/3 done)
Which command turns on httpd_enable_homedirs so that it stays on after reboot?
httpd fails to start on port 8888 with "Permission denied ... could not bind". The firewall is open. What is missing?
Write ports_for(semanage_output, port_type) for text like the output of "semanage port -l". Each line is: type, protocol, then a comma-separated list of ports or ranges ("80, 81, 8008-8009"). Return a sorted list of all TCP port numbers for that type, expanding ranges.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Troubleshooting SELinux denialsSELinux and SSH · same module
- Ports and protocolsCyber Security: from zero to SOC analyst · Networking for security
- Ports, services and what scanning doesEthical Hacking and Penetration Testing (legal labs) · Scanning with nmap
- Persistent storage and containers as systemd servicesBoot, recovery and containers
- Booleans and comparisonsPython Programming · Text and decisions
- Running containers: ports, volumes, networks and limitsDevOps: Git, Docker, Kubernetes and CI/CD · Docker
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What happens to a boolean set with setsebool (no -P) after reboot?
Answer: It returns to its saved value
Without -P the change is runtime only.
Which command shows booleans with descriptions?
Answer: semanage boolean -l
semanage boolean -l includes descriptions.
A port already has a different label. Which semanage option changes it?
Answer: -m
-m modifies an existing record.
Which boolean lets a web app connect to a remote database?
Answer: httpd_can_network_connect_db
httpd_can_network_connect_db.
Which command applies the labels from the policy database to existing files?Related
Answer: restorecon
restorecon resets labels to the database values.
From Contexts and file labels: ls -Z, semanage fcontext and restorecon
What does SELinux add to normal Linux permissions?Related
Answer: Mandatory, label-based rules that even root processes must follow
It is mandatory access control based on labels.
Which command shows SELinux denials from the last few minutes?Related
Answer: ausearch -m AVC -ts recent
ausearch filters audit records by type and time.
Which port does HTTPS use by default?Related
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 2 more topics in “SELinux and SSH” before its exam: Troubleshooting SELinux denials, SSH: keys, ssh-copy-id and hardening sshd.
- Module 6 exam: SELinux and SSH · 13 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Automation basics: scheduling, archives and shell scripts. 57 topics left in the course before the final exam.
← Previous: Contexts and file labels: ls -Z, semanage fcontext and restorecon