My situation is this: I’m setting up a server running at home Ubuntu to run an email server and a few other online services. As we all know, for my email to work reliably and not get blocked I need to have an unchanging public IP address. Due to my circumstances I am not able to get a static IP address through my ISP or change ISPs at the moment.
The solution I have found is to buy a 4G SIM card with a static IP (from an ISP that offers that), which I can then use with a USB dongle. However this 4G connection costs me substantially per MB to use.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
But. Mail is the only server that needs a static IP address. For everything else using my home network connection and updating my DNS records with DDNS would be fine. I have tested this setup previously for other services and it has worked.
So. I was wondering. Would it in theory be possible to: connect the server to two network interfaces at the same time and route traffic depending on destination port. I.e. all outgoing connections to ports 25, 465, 587, and possibly 993 should be sent through the 4G dongle interface (enx344b50000000) and all other connections sent over eth0. Similarly, the server should listen for incoming connections on the same ports on enx344b50000000 and listen on all other ports (if allowed by ufw) on eth0.
I would then need DNS records from mail.mydomain.tld —> <4g static public IP> and mydomain.tld —> (updated with DDNS, and NAT configured on my home router).
Computers on the internet would then be able to seamlessly connect to these two IP addresses, not “realising” that they are in fact the same machine, as long as requests to mail.mydomain.tld are always on the above mentioned ports.
Question: Is this possible? Could it be a robust solution that works the way I hope? Would someone be able to help me set it up?
I have come across a few different guides in my DuckDuckGo-ing, I understand it has to do with setting a mark in iptables and assigning them to a table using ip route. However I haven't managed to get it to work yet, and many of these guides are for VPNs and they all seem to be slightly different to each other. So I thought I would ask about my own specific use case
Assistant
Yes, it is possible to set up a dual interface server configuration where specific traffic is routed through a particular interface based on destination port numbers. This is known as policy-based routing, and it can be implemented on a Linux system using a combination of iptables and ip route.
Here is a general outline of steps you would take to achieve this:
Configure Network Interfaces: Ensure both your Ethernet (eth0) and USB dongle (enx344b50000000) interfaces are configured correctly with their respective IP addresses.
Set Up IPTables Rules: Use iptables to mark packets based on the destination port. For example, to mark traffic for SMTP:
iptables -t mangle -A OUTPUT -p tcp --dport 25 -j MARK --set-mark 1
iptables -t mangle -A OUTPUT -p tcp --dport 465 -j MARK --set-mark 1
iptables -t mangle -A OUTPUT -p tcp --dport 587 -j MARK --set-mark 1
iptables -t mangle -A OUTPUT -p tcp --dport 993 -j MARK --set-mark 1
These commands add rules to the mangle table, marking packets destined for ports 25, 465, 587, and 993 with a mark of 1.
Create a Custom Routing Table: Create a new routing table in /etc/iproute2/rt_tables by adding a line like:
100 4g
This creates a table named 4g with an arbitrary ID of 100.
Add Routes to the Custom Table: Add routing rules to direct marked packets to use the 4g table:
ip rule add fwmark 1 table 4g
ip route add default via <4G-Gateway-IP> dev enx344b50000000 table 4g
Replace <4G-Gateway-IP> with the actual gateway IP of your USB dongle connection.
Configure NAT (if necessary): If your 4G ISP doesn't provide you with a public IP address directly on your dongle interface, you might need to set up Source NAT for traffic going out via the dongle to ensure responses come back to the right interface. This can be done with iptables in the nat table:
iptables -t nat -A POSTROUTING -o enx344b50000000 -j MASQUERADE
Configure DNS: As you described, you will need to have two DNS A records: one for mail.mydomain.tld pointing to your 4G static IP, and another for mydomain.tld pointing to your home IP (updated via DDNS).
Firewall Configuration: Ensure your ufw or iptables firewall rules allow incoming connections on the required ports for both interfaces.
Testing: After setting up the above configuration, test thoroughly to ensure that your email is being sent out via the 4G connection and that all other traffic is going out via your home connection. You can use tools like tcpdump to monitor the traffic and confirm it’s taking the right path.
This setup can be robust, but it's essential to monitor your system to ensure everything continues to work as expected, especially after reboots or network changes. You may need to create a startup script or use your system's network manager to ensure these rules are applied consistently.
Keep in mind that any future updates to your Linux distribution or its networking components could potentially disrupt this configuration, so it's important to have good documentation and backups of your scripts and settings.