2

Which IP does an interface use when the host acts as a client?

Let's say I have configured eth0 with 2 IP addresses: 192.168.1.7 and 192.168.1.8

The route command shows something like this:

$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.240.0   0.0.0.0         255.255.240.0   U     256    0        0 eth0
...

This basically means that when I try to connect to any host from the 192.168.240.0 network it uses the eth0 interface. Ok, but... Which IP address from that interface?

If the host acts as a server and a client connects to my computer using the IP address 192.168.1.7 I understand that eth0 will use 192.168.1.7 to communicate with the client, but what if I am the client?


EDIT

The IP addresses are made up, I can't add another IP address to an interface in my Ubuntu WSL because I get this error:

$ ip address add 192.168.1.7/24 dev eth0
RTNETLINK answers: Permission denied

The output of ip r s is something like this:

$ ip r s
none 224.0.0.0/4 dev eth0 proto unspec metric 256
none 255.255.255.255 dev eth0 proto unspec metric 256
none 224.0.0.0/4 dev eth1 proto unspec metric 256
none 255.255.255.255 dev eth1 proto unspec metric 256
...

EDIT 2

I upgraded to WSL2 and now the command to add ip addresses work (with sudo).

$ ip -4 a s dev eth0
4: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    inet 192.168.249.181/20 brd 192.168.255.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.1.7/24 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.1.8/24 scope global secondary eth0
       valid_lft forever preferred_lft forever
Adrian
  • 179
  • 1
  • 1
  • 11
  • what is the result of `ip r s` ? (ip route show) on my pi, it show explicitly which route is used. – Archemar May 30 '21 at 18:22
  • @Archemar Take a look at the EDIT section – Adrian May 30 '21 at 18:52
  • @Adrian: if you want to add an IP address to your iface `eth0`, do `sudo ip a add .... eth0`. – Cbhihe May 30 '21 at 19:05
  • @Cbhihe Apparently sudo wasn't enough. I had to upgrade to WSL2. – Adrian May 30 '21 at 19:06
  • Ok, so now I'm a little unsure. By adding an IPv4 address to your iface `eth0` do you want to be able to route outgoing packets from one IPv4 address or the other, depending on service ? What is the purpose ? – Cbhihe May 30 '21 at 19:11
  • @Cbhihe I just want to understand how the network interfaces work with multiple IP addresses. When someone connects to my computer through the IP address 192.168.1.8 my understanding is that my computer will communicate with the client using that IP address. However I don't understand which IP would my computer use when it acts as a client instead of a server. – Adrian May 30 '21 at 19:14
  • ok. I see below that your problem is fixed (?)... or do you still need to clear up how exactly your host routes outgoing packets through any number of available IP addresses attached to an iface such as `eth0` ? – Cbhihe May 30 '21 at 19:19
  • Does this answer your question? [Linux Source Routing, Strong End System Model / Strong Host Model?](https://unix.stackexchange.com/questions/258810/linux-source-routing-strong-end-system-model-strong-host-model) – Eduardo Trápani May 30 '21 at 19:24
  • @Cbhihe It was, but for some reason you removed your answer? "do you still need to clear up how exactly your host routes outgoing packets through any number of available IP addresses attached to an iface such as eth0 ?" I think I got what I was looking for, but that would be helpful. – Adrian May 30 '21 at 19:25
  • @EduardoTrápani It doesn't because my question was much simpler than that, but I appreciate it. If I say yes, then this post will be marked as duplicate, but people will get frustrated when they have this same question and see the duplicate is a post with such different title and complex terminology. I am a noob btw. – Adrian May 30 '21 at 19:29
  • @Cbhihe I executed `nc google.com 80` and executed Archemar's command and got a different IP address from the awk commnad that you posted in the answer you later deleted. The IP that shows in the `ip r s` command, in the line that starts with 'default via...' is not the address I wanted. I have understood that address is the default gateway i.e. the address to send packets to when the destination IP is outside of the local network. Besides, @Archemar would you mind to post your comment as answer? – Adrian May 30 '21 at 19:49

2 Answers2

6

For Linux, the answer to your question is given here:

The initial source address for an outbound packet is chosen in according to the following series of rules.

The application can request a particular IP, the kernel will use the src hint from the chosen route path, or, lacking this hint, the kernel will choose the first address configured on the interface which falls in the same network as the destination address or the nexthop router.

Eduardo Trápani
  • 12,032
  • 1
  • 18
  • 35
  • @Adrian: I deleted my answer because I don't think it answered your basic interrogation. This answer probably comes closer to doing that. If not, just rephrase your question and simplify the post (e.g. the 1st EDIT is probably not needed) so it narrows down on a well defined issue. You could also improve your question by making your title more expressive. Are you talking about outgoing/incoming traffic ? on what OS ? with what kernel ? Good luck. ;-) – Cbhihe May 30 '21 at 20:19
  • that what's I tought (but without ref): unless you enforce, kernel choose first configured compatible with outgoing. – Archemar May 30 '21 at 21:06
  • Does the MAC address play no role here? Even on the same subnet? – Seamus May 31 '21 at 15:18
2

The client will lookup the ROUTES matching the address it is trying to connect to. If, as in your example, there is more than one matching route it will pick the route entry with the highest metric and send the packet out of the interface found in the route table, using the IP address of that interface.

You have truncated the output of route, and did not say what you were connecting to - so I can't tell you which interface that will be.

On the server....well things get a bit more complicated there. I have Centos and Redhat hosts with multiple interfaces on the same subnet. Those will reply on the same adress / interface the packets were sent to. But I also have recent Ubuntu and Debian systems which will revisit the routing table to find the interface and address to use, ignoring the addressing on the client's packet. The client will ignore the packets coming back from an IP address it's not talking to.

symcbean
  • 5,008
  • 2
  • 25
  • 37