- 2 Mail Server Setup, 3 NGINX Configuration and 4 Docker Setup must be done.
Assumptions
- Firewalld was turned off in 1 Post-Install Configuration, and iRedMail’s Firewalld configuration was not applied during the installation.
- The outside network interface is
eth0, and SSH runs on the default port, 22. - Commands are run on the server with root rights (
sudo).
Which Ports We’ll Open#
| Service | Port | Why |
|---|---|---|
| ssh | 22/tcp | The default port for SSH. If SSH runs on another port, add it with --add-port=<PORT>/tcp |
| http / https | 80/tcp, 443/tcp | NGINX |
| smtp | 25/tcp | Mail coming from other servers |
| smtp-submission | 587/tcp | Clients sending mail (STARTTLS) |
| smtps | 465/tcp | Clients sending mail (TLS) |
| imap / imaps | 143/tcp, 993/tcp | Reading mail |
Starting Point#
Let’s check the starting point:
sudo systemctl is-active firewalld
sudo firewall-offline-cmd --get-default-zone
sudo firewall-offline-cmd --zone=public --list-allThe output should look like this:
inactive
public
public (default)
target: default
ingress-priority: 0
egress-priority: 0
icmp-block-inversion: no
interfaces:
sources:
services: cockpit dhcpv6-client ssh
ports:
protocols:
forward: yes
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:This is the default Firewalld configuration. Firewalld is off right now; we’ll keep it that way for now.
Public Zone Configuration#
Cockpit is allowed by default; we won’t use it, so let’s remove it. Firewalld is off, so we’ll use firewall-offline-cmd instead of firewall-cmd.
sudo firewall-offline-cmd --zone=public --remove-service-from-zone=cockpitWe add the services we use:
sudo firewall-offline-cmd --zone=public \
--add-service=ssh --add-service=http --add-service=https \
--add-service=smtp --add-service=smtp-submission --add-service=smtps \
--add-service=imap --add-service=imapsssh is on by default, so the warning Warning: ALREADY_ENABLED: ssh is normal. After it, a success output follows.
The services we added are http and https for NGINX, and the ones the mail service needs. I won’t use POP3, so I didn’t add it.
Let’s check the changes:
sudo firewall-offline-cmd --zone=public --list-allThe output should look like this:
public (default)
target: default
ingress-priority: 0
egress-priority: 0
icmp-block-inversion: no
interfaces:
sources:
services: dhcpv6-client http https imap imaps smtp smtp-submission smtps ssh
ports:
protocols:
forward: yes
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:Rescue Timer and Start#
In case something goes wrong after Firewalld is on and we can’t connect over SSH, let’s set up a timer that stops Firewalld after 10 minutes:
sudo systemd-run --on-active=10min --unit=fw-rescue systemctl stop firewalldThen we can enable and start Firewalld:
sudo systemctl enable --now firewalld
sudo firewall-cmd --get-active-zonesWhen Firewalld starts, the current SSH session doesn’t drop, because connections that are already open are still allowed. So let’s do the test by opening a new SSH connection from a new terminal. If the connection works, we can cancel the timer:
sudo systemctl stop fw-rescue.timerDocker Integration#
We installed Docker before configuring Firewalld, so Docker couldn’t create its own Firewalld settings (the docker zone and the docker-forwarding policy). Restarting Docker is enough for this:
sudo systemctl restart dockerCheck#
sudo firewall-cmd --get-active-zonesdocker
interfaces: docker0
public (default)
interfaces: eth0sudo firewall-cmd --zone=docker --list-alldocker (active)
target: ACCEPT
ingress-priority: 0
egress-priority: 0
icmp-block-inversion: no
interfaces: docker0
sources:
services:
ports:
protocols:
forward: yes
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:sudo firewall-cmd --get-policiesallow-host-ipv6 docker-forwarding gateway-dmz-to-HOST gateway-lan-to-HOST gateway-lan-to-work gateway-lan-to-world gateway-world-to-HOSTIf the outputs show the docker zone and the docker-forwarding policy, Docker’s Firewalld configuration was done.
Blocking Outgoing Port 25#
Everything that goes out to the internet through the server, so the containers and the VPN clients we’ll set up in 7 WireGuard Setup, uses the same IP addresses as the mail server. The PTR record of these addresses points to mail.xenonly.com. If a compromised container or a device on the VPN sends spam directly out on port 25, our mail server’s IP gets on a blocklist.
Mail servers talk to each other on port 25, and only Postfix on the host needs to go out on this port. The containers send their mail to our own mail server on port 587, with a login. That connection is not forwarded, it comes straight to the host, so the block doesn’t affect it.
First let’s see the problem. Let’s try to connect from a container to Gmail’s mail server on port 25:
sudo docker run --rm alpine nc -v -w 5 gmail-smtp-in.l.google.com 25gmail-smtp-in.l.google.com (64.233.167.26:25) openThe open line shows that the container can connect directly to a mail server outside on port 25.
Let’s create a policy that rejects traffic that goes through the server (is forwarded) to port 25 outside:
# Creates a new, empty policy named no-smtp-out.
sudo firewall-cmd --permanent --new-policy=no-smtp-out
# Applies to traffic forwarded from any zone (ANY) to the public zone (the internet).
# ANY doesn't include the server's own traffic (HOST), so Postfix is not affected.
sudo firewall-cmd --permanent --policy=no-smtp-out --add-ingress-zone=ANY
sudo firewall-cmd --permanent --policy=no-smtp-out --add-egress-zone=public
# Makes the policy run before the zone rules and the other policies.
sudo firewall-cmd --permanent --policy=no-smtp-out --set-priority=-10
# Rejects connections to port 25. Logs at most 3 lines per minute.
sudo firewall-cmd --permanent --policy=no-smtp-out \
--add-rich-rule='rule port port="25" protocol="tcp" log prefix="SMTP-OUT-BLOCK: " level="warning" limit value="3/m" reject'
sudo firewall-cmd --reloadPolicies are checked in order of their priority value; the smaller value comes first. Policies with a negative priority run before the zone rules. -10 makes this policy run before the ACCEPT target of the docker zone and before the vpn-internet policy (-1) we’ll create in part 7.
We didn’t change the policy’s target; the default is CONTINUE. Traffic to ports other than 25 moves on to the next rule and works as before. We didn’t set family in the rich rule, so the rule applies to both IPv4 and IPv6.
We used ANY as the ingress zone, so the rule also applies by itself to zones added later (for example the vpn zone in part 7).
Let’s check the policy:
sudo firewall-cmd --info-policy=no-smtp-outThe output should show priority: -10, ingress-zones: ANY, egress-zones: public and the rich rule we added.
Let’s run the test again:
sudo docker run --rm alpine nc -v -w 5 gmail-smtp-in.l.google.com 25This time the open line should not show up; nc should print a connection error. The blocked attempts go to the kernel log:
sudo journalctl -k -g SMTP-OUT-BLOCKThe SRC= field in the log shows the IP address of the container or VPN client that tried to go out on port 25. If there is an unexpected entry here, that container or device may be compromised.
To see that Postfix is not affected, let’s send a mail to a Gmail address and look for the status=sent line in Postfix’s log:
sudo journalctl -t postfix/smtp -n 20mail.example.com). It is set in the VPS provider’s panel, not at the domain’s DNS provider.https:// and secure mail connections. The server proves who it is with a certificate, for example one from Let’s Encrypt.