- Ubuntu Server17.10 开始,弃用 /etc/network/interfaces 配置文件,改成 netplan ⽅式,配置⽂件是:/etc/netplan/01-netcfg.yaml
1.配置参考
vim /etc/netplan/01-netcfg.yaml
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
ens33:
dhcp4: no
addresses: [192.168.21.180/24]
gateway4: 192.168.21.2
nameservers:
addresses: [114.114.114.114]
addresses: [8.8.4.4]
#使配置文件生效
netplan apply
2.配置临时IP路由默认网关
ip addr add 192.168.0.99/24 dev eno1 # 临时IP
ip route add 192.168.0.99/24 via 192.168.1.1 dev eno1 #临时路由
ip route add default via 192.168.1.1 dev eno1 #临时网关
ip link set eth0 up #启用网卡
ifconfig eth0 192.168.1.100 netmask 255.255.255.0
ifconfig eth0 up # 启用
ifconfig eth0 down # 禁用
route add default gw 192.168.1.1
3.ubuntu 22.04 IP和网关不同段配置
4.命令
# 生效
sudo netplan apply
# 这个命令会尝试应用配置,并在超时之前允许你确认配置是否正确。如果配置不正确,你可以选择回滚到之前的状态。
sudo netplan try
#验证配置
#检查 IP 地址和路由是否正确设置。
ip addr show enp0s3
ip route show
#注意事项
确保替换 enp0s3 为你的实际网络接口名称。
确保网关 172.20.0.1 是正确的,并且在你的网络拓扑中可达。