• I have two network interfaces, A and B (in ifconfig)
  • I run nc -l 20101 -v to listen on the interface B.
  • I run nc -v 20101 -s -4 because I want to use the interface A.

It connects but when I check with wireshark, no packet from A or B, only in lo...

Why it doesn't use the interface with the associated IP ? What should I do to force them to use the associated interface ?


After following the piece of advice of Patrick:

ip route add local dev B table main
ip route del local dev B table local
ip route add dev B table local

I run nc -l 20101 but I get an error when I create the tcp server Ncat: bind to Cannot assign requested address. QUITTING.

17:10:38 alexis:~  $ ip route list table local dev B  scope link 

17:10:40 alexis:~  $ ip route list table main
default via dev eth0 
local dev B  scope host
  • Side note, if the netmask on both these interfaces/addresses is /24, you're likely going to be frequently running into problems. Having 2 interfaces on the same subnet can be done, but it presents numerous difficulties to get working properly.
    – phemmer
    Jun 2, 2017 at 4:23
  • Also, please specify the operating system. The answer/explanation can vary by OS.
    – phemmer
    Jun 2, 2017 at 4:26
  • The netmask should be /32 since I want only one address for each interface. We are in Unix&Linux stackexchange. I'm using CentOS tho.
    – None
    Jun 2, 2017 at 6:12
  • 1
    Yes, but Unix & Linux is very broad. You've got Linux, HP-UX, Solaris, FreeBSD, OpenBSD, Plan9, ...
    – phemmer
    Jun 2, 2017 at 12:08

4 Answers 4


When you tell an application to use a specific IP address, the application is using the IP address, not the interface. Some applications do let you use a specific interface, but this is a separate behavior (SO_BINDTODEVICE).

Since the application is binding to an IP address, and not an interface, the kernel is free to use whatever interface it wants. To determine which interface to use, it uses the routing tables (yes, there are multiple).

If you just want a quick way to determine what interface/route the traffic will take, you can use ip route get from, which will output something like:

# ip route get from
local from dev lo 
    cache <local>

This shows that the kernel is going to send the traffic over the lo interface.

To walk through why, lets start with the command ip rule:

# ip rule
0:  from all lookup local 
32766:  from all lookup main 
32767:  from all lookup default 

This shows all the routing tables the kernel is going to use to find the route for traffic. It starts with the top, and stops on the first match. The from all means that the rule matches any source address. So it's going to consult table local first. We can then look inside this table:

# ip route show table local
broadcast dev lo  proto kernel  scope link  src 
local dev lo  proto kernel  scope host  src 
local dev lo  proto kernel  scope host  src 
broadcast dev lo  proto kernel  scope link  src 
broadcast dev eth0  proto kernel  scope link  src 
local dev eth0  proto kernel  scope host  src 
broadcast dev eth0  proto kernel  scope link  src 
local dev eth1  proto kernel  scope host  src

(yours will likely look different)

From this we then look to see if any of the routes match the destination address ( by looking to see if the destination matches the second field. In the output above, the very last one matches. In this line, the first field is local, which according to man ip-route means:

local - the destinations are assigned to this host. The packets are looped back and delivered locally.

This means that the traffic is going to flow over the lo interface.

As for how to make it use the A/B interface, you have 2 options:

1) The application would need to provide you with an argument where you can specify the interface. There are a dozen flavors of netcat, but the version on my systems does not have such an option. socat does though (I personally recommend socat over netcat because of the inconsistency & portability nightmare that is netcat. It's also way more powerful).

2) Create a non-local route that matches before the local one:

ip route add local dev B table main
ip route del local dev B table local
ip route add dev B table local

In these rules, the first 2 rules move the local route into the main table. Adding the route to the main table has to come first as the host has to have a local route somewhere for the kernel to accept traffic for that address. Having the route in 2 tables is OK. After that we then add a new route to the local table which does not have the local designation, which will result in traffic not going over the lo interface.

  • I understood. Asking to the application to use one interface is not a solution. I am using nc just because it's easy and can reproduce what the app will do. I need to use ip route. Unfortunately, I have an error Ncat: bind to Cannot assign requested address. QUITTING. when trying to listen on this interface B.
    – None
    Jun 2, 2017 at 6:15

With netcat-openbsd on debian and opensd, you can choose the routing table you want to use.

The option is -V.

With regular netcat I believe this is not possible.

With socat you can use a specific interface declaring it at call time.


Access to local addresses always uses the loopback interface since network interfaces are designed to send packets to other nodes on the wire, not yourself.


Simple answer

Linux Kernel has two packet queues in kernel, one for sending packets, and one for received packets.

And Kernel decides (based on routing table and destination address, to which NIC packet will physically go)

But If kernel sees that destination IP address is on same host as source IP, it makes no sense to physically send this packet on one NIC and receive it latter on another NIC. It would be overkill, a waste of time and resources. So kernel just places/removes packet from sending queue and puts it to receiving queue. It all happens in kernel using pseudo NIC, loopback interface lo.

Simple schematic, lets imagine there is another queue for received packets too:

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .