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 connect a server to two network interfaces and route traffic based on destination port. This can be done using a combination of iptables and routing rules. Here are the general steps involved:
Configure the two network interfaces on your server.
Assign a default gateway to each interface.
Create a new iptables table for routing traffic.
Add rules to the iptables table to route traffic to the appropriate interface based on destination port.
Add a default route to the routing table that points to the default gateway for the home network interface.
Enable IP forwarding on the server.
Once you have completed these steps, traffic to ports 25, 465, 587, and 993 will be routed through the 4G dongle interface, while all other traffic will be routed through the home network interface.
Here is a more detailed explanation of each step:
Configure the two network interfaces on your server.
You will need to configure the two network interfaces on your server with static IP addresses. The 4G dongle interface should be assigned the static IP address provided by your ISP. The home network interface should be assigned an IP address from the same subnet as your home router.
Assign a default gateway to each interface.
You will need to assign a default gateway to each network interface. The default gateway for the 4G dongle interface should be the IP address of the ISP's gateway. The default gateway for the home network interface should be the IP address of your home router.
Create a new iptables table for routing traffic.
You will need to create a new iptables table to route traffic based on destination port. This can be done using the following command:
code
iptables -t mangle -N PORT-ROUTING
Add rules to the iptables table to route traffic to the appropriate interface based on destination port.
You will need to add rules to the iptables table to route traffic to the appropriate interface based on destination port. This can be done using the following commands:
code
iptables -t mangle -A PORT-ROUTING -p tcp --dport 25 -j MARK --set-mark 1
iptables -t mangle -A PORT-ROUTING -p tcp --dport 465 -j MARK --set-mark 1
iptables -t mangle -A PORT-ROUTING -p tcp --dport 587 -j MARK --set-mark 1
iptables -t mangle -A PORT-ROUTING -p tcp --dport 993 -j MARK --set-mark 1
These rules will mark all traffic to ports 25, 465, 587, and 993 with the mark 1.
Add a default route to the routing table that points to the default gateway for the home network interface.
You will need to add a default route to the routing table that points to the default gateway for the home network interface. This can be done using the following command:
code
ip route add default via <home network gateway IP address>
This route will ensure that all traffic that is not marked with the mark 1 is routed through the home network interface.
Enable IP forwarding on the server.
You will need to enable IP forwarding on the server to allow traffic to be routed between the two network interfaces. This can be done using the following command:
code
echo 1 > /proc/sys/net/ipv4/ip_forward
Once you have completed these steps, traffic to ports 25, 465, 587, and 993 will be routed through the 4G dongle interface, while all other traffic will be routed through the home network interface.
This solution should be robust and reliable, as long as the two network interfaces are stable and the iptables rules are configured correctly.