天天看點

Centos網絡管理(四)-路由轉發與靜态路由

實驗環境:VMware Workstation Pro 14(試用版)

系統平台:

CentOS release 6.9 (Final)       核心  2.6.32-696.el6.x86_64

CentOS Linux release 7.4.1708 (Core)  核心  3.10.0-693.el7.x86_64

機器

簡稱

角色

IP位址

6-2-A

A

模拟電腦A

eth1 192.168.27.210/24

7-2-R1

R1

路由器R1

ens33 192.168.27.200/24

ens36 10.0.0.200/8

6-3-R2

R2

路由器R2

eth0 10.0.0.201/8

eth1 172.18.0.200/16

7-3-R3

R3

路由器R3

ens33 172.18.0.201/16

ens36 188.168.0.200/16

7-4-B

B

模拟電腦B

ens36 188.168.0.201/16

網絡拓撲圖

虛拟機網絡設定

配置A的ip位址

#cat > /etc/sysconfig/network-scripts/ifcfg-eth1 <<EOF

DEVICE=eth1

BOOTPROTO=static

ONBOOT=yes

IPADDR=192.168.27.210

PREFIX=24

EOF

生成配置網卡配置檔案後,需要重新啟動網絡服務。

#service network restart

驗證IP

配置R1的ip位址

ens33

#nmcli connection add con-name ens33 ifname ens33 type ethernet ipv6.method ignore ipv4.never-default yes ipv4.ignore-auto-dns yes ipv4.method manual ipv4.addresses 192.168.27.200/24

ens36

#nmcli connection add con-name ens36 ifname ens36 type ethernet ipv6.method ignore ipv4.never-default yes ipv4.ignore-auto-dns yes ipv4.method manual ipv4.addresses 10.0.0.200/8

測試

由于A的eth1接口與R1的ens33接口是同一個網絡,是以直接可以測試了

R1 > A

#ping 192.168.27.210 -c 2 -s 192.168.27.200

PING 192.168.27.210 (192.168.27.210) 56(84) bytes of data.

64 bytes from 192.168.27.210: icmp_seq=1 ttl=64 time=0.408 ms

64 bytes from 192.168.27.210: icmp_seq=2 ttl=64 time=0.308 ms

A > R1

#ping 192.168.27.200 -c 2

PING 192.168.27.200 (192.168.27.200) 56(84) bytes of data.

64 bytes from 192.168.27.200: icmp_seq=1 ttl=64 time=0.927 ms

64 bytes from 192.168.27.200: icmp_seq=2 ttl=64 time=0.323 ms

配置R2的ip位址

eth0

#cat > /etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF

DEVICE=eth0

IPADDR=10.0.0.201

PREFIX=8

#ifup eth0

eth1

BOOTPROTO=statice

IPADDR=172.18.0.200

PREFIX=16

#ifup eth1

由于R2的eth0接口與R1的ens36接口是同一個網絡,是以直接可以測試了

R1 > R2

#ping 10.0.0.201 -c 2 -s 10.0.0.200

PING 10.0.0.201 (10.0.0.201) 10(38) bytes of data.

18 bytes from 10.0.0.201: icmp_seq=1 ttl=64

18 bytes from 10.0.0.201: icmp_seq=2 ttl=64

R2 > R1

#ping 10.0.0.200 -c 2 -s 10.0.0.201

PING 10.0.0.200 (10.0.0.200) 10(38) bytes of data.

18 bytes from 10.0.0.200: icmp_seq=1 ttl=64

18 bytes from 10.0.0.200: icmp_seq=2 ttl=64

配置R3的ip位址

#nmcli connection add con-name ens33 ifname ens33 type ethernet ipv6.method ignore ipv4.never-default yes ipv4.ignore-auto-dns yes ipv4.method manual ipv4.addresses 172.18.0.201/16

#nmcli connection add con-name ens36 ifname ens36 type ethernet ipv6.method ignore ipv4.never-default yes ipv4.ignore-auto-dns yes ipv4.method manual ipv4.addresses 188.168.0.200/16

由于R3的ens33接口與R2的eth1接口是同一個網絡,是以直接可以測試了

R3 > R2

#ping 172.18.0.200 -c 2 -s 172.18.0.201

PING 172.18.0.200 (172.18.0.200) 172(200) bytes of data.

180 bytes from 172.18.0.200: icmp_seq=1 ttl=64 time=0.892 ms

180 bytes from 172.18.0.200: icmp_seq=2 ttl=64 time=0.415 ms

R2 > R3

#ping 172.18.0.201 -c 2 -s 172.18.0.200

PING 172.18.0.201 (172.18.0.201) 172(200) bytes of data.

180 bytes from 172.18.0.201: icmp_seq=1 ttl=64 time=0.915 ms

180 bytes from 172.18.0.201: icmp_seq=2 ttl=64 time=0.360 ms

配置B的ip位址

#nmcli connection add con-name ens36 ifname ens36 type ethernet ipv6.method ignore ipv4.never-default yes ipv4.ignore-auto-dns yes ipv4.method manual ipv4.addresses 188.168.0.210/16

由于B的ens36接口與R3的ens36接口是同一個網絡,是以直接可以測試了

B > R3

#ping 188.168.0.200 -c 2

PING 188.168.0.200 (188.168.0.200) 56(84) bytes of data.

64 bytes from 188.168.0.200: icmp_seq=1 ttl=64 time=1.08 ms

64 bytes from 188.168.0.200: icmp_seq=2 ttl=64 time=0.335 ms

實驗目标

A 與 B 可以互相通訊

經過上面對5台Centos 主機的IP設定後,還需要做以下設定:

1. 所有的主機關閉selinux

#sed -i.bak 's@SELINUX=enforcing@SELINUX=disabled@g' /etc/selinux/config;setenforce 0;getenforce

顯示如下為成功

Permissive 或 Disabled

2. 所有的主機的iptables

Centos 6

#service iptables stop;chkconfig iptables off;service iptables status

iptables: Firewall is not running.

Centos 7

#systemctl stop firewalld.service ;systemctl disable firewalld.service |systemctl status firewalld.service|grep Active

Active: inactive (dead)

3. R1,R2,R3開啟路由轉發功能

#sysctl -w net.ipv4.ip_forward=1;echo 'net.ipv4.ip_forward = 1' > /etc/sysctl.d/ipv4.conf

顯示1為成功

此時從A ping B

#ping 188.168.0.201

connect: Network is unreachable

此時從B ping A

#ping 192.168.27.210

配置A靜态路由

#route -n

Kernel IP routing table 這是原來的路由

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

192.168.27.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1

先臨時指定測試,正常了再寫入檔案,以便重新開機後生效

#ip route add default via 192.168.27.200

#cat > /etc/sysconfig/network-scripts/route-eth1 <<EOF

ADDRESS0=0.0.0.0

NETMASK0=0.0.0.0

GATEWAY0=192.168.27.200

Kernel IP routing table 添加了一條預設路由

0.0.0.0         192.168.27.200  0.0.0.0         UG    0      0        0 eth1

配置R1靜态路由

由于R1屬于邊緣路由器,是以,設定一條預設路由,從ens36出,到R2的eth0的10.0.0.201

10.0.0.0        0.0.0.0         255.0.0.0       U     100    0        0 ens36

192.168.27.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33

#ip route add default via 10.0.0.201

#cat > /etc/sysconfig/network-scripts/route-ens36 <<EOF

GATEWAY0=10.0.0.201

Kernel IP routing table  添加了一條預設路由

0.0.0.0         10.0.0.201      0.0.0.0         UG    0      0        0 ens36

10.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 ens36

配置R2靜态路由

要配置2條路由

一條路由去往VMnet2的網絡192.168.27.0/24,從R2的eth0去往R1的ens36的10.0.0.200

一條路由去往VMnet5的網絡188.168.0.0/16,從R2的eth1去往R3的ens33的172.18.0.201

172.18.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth1

10.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 eth0

#ip route add 192.168.27.0/24 via 10.0.0.200

#cat > /etc/sysconfig/network-scripts/route-eth0 <<EOF

ADDRESS0=192.168.27.0

NETMASK0=255.255.255.0

GATEWAY0=10.0.0.200

#ip route add 188.168.0.0/16 via 172.18.0.201

ADDRESS0=188.168.0.0

NETMASK0=255.255.0.0

GATEWAY0=172.18.0.201

Kernel IP routing table  添加了2條路由

192.168.27.0    10.0.0.200      255.255.255.0   UG    0      0        0 eth0

188.168.0.0     172.18.0.201    255.255.0.0     UG    0      0        0 eth1

配置R3靜态路由

由于R3屬于邊緣路由器,是以,設定一條預設路由,從ens33出,到R2的eth1的172.18.0.200

172.18.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens33

188.168.0.0     0.0.0.0         255.255.0.0     U     100    0        0 ens36

#ip route add default via 172.18.0.200

#cat > /etc/sysconfig/network-scripts/route-ens33 <<EOF

GATEWAY0=172.18.0.200

0.0.0.0         172.18.0.200    0.0.0.0         UG    0      0        0 ens33

172.18.0.0      0.0.0.0         255.255.0.0     U     0      0        0 ens33

配置B靜态路由

Kernel IP routing table 這是現在的路由

#ip route add default via 188.168.0.200

GATEWAY0=188.168.0.200

B > A

#ping 192.168.27.210 -c 2

64 bytes from 192.168.27.210: icmp_seq=1 ttl=61 time=2.26 ms

64 bytes from 192.168.27.210: icmp_seq=2 ttl=61 time=1.08 ms

A > B

[root@centos6 ~]#ping 188.168.0.210 -c 2

PING 188.168.0.210 (188.168.0.210) 56(84) bytes of data.

64 bytes from 188.168.0.210: icmp_seq=1 ttl=61 time=2.06 ms

64 bytes from 188.168.0.210: icmp_seq=2 ttl=61 time=1.23 ms

每經過一路由器,ttl值-1,64-3=61

路由跟蹤

#traceroute -n 188.168.0.210

traceroute to 188.168.0.210 (188.168.0.210), 30 hops max, 60 byte packets

1  192.168.27.200  1.937 ms  1.194 ms  0.981 ms

2  10.0.0.201  8.910 ms  8.417 ms  8.276 ms

3  172.18.0.201  8.216 ms  8.211 ms  8.159 ms

4  188.168.0.210  8.082 ms  8.027 ms  7.988 ms

#mtr -rnc 2 192.168.27.210

Start: Wed Dec 20 13:29:12 2017

HOST: centos7.hunk.teh            Loss%   Snt   Last   Avg  Best  Wrst StDev

1.|-- 188.168.0.200              0.0%     2    0.3   0.3   0.3   0.3   0.0

2.|-- 172.18.0.200               0.0%     2    0.5   0.5   0.5   0.6   0.0

3.|-- 10.0.0.200                 0.0%     2    0.7   0.7   0.7   0.8   0.0

4.|-- 192.168.27.210             0.0%     2    1.6   1.3   1.0   1.6   0.0

實驗完成,總結下:

還有一點就是,假如你用nmcli connection down 和 up 網卡,就不要同時混用fdown 和 ifup。貌似會

讓網卡配置失效。

多網卡配置時,配置檔案中的DEFROUTE=no,不是此值的時候,配置預設路由時不會生效,同時,

ip route add default via 188.168.0.200 指令也不會報錯。

本文轉自 ljpwinxp 51CTO部落格,原文連結:http://blog.51cto.com/191226139/2052490

繼續閱讀