Wednesday, June 29, 2011

Forcing Windows 7 to use wired when available

For unknown reason, Windows 7 prefers using wireless connection over wired. In order to force Windows 7 to use the wired connection when available, you need to adjust the setting of gateway metric among network adapters. A lot of posts found online recommended to do it via via Network Connection (ncpa.cpl) by unchecking Automatic metric checkbox and manually setting Interface metric on each network adapter. Unfortunately, this only updates interface metric, not gateway metric. Thus, it won't have any effect on Windows 7 and the problem persists.

You can type the following command at the command prompt for the detail of your network adapter settings [see example]:

netsh int ip show config

Or type the following for the settings of the route table. In this command, the metric column is only for gateway metric. [see example]

route print

To change gateway metric, there are two options. First is to use route change command at the command prompt. For example,

route change 0.0.0.0 mask 0.0.0.0 192.168.1.1 metric 20 if 13

where

  • 0.0.0.0 is the target network destination (IP address) found in route table.

  • mask 0.0.0.0 is the subnet mask associated with the target network destination.

  • 192.168.1.1 is the IP address of the gateway, my router.

  • metric 20 is setting gateway metric to 20. The network interface with a lower metric takes precedence. In this example, I am assigning 20 to my wired network card, which receives the lowest gateway metric. It enforces Windows 7 to use the wired whenever it is available. Also see KB299540.

  • if 13 means to apply the changes only to the network interface index equivalent to 13. In this example, 13 is my Intel(R) 82577LM Gigabit Network Connection that can be found in the Interface List section of route print.

There is no need to log out or reboot. The changes should take effect immediately.

The second option presented in my next post may be the preferable way, especially if you don't want to execute any command. Go and see my next how-to.

References:
The meaning of metric numbers, see KB299540.
How to use Route Command.

Example of netsh int ip show config

Configuration for interface "Wireless Network Connection"
    DHCP enabled:                         Yes
    IP Address:                           192.168.1.2
    Subnet Prefix:                        192.168.1.0/24 (mask 255.255.255.0)
    Default Gateway:                      192.168.1.1
Gateway Metric: 25 InterfaceMetric: 50
DNS servers configured through DHCP: 192.168.1.1 192.168.1.1 ... ... Register with which suffix: Primary only WINS servers configured through DHCP: None Configuration for interface "Local Area Connection" DHCP enabled: No IP Address: 192.168.1.200 Subnet Prefix: 192.168.1.0/24 (mask 255.255.255.0) Default Gateway: 192.168.1.1
Gateway Metric: 256 InterfaceMetric: 20
Statically Configured DNS Servers: 192.168.1.1 ... Register with which suffix: Primary only Statically Configured WINS Servers: None ...

Example of route print

The column of Metric in the section of IPv4 Route Table is gateway metric.

===========================================================================
Interface List
 13...5c 26 0a 23 40 d5 ......Intel(R) 82577LM Gigabit Network Connection
 14...00 24 d7 6c a6 fc ......Intel(R) Centrino(R) Ultimate-N 6300 AGN
 15...00 24 d7 6c a6 fd ......Microsoft Virtual WiFi Miniport Adapter
 10...5c ac 4c fd 7b 5e ......Bluetooth Device (Personal Area Network)
 16...00 50 56 c0 00 01 ......VMware Virtual Ethernet Adapter for VMnet1
 17...00 50 56 c0 00 08 ......VMware Virtual Ethernet Adapter for VMnet8
  1...........................Software Loopback Interface 1
===========================================================================

IPv4 Route Table
===========================================================================
Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
0.0.0.0 0.0.0.0 192.168.1.1 192.168.1.200 266 0.0.0.0 0.0.0.0 192.168.1.1 192.168.1.2 50
127.0.0.0 255.0.0.0 On-link 127.0.0.1 306 127.0.0.1 255.255.255.255 On-link 127.0.0.1 306 127.255.255.255 255.255.255.255 On-link 127.0.0.1 306 192.168.1.0 255.255.255.0 On-link 192.168.1.200 266 192.168.1.0 255.255.255.0 On-link 192.168.1.2 281 192.168.1.2 255.255.255.255 On-link 192.168.1.2 281 ... ... ... ... ...

10 comments:

  1. Hi ,
    It would be very very helpful for me if you could explain . How does the Gateway metric of 256 come about being . Does that usually happen if an external wifi card is connected to a computer . ( I have a laptop with a wifi card and and an ethernet port that are inbuilt ) .. Both of them have a gateway metric of 0 . However when i connect an external wifi card .. its gateway metric becomes 256 . Is it always the case for externally connected devices ?

    Thanks
    --
    Vivek

    ReplyDelete
  2. @Vivek, the default metric value 0 means that the host is the endpoint of the route itself. Any network changes will automatically trigger the update event (such as putting in your external WIFI card). By default, TCP/IP will automatically
    calculates the interface metric based on the link speed. But I also would like to know what algorithm is used by MS to calculate it on Windows 7.

    ReplyDelete
  3. How can I disable or fix the Gateway Metric?

    ReplyDelete
  4. You can use "route change" command to alter metric index.

    ReplyDelete
  5. If I'm following you correctly, Windows 7 will prioritize the lowest metric but does that mean the lowest interface or gateway metric? From my understanding the gateway metric is the interface metric*2, so if I set my Ethernet adapter to an interface metric of 5 and my wifi adapter to an interface metric of 50, I should have gateway metrics of 10 and 100 respectively. Where I'm getting confused is Windows 7 seems to prioritize the HIGHER gateway metric as your IPv4 route table picture shows. Please explain.

    ReplyDelete
  6. @Brian, I am unaware that the metric is the power of 2. My example of "route print" is NOT the result of "route change" command. Sorry for the confusion. It is just an example to show where to find/read the info. Lowers numbers of metric always take precedence. Thus, in my "route change" example, my wireless is chosen.

    ReplyDelete
  7. Thanks for sharing this tweak. But i saw every time if WiFi and Ethernet both are available the connection establish with Ethernet.

    ReplyDelete
  8. Windows calculates an automatic interface metric based upon its reported link speed. See: http://support.microsoft.com/kb/299540 for the values used.

    Your "problem" is most likely that your wireless connection is reporting a faster link speed than your wired connection. I'll bet you have a 802.11N access point (up to 300 Mbit) for your wireless and a 100 Mbit switch for your wired connection.

    If you want Windows to prefer your wired connection, then the easy solution is to modify the Interface metric of your wireless interface to be higher than your wired interface. Under the properties for your Wireless Interface -> TCP/IP Properties -> Advanced -> uncheck "Automatic Metric" and manually specify something larger then 20 (which should be the calculated metric for a 100 Mbit connection). No need to mess with gateway metric values.

    (Or you could upgrade your wired connection to 1 Gb and that would also "solve" the problem automatically.)

    ReplyDelete
  9. How can I bypass one router to get a gateway metreci of another?
    Both routers are connected to the internet, I don't want to change settings on the routers. I'm connected to router 1 with isp 1. I want to be able to use isp 2 connected to router 2 on only one machine. Is this possible and how would I change the routing table/metric

    ReplyDelete
  10. Very nice post, impressive. its quite different from other posts. Thanks for sharing.
    Data Recovery Services Services In Delhi

    ReplyDelete