How to NAT to different interfaces under linux?

Posted on

Problem :

I have an Ubuntu 12.04 server with 4 ethernet interfaces, 2 of which connect to nonconfigurable network devices on eth5 and eth6. Each device can only be reached on 192.168.100.1 via telnet. I believe NAT can provide me a way to reach both devices. Other solutions are welcome.

What I would like to do is something like this: telnet 192.168.1.100 and have it translate to 192.168.100.1 on eth5. Similarly, i’d like to telnet 192.168.1.101 and have it translate to 192.168.100.1 on eth6. In this way, I have a sort of IP alias which uses specific ethernet interfaces. Without a solution like this, I obviously have an IP conflict.

Most NAT tutorials I see have to do with internet connection sharing. I’m trying to do something much simpler, but seemingly undocumented.

Any tips?

Solution :

NAT (or network address translation) treats all ‘inward’ IP’s as a single ‘outward’ IP (IP masquerading) to allow multiple local devices to connect to the same outward connection (e.g. a wifi router hooked to a cable modem has NAT (and probably DHCP) turned on to allow the devices to connect to the internet via the single IP assigned to you via your ISP).

What you are asking for (from what I could surmise) is IP aliasing, this allows you to have a single physical interface (say eth0) and ‘assign it’ multiple IP’s.

For example, say you have a /etc/sysconfig/network-scripts/eth0 config file with the following in it:

DEVICE=eth0
IPADDR=192.168.1.100
NETMASK=255.255.255.0
NETWORK=192.168.1.0
BROADCAST=192.168.1.255
ONBOOT=yes
NAME="192.168.1.100 (eth0)"
BOOTPROTO=none
USERCTL=no

This config file would assign the IP of 192.168.1.100 to the device eth0. So from another machine you could ping 192.168.1.100 and it would go to the eth0 device of your machine. If you wanted eth0 to also ‘see’ traffic from the 10.0.0.0 network as well without adding a router or iptables/PF in the mix, you could alias an IP to the eth0 interface by doing the following:

create a file of the device you want to add an alias to, in our case it’s the eth0 config file, but add a :X (where X is the number of the alias you want to assign), example: /etc/sysconfig/network-scripts/eth0:1. This file would then contain the alias info you wanted to add, for example:

DEVICE=eth0:1
IPADDR=10.0.0.100
NETMASK=255.255.255.0
NETWORK=10.0.0.0
BROADCAST=10.0.0.255
ONBOOT=yes
NAME="10.0.0.100 (eth0:1)"
BOOTPROTO=none
USERCTL=no

Adding this config file will allow you to communicate to both 192.168.1.100 and 10.0.0.100 via the eth0 physical device.

If on the other hand you are wanting traffic on one physical interface to be forwarded to another physical interface, you’ll have to consider using iptables to do this.

Based on your question though you are asking about sending traffic from 2 different IP’s (192.168.1.100 and 192.168.1.101) to 2 separate NIC’s (eth5 and eth6) that seem to share the same IP (192.168.100.1). I’m not sure how your eth5 and eth6 are currently configured, but if they are in a bond to ‘share’ the same IP then you might not be able to achieve what you want with IP aliasing and should check out iptables instead.

I hope this can add some clarity.

Leave a Reply

Your email address will not be published. Required fields are marked *