Ubuntu22.04 设置静态IP

安装net-tools、openvswitch-switch

Bash
apt install net-tools
apt install openvswitch-switch

查看网卡名

Bash
ifconfig
#enp3s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
#        inet 192.168.50.3  netmask 255.255.255.0  broadcast 192.168.50.255
#        inet6 fe80::2e0:b4ff:fe1c:704b  prefixlen 64  scopeid 0x20<link>
#        inet6 fdc2:8c5e:7c48:5348:2e0:b4ff:fe1c:704b  prefixlen 64  scopeid 0x0<global>
#        ether 00:e0:b4:1c:70:4b  txqueuelen 1000  (Ethernet)
#        RX packets 26226  bytes 34068377 (34.0 MB)
#        RX errors 0  dropped 1196  overruns 0  frame 0
#        TX packets 2935  bytes 362905 (362.9 KB)
#        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
#        device memory 0xd0600000-d061ffff  

#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 146  bytes 13595 (13.5 KB)
#        RX errors 0  dropped 0  overruns 0  frame 0
#        TX packets 146  bytes 13595 (13.5 KB)
#        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

配置netplan

Bash
cd /etc/netplan
cp 50-cloud-init.yaml 50-cloud-init.yaml.bak
#文件名会有不同
vi 50-cloud-init.yaml
YAML
network:
 version: 2
 renderer: networkd
 ethernets:
  enp3s0:
  #网卡名
   addresses:
    - 192.168.50.3/24
    #静态ip
   nameservers:
    addresses: [8.8.8.8,114.114.114.114]
    #dns
   routes:
    - to: default
      via: 192.168.50.1
      #网关
Bash
netplan apply

禁止Ubuntu默认云覆盖

Bash
vi /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
JSON
network: {config: disabled}

参考资料:

如何在 Ubuntu 服务器 22.04 上设置静态 IP 地址

Ubuntu Server 22.04.5 LTS重启后IP被重置问题