scenario

The author got a Linux device with two network ports, one of which can connect to the external network and the other to the internal network. When the external network port is connected, any process on the device can communicate with the outside world. When the internal network port is connected, the entire external network is disconnected.

use

ifconfig -a
Copy the code

Get all network connection information

Eth0 Link encap: Ethernet hardware address 00:16:3e:06:29: F9 INET address :192.168.1.101 Broadcast address :192.168.1.255 Mask :255.255.255.0Copy the code

and

Eth1 Link encap: Ethernet hardware address 00:16:3E: 07:2f: F9 INET address :192.168.2.101 Broadcast address :192.168.2.255 Mask :255.255.255.0Copy the code

192.168.1.101 can be connected to the extranet. You can use curl to test the extranet.

Analysis of the

The common reasons for network failure are as follows

  • DNS issues
  • The gateway is blocked.
  • The wrong gateway was connected

In this case, I first suspected DNS problems, so execute

Ping 114.114.114.114Copy the code

If no, it means that the connection to the Internet is also unavailable. Generally, this address is through. If you are not sure, you can verify it in another environment.

The gateway cannot be connected. Verify again. Other computers connected to the gateway are connected.

One last problem, connecting to the wrong gateway.

Before we connect, we know that our two network ports are connected to two different gateways

  • 192.168.1.1 (No External network connection)
  • 192.168.2.1 (With external network connection)

We started checking our routes.

 /sbin/route -n
Copy the code

get

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
Copy the code

The information above indicates that eth0 is used to access both 0.0.0.0 and 192.168.1.0, and eth1 is used to access 192.168.2.0.

The solution

You need to perform this if you want to allow network access through an extranet

Route add default gw 192.168.0.1 eth1Copy the code

After the command is passed

Ping 114.114.114.114Copy the code

Test successful!

This also applies to networks that have both WIFI and Ethernet, with this command you can optimize the connection (such as wired) to transfer data over a faster and more stable network.