搭建了一个本地服务器,在局域网内部署了一些服务,但是路由器重启后,服务器的IP地址有时候会变,这个时候该怎么办呢,如何进行固定IP设置呢?
图形化版类似于windows的网络设置,比较简单,此处不做描述。
ubuntu@ubuntu:~$ cd /etc/netplan/
ubuntu@ubuntu:/etc/netplan$ ll
total 12
drwxr-xr-x 2 root root 4096 Apr 1 2020 ./
drwxr-xr-x 100 root root 4096 May 5 01:32 ../
-rw-r--r-- 1 root root 416 Apr 1 2020 50-cloud-init.yaml
ubuntu@ubuntu:/etc/netplan$ cat 50-cloud-init.yaml
# This file is generated from information provided by the datasource. Changes
# to it will not persist across an instance reboot. To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
ethernets:
eth0:
dhcp4: true
optional: true
version: 2
ubuntu@ubuntu:/etc/netplan$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.xxxx netmask 255.255.255.0 broadcast 192.168.0.255
inet6 xxxx::xxxx:xxxx:xxxx:xxxx prefixlen 64 scopeid 0x20<link>
ether e4:5f:01:00:4d:49 txqueuelen 1000 (Ethernet)
RX packets 3902 bytes 3716301 (3.7 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2633 bytes 216207 (216.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ubuntu@ubuntu:/etc/netplan$ sudo vi 50-cloud-init.yaml
network:
ethernets:
eth0: # 网卡名称
addresses: [192.168.0.101/24] # ip地址
gateway4: 192.168.0.1 # 网关地址
nameservers:
addresses: [8.8.8.8,114.114.114.114] # DNS解析地址
# dhcp4: true
# optional: true
version: 2
renderer: networkd
sudo netplan apply
注:其他版本可能不适用该方法。