I've followed this guide for Multiple IP addresses on Amazon EC2 and have gone with the second method listed of adding two IP addresses to a single interface. This was done by adding two private IPs to the adapter and then associating an elastic IP with each of the private addresses.

When I run a command like curl it uses the primary IP address and I would like to know how to change it to use the other IP address by default. Here is the setup:

Public (elastic) IPs

Private IPs

After adding the elastic and private IPs I do this:

ip addr add dev eth0

To confirm it's working I can do:

curl icanhazip.com

curl --interface icanhazip.com

curl --interface icanhazip.com

As you can see the default route is done via the original / primary IP.

Here is the data from running ifconfig

eth0      Link encap:Ethernet  HWaddr 12:34:56:78:90:ab
          inet addr:  Bcast:  Mask:
          inet6 addr: aabb::cccc:dddd:eeee:ffff/64 Scope:Link
          RX packets:713 errors:0 dropped:0 overruns:0 frame:0
          TX packets:631 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:63726 (63.7 KB)  TX bytes:827217 (827.2 KB)

lo        .... omitted ....

And this is what I get from running route

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         UG    0      0        0 eth0      *        U     0      0        0 eth0

I tried changing the 'default' route with a script like this. I used screen so that the script would continue after I get disconnected (guessing that the first line causes that):

/sbin/route del default
/sbin/route add default via dev eth0 tab 1

I also tried without the tab 1 but still had no luck.

How can I set what outgoing IP is used 'by default' ?

I would also like to know if it is possible to specify that only traffic to a certain range of IPs use the secondary address as the default address.

  • Why not create separate subnet for second IP on the device and then add appropriate route for that subnet? – Milind Dumbare Mar 24 '15 at 19:39
  • 1
    If all you're looking to do is change the default gateway you can use ip route change default via <gateway-ip> Deleting your gateway probably is what's causing the problems if you're not on the same subnet. – Bratchley Mar 24 '15 at 19:47
  • @Miline - I could also add a second interface. That article has some good details about that method, too. I just thought there should be a simpler solution. – cwd Mar 24 '15 at 19:48
  • For your second question: You would set up an alternate routing table and use ip rule to say that all traffic coming from the secondary IP will use the gateway you're wanting. – Bratchley Mar 24 '15 at 19:48
  • @Bratchley - just attempted ip route change default via and have lost the connection / server is no longer responding. Am I missing something? – cwd Mar 24 '15 at 19:51
ip route replace default via src
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.