搭建GRE隧道实现IPv4地址的映射

753次阅读
5 条评论

5年前,我写了一篇文章,介绍如何使用IPIP隧道实现IPv4地址的映射。今天,我来介绍一下使用更通用的GRE隧道(Generic Routing Encapsulation tunnel)实现IPv4地址的映射。

有很多场景需要使用IP地址映射。比如,我们有两台服务器,服务器A有大容量的硬盘和很多个IP地址,我们打算用它来做媒体播放储存;服务器B有很高的性能,但是只有一个IP地址,我们打算用它来做多个独立IP站点或者独立IP虚拟机。此时,我们就希望能够将服务器A的IP地址映射到服务器B上使用。而这个映射IP地址的过程就需要使用隧道。

我们假设将服务器A上的多余IP地址映射到服务器B上。这里假设服务器A的多余IP地址为88.88.88.88,服务器B的IP地址为99.99.99.99.  下面我们开始详细教学。

本文作者为香菇肥牛,原文链接为https://qing.su/article/create-gre-tunnel-for-ipv4-mapping.html,转载需注明原文链接。谢谢大家的支持!

1, 绑定IP地址

一般来说,服务器A上只绑定了一个主IP地址;其他的分配的IP地址需要手动绑定到服务器上。当然,部分主机商提供了自动绑定IP的功能,然而大部分是没有提供这个功能的。

Ubuntu 20.04为例,想要在服务器A上绑定多个IP地址,首先执行

1
ifconfig

查询网卡名称和MAC地址。我这里网卡名称是eno3, MAC地址假设为00:00:00:00:00:00

编辑文件/etc/netplan/50-cloud-init.yaml,添加下面这一段:

1
2
3
4
5
6
7
8
9
10
network:
    version: 2
    ethernets:
        eno3:
            dhcp4: true
            match:
                macaddress: 00:00:00:00:00:00
            set-name: eno3
            addresses:
            – 88.88.88.88/32

如果主机商给你分配了多个IP地址,需要逐条在addresses栏中列出。

保存退出后,启用并保存网卡设置:

1
2
netplan try
netplan apply

这样就在服务器上绑定好了IP地址。此时,如果你使用SSH登录IP为88.88.88.88的服务器,你应该能登录到服务器A上。

如果您的服务器不是Ubuntu 20.04, 那么可以参考这里绑定您的IP地址。https://docs.ovh.com/gb/en/dedicated/network-ipaliasing/

2, 开启GRE模块与其他前期准备

分别在A, B两台服务器上执行

1
2
modprobe ip_gre
lsmod | grep gre

如果能看到类似ip_gre或者gre这样的模块,说明GRE模块是开启的。有部分老版本的内核不支持GRE,以及部分老的OpenVZ 6的VPS也不支持;大部分情况都应该是支持的。

然后,需要在服务器A上开启IP转发功能。

1
2
echo ‘net.ipv4.ip_forward=1’ >> /etc/sysctl.conf
sysctl -p

如果您使用的是CentOS或者其他基于RHEL的Linux发行版,您可能需要禁用Selinux.  如果您的服务器上安装了防火墙,您可能需要设定相关规则,或者禁用防火墙。

我们还需要使用路由表。如果您的服务器上没有安装iptables与iproute2,需要在两台服务器上使用apt或者yum安装相关的程序。

1
apt-get install -y iptables iproute2

至此,前期准备完毕,开始构建GRE隧道。

3, 搭建GRE隧道

首先,在服务器A(提供多余IP的服务器)上,执行:

1
2
3
ip tunnel add qingsu mode gre local 88.88.88.88 remote 99.99.99.99 ttl 255
ip addr add 10.0.0.1/30 dev qingsu
ip link set qingsu up

这里qingsu是隧道的网络名称,可以任意替换为你想设定的名称。如果你有多个IP地址需要映射,这里需要对每个IP分别设置。

然后同样地,在服务器B(接受IP映射的服务器)上,执行:

1
2
3
ip tunnel add qingsu mode gre local 99.99.99.99 remote 88.88.88.88 ttl 255
ip addr add 10.0.0.2/30 dev qingsu
ip link set qingsu up

可以看到,我们搭建了名为qingsu的GRE隧道,并且将服务器A和B构建成了一个虚拟局域网,服务器A的局域网内地址为10.0.0.1,服务器B的局域网内地址为10.0.0.2.  如果一切无误,此时GRE隧道应该已经通了。

此时,在服务器A上ping 10.0.0.2,或者在服务器B上ping 10.0.0.1,应该都可以ping通了。

4, 设置路由表、数据与端口转发

首先,需要在服务器B上设置路由路径,保证经由GRE隧道传输的数据的正常通行。

1
2
3
echo ‘100 GRE’ >> /etc/iproute2/rt_tables
ip rule add from 10.0.0.0/30 table GRE
ip route add default via 10.0.0.1 table GRE

然后,需要在服务器A上设置并允许通向服务器B的数据传输。

1
2
3
iptables -t nat -A POSTROUTING -s 10.0.0.0/30 ! -o gre+ -j SNAT –to-source 88.88.88.88
iptables -A FORWARD -d 10.0.0.2 -m state –state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -s 10.0.0.2 -m state –state NEW,ESTABLISHED,RELATED -j ACCEPT

最后,在服务器A上设置端口转发。

1
2
iptables -t nat -A PREROUTING -d 88.88.88.88 -p TCP -m TCP  -j DNAT –to-destination 10.0.0.2
iptables -t nat -A PREROUTING -d 88.88.88.88 -p UDP -m UDP  -j DNAT –to-destination 10.0.0.2

至此,GRE隧道全部搭建完毕。此时,如果你再使用SSH登录IP为88.88.88.88的服务器,你应该能登录到服务器B上。后续在服务器B上,就可以使用这个新的IP地址建立网站、建立虚拟机了。需要注意的是,从第2节开始的上述网络设置在重启之后会失效,因此需要将这些命令放进系统的启动文件中,跟随系统的启动自动运行。

综上,在这篇文章中我们在两台服务器间搭建了GRE隧道,并且通过路由表设置了数据与端口的转发,实现了将IPv4从一台服务器上映射到另一台服务器上。如果您有任何疑问,欢迎在这里留言,我将尽力解答。本文作者为香菇肥牛,原文链接为https://qing.su/article/create-gre-tunnel-for-ipv4-mapping.html,转载需注明原文链接。谢谢大家的支持!

 

参考文献:

  1. https://www.karlrupp.net/en/computer/nat_tutorial
  2. https://www.xmodulo.com/create-gre-tunnel-linux.html
  3. https://community.hetzner.com/tutorials/linux-setup-gre-tunnel

Read More 

正文完
可以使用微信扫码关注公众号(ID:xzluomor)
post-qrcode
 
评论(5 条评论)
2024-04-16 03:31:25 回复

naturally like your web site however you need to take a look at the spelling on several of your posts. A number of them are rife with spelling problems and I find it very bothersome to tell the truth on the other hand I will surely come again again.

     泰国
2024-04-16 07:27:53 回复

I like the efforts you have put in this, regards for all the great content.

     泰国
2024-04-16 08:57:57 回复

This is really interesting, You’re a very skilled blogger. I’ve joined your feed and look forward to seeking more of your magnificent post. Also, I’ve shared your site in my social networks!

     泰国
2024-04-20 05:17:15 回复

You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!

     泰国
2024-04-25 08:01:21 回复

Pretty! This has been a really wonderful post. Many thanks for providing these details.

     泰国
Generated by Feedzy