1.Applicable system
The static IP configuration method introduced in this article is applicable to Ubuntu system versions 18.04 and above.
2.Confirm the network interface for Ethernet connection
In general, a personal PC only has one network card, but there may be multiple network cards in the server. Use the ifconfig command to view the corresponding IP network interface.
If the prompt does not find the ifconfig command, use the following command to install the tool.
jb@X1:~$ sudo apt-get install net-tools
View the network cards that need to be modified, as shown below, you can find all the current network interface information. Since my host only has one network card, only modify it.
jb@X1:~$ ifconfig
enp1s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.6.186 netmask 255.255.255.0 broadcast 192.168.6.255
inet6 fe80::9412:4614:58fe:d3e4 prefixlen 64 scopeid 0x20<link>
ether e0:51:d8:11:8b:1a txqueuelen 1000 (Ethernet)
RX packets 23334 bytes 26924228 (26.9 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 13582 bytes 2259559 (2.2 MB)
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 1872 bytes 247725 (247.7 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1872 bytes 247725 (247.7 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
3.Modify the default configuration file of the network card
The default configuration information path is as follows
jb@X1:~$ sudo vim /etc/netplan/01-network-manager-all.yaml
No static IP has been configured (the system defaults to receiving IP addresses assigned by router DHCP), and the file content is as follows:
# Let NetworkManager manage all devices on this system
network:
version: 2
renderer: NetworkManager
4.Modify the static IP reference to the following configuration file
network:
ethernets:
ens33:
dhcp4: no
dhcp6: no
addresses:
- 192.168.1.66/24 #静态IP
routes:
- to: default
via: 192.168.1.1 #网关
nameservers:
addresses:
- 192.168.1.1 #DNS1
- 114.114.114.114 #DNS2
version: 2
renderer: networkd
Explanation of static IP writing method:
Specific IP/network segment
192.168.1.66/24
wherein:
192.168.1.66 #It's an IP address
24 is a network segment, indicating that the first 24 bits represent the network segment, and the last 8 bits represent the host bit. This indicates that the network segment can accommodate up to 2 ^ (8) -2=256-2=253 machines
Apply to make this configuration effective
jb@X1:~$ sudo netplan apply
5.Static IP and IP switching using DHCP allocation
By modifying the/etc/netplan/01 network manager all. yaml file
Assign IP using DHCP: Restore the file to default and make the configuration effective
Using static IP: refer to the configuration in point 4