
我有 2 个分配的地址范围,例如
- 10.10.12.25/28
- 10.12.12.24/28
如何使用 Centos 在同一网卡上添加两个网关的 IP 别名?
答案1
ip
使用命令或简单地创建文件添加不同范围内的 IP 地址非常简单ifcfg-eth0:xxx
,但您只能有一个默认网关。这是我的一台服务器上的示例:
[dkaarsemaker@gateway-001 network-scripts]$ cat ifcfg-eth1:179
# Broadcom Corporation NetXtreme BCM5704 Gigabit Ethernet
DEVICE=eth1:179
BOOTPROTO=static
ONBOOT=yes
IPADDR=87.233.215.179
NETMASK=255.255.255.240
[dkaarsemaker@gateway-001 network-scripts]$ cat ifcfg-eth1:42
DEVICE=eth1:179
BOOTPROTO=static
ONBOOT=yes
IPADDR=213.239.169.42
NETMASK=255.255.255.240
[dkaarsemaker@gateway-001 network-scripts]$ ip a l dev eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether e4:11:5b:e0:14:5c brd ff:ff:ff:ff:ff:ff
inet 87.233.215.178/28 brd 87.233.215.191 scope global eth1
inet 87.233.215.179/28 brd 87.233.215.191 scope global secondary eth1:179
inet 213.239.169.42/25 brd 213.239.169.127 scope global secondary eth1:42
inet6 fe80::e611:5bff:fee0:145c/64 scope link
valid_lft forever preferred_lft forever
但仍然只有一个默认网关:
[dkaarsemaker@gateway-001 ~]$ route -n | grep G
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 87.233.215.177 0.0.0.0 UG 0 0 0 eth1
答案2
此处添加默认网关。
cat<<.>>/etc/sysconfig/network
GATEWAY=<default gateway ip>
.
对于第一个区块 10.10.12.25/28;网络 = 10.10.12.16,广播 = 10.10.12.31。假设主机 ip = 10.10.12.17,网关 ip = 10.10.12.18。
cd /etc/sysconfig/network-scripts
cat<<. >ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes
IPADDR=10.10.12.17
PREFIX=28
DEFROUTE=no
GATEWAY=10.10.12.18
.
添加静态路由;
cat<<.>route-eth0
ADDRESS0=<address of the network you want to reach thru this gateway>
NETMASK0=<netmask for ADDRESS0>
GATEWAY0=10.10.12.18
.
对于第二块10.12.12.24/28;网络 = 10.12.12.16,广播 = 10.12.12.31。假设主机 ip = 10.12.12.17,网关 ip = 10.12.12.18。
cat<<. >ifcfg-eth0:1
BOOTPROTO=static
ONBOOT=yes
IPADDR=10.12.12.17
NETMASK=255.255.255.240
DEFROUTE=no
GATEWAY=10.12.12.18
.
添加静态路由;
cat<<.>route-eth0:1
ADDRESS0=<address of the network you want to reach thru this gateway>
NETMASK0=<netmask for ADDRESS0>
GATEWAY0=10.12.12.18
.
service network restart
netstat -rn