CentOS7下配置ip forward的方法

2021-02-23 07:36:37 181 0
tangjin

这篇文章将为大家详细讲解有关CentOS7下配置ip forward的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

Linux 中ip forward功能是一个比较方便实用的功能,只需要简单配置,添加几条路由,即可实现ip转发。本文基于CentOS 7下采取一台机器充当虚拟路由,来实现Windows环境与Linux环境中相互通信。具体见下文,供大家参考。

一、环境及需求说明

CentOS7下配置ip forward的方法

本地测试环境,由于Win7+VMware这台机器网卡太新,无法被VMware EXSi识别,无奈之下就直接装了个Win7,然后再上面基于Vmware workstation 12开启了一些临时测试的虚拟机。其次本地直连网段192.168.1.0 IP有限,而又希望直接从本机(如图Win10) ssh连接到这些虚拟机,因此考虑使用Linux的ip转发功能来实现。

上述图片中已经列出了各个节点的IP信息,红色的虚拟机充当软路由,配置好之后,实现Win10 直接ssh到虚拟机。

二、虚拟路由服务器配置

###查看网卡信息
[root@centos7-router~]#ipaddr
1:lo:<LOOPBACK,UP,LOWER_UP>mtu65536qdiscnoqueuestateUNKNOWN
link/loopback00:00:00:00:00:00brd00:00:00:00:00:00
inet127.0.0.1/8scopehostlo
valid_lftforeverpreferred_lftforever
inet6::1/128scopehost
valid_lftforeverpreferred_lftforever
2:eno16777728:<BROADCAST,MULTICAST,UP,LOWER_UP>mtu1500qdiscpfifo_faststateUPqlen1000
link/ether00:0c:29:9c:eb:2dbrdff:ff:ff:ff:ff:ff
inet172.24.8.254/24brd172.24.8.255scopeglobaleno16777728
valid_lftforeverpreferred_lftforever
inet6fe80::20c:29ff:fe9c:eb2d/64scopelink
valid_lftforeverpreferred_lftforever
3:eno33554960:<BROADCAST,MULTICAST,UP,LOWER_UP>mtu1500qdiscpfifo_faststateUPqlen1000
link/ether00:0c:29:9c:eb:37brdff:ff:ff:ff:ff:ff
inet192.168.1.175/24brd192.168.1.255scopeglobaldynamiceno33554960
valid_lft82384secpreferred_lft82384sec
inet6fe80::20c:29ff:fe9c:eb37/64scopelink
valid_lftforeverpreferred_lftforever

###查看路由表
[root@centos7-router~]#route-n
KernelIProutingtable###缺省情况下,每块网卡都有一个基于本网段的路由记录
DestinationGatewayGenmaskFlagsMetricRefUseIface
0.0.0.0192.168.1.10.0.0.0UG10000eno33554960
172.24.8.00.0.0.0255.255.255.0U10000eno16777728
192.168.1.00.0.0.0255.255.255.0U10000eno33554960

###使用ip命令方式查看路由
[root@centos7-router~]#iproutelist
defaultvia192.168.1.1deveno33554960protostaticmetric100
172.24.8.0/24deveno16777728protokernelscopelinksrc172.24.8.254metric100
192.168.1.0/24deveno33554960protokernelscopelinksrc192.168.1.175metric100

###开启ipforward
[root@centos7-router~]#cp/usr/lib/sysctl.d/50-default.conf/usr/lib/sysctl.d/50-default.conf.bk
[root@centos7-router~]#echo"net.ipv4.ip_forward=1">>/usr/lib/sysctl.d/50-default.conf
[root@centos7-router~]#grepip_forward/usr/lib/sysctl.d/50-default.conf
net.ipv4.ip_forward=1
[root@centos7-router~]#sysctl-p###如不生效重启一下OS

三、CentOS7-a主机下添加路由

###查看当前主机ip信息
[root@centos7-a~]#ipaddrlist
1:lo:<LOOPBACK,UP,LOWER_UP>mtu65536qdiscnoqueuestateUNKNOWN
link/loopback00:00:00:00:00:00brd00:00:00:00:00:00
inet127.0.0.1/8scopehostlo
valid_lftforeverpreferred_lftforever
inet6::1/128scopehost
valid_lftforeverpreferred_lftforever
2:eno16777728:<BROADCAST,MULTICAST,UP,LOWER_UP>mtu1500qdiscpfifo_faststateUPqlen1000
link/ether00:0c:29:57:26:9dbrdff:ff:ff:ff:ff:ff
inet172.24.8.131/24brd172.24.8.255scopeglobaldynamiceno16777728
valid_lft1088secpreferred_lft1088sec
inet6fe80::20c:29ff:fe57:269d/64scopelink
valid_lftforeverpreferred_lftforever

###查看当前主机路由信息
[root@centos7-a~]#iproutelist
172.24.8.0/24deveno16777728protokernelscopelinksrc172.24.8.131metric100

[root@centos7-a~]##Author:Leshami
[root@centos7-a~]##Blog:http://blog.csdn.net/leshami
[root@centos7-a~]#iprouteadddefaultvia172.24.8.254
[root@centos7-a~]#iproutelist
defaultvia172.24.8.254deveno16777728
172.24.8.0/24deveno16777728protokernelscopelinksrc172.24.8.131metric100

###测试到虚拟路由器网关ip连通性
[root@centos7-a~]#ping172.24.8.254-c2
PING172.24.8.254(172.24.8.254)56(84)bytesofdata.
64bytesfrom172.24.8.254:icmp_seq=1ttl=64time=0.268ms
64bytesfrom172.24.8.254:icmp_seq=2ttl=64time=0.302ms

---172.24.8.254pingstatistics---
2packetstransmitted,2received,0%packetloss,time1000ms
rttmin/avg/max/mdev=0.268/0.285/0.302/0.017ms

###测试到虚拟路由器ip连通性
[root@centos7-a~]#ping192.168.1.175-c2
PING192.168.1.175(192.168.1.175)56(84)bytesofdata.
64bytesfrom192.168.1.175:icmp_seq=1ttl=64time=0.475ms
64bytesfrom192.168.1.175:icmp_seq=2ttl=64time=0.443ms

---192.168.1.175pingstatistics---
2packetstransmitted,2received,0%packetloss,time1000ms
rttmin/avg/max/mdev=0.443/0.459/0.475/0.016ms

四、Windows 10下配置路由

###当前环境
C:\>systeminfo|findstrBuild
OSVersion:10.0.14393N/ABuild14393
OSBuildType:MultiprocessorFree

###查看当前的路由信息
C:\>routeprint-4
===========================================================================
InterfaceList
7...54ab3axxb8ea......Intel(R)EthernetConnectionI219-V
21...ac2b6e5f3b57......MicrosoftWi-FiDirectVirtualAdapter
......
===========================================================================
IPv4RouteTable
===========================================================================
ActiveRoutes:
NetworkDestinationNetmaskGatewayInterfaceMetric
0.0.0.00.0.0.0192.168.1.1192.168.1.24235
127.0.0.0255.0.0.0On-link127.0.0.1331
127.0.0.1255.255.255.255On-link127.0.0.1331
127.255.255.255255.255.255.255On-link127.0.0.1331
172.24.10.0255.255.255.0On-link172.24.10.1291
172.24.10.1255.255.255.255On-link172.24.10.1291
172.24.10.255255.255.255.255On-link172.24.10.1291
192.168.1.0255.255.255.0On-link192.168.1.242291
192.168.1.242255.255.255.255On-link192.168.1.242291
192.168.1.255255.255.255.255On-link192.168.1.242291
192.168.16.0255.255.255.0On-link192.168.16.1291
192.168.16.1255.255.255.255On-link192.168.16.1291
192.168.16.255255.255.255.255On-link192.168.16.1291
224.0.0.0240.0.0.0On-link127.0.0.1331
224.0.0.0240.0.0.0On-link192.168.1.242291
224.0.0.0240.0.0.0On-link192.168.16.1291
224.0.0.0240.0.0.0On-link172.24.10.1291
255.255.255.255255.255.255.255On-link127.0.0.1331
255.255.255.255255.255.255.255On-link192.168.1.242291
255.255.255.255255.255.255.255On-link192.168.16.1291
255.255.255.255255.255.255.255On-link172.24.10.1291
===========================================================================
PersistentRoutes:
None

###添加持久化路由
C:\>routeadd172.24.8.0mask255.255.255.0192.168.1.175-p
OK!

C:\>routeprint-4

IPv4RouteTable
===========================================================================
ActiveRoutes:
NetworkDestinationNetmaskGatewayInterfaceMetric
0.0.0.00.0.0.0192.168.1.1192.168.1.24235
127.0.0.0255.0.0.0On-link127.0.0.1331
127.0.0.1255.255.255.255On-link127.0.0.1331
127.255.255.255255.255.255.255On-link127.0.0.1331
172.24.8.0255.255.255.0192.168.1.175192.168.1.24236
172.24.10.0255.255.255.0On-link172.24.10.1291
172.24.10.1255.255.255.255On-link172.24.10.1291
172.24.10.255255.255.255.255On-link172.24.10.1291
192.168.1.0255.255.255.0On-link192.168.1.242291
192.168.1.242255.255.255.255On-link192.168.1.242291
192.168.1.255255.255.255.255On-link192.168.1.242291
192.168.16.0255.255.255.0On-link192.168.16.1291
192.168.16.1255.255.255.255On-link192.168.16.1291
192.168.16.255255.255.255.255On-link192.168.16.1291
224.0.0.0240.0.0.0On-link127.0.0.1331
224.0.0.0240.0.0.0On-link192.168.1.242291
224.0.0.0240.0.0.0On-link192.168.16.1291
224.0.0.0240.0.0.0On-link172.24.10.1291
255.255.255.255255.255.255.255On-link127.0.0.1331
255.255.255.255255.255.255.255On-link192.168.1.242291
255.255.255.255255.255.255.255On-link192.168.16.1291
255.255.255.255255.255.255.255On-link172.24.10.1291
===========================================================================
PersistentRoutes:###以下为添加的持久化路由记录
NetworkAddressNetmaskGatewayAddressMetric
172.24.8.0255.255.255.0192.168.1.1751
===========================================================================

五、验证两边连通性

###从Linux端pingwindows10
[root@centos7-a~]#ping192.168.1.242-c2
PING192.168.1.242(192.168.1.242)56(84)bytesofdata.
64bytesfrom192.168.1.242:icmp_seq=1ttl=63time=1.70ms
64bytesfrom192.168.1.242:icmp_seq=2ttl=63time=1.10ms

---192.168.1.242pingstatistics---
2packetstransmitted,2received,0%packetloss,time1002ms
rttmin/avg/max/mdev=1.101/1.404/1.707/0.303ms

###从windows10端pingLinux
C:\>ping172.24.8.131-t

Pinging172.24.8.131with32bytesofdata:
Replyfrom172.24.8.131:bytes=32time=1msTTL=63
Replyfrom172.24.8.131:bytes=32time=1msTTL=63

注,如果测试有问题,请考虑是否被防火墙拦住。

关于“CentOS7下配置ip forward的方法”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

收藏
分享
海报
0 条评论
181
上一篇:怎么搭建docker内网私服 下一篇:在Centos上怎么搭建Maven中央仓库

本站已关闭游客评论,请登录或者注册后再评论吧~

忘记密码?

图形验证码