How to set the default source (src) ip address when you have mulitple IP’s on a virtual interface

networking

How do I tell the networking stack which ip to be the source (src) to communicate with in the case of multiple ips on a virtual interface?

auto eth0 
iface eth0 inet static
            address x.x.x.209
            netmask 255.255.255.0
            network x.x.x.0
            broadcast x.x.x.255
            gateway x.x.x.254

auto eth0:1
iface eth0:1 inet static
            address x.x.x.210
            netmask 255.255.255.0
            network x.x.x.0
            broadcast x.x.x.255
            gateway x.x.x.254

etc. etc.

It seems to be picking the last one in /etc/networking/interfaces

~# ip -s route get 173.194.67.113; 
173.194.67.113 via x.x.x.254 dev eth0  src x.x.x.210 
  cache  users 1 used 11 mtu 1500 advmss 1460 hoplimit 64

But this behaviour is unpredictable I have about 30 ip's assigned per interface, and sometimes the (scr) source ip changes.

I need to understand how it chooses.

I've tried setting metric to standardize the src but it doesn't appear to support virtual interfaces.
NETLINK: Packet too small or truncated!

EDIT: Ubuntu 10.04 64bit

EDIT Following @TheCleaner's link my /etc/network/interfaces is now a lot cleaner.
Sorry for not providing more information earlier, It's a complicated environment, and all I really wanted to know is how the kernel decides the source address when you run say an apt-get update from the command line. All the load banacing stuff, failover etc is working great.

auto eth0
iface eth0 inet static
            address x.x.x.183
            netmask 255.255.255.0
            network x.x.x.0
            broadcast x.x.x.255
            gateway x.x.x.254
up ip addr add x.x.x.209 dev eth0 label eth0:1
up ip addr add x.x.x.210 dev eth0 label eth0:2
up ip addr add x.x.x.211 dev eth0 label eth0:3
up ip addr add x.x.x.212 dev eth0 label eth0:4
up ip addr add x.x.x.213 dev eth0 label eth0:5
up ip addr add x.x.x.214 dev eth0 label eth0:6
up ip addr add x.x.x.215 dev eth0 label eth0:7

IDK if its fixed. but so far so good. I'm just going to run
while sleep 5; do ip -s route get 173.194.67.113; done
for some time to see if i have a stable ip when connecting outbound.

This is working great! thanks everyone who chimed in. I'm still curios as to WHY exactly my source ip was changing, but ill just chalk it up to having an improperly written interfaces file and move on.

Best Answer

In the alias stanza, do not include the gateway. You also don't need the 'network' line at all.

auto eth0 
iface eth0 inet static
            address x.x.x.209
            netmask 255.255.255.0
            broadcast x.x.x.255
            gateway x.x.x.254

auto eth0:1
iface eth0:1 inet static
            address x.x.x.210
            netmask 255.255.255.0