How to switch between Internet connections in Windows
Table of contents
1. Choosing an Internet connection
2. How to change the default route in Windows
3. How to set the metric for network connections in Windows
4. Features of the route CHANGE command in Windows
5. Creating persistent routes that persist after a reboot
If you have Linux, see the article ‘Setting up network routes: the choice of connection used for the Internet; simultaneous use of multiple connections for different purposes’ In it you will find additional tricks for manipulating network routes that will work on Windows also.
Choosing an Internet connection
A computer can be connected directly to several networks: for example, to a wired network and to a wireless network; or to two Wi-Fi networks; or use the phone as a modem via Bluetooth and at the same time be connected to a wired and Wi-Fi network; or be connected to four Wi-Fi networks — whatever!
If you go online, how does the operating system choose which of several connections to use? The system acts very wisely - it does not neglect any of the connections. And in case of one of them fails, it uses another connection.
But after all, in any case, OS needs to choose which connection to use at the moment! And the system again operates wisely - it uses the best connection. To find out which connection is best, the so-called Metric (a conditional value that takes into account several parameters at once - network speed, packet loss, and so on.) is calculated for each of them.
Let's consider a real example. My computer is connected to two Wi-Fi networks. To learn the characteristics of the routes, open the Windows command line, for this press Win+x, and select Windows PowerShell (Administrator). In the command window that opens, type the command
route print
Pay special attention to the lines:
Network Address Network Mask Gateway Interface Metric 0.0.0.0 0.0.0.0 192.168.0.1 192.168.0.49 55 0.0.0.0 0.0.0.0 192.168.1.1 192.168.1.43 70
The network address is 0.0.0.0 and the netmask 0.0.0.0 is designation for the default route. The default route is the route to which traffic is sent, for which there is no other certain route.
For example, there we can see the line
192.168.56.0 255.255.255.0 On-link 192.168.56.1 281
It has a network address of 192.168.56.0 with a subnet mask of 255.255.255.0 - that is, any IP addresses in the range of 192.168.56.0-192.168.56.255. So, for these addresses the route is clearly set – this traffic will be sent to 192.168.56.1.
But requests to any other IP that is not in the table (i.e., for which no certain route is specified), then it will be sent by default route - this is what is specified for the network 0.0.0.0 with the mask 0.0.0.0. The most typical traffic sent by default route is Internet traffic (as well as traffic to other local networks to which your computer is not directly connected - but for home networks this is a rare situation).
To select the default Internet connection, you must change the default route.
How to change the default route in Windows
Let's return to the previously received information about default route:
It means that one of my interfaces has an IP address of 192.168.0.49 and its metric is 55, the other network interface has IP 192.168.1.43 and the metric is 70. The route that has the less metric value wins (the smaller the metric, the better connection).
Thus, if I access the Internet, the network interface 192.168.0.49 will be used.
Let us verify this in several ways. Let's remember our IP addresses, as external services see them. For this I use to https://suip.biz/?act=myip:
I got that my IP is 109.126.249.183, and my local IP is 192.168.0.49. At the Windows command prompt, I do the following:
.\TRACERT.EXE suip.biz
As you can see from the first line, I actually go online through the gateway 192.168.0.1.
So, there are several ways to change the default route. For example, you can change the metric value so that another route becomes a priority. The second option is to delete the other routes, leaving only one.
How to set the metric for network connections in Windows
To be clear, I will show you a concrete example. I have two network interfaces with IP addresses:
- 192.168.0.49 (has the gateway 192.168.0.1) - is used by default
- 192.168.1.43 (has the gateway 192.168.1.1) - I want it to be used by default
To do this, I delete all default routes:
route DELETE 0.0.0.0
Now I add the one I want to make the default route by a command of the form:
route ADD 0.0.0.0 MASK 0.0.0.0 GATEWAY
In it, the GATEWAY must be replaced with the IP address of the gateway (router) of the interface through which you want to go online. For me it:
route ADD 0.0.0.0 MASK 0.0.0.0 192.168.1.1
At this stage, the Internet connection has already returned and if one connection is enough for you, then you may stop here.
However, I want the second connection to be active as a backup, but it should not be used by default.
To add it, use the following command:
route ADD 0.0.0.0 MASK 0.0.0.0 GATEWAY METRIC 100
Note that instead of the GATEWAY, you need to enter the IP address of the gateway of the ‘backup’ interface. In addition, the value is not absolute, but relative!!! Remember, that the specified value ADDS to that value of the metric that the operating system calculates. The value of 100 can be changed to another (for example, 50). But choose it so that the value in total with the calculated metric was greater than the metric of connection that we want to use by default.
My command:
route ADD 0.0.0.0 MASK 0.0.0.0 192.168.0.1 METRIC 100
We check:
route print
As you can see, in the default routes two network interfaces are still available. But now the interface 192.168.0.49 has a very large metric value, so by default the network interface 192.168.1.43 will be used.
Let's check the IP in the browser https://suip.biz/?act=myip:
As you can see, the external IP changed (it was 109.126.249.183, and it became 213.167.219.207) and local (it was 192.168.0.49, and it became 192.168.1.43).
At the Windows command prompt, I do the following:
.\TRACERT.EXE suip.biz
As you can see from the first line, I actually go online through the gateway 192.168.1.1.
Features of the route CHANGE command in Windows
It should be noted a couple of points in the behavior of the route command, which are counterintuitive (we are waiting for one result, but we get another, unexpected result).
It is not necessary to delete/add routes, including the default route, it can be modified using the route CHANGE command. But remember the peculiarity of this command, if you have two or more default routes: it will delete ALL routes and set new one that is specified with this command! At first glance this is not obvious, but if you think about it, it becomes clear why it acts in this way.
Although this has already been said above, the value of the metric is RELATIVE! The value that you set for the metric is added to the value calculated by the system. At the same time, it constantly floats: that is, the system regularly recalculates the metric values, but adds to each received number the one that you specified with the route ADD command.
Creating persistent routes that persist after a reboot
By default, all set routes, including metric values, will be reset when the computer restarts.
Using the -p switch, you can create persistent routes that will be saved after a reboot.
Example of adding a permanent route:
route -p add 192.168.50.0 mask 255.255.255.0 192.168.1.170 if <interface_ID>
Part if <interface_ID> is optional for all commands.
The interface numbers can be seen by the route print command (at the very top of the output):
The interface numbers stay in the first column - before the MAC addresses.
In addition to the -p option, the route command has a few more options, consider them.
ROUTE command Help
Manipulates network routing tables. ROUTE [-f] [-p] [-4|-6] command [destination] [MASK netmask] [gateway] [METRIC metric] [IF interface] -f Clears the routing tables of all gateway entries. If this is used in conjunction with one of the commands, the tables are cleared prior to running the command. -p When used with the ADD command, makes a route persistent across boots of the system. By default, routes are not preserved when the system is restarted. Ignored for all other commands, which always affect the appropriate persistent routes. -4 Force using IPv4. -6 Force using IPv6. command One of these: PRINT Prints a route ADD Adds a route DELETE Deletes a route CHANGE Modifies an existing route destination Specifies the host. MASK Specifies that the next parameter is the 'netmask' value. netmask Specifies a subnet mask value for this route entry. If not specified, it defaults to 255.255.255.255. gateway Specifies gateway. interface the interface number for the specified route. METRIC specifies the metric, ie. cost for the destination. All symbolic names used for destination are looked up in the network database file NETWORKS. The symbolic names for gateway are looked up in the host name database file HOSTS. If the command is PRINT or DELETE. Destination or gateway can be a wildcard, (wildcard is specified as a star '*'), or the gateway argument may be omitted. If Dest contains a * or ?, it is treated as a shell pattern, and only matching destination routes are printed. The '*' matches any string, and '?' matches any one char. Examples: 157.*.1, 157.*, 127.*, *224*. Pattern match is only allowed in PRINT command. Diagnostic Notes: Invalid MASK generates an error, that is when (DEST & MASK) != DEST. Example> route ADD 157.0.0.0 MASK 155.0.0.0 157.55.80.1 IF 1 The route addition failed: The specified mask parameter is invalid. (Destination & Mask) != Destination. Examples: > route PRINT > route PRINT -4 > route PRINT -6 > route PRINT 157* .... Only prints those matching 157* > route ADD 157.0.0.0 MASK 255.0.0.0 157.55.80.1 METRIC 3 IF 2 destination^ ^mask ^gateway metric^ ^ Interface^ If IF is not given, it tries to find the best interface for a given gateway. > route ADD 3ffe::/32 3ffe::1 > route CHANGE 157.0.0.0 MASK 255.0.0.0 157.55.80.5 METRIC 2 IF 2 CHANGE is used to modify gateway and/or metric only. > route DELETE 157.0.0.0 > route DELETE 3ffe::/32
Related articles:
- Setting up network routes: the choice of connection used for the Internet; simultaneous use of multiple connections for different purposes (93.9%)
- Introduction to IPv6 Addresses: How to Use and How to Explore the Network (Part 2) (55.2%)
- Best Kali Linux tools in WSL (Windows Subsystem for Linux) (Part 1) (54.4%)
- Commando VM: Windows for Hackers (52.1%)
- How to install Python and PIP on Windows 10. How to set up Python as a web server module (52.1%)
- Why in Tails the history of the entered commands is not saved. How to make so that the history of the last Bash commands are saved (RANDOM - 50%)