企业内部不同网段的服务器之间互通-配置示例

来源:互联网 发布:sql select 字段类型 编辑:程序博客网 时间:2024/05/17 02:26

路由部署项目拓扑

这里写图片描述

上图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 ~]# hostnamelocalhost.localdomain[root@localhost ~]# hostname server01[root@localhost ~]# hostnameserver01[root@localhost ~]# grep -i 'hos' /etc/sysconfig/networkHOSTNAME=localhost.localdomain[root@localhost ~]# sed -i 's#localhost.localdomain#server01#g' /etc/sysconfig/network[root@localhost ~]# grep -i 'hos' /etc/sysconfig/networkHOSTNAME=server01###修改hosts文件[root@localhost ~]# echo "10.0.0.10 server01" >>/etc/hosts[root@localhost ~]# cat /etc/hosts127.0.0.1   localhost.localdomain localhost::1     localhost6.localdomain6 localhost610.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            ###ipGATEWAY=10.0.0.254          ###网关TYPE=EthernetDNS1=223.5.5.5              ###DNSDNS2=223.6.6.6###重启网卡让配置生效ifdown eth0 && ifup eth0###查看网卡信息[root@server01 ~]# ip a s eth02: 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/networkHOSTNAME=server03###修改hosts文件[root@localhost ~]# echo "10.0.1.10 server03" >>/etc/hosts###配置网卡相关信息(主要就是网关和ip)IPADDR=10.0.1.10            ###ipGATEWAY=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.11GATEWAY=10.0.0.254###修改网卡2信息(网卡默认一块,需要setup添加网卡并配置)IPADDR=10.0.1.11GATEWAY=10.0.1.254###重启网卡让配置生效/etc/sysconfig/network restart  ###重启所有网卡###查看网卡网卡等情况[root@server02 ~]# ip a s2: 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 forever3: 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 -nKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth00.0.0.0         10.0.0.254      0.0.0.0         UG    0      0        0 eth0###查看server02路由表信息[root@server02 ~]# route -nKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth010.0.1.0        0.0.0.0         255.255.255.0   U     0      0        0 eth1169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth10.0.0.0         10.0.1.254      0.0.0.0         UG    0      0        0 eth1###查看server03路由表信息[root@server01 ~]# route -nKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface10.0.1.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth00.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 -nKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth010.0.1.0        10.0.0.11       255.255.255.0   UG    0      0        0 eth0169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth00.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 -nKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface10.0.1.0        0.0.0.0         255.255.255.0   U     0      0        0 eth010.0.0.0        10.0.1.11       255.255.255.0   UG    0      0        0 eth0169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth00.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.10traceroute 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.10traceroute 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-eth010.0.1.0/24 via 10.0.0.11###server03添加#vi /etc/sysconfig/network-scripts/route-eth010.0.0.0/24 via 10.0.1.11
阅读全文
1 0
原创粉丝点击