This document describes the firewall configuration required for the secure email server architecture.
A properly configured firewall is essential for securing your email server. This guide covers firewall configuration for both the proxy server and client server:
- Proxy Server Firewall: Controls which ports are accessible from the internet and configures port forwarding for specific mail-related services
- Client Server Firewall: Minimal configuration that only allows WireGuard VPN communication
If you're using IONOS as your VPS provider for the proxy server (as recommended in the testing environment), you'll need to configure their provider-level firewall in addition to the system firewall (UFW).
IONOS provides a web-based firewall configuration interface that you must use to open ports before they can be accessed, even if they are allowed in your system firewall.
- Login to your IONOS control panel
- Navigate to the "Server & Cloud" section
- Select your VPS
- Go to "Network" → "Firewall Rules"
- Create rules to open the following ports:
- 22/TCP (SSH)
- 80/TCP (HTTP)
- 443/TCP (HTTPS)
- 51820/UDP (WireGuard)
- 587/TCP (SMTP Submission)
- 993/TCP (IMAPS)
IONOS blocks port 25 by default as an anti-spam measure, and this port cannot be opened directly through the control panel. To open port 25:
- Contact IONOS customer support by phone (available 24/7)
- Request to open port 25 for your VPS
- Explain that you're setting up a mail server
- They will typically open the port without issues after verifying your account
This extra step helps prevent the VPS from being used for spam but may cause a slight delay in your setup process.
UFW is usually pre-installed on most Ubuntu/Debian systems. If not, install it:
sudo apt update
sudo apt install ufw
Before configuring the firewall, you must enable IP forwarding at the system level to allow the server to forward packets between interfaces:
# Edit sysctl.conf
sudo nano /etc/sysctl.conf
# Add or uncomment the following line
net.ipv4.ip_forward=1
# Apply the changes without rebooting
sudo sysctl -p
This enables IP forwarding permanently across reboots. You can verify it's enabled by running:
sysctl net.ipv4.ip_forward
It should return net.ipv4.ip_forward = 1
.
Start with a secure default policy:
# Set default policies (deny incoming, allow outgoing)
sudo ufw default deny incoming
sudo ufw default allow outgoing
UFW by default blocks forwarded packets, which would prevent our port forwarding from working. Enable packet forwarding in UFW by editing its configuration:
sudo nano /etc/default/ufw
Find the DEFAULT_FORWARD_POLICY
line and change it from DROP
to ACCEPT
:
DEFAULT_FORWARD_POLICY="ACCEPT"
Save and close the file.
Configure UFW to allow the necessary services:
# SSH access for administration
sudo ufw allow 22/tcp
# WireGuard VPN
sudo ufw allow 51820/udp
# HTTP/HTTPS for web interfaces and Let's Encrypt
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
# Mail server ports
sudo ufw allow 25/tcp # SMTP
sudo ufw allow 587/tcp # Submission
sudo ufw allow 993/tcp # IMAPS
For enhanced security, certain services can be restricted to specific IP addresses. For example, allowing SMTP relay only from the Mailcow server via WireGuard:
# Allow SMTP from Mailcow server via WireGuard
sudo ufw allow from 10.0.0.2 to any port 2526/tcp
UFW uses iptables under the hood. To configure port forwarding, you need to edit the /etc/ufw/before.rules
file, which contains rules that are processed before the UFW rules.
Add the following configuration to the top of the /etc/ufw/before.rules
file, just after the *nat
line:
*nat
:PREROUTING ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
# Forward IMAPS (port 993) to Mailcow server via WireGuard
-A PREROUTING -p tcp --dport 993 -j DNAT --to-destination 10.0.0.2:993
-A POSTROUTING -p tcp -d 10.0.0.2 --dport 993 -j MASQUERADE
# Forward Submission (port 587) to Mailcow server via WireGuard
-A PREROUTING -p tcp --dport 587 -j DNAT --to-destination 10.0.0.2:587
-A POSTROUTING -p tcp -d 10.0.0.2 --dport 587 -j MASQUERADE
COMMIT
# Don't delete these required lines, otherwise there will be errors
*filter
Additionally, add the following forwarding rules to the ufw-before-forward
chain in the same file:
# Forward traffic to Mailcow server
-A ufw-before-forward -p tcp -d 10.0.0.2 --dport 993 -j ACCEPT
-A ufw-before-forward -p tcp -d 10.0.0.2 --dport 587 -j ACCEPT
The port forwarding configuration is critical for the email server architecture:
-
Why Port Forwarding Is Needed
- Client email applications (Thunderbird, Outlook, etc.) need to authenticate with the mail server
- These clients connect directly to ports 993 (IMAPS) and 587 (Submission)
- Instead of implementing authentication on the proxy server, forwarding these connections to the Mailcow server is more efficient
-
Benefits of This Approach
- Simplifies configuration and maintenance
- Leverages Mailcow's robust authentication system
- Ensures certificate validation works correctly
- Uses the secure WireGuard tunnel for all sensitive communications
-
Implementation Details
PREROUTING
rules redirect incoming traffic to the WireGuard interfacePOSTROUTING
rules with MASQUERADE handle the return traffic- Additional
ufw-before-forward
rules explicitly allow the forwarded connections
After configuring UFW, enable it and verify the configuration:
# Reload UFW to apply all changes
sudo ufw reload
# Enable UFW if not already enabled
sudo ufw enable
# Check UFW status and rules
sudo ufw status verbose
Expected output should show all configured rules with appropriate ports and services.
The client server requires a much simpler firewall configuration since it does not interact directly with the internet. It only needs to allow the WireGuard VPN connection.
# Install UFW if not already installed
sudo apt update
sudo apt install ufw
# Set default policies (deny incoming, allow outgoing)
sudo ufw default deny incoming
sudo ufw default allow outgoing
# Allow WireGuard VPN traffic
sudo ufw allow 51820/udp
# Enable UFW
sudo ufw enable
That's it! The client server is now configured to:
- Deny all incoming connections by default
- Allow only WireGuard VPN traffic on UDP port 51820
- Allow all outgoing connections
This minimal configuration ensures that the client server is completely invisible on the public internet, with the only access point being through the secure WireGuard tunnel from the proxy server.
-
Proxy Server Security
- Only essential ports are opened to the internet
- Legacy and insecure protocols are kept closed
- Administrative access is restricted to SSH only
- All forwarded connections go through the encrypted WireGuard tunnel
-
Client Server Security
- Completely isolated from the internet
- Only the WireGuard VPN port is open
- All email services are only accessible through the encrypted tunnel
- Authentication happens on the internal Mailcow server, not the exposed proxy
You are at Step 2 of 7 in the setup process
After configuring your firewall:
➡️ If you haven't already, proceed to Step 3: Configure the WireGuard VPN
If you've completed the previous steps:
- Verify all services are accessible through the firewall
- Test email client connectivity
- Ensure all ports are properly secured