After Windows and Ubuntu can ping each other, linux cannot access the Internet. Reasons: ① routing is not set properly, ② DNS is not set properly

After ensuring that the Windows and Ubuntu networks can ping each other, if Ubuntu cannot access the Internet, there are usually two reasons: the routing is not set up and DNS is not set up.
If the following command is not executed successfully, it indicates that the route is not set properly:
$ ping 8.8.8.8
connect: Network is unreachable
If "ping 8.8.8.8" succeeds, but "ping www.baidu.com" fails, DNS is not set properly:
$ ping www.baidu.com
ping: unknown host www.baidu.com

1. Set DNS method

DNS settings are relatively simple. 8.8.8.8 is a DNS server easy to remember and use. Modify / etc / resolv. In Ubuntu Conf file,
The content is as follows: nameserver 8.8.8.8

2. View network information and route

Assume that the network interface configuration and routing table on a host are as follows:
$ ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:C2:8D:7E
inet addr:192.168.10.223 Bcast:192.168.10.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:0 (0.0 b) TX bytes:420 (420.0 b)
Interrupt:10 Base address:0x10a0
eth1 Link encap:Ethernet HWaddr 00:0C:29:C2:8D:88
inet addr:192.168.56.136 Bcast:192.168.56.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:603 errors:0 dropped:0 overruns:0 frame:0
TX packets:110 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:55551 (54.2 Kb) TX bytes:7601 (7.4 Kb)
Interrupt:9 Base address:0x10c0
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:37 errors:0 dropped:0 overruns:0 frame:0
TX packets:37 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:3020 (2.9 Kb) TX bytes:3020 (2.9 Kb)
$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.10.0 * 255.255.255.0 U 0 0 0 eth0
192.168.56.0 * 255.255.255.0 U 0 0 0 eth1
127.0.0.0 * 255.0.0.0 U 0 0 0 lo
default 192.168.10.1 0.0.0.0 UG 0 0 0 eth0
See the following table for the meanings of the items in the output information of the above route command:
Destination - destination network segment or host
Gateway gateway address, "*" indicates that the target is the network to which the host belongs, and routing is not required
Genmask} netmask
Flags flag. Some possible markers are as follows
U - route is active
H - the target is a host
G - route to gateway
R - restore table entries generated by dynamic routing
D - dynamically installed by the routing daemon
M - modified by the routing daemon
          ! - Reject route
Metric} routing distance, the number of medium revolutions required to reach the specified network
Ref - number of route item references
Use = the number of times this routing item is searched by the routing software
Iface = the output interface corresponding to the routing table item
In the above example, this host has two network interfaces:
① A network interface is connected to the 192.168.10.0/24 network
② The other network interface is connected to the 192.168.56.0/24 network.
If the destination address of the data packet to be sent is 192.168.56.3, the sum operation with the subnet mask in the first row yields 192.168.56.0, which is inconsistent with the destination network address in the first row, and then the sum operation with the subnet mask in the second row yields 192.168.56.0, which is the destination network address in the second row, so it is sent from the eth1 interface, Since 192.168.56.0/24 is the network directly connected to eth1 interface, it can be directly sent to the destination host without forwarding through the router. If the destination address of the packet to be sent is 202.10.1.2, which does not match the first three lines of routing table entries, it should be sent from the eth0 interface according to the default routing entry, first to the 192.168.10.1 router, and then let the router determine the next hop address according to its routing table.

3. Manage route command

You can use the route command to manage routes.
Example:
1) Add route: first determine the gateway IP, assuming 192.168.1.1
$ sudo route add default gw 192.168.1.1
$ping 8.8.8.8 / / verification
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=53 time=19.8 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=53 time=19.8 ms
2) Delete route:
$ sudo route del default gw 192.168.1.1

Keywords: DNS

Added by scraptoft on Sun, 16 Jan 2022 01:07:21 +0200