Centos下新增靜態路由(臨時和永久有效)的操作記錄

散盡浮華發表於2017-12-10

 

公司IDC機房伺服器上部署了一套外網LB環境,預設配置的是外網ip的路由地址,由於要和其他內網機器通訊,所以需要配置內網ip的路由地址。整個操作過程,記錄如下,以供以後參考學習:

1)內網網路卡繫結
[root@external-lb01 ~]# cd /etc/sysconfig/network-scripts/
[root@external-lb01 network-scripts]# cat ifcfg-em1 
DEVICE=em1
HWADDR=80:18:44:EB:0F:F4
TYPE=Ethernet
UUID=1bb1d77b-2cda-4640-b425-3ef692138937
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
[root@external-lb01 network-scripts]# cat ifcfg-em2
DEVICE=em2
HWADDR=80:18:44:EB:0F:F5
TYPE=Ethernet
UUID=e28f73ac-9149-47d9-84eb-3e7c027038e2
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
[root@external-lb01 network-scripts]# cat ifcfg-bond0
DEVICE=bond0
name='bond0'
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=none
IPADDR=10.0.36.21
NETMASK=255.255.255.0
GATEWAY=10.0.36.1
BONDING_OPTS='mode=4 miimon=100'

2)外網網路卡繫結
[root@external-lb01 network-scripts]# cat ifcfg-em3 
DEVICE=em3
HWADDR=80:18:44:EB:0F:F6
TYPE=Ethernet
UUID=4f0384bb-7140-4161-9a87-561e0964120c
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=none
MASTER=bond1
SLAVE=yes
[root@external-lb01 network-scripts]# cat ifcfg-em4
DEVICE=em4
HWADDR=80:18:44:EB:0F:F7
TYPE=Ethernet
UUID=2e9236cb-4745-46b7-bfb7-9bf7333fd99a
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=none
MASTER=bond1
SLAVE=yes
[root@external-lb01 network-scripts]# cat ifcfg-bond1
DEVICE=bond1
name='bond1'
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=none
IPADDR=115.182.11.21
NETMASK=255.255.255.240
GATEWAY=115.182.11.17
BONDING_OPTS='mode=4 miimon=100'

3)修改網路卡繫結配置
[root@external-lb01 network-scripts]# cat /etc/modprobe.d/bonding.conf 
alias bond0 bonding
options bonding mode=4 miimon=100

alias bond1 bonding
options bonding mode=4 miimon=100

網路卡繫結生效:
[root@external-lb01 network-scripts]# modprobe bonding
[root@external-lb01 network-scripts]# /etc/init.d/network restart
[root@external-lb01 network-scripts]# modprobe bonding

檢視網路卡繫結結果:
[root@external-lb01 network-scripts]# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)

Bonding Mode: IEEE 802.3ad Dynamic link aggregation
Transmit Hash Policy: layer2 (0)
MII Status: up
........
........

[root@external-lb01 network-scripts]# cat /proc/net/bonding/bond1
Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)

Bonding Mode: IEEE 802.3ad Dynamic link aggregation
Transmit Hash Policy: layer2 (0)
MII Status: up
........
........

4)檢視路由
[root@external-lb01 ~]# route -n         //預設配置了外網閘道器地址
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
115.182.11.16   0.0.0.0         255.255.255.240 U     0      0        0 bond1
10.0.36.0       0.0.0.0         255.255.255.0   U     0      0        0 bond0
169.254.0.0     0.0.0.0         255.255.0.0     U     1006   0        0 bond0
169.254.0.0     0.0.0.0         255.255.0.0     U     1007   0        0 bond1
0.0.0.0         115.182.11.17   0.0.0.0         UG    0      0        0 bond1

[root@external-lb01 ~]# cat /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4

如上配置後,該機器可以ping通別的機器,及網路出去沒有問題,因為配置了外網地址。
但是別的內網機器ping不通該機,這就需要配置該機的內網閘道器。

5)設定內網閘道器
臨時設定內網閘道器(重啟網路卡服務後,這個臨時設定的閘道器地址就消失了)
[root@external-lb01 ~]# route add -net 10.0.0.0/8 gw 10.0.36.1

永久設定內網閘道器(這樣重啟網路卡服務後,這個閘道器地址還會在)
[root@external-lb01 ~]# vim /etc/sysconfig/network
GATEWAY=10.0.36.1

[root@external-lb01 ~]# vim /etc/sysconfig/static-routes
any net 10.0.0.0/8 gw 10.0.36.1

開啟IP轉發
[root@external-lb01 ~]# echo "1" >/proc/sys/net/ipv4/ip_forward    //臨時開啟
[root@external-lb01 ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1                 //永久開啟

重啟網路卡
[root@external-lb01 ~]# /etc/init.d/network restart

相關文章