Return IP from a specific NIC (Network Binding Order) for Windows Server 2016 and above

For some applications it is important to see and bind to a correct IP when there are multiple NICs (hence multiple IPs assigned) on a Windows server. For example, if a server has two NICs with two different IPs from 2 subnets, one for public traffic and another for the storage network. You may want your application to bind to the public IP.

If you are lucky, and the public IP is returned by ping, you don’t have to do anything. If not, but you need the public IP returned instead of another IP, you need to do something.

Before Windows 2016, it’s called Network Binding Order and the order can be changed in the “Advanced Settings -> Adapters and Bindings” of “change adapter settings”.

But starting with Windows 2016, that method is gone. Adding an entry manually in hosts file won’t work.

In Windows 2016 and above (including Windows 10), it’s called the interface metric which gives preference to a particular interface, such as using wired if both wired and wireless are available.

“A metric is a value that’s assigned to an IP route for a particular network interface. It identifies the cost that’s associated with using that route. For example, the metric can be valued in terms of link speed, hop count, or time delay. Automatic Metric is a new feature in Windows that automatically configures the metric for the local routes that are based on link speed. The Automatic Metric feature is enabled by default, and it can also be manually configured to assign a specific metric.”

“The Automatic Metric feature is configured independently for each network interface in the network. This feature is useful in situations where you have more than one network interface of the same speed, for example, when each network interface has been assigned a default gateway. In this situation, you may want to manually configure the metric on one network interface, and enable the Automatic Metric feature to configure the metric of the other network interface. This setup can enable you to control the network interface that is used first in the routing of IP traffic.”

Two possible ways to modify this metric setting:

A. Using GUI:

  1. In Control Panel, double-click Network Connections.
  2. Right-click a network interface, and then select Properties.
  3. Click Internet Protocol (TCP/IP), and then select Properties.
  4. On the General tab, select Advanced.
  5. To specify a metric, on the IP Settings tab, clear the Automatic metric check box, and then enter the metric that you want in the Interface Metric field. Lower valuer, lower cost, higher preference.

B. Using PowerShell command Set-NetIPInterface

— Get NIC names first

PS C:\temp> Get-NetAdapter

Name             InterfaceDescription          ifIndex Status    MacAddress          LinkSpeed
----             --------------------          ------- ------    ----------          ---------
Ethernet02       vmxnet3 Ethernet Adapter            7 Up        00-54-46-C8-B1-78     10 Gbps
Ethernet1        vmxnet3 Ethernet Adapter #2         5 Up        00-54-46-C8-6E-1E     10 Gbps

— Check current metric “InterfaceMetric”

PS C:\temp> Get-NetIPInterface -AddressFamily IPv4 -InterfaceAlias "Ethernet02"

ifIndex InterfaceAlias   AddressFamily NlMtu(Bytes) InterfaceMetric Dhcp     ConnectionState PolicyStore
------- --------------   ------------- ------------ --------------- ----     --------------- -----------
7       Ethernet02       IPv4                  1500               1 Disabled Connected       ActiveStore

PS C:\temp> Get-NetIPInterface -AddressFamily IPv4 -InterfaceAlias "Ethernet1"

ifIndex InterfaceAlias   AddressFamily NlMtu(Bytes) InterfaceMetric Dhcp     ConnectionState PolicyStore
------- --------------   ------------- ------------ --------------- ----     --------------- -----------
5       Ethernet1        IPv4                  1500               2 Disabled Connected       ActiveStore

— Set the value for metric

Set-NetIPInterface -InterfaceAlias "Ethernet1" -InterfaceMetric 1
Set-NetIPInterface -InterfaceAlias "Ethernet02" -InterfaceMetric 2

References:

  1. https://docs.microsoft.com/en-us/windows-server/networking/technologies/network-subsystem/net-sub-interface-metrich
  2. https://docs.microsoft.com/en-us/troubleshoot/windows-server/networking/incorrect-ip-address-returned-ping-netbios
  3. https://docs.microsoft.com/en-US/troubleshoot/windows-server/networking/automatic-metric-for-ipv4-routes
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s