如何在Linux中配置别名 Linux ip address add secondary

linux中配置secondary IP可通过ip命令临时添加或配置文件永久设置,如centos中编辑ifcfg-eth0:0,ubuntu中修改interfaces或Netplan文件并应用。

如何在Linux中配置别名 Linux ip address add secondary

在Linux中为网络接口配置 secondary IP地址(辅助IP地址)是一项常见操作,特别是在需要在同一接口上绑定多个IP地址的场景中,比如运行多个Web服务或实现网络隔离。可以通过 ip 命令临时添加,也可以通过配置文件实现永久生效。

使用 ip 命令添加 secondary IP

临时添加一个辅助IP地址,使用 ip address add 命令:

 ip address add 192.168.1.100/24 dev eth0 

说明:

  • 192.168.1.100 是你要添加的 secondary IP
  • /24 是子网掩码(等同于 255.255.255.0)
  • dev eth0 指定网络接口,根据实际接口名称修改(如 ens33、enp0s3 等)

添加后可通过以下命令查看:

 ip addr show dev eth0 

永久配置辅助IP(以 CentOS/RHEL 为例)

若需重启后仍生效,需编辑网络配置文件。

进入网络脚本目录:

 /etc/sysconfig/network-scripts/ 

创建或编辑接口的别名配置文件。例如为主接口 eth0 添加一个辅助IP,创建文件:

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

内容如下:

 DEVICE=eth0:0 BOOTPROTO=static ONBOOT=yes ipadDR=192.168.1.100 NETMASK=255.255.255.0 

保存后重启网络服务:

 systemctl restart network 

Ubuntu/debian 系统配置方法

Ubuntu 使用 Netplan 或传统 interfaces 文件。若使用 /etc/network/interfaces,可添加:

 auto eth0:0 iface eth0:0 inet static     address 192.168.1.100     netmask 255.255.255.0 

然后重启网络:

 sudo systemctl restart networking 

若使用 Netplan(Ubuntu 18.04+),在 .yaml 文件中添加第二个地址到 addresses 列表:

 addresses:   - 192.168.1.10/24   - 192.168.1.100/24 

应用配置:

 sudo netplan apply 

基本上就这些。根据发行版选择合适的方法,临时用 ip 命令,长期用配置文件。确保IP不冲突,子网正确。

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享