CentOS7下如何配置ip forward(虚拟路由器)

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

一、环境及需求说明

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

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

二、虚拟路由服务器配置

###查看网卡信息
[root@centos7-router ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
  link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  inet 127.0.0.1/8 scope host lo
   valid_lft forever preferred_lft forever
  inet6 ::1/128 scope host
   valid_lft forever preferred_lft forever
2: eno16777728: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  link/ether 00:0c:29:9c:eb:2d brd ff:ff:ff:ff:ff:ff
  inet 172.24.8.254/24 brd 172.24.8.255 scope global eno16777728
   valid_lft forever preferred_lft forever
  inet6 fe80::20c:29ff:fe9c:eb2d/64 scope link
   valid_lft forever preferred_lft forever
3: eno33554960: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  link/ether 00:0c:29:9c:eb:37 brd ff:ff:ff:ff:ff:ff
  inet 192.168.1.175/24 brd 192.168.1.255 scope global dynamic eno33554960
   valid_lft 82384sec preferred_lft 82384sec
  inet6 fe80::20c:29ff:fe9c:eb37/64 scope link
   valid_lft forever preferred_lft forever

###查看路由表
[root@centos7-router ~]# route -n
Kernel IP routing table ###缺省情况下,每块网卡都有一个基于本网段的路由记录
Destination  Gateway    Genmask    Flags Metric Ref  Use Iface
0.0.0.0    192.168.1.1  0.0.0.0    UG  100  0    0 eno33554960
172.24.8.0   0.0.0.0    255.255.255.0 U  100  0    0 eno16777728
192.168.1.0  0.0.0.0    255.255.255.0 U  100  0    0 eno33554960

###使用ip命令方式查看路由
[root@centos7-router ~]# ip route list
default via 192.168.1.1 dev eno33554960 proto static metric 100
172.24.8.0/24 dev eno16777728 proto kernel scope link src 172.24.8.254 metric 100
192.168.1.0/24 dev eno33554960 proto kernel scope link src 192.168.1.175 metric 100 

###开启ip forward
[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 ~]# grep ip_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 ~]# ip addr list
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
  link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  inet 127.0.0.1/8 scope host lo
   valid_lft forever preferred_lft forever
  inet6 ::1/128 scope host
   valid_lft forever preferred_lft forever
2: eno16777728: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  link/ether 00:0c:29:57:26:9d brd ff:ff:ff:ff:ff:ff
  inet 172.24.8.131/24 brd 172.24.8.255 scope global dynamic eno16777728
   valid_lft 1088sec preferred_lft 1088sec
  inet6 fe80::20c:29ff:fe57:269d/64 scope link
   valid_lft forever preferred_lft forever

###查看当前主机路由信息
[root@centos7-a ~]# ip route list
172.24.8.0/24 dev eno16777728 proto kernel scope link src 172.24.8.131 metric 100 

[root@centos7-a ~]# # Author : Leshami
[root@centos7-a ~]# # Blog : http://blog.csdn.net/leshami
[root@centos7-a ~]# ip route add default via 172.24.8.254
[root@centos7-a ~]# ip route list
default via 172.24.8.254 dev eno16777728
172.24.8.0/24 dev eno16777728 proto kernel scope link src 172.24.8.131 metric 100 

###测试到虚拟路由器网关ip连通性
[root@centos7-a ~]# ping 172.24.8.254 -c 2
PING 172.24.8.254 (172.24.8.254) 56(84) bytes of data.
64 bytes from 172.24.8.254: icmp_seq=1 ttl=64 time=0.268 ms
64 bytes from 172.24.8.254: icmp_seq=2 ttl=64 time=0.302 ms

--- 172.24.8.254 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.268/0.285/0.302/0.017 ms

###测试到虚拟路由器ip连通性
[root@centos7-a ~]# ping 192.168.1.175 -c 2
PING 192.168.1.175 (192.168.1.175) 56(84) bytes of data.
64 bytes from 192.168.1.175: icmp_seq=1 ttl=64 time=0.475 ms
64 bytes from 192.168.1.175: icmp_seq=2 ttl=64 time=0.443 ms

--- 192.168.1.175 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.443/0.459/0.475/0.016 ms

四、Windows 10下配置路由

###当前环境
C:\>systeminfo | findstr Build
OS Version:        10.0.14393 N/A Build 14393
OS Build Type:      Multiprocessor Free

###查看当前的路由信息
C:\>route print -4
===========================================================================
Interface List
 7...54 ab 3a xx b8 ea ......Intel(R) Ethernet Connection I219-V
21...ac 2b 6e 5f 3b 57 ......Microsoft Wi-Fi Direct Virtual Adapter
            ......
===========================================================================
IPv4 Route Table
===========================================================================
Active Routes:
Network Destination    Netmask     Gateway   Interface Metric
     0.0.0.0     0.0.0.0   192.168.1.1  192.168.1.242  35
    127.0.0.0    255.0.0.0    On-link    127.0.0.1  331
    127.0.0.1 255.255.255.255    On-link    127.0.0.1  331
 127.255.255.255 255.255.255.255    On-link    127.0.0.1  331
   172.24.10.0  255.255.255.0    On-link   172.24.10.1  291
   172.24.10.1 255.255.255.255    On-link   172.24.10.1  291
  172.24.10.255 255.255.255.255    On-link   172.24.10.1  291
   192.168.1.0  255.255.255.0    On-link  192.168.1.242  291
  192.168.1.242 255.255.255.255    On-link  192.168.1.242  291
  192.168.1.255 255.255.255.255    On-link  192.168.1.242  291
  192.168.16.0  255.255.255.0    On-link   192.168.16.1  291
  192.168.16.1 255.255.255.255    On-link   192.168.16.1  291
 192.168.16.255 255.255.255.255    On-link   192.168.16.1  291
    224.0.0.0    240.0.0.0    On-link    127.0.0.1  331
    224.0.0.0    240.0.0.0    On-link  192.168.1.242  291
    224.0.0.0    240.0.0.0    On-link   192.168.16.1  291
    224.0.0.0    240.0.0.0    On-link   172.24.10.1  291
 255.255.255.255 255.255.255.255    On-link    127.0.0.1  331
 255.255.255.255 255.255.255.255    On-link  192.168.1.242  291
 255.255.255.255 255.255.255.255    On-link   192.168.16.1  291
 255.255.255.255 255.255.255.255    On-link   172.24.10.1  291
===========================================================================
Persistent Routes:
 None

###添加持久化路由
C:\>route add 172.24.8.0 mask 255.255.255.0 192.168.1.175 -p
OK!

C:\>route print -4

IPv4 Route Table
===========================================================================
Active Routes:
Network Destination    Netmask     Gateway   Interface Metric
     0.0.0.0     0.0.0.0   192.168.1.1  192.168.1.242  35
    127.0.0.0    255.0.0.0    On-link    127.0.0.1  331
    127.0.0.1 255.255.255.255    On-link    127.0.0.1  331
 127.255.255.255 255.255.255.255    On-link    127.0.0.1  331
   172.24.8.0  255.255.255.0  192.168.1.175  192.168.1.242  36
   172.24.10.0  255.255.255.0    On-link   172.24.10.1  291
   172.24.10.1 255.255.255.255    On-link   172.24.10.1  291
  172.24.10.255 255.255.255.255    On-link   172.24.10.1  291
   192.168.1.0  255.255.255.0    On-link  192.168.1.242  291
  192.168.1.242 255.255.255.255    On-link  192.168.1.242  291
  192.168.1.255 255.255.255.255    On-link  192.168.1.242  291
  192.168.16.0  255.255.255.0    On-link   192.168.16.1  291
  192.168.16.1 255.255.255.255    On-link   192.168.16.1  291
 192.168.16.255 255.255.255.255    On-link   192.168.16.1  291
    224.0.0.0    240.0.0.0    On-link    127.0.0.1  331
    224.0.0.0    240.0.0.0    On-link  192.168.1.242  291
    224.0.0.0    240.0.0.0    On-link   192.168.16.1  291
    224.0.0.0    240.0.0.0    On-link   172.24.10.1  291
 255.255.255.255 255.255.255.255    On-link    127.0.0.1  331
 255.255.255.255 255.255.255.255    On-link  192.168.1.242  291
 255.255.255.255 255.255.255.255    On-link   192.168.16.1  291
 255.255.255.255 255.255.255.255    On-link   172.24.10.1  291
===========================================================================
Persistent Routes: ###以下为添加的持久化路由记录
 Network Address     Netmask Gateway Address Metric
   172.24.8.0  255.255.255.0  192.168.1.175   1
===========================================================================

五、验证两边连通性

###从Linux端ping windows 10
[root@centos7-a ~]# ping 192.168.1.242 -c 2
PING 192.168.1.242 (192.168.1.242) 56(84) bytes of data.
64 bytes from 192.168.1.242: icmp_seq=1 ttl=63 time=1.70 ms
64 bytes from 192.168.1.242: icmp_seq=2 ttl=63 time=1.10 ms

--- 192.168.1.242 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 1.101/1.404/1.707/0.303 ms

###从windows 10端ping Linux
C:\>ping 172.24.8.131 -t

Pinging 172.24.8.131 with 32 bytes of data:
Reply from 172.24.8.131: bytes=32 time=1ms TTL=63
Reply from 172.24.8.131: bytes=32 time=1ms TTL=63

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • centos 下面安装python2.7 +pip +mysqld

    python2.7 复制代码 代码如下: wget http://www.python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz yum install xz-libs xz -d Python-2.7.6.tar.xz ls tar -xvf Python-2.7.6.tar cd Python-2.7.6 yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-deve

  • linux: centos设置ip以及连接外网的方法

    注明:我使用的的使centos 7,所有文件名是ifcfg-enp0s3, 一. 设置虚拟机中linux的ip,使本地能连通虚拟机的linux系统 1>.进入本地windows的cmd,输入ipconfig 2>.根据上面的信息,设置虚拟机中的linux的ip必须要与本机ip在同一网段,这样才能在本地使用xshell等连接工具连接登录虚拟机的linux系统 以下是linux中/etc/sysconfig/network-scripts/ifcfg-enp0s3档案的信息(设置如下) TYPE=

  • Centos 7开启网卡自动获取IP的详细方法

    Centos7默认安装是没有开启配置网卡信息的,无法访问外网. 下边配置打开网络自动获取Ip地址 1.查看网卡信息 执行Ifconfig eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet6 fe80::20c:29ff:fee8:8637 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:e8:86:37 txqueuelen 1000 (Ether

  • CentOS7安装iptables防火墙的方法

    CentOS7默认的防火墙不是iptables,而是firewalle. 安装iptable iptable-service #先检查是否安装了iptables service iptables status #安装iptables yum install -y iptables #升级iptables yum update iptables #安装iptables-services yum install iptables-services 禁用/停止自带的firewalld服务 #停止fir

  • centos6.7 安装python2.7、pip2.7、easy_install-2.7的方法

    centos6自带python2.6版本,根据需要,安装python2.7.easy_install-2.7.pip2.7 依赖 yum groupinstall -y Development tools yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

  • 详解如何设置CentOS 7开机自动获取IP地址

    本例中以CentOS 7举例说明如何设置Linux开机自动获取IP地址和设置固定IP地址. 自动获取动态IP地址 1.输入"ip addr"并按回车键确定,发现无法获取IP(CentOS 7默认没有ifconfig命令),记录下网卡名称(本例中为ens33,下图黄色框内). 2.输入"cd /etc/sysconfig/network-scripts/"按回车键确定,继续输入"ls"按回车键查看文件. 3.输入"vi ifcfg-ens

  • 详解CentOS升级Python2.6到Python2.7并安装pip

    现在大多数Centos6.x版本的系统python都是2.x,现因开发需求需要安装前端代码的构建工具glue,故必须要做python版本的升级 升级python 系统默认安装的python是2.6.6的,我们需要升级到Python2.7 wget https://www.python.org/ftp/python/2.7.12/Python-2.7.12.tar.xz tar xf Python-2.7.12.tar.xz cd Python-2.7.12 ./configure --enabl

  • centOS7 下利用iptables配置IP地址白名单的方法

    编辑iptables配置文件,将文件内容更改为如下,则具备了ip地址白名单功能 #vim /etc/sysconfig/iptables *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -N whitelist -A whitelist -s 1.2.3.0/24 -j ACCEPT -A whitelist -s 4.5.6.7 -j ACCEPT -A INPUT -m state --state

  • Centos下IP与DNS设置方法详解

    本文较为详细的讲述了Centos下IP与DNS设置方法.分享给大家供大家参考,具体如下: 1.CentOS 修改DNS 修改对应网卡的DNS的配置文件 # vi /etc/resolv.conf 修改以下内容 nameserver 8.8.8.8 #google域名服务器 nameserver 8.8.4.4 #google域名服务器 2.CentOS 修改网关 修改对应网卡的网关的配置文件 [root@centos]# vi /etc/sysconfig/network 修改以下内容 NETW

  • 如何为CentOS 7配置静态IP地址的两种方法

    如果你想要为CentOS 7中的某个网络接口设置静态IP地址,有几种不同的方法,这取决于你是否想要使用网络管理器. 网络管理器(Network Manager)是一个动态网络的控制器与配置系统,它用于当网络设备可用时保持设备和连接开启并激活.默认情况下,CentOS/RHEL 7安装有网络管理器,并处于启用状态. 使用下面的命令来验证网络管理器服务的状态: $ systemctl status NetworkManager.service 运行以下命令来检查受网络管理器管理的网络接口: $ nm

随机推荐