LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   How do I route internet traffic over my second IP address? (https://www.linuxquestions.org/questions/linux-networking-3/how-do-i-route-internet-traffic-over-my-second-ip-address-793252/)

FireRaven 03-04-2010 03:51 PM

How do I route internet traffic over my second IP address?
 
Hi,

My ISP has given me 3 IP addresses to use for the internet (76.148.200.3, 76.148.200.4 and 76.148.200.5).

If I do a "wget whatismyip.com" or netcat into another server, I am appearing as 76.148.200.3.

How do I change my route (command line) in linux to route internet traffic through 76.148.200.5?



Code:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback


# The primary network interface
auto eth0
iface eth0 inet static
        address 76.148.200.3
        netmask 255.255.255.248
        network 76.148.200.0
        broadcast 76.148.200.7
        gateway 76.148.200.1
        dns-nameservers 4.2.2.2


auto eth0:0
iface eth0:0 inet static
        address 76.148.200.4
        netmask 255.255.255.248
        network 76.148.200.0
        broadcast 76.148.200.7
        gateway 76.148.200.1
        dns-nameservers 4.2.2.2


auto eth0:1
iface eth0:1 inet static
        address 76.148.200.5
        netmask 255.255.255.248
        network 76.148.200.0
        broadcast 76.148.200.7
        gateway 76.148.200.1
        dns-nameservers 4.2.2.2





Here's the into if I type "ip route":

Code:

root@ubuntu:~# ip route
76.148.200.0/29 dev eth0  proto kernel  scope link  src 76.148.200.3
default via 76.148.200.1 dev eth0  src 76.148.200.5  metric 100
default via 76.148.200.1 dev eth0  src 76.148.200.4  metric 100
default via 76.148.200.1 dev eth0  metric 100


carltm 03-04-2010 04:35 PM

The easy way would be to configure eth0 as 76.148.200.5 and eth0:1
as 76.148.200.3.

FireRaven 03-04-2010 04:41 PM

But I want the hard way or I learn nothing :)

carltm 03-04-2010 04:50 PM

lol. I guess you get nothing from me!

nimnull22 03-04-2010 06:51 PM

Try to delete default and add new one:
ip route add default via 76.148.200.1 dev eth0:1

FireRaven 03-04-2010 10:24 PM

Quote:

Originally Posted by nimnull22 (Post 3886273)
Try to delete default and add new one:
ip route add default via 76.148.200.1 dev eth0:1


I tried that but it shows in routing table as eth0 (not eth0:1) and still goes through first one.

nimnull22 03-04-2010 11:17 PM

Quote:

Originally Posted by FireRaven (Post 3886452)
I tried that but it shows in routing table as eth0 (not eth0:1) and still goes through first one.

It is not just first one - it is your physical interface. I think it will never let you make default route through alias.You can give, as you were suggested already, different IP to eth0.

FireRaven 03-08-2010 04:35 PM

Ok guess I'll have to do that, thanks.

Skaperen 03-09-2010 09:40 AM

Routing determines where packets go to. If all your IP addresses have the same destination gateway, then the routing will be the same no matter which you want to use as your "source address".

The "source address" is your address on packets you originate. For incoming connections (e.g. if you are running a server) the "source address" for the return packets will be the one the connection was established with. But for packets you originate (make a connection to somewhere on the internet) then a decision has to be made. That decision can be made by the application when it sets up the sockets. Most applications don't do that. So then the decision is made by the network stack. When that happens, the decision is the first IP address matching the specified address family, for an interface the outgoing route will work on. It will probably be the first usable interface if the applicable route is not interface specific.

So use carltm's easy way ... it's THE WAY. If you really want a hard way, then write a program in assembly language to write the /etc/network/interfaces file directly ... that should be plenty hard enough :-)

FireRaven 03-09-2010 02:04 PM

I worked out one way to do it...
Code:

iptables -t nat -A POSTROUTING -j SNAT --to 76.148.200.5
It feels like a bit of a hack but it sure works!


All times are GMT -5. The time now is 04:33 PM.