Solve the problem of (ubuntu is not connected to the Internet in the virtual machine, and the xshell remote connection reports an error)

Briefly introduce the three network modes in ubuntu:

1. The bridge mode of linux

  In the bridge mode, the virtual machine and the host are equivalent to two independent machines, but we need to configure the virtual machine with independent ip address, default gateway, subnet mask and dns, and the IP address must be in the same network segment as the host Inside. 
  This configuration allows the virtual machine to be the same as a separate machine in the local area network, and the virtual machine can be accessed in other machines

       Disadvantage: The computer cannot open the shared hotspot .

2. NAT mode of Linux

  NAT is the simplest way of networking. In this mode, the virtual machine can access the virtual machine in the host with the help of the host network, but other machines in the LAN cannot access the virtual machine . This mode is the most convenient for networking, as long as the host is connected to the Internet, it can be easily connected in the virtual machine.

3. The host-only mode of linux

  Only the host mode separates the virtual network from the real network . The virtual machine in the local area network can access the virtual machine, and the host of the virtual machine can also access the virtual machine, but other hosts in the local area network cannot access the virtual machine . The virtual network and the real network are equivalent to two local area networks, but the virtual machine and its host are connected by a twisted pair.
 

 

It is recommended to use NAT network connection:

Step 1: Set up NAT networking

 

In the above steps, you can check the Internet by ping www.biadu.com, the following figure is considered to be Internet available (ctrl + c to end the connection)   

 

 

If you want to connect to the virtual ubuntu through xshell, you can follow the second step to set

Step 2: Turn on the virtual machine and set static ip

Open the virtual network editor

Check DHCP, check the start IP address, end IP address, this range is the IP you can choose

Click NAT to view your own gateway

[Ubuntu 12.04] vmware NAT mode

Open the /etc/network/interfaces file, the file at the beginning, only the following content

auto lo
iface lo inet loopback

Then directly add settings behind the file, the modified content becomes

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 你选择的ip 例如:192.168.112.225
netmask 255.255.255.0
gateway 你的网关IP  例如:192.168.112.2
dns-nameservers 8.8.8.8

The eth0 parameter should match the fconfig output of your own computer.

 

【ubuntu 14.04】

In ubuntu 14.04, the previous settings are the same as ubuntu 12.04, but you need to modify another file /etc/NetworkManager/NetworkManager.conf, set the managed parameter inside to true, and then restart the machine.

 

【ubuntu 16.04】

First need to modify /etc/network/interfaces

increase

auto ens33
iface ens33 inet static
address 你选择的IP
netmask 255.255.255.0
gateway 你的网关IP
dns-nameservers 8.8.8.8

(Note that the name of the network card after ubuntu 16 is no longer eth0, but other names. For example, the author here is ens33, and the line dns-server must be written, otherwise the setting will fail)

 

Modify the /etc/NetworkManager/NetworkManager.conf file and set managed to true

 

Set /etc/resolvconf/resolv.conf.d/base, increase

nameserver 8.8.8.8

Because after the machine restarts, the real configuration of dns is read in /etc/resolv.conf, but the data of /etc/resolv.conf comes from /etc/resolvconf/resolv.conf.d/base

 

Users can check whether the value of /etc/resolvconf/resolv.conf.d/base is flushed to /etc/resolv.conf through the following command

resolvconf -u

Restart network service

/etc/init.d/networking restart

 

【ubuntu 18.04】

Open the /etc/netplan/01-network-manager-all.yaml configuration file, the original content is as follows

# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager

 

Modified configuration

# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager

  ethernets:
    ens33:   #配置的网卡名称
      dhcp4: no    #dhcp4关闭
      dhcp6: no    #dhcp6关闭
      addresses: [你选择的IP/24]   #设置本机IP及掩码
      gateway4: 你的网关IP   #设置网关
      nameservers:
          addresses: [114.114.114.114, 8.8.8.8]   #设置DNS

 

Execute the following command to make the configuration effective, then the IP address becomes static, and the ping Internet address is normal

netplan apply

 As shown in the figure below, the static ip is successfully set, and you can happily connect through Xshell

In addition, if there is frequent network disconnection, you can solve it as follows:

       Reason: Many options of ppp are default, among which the number of lcp-echo-failure is set to 4, and lcp-echo-interval is set to 30 seconds. In other words, if the ADSL server does not give back an echo-reply signal within 120 seconds, UBuntu will think that there is a problem with the network and disconnect the network.

Reference link 2: Extended time

Guess you like

Origin blog.csdn.net/Growing_hacker/article/details/95971641