在 Linux 中,使用 ifconfig 给一块网卡绑定多个 IP(即给接口添加多个 IP 地址)通常是通过**给接口添加子接口(alias)**来实现的。
下面以常见的网卡 eth0 为例说明。
ifconfig eth0:0 192.168.1.100 netmask 255.255.255.0 up
ifconfig eth0:1 192.168.1.101 netmask 255.255.255.0 up
ifconfig
或
ifconfig -a
你会看到类似:
eth0 inet 192.168.1.10
eth0:0 inet 192.168.1.100
eth0:1 inet 192.168.1.101
ifconfig eth0:0 down
⚠️ ifconfig 本身不能永久保存配置,需要修改系统网络配置文件。
vi /etc/sysconfig/network-scripts/ifcfg-eth0:0
内容:
DEVICE=eth0:0
IPADDR=192.168.1.100
NETMASK=255.255.255.0
ONBOOT=yes
vi /etc/network/interfaces
auto eth0:0
iface eth0:0 inet static
address 192.168.1.100
netmask 255.255.255.0
现在更推荐使用 ip 命令(功能更强):
ip addr add 192.168.1.100/24 dev eth0
ip addr add 192.168.1.101/24 dev eth0
查看:
ip addr show eth0
如果你告诉我:
我可以给你最合适的配置方式。