Ubuntu22.04 设置静态IP
先查看一下网关
输入 ifconfig 查看IP地址和网卡,我这里的网卡是enp2s0
root@ubuntu22:~# ifconfig
enp2s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.2.111 netmask 255.255.255.0 broadcast 192.168.2.255
inet6 fe80::7a24:afff:fe85:b57a prefixlen 64 scopeid 0x20<link>
inet6 240e:37a:4107:bb10:7a24:afff:fe85:b57a prefixlen 64 scopeid 0x0<global>
ether 78:24:af:85:b5:7a txqueuelen 1000 (Ethernet)
RX packets 120 bytes 14716 (14.7 KB)
RX errors 0 dropped 6 overruns 0 frame 0
TX packets 104 bytes 15952 (15.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 94 bytes 7238 (7.2 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 94 bytes 7238 (7.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
切换到 /etc/netplan/ 目录下,看下配置文件,有的人是01,我这边是00
root@ubuntu22:~# cd /etc/netplan/
root@ubuntu22:/etc/netplan# ls
00-installer-config.yaml
修改配置文件,默认的应该是这样
network:
ethernets:
enp2s0:
dhcp4: true
version: 2
修改内容如下
network:
renderer: networkd
ethernets:
enp2s0:
addresses:
- 192.168.2.111/24
nameservers:
addresses: [114.114.114.114, 8.8.8.8]
routes:
- to: default
via: 192.168.2.1
version: 2
应用配置
root@ubuntu22:~# netplan apply