Ubuntu18.04 configures dual network cards to surf the Internet

 

Due to work reasons, it is necessary to use wired and wireless network cards at the same time. The
wireless network card accesses the external network, and the wired network card accesses the internal network.

For example:
the ip address of the wired network card is 172.1.2.3, and the subnet mask is 255.255.255.0
The ip address of the wireless network card is 192.168.1.111, and the subnet mask is 255.255.255.0

If you just want to access the external network through the wireless network card, you only need to increase the priority of the wireless network card.
Then we use the command:

ip route show

View the routing table as follows:

default via 192.168.1.1 dev enx2 proto dhcp metric 600 
default via 172.1.2.1 dev enp4 proto dhcp metric 100 
172.1.2.0/24 dev enp4 proto kernel scope link src 172.1.2.3 metric 100 
192.168.1.0/24 dev enx2 proto kernel scope link src 192.168.1.111 metric 600

Among them, enx2 is the device name of my wireless network card, and enp4 is the device name of my wired network card.
You can see that the wired network card accesses the network by default through the gateway 172.1.2.1, and its metric is 100,
while the wireless network card is through the gateway. 192.168.1.1 accesses the network by default, and its metric is 600

Since the system will give priority to the network card with a lower hop for connection, the so-called network requests go to the wired network card,
then we only need to set the hop of the wired network card to be higher, and its priority will be reduced.
Executable command:

sudo ip route del default via 172.1.2.1 dev enp4 proto dhcp metric 100
sudo ip route add default via 172.1.2.1 dev enp4 proto dhcp metric 1000

Or directly delete the default routing settings of the wired network card:

sudo ip route del default via 172.1.2.1 dev enp4 proto dhcp metric 100

It can be changed to use the wireless network card to access the network.

In this way, all network connections except 172.1.2.0/24 will use the wireless network card
, but I also want other internal subnets 172.1.3.0 to also use the wired network card
. Then I can add subnet routing information:

sudo ip route add 172.1.3.0/24 dev enp4 proto kernel scope link src 172.1.2.3 metric 100

Or directly add ip routing information:

sudo ip route add 172.1.4.1 dev enp4 proto kernel scope link src 172.1.2.3 metric 100

The final my routing configuration is:

default via 192.168.1.1 dev enx2 proto dhcp metric 600 
172.1.2.0/24 dev enp4 proto kernel scope link src 172.1.2.3 metric 100 
172.1.3.0/24 dev enp4 proto kernel scope link src 172.1.2.3 metric 100
172.1.4.1 dev enp4 proto kernel scope link src 172.1.2.3 metric 100
192.168.1.0/24 dev enx2 proto kernel scope link src 192.168.1.111 metric 600

Then my computer will:
access the devices in the two subnets of 172.1.2.0/24 and 172.1.3.0/24 through the wired network card,
access the single device 172.1.4.1 through the wired network card,
and access other connections through the wireless network card.

You're done!

Guess you like

Origin blog.csdn.net/cuiyuan605/article/details/113397856