文章插图
实现目标
- Ubuntu 18 与华为路由器建立 GRE 隧道;
- 华为防火墙背后的内网网段 192.168.1.0/24 通过 GRE 隧道从 CentOS 到 Internet;
- Ubuntu 配置端口映射,将 192.168.1.10 的 8080 端口映射到 CentOS 的公网地址 200.1.1.1 的 8080 端口 。
配置
- Ubuntu
root@ubunt18demo:~# vim /etc/netplan/00-installer-config.yaml
network:
ethernets:
ens3:
addresses:
- 200.1.1.1/24
gateway4: 200.1.1.254
nameservers:
addresses:
- 114.114.114.114
tunnels:
tun0:
mode: gre
local: 200.1.1.1
remote: 100.1.1.1
addresses: [ 172.16.1.1/24 ]
routes:
- to: 192.168.1.0/24
via: 172.16.1.2
# 可以先执行 netplan try 验证一下,如果没有断掉的话可以按 ENTER 确认配置
# 如果和主机 SSH 中断,可以等待 120S 会自动恢复
root@ubunt18demo:~# netplay try
iptables 设置
Ufw 是 Ubuntu 的防火墙配置工具,底层还是调用 iptables 处理的:# 启用 ufw华为路由器
ufw enable
# 放行 SSH
ufw allow ssh
# 放行 GRE 对端进入的流量
ufw allow from 100.1.1.1/32
# 配置 nat 映射
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j SNAT --to-source 200.1.1.1
iptables -t nat -A PREROUTING -d 200.1.1.1 -p tcp --dport 8080 -j DNAT --to-dest 192.168.1.10:8080
# 将 ufw 设置为开机自启动
systemctl enable ufw开启 ipv4 转发:echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p
以 AR1200 系列路由器为例:
配置接口interface Tunnel0/0/1
ip address 172.16.1.2 255.255.255.0
tunnel-protocol gre
source 100.1.1.1
destination 200.1.1.1
配置策略路由# 配置 ACL
[AR1200] acl number 3000
[AR1200-acl-adv-3000] rule 10 permit ip destination 192.168.1.0 0.0.0.255
# 配置流分类
[AR1200] traffic classifier togretunnel
[AR1200-classifier-togretunnel] if-match acl 3000
# 配置流行为
[AR1200] traffic behavior togretunnel
[AR1200-behavior-togretunnel] redirect ip-nexthop 172.16.1.1
# 配置流策略
[AR1200] traffic policy togretunnel
[AR1200-trafficpolicy-vlan10] classifier togretunnel behavior togretunnel
# 在内网口调用流策略
[AR1200] interface gigabitethernet 1/0/1
[AR1200-GigabitEthernet3/0/0] traffic-policy togretunnel inbound
验证
验证方法同 CentOS 与 华为防火墙建立 GRE 隧道一致 。
Juniper SRX 防火墙的 GRE 配置
SRX 防火墙的出接口如果使用了 route-instances,那么配置 tunnel 口时,一定要注意增加 route-instance destination,如下所示:
set interfaces gr-0/0/0 unit 0 tunnel source 100.1.1.1set interfaces gr-0/0/0 unit 0 tunnel destination 200.1.1.1set interfaces gr-0/0/0 unit 0 tunnel routing-instance destination EXAMPLE-INSTANCEset interfaces gr-0/0/0 unit 0 family inet address 172.16.1.2/24另外策略路由在 SRX 中称为 FBF,还有 No-NAT的配置示例如下:# 配置 firewall filter,匹配需要进入隧道的流量set firewall filter to-GreTunnel term 1 from source-address 192.168.1.0/24set firewall filter to-GreTunnel term 1 then routing-instance EXAMPLE-INSTANCEset firewall filter to-GreTunnel term 3 then acceptset routing-options rib-groups global import-rib EXAMPLE-INSTANCE.inet.0# 配置去往 Gre Tunnel 的路由set routing-instances EXAMPLE-INSTANCE instance-type forwardingset routing-instances EXAMPLE-INSTANCE routing-options interface-routes rib-group inet globalset routing-instances EXAMPLE-INSTANCE routing-options static route 0.0.0.0/0 next-hop 172.16.1.1# 在内网口调用 firewall filterset interfaces reth2 unit 0 family inet filter input to-GreTunnel# 去往隧道口的流量不做 SNATset security nat source rule-set Gre-snat from zone Trustset security nat source rule-set Gre-snat to zone EXAMPLE-INSTANCEset security nat source rule-set Gre-snat rule to-cn2-no-nat match source-address 192.168.1.0/24set security nat source rule-set Gre-snat rule to-cn2-no-nat match destination-address 0.0.0.0/0set security nat source rule-set Gre-snat rule to-cn2-no-nat then source-nat off CentOS 的策略路由
如果有使用 Linux 作为中转的场景,也就是说华为防火墙和 Linux 建立 GRE 隧道,Linux 又和其他设备建立,由 Linux 做中转流量,这种场景下,可以在 Linux 配置策略路由,如下所示:
# 临时配置,重启后会消失,可以作为调试使用ip rule add from 192.168.1.0/24 table 100 pref 10ip route add 0.0.0.0/0 via 200.1.1.254 table 100# 将配置持久化vim /etc/sysconfig/network-scripts/rule-eth0from 192.168.1.0/24 table 100 pref 10vim /etc/sysconfig/network-scripts/route-eth0default via 172.16.1.1 dev tun0# 验证命令ip rule showip route show table 100
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 冰糖柠檬的功效与作用,冰糖柠檬茶的做法
- Linux,软件安装依赖该怎么解决?
- 创造与魔法礼包码20211月9日 创造与魔法最新礼包码2020
- 绿茶的作用与功效,绿茶粉的功效与作用,1
- 普洱茶部分作用,普洱茶和皖西黄大茶功效与作用的区别
- 绿茶的功效与作用,绿茶粉的功效与作用,1
- 白茶煮多长时间最好,福鼎白茶的功效与冲泡
- 丝瓜的功效与作用 食疗方治日常小病有奇招
- 绿茶与白茶的区别,浙江安吉白茶的保健功效
- 绿茶精油的功效与用法,茶籽油山茶油和精油的区别
