Common ways to configure network and static IP address in Ubuntu

The environment here is the network configuration of the Ubuntu18 version in the virtual machine. It is more common to use virtual machines, because the host is usually directly connected by wire, and the configuration is very simple.

1. NAT mode

The simplest way to connect a virtual machine to the Internet is to check "NAT mode" . No configuration is required. If the host can access the Internet, the virtual machine can access the Internet. It is enough for those who only need to access the Internet, as shown below:

2. Host only mode

In the above NAT mode, the IP address cannot be changed or the range of IP addresses allocated by DHCP, so it is more flexible to configure "Host Only Mode" , check "Connect the host virtual adapter to this network" and "Use local DHCP service to The IP address assigned to the virtual machine is as shown below:

3. Bridge mode

Bridge mode can connect to external network devices. What does it mean? For example, if the host is plugged into a USB WIFI, then multiple virtual machines in the host can actively connect to this USB-WIFI. The significance of this is that these devices can be configured in the same network segment. This is rarely used, and some It is more convenient when you need to do distributed processing testing.

4. Static IP

The above are all dynamic IPs automatically assigned by DHCP. Next, let’s look at setting a static IP, which is very practical in many scenarios.

4.1. User interface configuration

We open the network settings (in the upper right corner), click "IPv4" , select "Manual" (manual configuration), enter the IP address, Netmask, Gateway, and DNS options respectively. After setting, Click  Apply in the upper right corner , as shown below:

4.2、netplan

Of course, many times we are also accustomed to using commands to operate, or sometimes the remote Linux server only has a command line interface. We can modify the network configuration file to set a fixed IP:

Check the IP status of this machine: ifconfig 

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.55  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 240e:338:c16:43a1:9017:c885:194a:4  prefixlen 128  scopeid 0x0<global>
        inet6 240e:338:c16:43a1:281d:8ea4:d748:21dd  prefixlen 64  scopeid 0x0<global>
        inet6 fe80::4531:a232:48b5:41f9  prefixlen 64  scopeid 0x20<link>
        inet6 240e:338:c16:43a1:53dd:958d:fd3e:1caf  prefixlen 64  scopeid 0x0<global>
        ether 00:0c:29:4b:0b:84  txqueuelen 1000  (Ethernet)
        RX packets 3152  bytes 4123672 (4.1 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1887  bytes 184966 (184.9 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 200  bytes 18080 (18.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 200  bytes 18080 (18.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

The name of the network card is ens33 . What you need to pay attention to is the IP address and subnet mask: inet 192.168.3.55 netmask 255.255.255.0
Check the IP routing table: route -n

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.3.1     0.0.0.0         UG    100    0        0 ens33
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 ens33
192.168.3.0     0.0.0.0         255.255.255.0   U     100    0        0 ens33  

Just follow the gateway: 192.168.3.1

cd /etc/netplan
backup: sudo cp 01-network-manager-all.yaml 01-network-manager-all1.yaml 

Modify the network configuration file: sudo gedit 01-network-manager-all.yaml 

network:
  version: 2
  renderer: NetworkManager
  ethernets:
    ens33:
      addresses: [192.168.3.110/24]
      gateway4: 192.168.3.1
      dhcp4: no
      nameservers:
        addresses: [114.114.114.114, 223.5.5.5]

Precautions:

IP address : [192.168.3.110/24]/24  required 

What does this 24 mean? We know that an IP address is composed of two parts, the network part + the host part, so how to distinguish them? Through the subnet mask, for example: 255.255.255.0, it means that the first three bytes are the network part, and the last 1 byte is the host part. If it is 255.255.0.0, then the network part is the first two bytes, and the last byte is the host part. Two bytes are the host part, so here we are 24, 24 bits are 3 bytes, which means 192.168.3 is the network part, and the following 110 represents one of the hosts in the host part.

gateway4 indicates the gateway address, dhcp4: no indicates no dynamic allocation

In addition to spaces for indentation alignment, a space is required after the colon.

Apply the new configuration: sudo netplan apply
Restart the network: sudo service network-manager start

We can also see it intuitively in the network settings. Of course, the ifconfig command can also be used, as follows: 

 

4.3、interfaces

Another one that was very common in the past, modify: gedit /etc/network/interfaces

auto lo
iface lo inet loopback
auto ens33
iface ens33 inet static
address 192.168.3.110
netmask 255.255.255.0
gateway 192.168.3.1
dns-nameserver 114.114.114.114

sudo systemctl restart networking
sudo /etc/init.d/networking restart

New modifications based on the original, you can freely select the IP address and open the network configuration

 After selecting, we will check that the IP address has changed.

The above is the three forms of network configuration in virtual machines and the basic knowledge of configuring static IP. I hope it can help everyone.

Guess you like

Origin blog.csdn.net/weixin_41896770/article/details/134282276