在Ubuntu 22.10中,您可以通过以下几种方式为单个网络接口配置多个IP地址:
Ubuntu 22.10默认使用netplan进行网络配置,这是最推荐的方法。
sudo nano /etc/netplan/01-netcfg.yaml
network:
version: 2
renderer: networkd
ethernets:
enp0s3: # 替换为您的网卡名称
dhcp4: no
addresses:
- 192.168.1.100/24 # 主IP
- 192.168.1.101/24 # 附加IP1
- 192.168.1.102/24 # 附加IP2
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
sudo netplan apply
如果您需要临时添加IP地址(重启后会失效):
sudo ip addr add 192.168.1.101/24 dev enp0s3
sudo ip addr add 192.168.1.102/24 dev enp0s3
验证添加的IP:
ip addr show enp0s3
sudo nano /etc/network/interfaces
auto enp0s3
iface enp0s3 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
up ip addr add 192.168.1.101/24 dev enp0s3
up ip addr add 192.168.1.102/24 dev enp0s3
sudo systemctl restart networking
检查所有IP地址是否已配置:
ip addr show
测试网络连通性:
ping -c 4 192.168.1.1 # 测试网关
ping -c 4 8.8.8.8 # 测试外网
如需删除临时添加的IP地址,可以使用:
sudo ip addr del 192.168.1.101/24 dev enp0s3