路由部署项目拓扑
上图server01 访问server03是可以通过路由器,访问外网实现的,但是我们需要的是不通网段直接之间进行内网互通。
原理
软路由是指利用台式机或服务器配合软件形成路由解决方案,主要靠软件的设置,达成路由器的功能。软路由通常使用普通计算机充当,使用通用的操作系统,如Linux或Windows,因此路由设置事实上是Windows或 Linux的设置,或者是对计算机的配置,现在以Linux主机为例实现路由功能。
拓扑目标
服务器01的10.0.0.10主机IP地址可以正常访问服务器03的10.0.1.10主机IP地址。
网络环境分析
要想server01能访问到server03,他们直接的server02就可以作为软路由来使用。所以server02配置为双网卡,并开启自身的路由功能(数据包转发功能)。
IP配置目标
虚拟服务器 | 网卡配置信息 | 虚拟网卡名称 | 虚拟网卡模式 |
---|---|---|---|
server01 | eth0:10.0.0.10/24 | vmnet8 | nat模式 |
server02-网卡1 | eth0:10.0.0.11/24 | vmnet8 | nat模式 |
server02-网卡2 | eth1:10.0.1.11/24 | vmnet1 | 仅主机模式 |
server03 | eth0:10.0.1.10/24 | vmnet1 | 仅主机模式 |
server01相关配置:
###修改主机名并设置永久生效
[root@localhost ~]# hostname
localhost.localdomain
[root@localhost ~]# hostname server01
[root@localhost ~]# hostname
server01
[root@localhost ~]# grep -i 'hos' /etc/sysconfig/network
HOSTNAME=localhost.localdomain
[root@localhost ~]# sed -i 's#localhost.localdomain#server01#g' /etc/sysconfig/network
[root@localhost ~]# grep -i 'hos' /etc/sysconfig/network
HOSTNAME=server01
###修改hosts文件
[root@localhost ~]# echo "10.0.0.10 server01" >>/etc/hosts
[root@localhost ~]# cat /etc/hosts
127.0.0.1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
10.0.0.10 server01
###配置网卡相关信息
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth0 ###网卡名
BOOTPROTO=none ###不固定ip的时候写DHCP就可以
ONBOOT=yes ###开机自启动
NETMASK=255.255.255.0 ###子网掩码
IPADDR=10.0.0.10 ###ip
GATEWAY=10.0.0.254 ###网关
TYPE=Ethernet
DNS1=223.5.5.5 ###DNS
DNS2=223.6.6.6
###重启网卡让配置生效
ifdown eth0 && ifup eth0
###查看网卡信息
[root@server01 ~]# ip a s eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:cd:5b:95 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.10/24 brd 10.0.0.255 scope global eth0
inet6 fe80::20c:29ff:fecd:5b95/64 scope link
valid_lft forever preferred_lft forever
server03相关配置:
###修改主机名并设置永久生效
[root@localhost ~]# sed -i 's#localhost.localdomain#server03#g' /etc/sysconfig/network
[root@localhost ~]# grep -i 'hos' /etc/sysconfig/network
HOSTNAME=server03
###修改hosts文件
[root@localhost ~]# echo "10.0.1.10 server03" >>/etc/hosts
###配置网卡相关信息(主要就是网关和ip)
IPADDR=10.0.1.10 ###ip
GATEWAY=10.0.1.254 ###网关
###重启网卡让配置生效
ifdown eth0 && ifup eth0
server02相关配置:
###修改主机名并设置永久生效
[root@localhost ~]# sed -i 's#localhost.localdomain#server02#g' /etc/sysconfig/network
[root@localhost ~]# hostname server02
###修改hosts文件
[root@localhost ~]# echo "10.0.1.11 server02" >>/etc/hosts
[root@localhost ~]# echo "10.0.0.11 server02" >>/etc/hosts
###修改网卡1信息
IPADDR=10.0.0.11
GATEWAY=10.0.0.254
###修改网卡2信息(网卡默认一块,需要setup添加网卡并配置)
IPADDR=10.0.1.11
GATEWAY=10.0.1.254
###重启网卡让配置生效
/etc/sysconfig/network restart ###重启所有网卡
###查看网卡网卡等情况
[root@server02 ~]# ip a s
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:49:75:a9 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.11/24 brd 10.0.0.255 scope global eth0
inet6 fe80::20c:29ff:fe49:75a9/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:49:75:b3 brd ff:ff:ff:ff:ff:ff
inet 10.0.1.11/24 brd 10.0.1.255 scope global eth1
inet6 fe80::20c:29ff:fe49:75b3/64 scope link
valid_lft forever preferred_lft forever
转发分析
- server01默认网关10.0.0.254,所以所有ip访问都走的是它,我们现在要访问server03,所以要设置访问server03,通过server02的数据转发功能。
- server03默认网关10.0.1.254,所以所有ip访问都走的是它,我们现在要访问server01,所以要设置访问server01,通过server02的数据转发功能。
- server02就需要开启数据转发功能满足server01,server03的转发需求。
查看三台服务器现有的路由配置,并添加新路由协议
###查看server01路由表信息
[root@server01 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
0.0.0.0 10.0.0.254 0.0.0.0 UG 0 0 0 eth0
###查看server02路由表信息
[root@server02 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
10.0.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth1
0.0.0.0 10.0.1.254 0.0.0.0 UG 0 0 0 eth1
###查看server03路由表信息
[root@server01 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
0.0.0.0 10.0.1.254 0.0.0.0 UG 0 0 0 eth0
###添加对应的路由
###server01添加
[root@server01 ~]# route add -net 10.0.1.0/24 gw 10.0.0.11
[root@server01 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
10.0.1.0 10.0.0.11 255.255.255.0 UG 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
0.0.0.0 10.0.0.254 0.0.0.0 UG 0 0 0 eth0
###server03添加
[root@server01 ~]# route add -net 10.0.0.0/24 gw 10.0.1.11
[root@server01 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
10.0.0.0 10.0.1.11 255.255.255.0 UG 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
0.0.0.0 10.0.0.254 0.0.0.0 UG 0 0 0 eth0
###server02主机开启路由转发
[root@server02 ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
[root@server02 ~]# sysctl -p ###重新加载内核文件就有转发功能
net.ipv4.ip_forward = 1
...其余显示部分省略...
说明:默认数值为0,将数值改为1,路由转发功能即表示开启
测试结果
###server01上面访问server03
[root@server01 ~]# traceroute 10.0.1.10
traceroute to 10.0.1.10 (10.0.1.10), 30 hops max, 40 byte packets
1 10.0.0.11 (10.0.0.11) 0.743 ms 0.842 ms 0.818 ms
2 10.0.1.10 (10.0.1.10) 1.082 ms 1.090 ms 1.066 ms
###server03上面访问server01
[root@server03 ~]# traceroute 10.0.0.10
traceroute to 10.0.0.10 (10.0.0.10), 30 hops max, 40 byte packets
1 (10.0.1.11) 0.157 ms 0.140 ms 0.141 ms
2 (10.0.0.10) 0.737 ms 0.769 ms 0.888 ms
设置路由永久生效(命令行方式重启网卡就失效)
###添加/etc/sysconfig/network-scripts/route-eth0 ###这个文件默认没有
###server01添加
#vi /etc/sysconfig/network-scripts/route-eth0
10.0.1.0/24 via 10.0.0.11
###server03添加
#vi /etc/sysconfig/network-scripts/route-eth0
10.0.0.0/24 via 10.0.1.11
版权声明:本文为jiedao_liyk原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。