How to determine/control which source IP address is used having multiple IPs on the same subnet?

Latest response

Hello folks,
I have assigned an alias IP address from the same subnet as the original one to my nic. So my ifcfg-eth0 and ifcfg-eth0:1 looks as follows:

# cat /etc/sysconfig/network-scripts/ifcfg-eth0
NAME=eth0
GATEWAY=192.168.1.1
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
NETMASK=255.255.255.0
IPADDR=192.168.1.40

# cat /etc/sysconfig/network-scripts/ifcfg-eth0\:1 
DEVICE=eth0:1
ONPARENT=yes
IPADDR=192.168.1.70
PREFIX=24
  • What IP address would be used as source IP for (initiating) outgoing traffic by default?
  • How could I control which IP should be used as source IP for outgoing traffic?

In Solaris one could mark a logical IP as deprecated. Is there a similar concept for Linux?

Best regards,
Joerg K.

Responses

Hi Jorg,

Check the 'ip route' output and it would only show the primary IP address over there and routing via eth0 interface that is assigned with '192.168.1.40' as you stated before. The same IP address would be used for outgoing traffic as well as per my knowledge. Let's see what others would say about this...

Hello Jorg,

In regards to what you said about, "What IP address would be used as source IP for (initiating) outgoing traffic by default?" One way is to change the metric for the IP. There are several discussions on this. Here is one.

https://www.linuxquestions.org/questions/linux-networking-3/setting-metric-for-eth-interfaces-591796/

In regards to what you said about, "How could I control which IP should be used as source IP for outgoing traffic?" In the past I have bound an IP to an application for various reasons.

https://daniel-lange.com/archives/53-Binding-applications-to-a-specific-IP.html

Hope this helps? Joe

As said, this is controlled by the routing table on Linux.

You can change the source of all traffic like: How do I use a Virtual IP as the source address of outgoing traffic?

Hello guys,
Thanks for your answers, so far.

I could not decide which answer is the best one, yet. I'm going to discuss the this matter with my colleagues next week. Thanks again for your help.

Best regards, Joerg K.