温馨提示×

linux ifconfig绑定ip怎么做

小樊
42
2026-10-07 22:04:18
栏目: 智能运维

在 Linux 中,ifconfig 本身不能直接“绑定多个 IP”到网卡长期生效,它只能临时添加/删除 IP。如果你说的是“给网卡绑定(添加)一个 IP”,做法如下:


一、用 ifconfig 临时绑定 IP(重启失效)

1️⃣ 给网卡添加 IP

ifconfig eth0 192.168.1.100 netmask 255.255.255.0 up

2️⃣ 给同一网卡绑定多个 IP(别名方式)

ifconfig eth0:0 192.168.1.101 netmask 255.255.255.0 up
ifconfig eth0:1 192.168.1.102 netmask 255.255.255.0 up

查看:

ifconfig

3️⃣ 删除绑定的 IP

ifconfig eth0:0 down

二、永久绑定 IP(推荐方式)

✅ CentOS / RHEL(传统)

编辑:

vi /etc/sysconfig/network-scripts/ifcfg-eth0:0

内容:

DEVICE=eth0:0
BOOTPROTO=static
IPADDR=192.168.1.101
NETMASK=255.255.255.0
ONBOOT=yes

重启网络:

service network restart

✅ Ubuntu(老版本)

编辑:

vi /etc/network/interfaces
auto eth0:0
iface eth0:0 inet static
address 192.168.1.101
netmask 255.255.255.0

✅ 新系统(推荐 ip 命令 + Netplan / NetworkManager)

现代 Linux 已不推荐 ifconfig,建议用:

ip addr add 192.168.1.101/24 dev eth0

三、常见用途说明

  • 单网卡多 IP:虚拟站点、多业务
  • VIP(高可用):Keepalived
  • 容器 / 桥接:bridge + IP

如果你能说明:

  • 系统版本(CentOS / Ubuntu / Debian)
  • 是临时还是永久
  • 单 IP 还是多 IP

我可以给你精确配置文件示例。

0 踩