Skip to content

Intermediate · SELinux and SSH · topic 31 of 57 · 25 min

SELinux booleans and port labels

Explanation

Booleans are on/off switches in the policy for common optional behaviour, such as letting httpd read home directories or connect to a database. getsebool lists them; setsebool -P changes them permanently. Ports have labels too: to run a service on a non-standard port, add the port with semanage port -a.

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.

bash
$ 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
semanage boolean -l shows the current and the default value and a description.
Careful: setsebool WITHOUT -P changes the running value only; it is lost at reboot. In exams and production, use -P (persistent). It takes a few seconds because the policy is rebuilt.
BooleanTurn on when…
httpd_can_network_connect_dbA PHP or Python web app talks to a database on another server
httpd_can_network_connectThe web server must connect to any network port (e.g. a reverse proxy to an app)
httpd_enable_homedirsUsers publish pages from ~/public_html
httpd_use_nfsWeb content lives on an NFS share
samba_enable_home_dirsSamba shares users' home directories
ftpd_anon_writeAnonymous 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.

bash
$ 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 --reload
Three things for a new port: the service config, the SELinux port label, and the firewall.

If 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

boolean = switch

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.

bash
# 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 sshd

Practice (0/3 done)

Task 1 · Question

Which command turns on httpd_enable_homedirs so that it stays on after reboot?

Task 2 · Question

httpd fails to start on port 8888 with "Permission denied ... could not bind". The firewall is open. What is missing?

Task 3 · Code · Python

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

Question 1

1.What happens to a boolean set with setsebool (no -P) after reboot?

Question 2

2.Which command shows booleans with descriptions?

Question 3

3.A port already has a different label. Which semanage option changes it?

Question 4

4.Which boolean lets a web app connect to a remote database?

Read more (free)

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.

From SELinux: what it is and its modes

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.

From Troubleshooting SELinux denials

Which port does HTTPS use by default?Related

Answer: 443

HTTPS uses TCP 443 (and UDP 443 for HTTP/3).

From Ports and protocols

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

  1. 2 more topics in “SELinux and SSH” before its exam: Troubleshooting SELinux denials, SSH: keys, ssh-copy-id and hardening sshd.
  2. Module 6 exam: SELinux and SSH · 13 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. 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