Linux多网卡配置

配置三网卡地址

分别配置:

# cat /etc/sysconfig/network-scripts/ifcfg-eth0
...
DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.1.2
NETMASK=255.255.255.0
GATEWAY=192.168.1.254
...


# cat /etc/sysconfig/network-scripts/ifcfg-eth1
...
DEVICE=eth1
BOOTPROTO=static
ONBOOT=yes
IPADDR=10.10.10.2
NETMASK=255.255.255.0
GATEWAY=10.10.10.254
...


# cat /etc/sysconfig/network-scripts/ifcfg-eth2
...
DEVICE=eth2
BOOTPROTO=static
ONBOOT=yes
IPADDR=172.16.1.2
NETMASK=255.255.255.0
GATEWAY=172.16.1.254
...

添加路由表别名

#192
echo "252     net0" >> /etc/iproute2/rt_tables
#10
echo "251     net1" >> /etc/iproute2/rt_tables
#172
echo "250     net2" >> /etc/iproute2/rt_tables

添加路由

#刷新路由表
ip route flush table net0
ip route flush table net1
ip route flush table net2
#在路由表中添加路由
ip route add 192.168.1.0/24 dev eth0 src 192.168.1.254 table net0
ip route add 10.10.10.0/24 dev eth1 src 10.10.10.254 table net1
ip route add 172.16.1.0/24 dev eth2 src 172.16.1.254 table net2
#在路由表添加默认路由
ip route add default via 192.168.1.254 table net0
ip route add default via 10.10.10.254 table net1
ip route add default via 172.16.1.254 table net2

设置系统默认路由

ip route add default via 192.168.1.2

更改默认路由,等同于先删除,后新增

ip route change default via 192.168.1.2 dev eth0

设置默认网关

ip route add default via 192.168.1.254 dev eth0

添加路由规则

ip rule add from 192.168.1.254 table ent0
ip rule add from 10.10.10.254 table ent1
ip rule add from 172.16.1.254 table ent2

配置启动生效

如果需要自启动生效可以写进配置文件也可以加入 /etc/rc.local

vim /etc/rc.local

#刷新路由表
ip route flush table net0
ip route flush table net1
ip route flush table net2
#添加路由
ip route add 192.168.1.0/24 dev eth0 src 192.168.1.254 table net0
ip route add 10.10.10.0/24 dev eth1 src 10.10.10.254 table net1
ip route add 172.16.1.0/24 dev eth2 src 172.16.1.254 table net2
#添加默认路由走向
ip route add default via 192.168.1.254 table net0
#ip route add default via 10.10.10.254 table net1
#ip route add default via 172.16.1.254 table net2
ip rule add from 192.168.1.254 table ent0
ip rule add from 10.10.10.254 table ent1
ip rule add from 172.16.1.254 table ent2

其他

#添加默认网关
route add default gw 172.16.1.254
# 查看路由表
route -n