Ubuntu system configuration static IP address

Why a static IP address is needed
In an enterprise's internal network, the computer obtains an IP address from the locally connected router. By default, this is a dynamic IP address, that is, the IP obtained from the router changes randomly each time. This rule has no impact on ordinary clients accessing the network, but if it is a server (such as Yueku Enterprise Network Disk Server), a static IP address is needed so that other clients can connect to the server in a fixed way. , instead of frequently changing the IP address of the connecting server.
How to configure a static IP address.
Ubuntu 18.04 will no longer support ifupdown configuration of the network, and will use Netplan instead. This tutorial is based on Netplan's network configuration method and has been verified on Ubuntu 18.04.
In the following example, the current dynamic IP is: 192.168.0.13, and we will configure it as a static IP: 192.168.0.180.
The static IP must be consistent with the network segment of the current dynamic IP, otherwise the network may be unable to connect. If the static IP has been assigned to other hosts in the network, it will cause an IP conflict and the network cannot be connected. Directly configuring the current dynamic IP as a static IP can avoid this situation.
View current dynamic IP

ifconfig

Output content

docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:81:00:3f:4b  txqueuelen 0  (以太网)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.13  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::bd31:b416:5049:bbfc  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:33:20:75  txqueuelen 1000  (以太网)
        RX packets 1882  bytes 257705 (257.7 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 365  bytes 32881 (32.8 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Among them, enp0s3 is the network card connected with a physical network cable. This is a LAN IP, address: 192.168.0.13.
2. Configure a static IP address.
List the network configuration files. They may differ in different Ubuntu versions, but they must be .yaml files.

ls /etc/netplan/

Output content

01-network-manager-all.yaml

Back up existing configuration files so you can restore them if a configuration error occurs.

sudo cp  /etc/netplan/01-network-manager-all.yaml /etc/netplan/01-network-manager-all.yaml.bak

Open with vi editor

sudo vim /etc/netplan/01-network-manager-all.yaml

If you use xshell mode, please enter in vi editor command mode (to avoid copy format confusion):

:set paste

The configuration file is modified as follows:
addresses is the static IP address, gateway4 is the gateway address, and nameservers is the DNS server address.

network:
    ethernets:
        enp0s3:
            dhcp4: no
            addresses: [192.168.0.180/24]
            optional: true
            gateway4: 192.168.0.1
            nameservers:
                    addresses: [192.168.0.1,223.6.6.6]

    version: 2
  1. Restart the network service to make the configuration take effect
sudo netplan apply
  1. Validation results
ifconfig

Output content

docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:81:00:3f:4b  txqueuelen 0  (以太网)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.180  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::bd31:b416:5049:bbfc  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:33:20:75  txqueuelen 1000  (以太网)
        RX packets 1882  bytes 257705 (257.7 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 365  bytes 32881 (32.8 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Ubuntu system deploys privately deployed enterprise network disk

Guess you like

Origin blog.csdn.net/Yuku226/article/details/131516005