
Ubuntu 系统中,无论是老版本还是最新版本,正确配置网卡 IP 都是保证网络正常的关键。本文涵盖 Ubuntu 10.04~24.04 全系列,详细说明 临时 IP、永久 IP、单 IP、多 IP 的配置方法,以及桌面和服务器环境下的操作。
一、查看网卡信息
# 查看网卡列表及状态
ip link show
# 查看网卡 IP
ip addr show
# 查看路由表
ip route show
常见网卡名称:
Ubuntu 10.04~16.04:
eth0,eth1Ubuntu 16.04+:
ens33,enp0s3等
二、临时配置 IP(重启失效)
1. 推荐方式:ip 命令
# 添加单个 IP
sudo ip addr add 192.168.1.100/24 dev eth0
# 添加多个 IP(示例)
sudo ip addr add 192.168.1.101/24 dev eth0
sudo ip addr add 192.168.1.102/24 dev eth0
# 删除 IP
sudo ip addr del 192.168.1.100/24 dev eth0
# 设置默认网关
sudo ip route add default via 192.168.1.1
2. 老版本可用:ifconfig + route
sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0 up
sudo ifconfig eth0:0 192.168.1.101 netmask 255.255.255.0 up # 多 IP
sudo route add default gw 192.168.1.1
Tip:
ifconfig在新版本已废弃,推荐用ip。
三、永久配置 IP(SSH/服务器专用)
1. Ubuntu 10.04~15.10:/etc/network/interfaces
单 IP
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8 8.8.4.4
多 IP(别名接口)
# 主 IP
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8 8.8.4.4
# 第二个 IP
auto eth0:0
iface eth0:0 inet static
address 192.168.1.101
netmask 255.255.255.0
# 第三个 IP
auto eth0:1
iface eth0:1 inet static
address 192.168.1.102
netmask 255.255.255.0
应用配置:
sudo service networking restart
2. Ubuntu 16.04~17.10:混合模式
桌面可用 GUI(忽略)
服务器推荐用 Netplan(参考下方 18.04+)
单 IP / 多 IP 配置方式同上
3. Ubuntu 18.04~24.04:Netplan
单 IP 示例
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
addresses:
- 192.168.1.100/24
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
多 IP 示例
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
addresses:
- 192.168.1.100/24
- 192.168.1.101/24
- 192.168.1.102/24
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
应用配置:
sudo netplan apply
Tip:服务器环境推荐
networkd渲染器,Netplan 数组支持多个 IP。
四、检查配置
# 查看 IP 是否生效
ip addr show eth0
# 测试网络连通性
ping 8.8.8.8
# 查看默认路由
ip route show
五、总结
临时配置:快速生效,重启失效
永久配置:修改配置文件,重启或
netplan apply生效多 IP:老版本用别名,新版本直接写数组

评论区