這篇文章主要介紹Linux與網(wǎng)絡(luò)設(shè)備GRE如何配置,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
玉州ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書(shū)銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書(shū)合作)期待與您的合作!
GRE 介紹及應(yīng)用場(chǎng)景
GRE(General Routing Encapsulation),即通用路由封裝,是一種三層技術(shù)。它的最大作用是可以對(duì)某些網(wǎng)絡(luò)層協(xié)議的報(bào)文進(jìn)行封裝,如對(duì)路由協(xié)議、語(yǔ)音、視頻等組播報(bào)文或IPv6報(bào)文進(jìn)行封裝。同時(shí),也能夠與 IPSec 結(jié)合,解決 GRE 的安全問(wèn)題。
GRE 報(bào)文
如下圖所示,GRE 是按照 TCPIP 協(xié)議棧進(jìn)行逐層封裝,新的 IP 頭會(huì)封裝在原有的 IP 頭中,然后運(yùn)送出去,封裝操作是通過(guò) Tunnel 接口完成的,GRE 協(xié)議經(jīng)過(guò) Tunnel 口時(shí),會(huì)將接口的封裝協(xié)議設(shè)置為 GRE 協(xié)議。
GRE 的配置場(chǎng)景
CentOS 7.6 與 華為防火墻建立 GRE 隧道
拓?fù)鋱D
實(shí)現(xiàn)目標(biāo)
CentOS 與 華為防火墻建立 GRE 隧道;
華為防火墻背后的內(nèi)網(wǎng)網(wǎng)段 192.168.1.0/24 通過(guò) GRE 隧道從 CentOS 到 Internet;
CentOS 配置端口映射,將 192.168.1.10 的 8080 端口映射到 CentOS 的公網(wǎng)地址 200.1.1.1 的 8080 端口。
配置
CentOS
配置接口與路由
[root@CentOS ~]# vim /etc/sysconfig/network-scripts/ifcfg-tun0 DEVICE=tun0 BOOTPROTO=none ONBOOT=yes DEVICETYPE=tunnel TYPE=GRE PEER_INNER_IPADDR=172.16.1.2 PEER_OUTER_IPADDR=100.1.1.1 MY_INNER_IPADDR=172.16.1.1 MY_OUTER_IPADDR=200.1.1.1 [root@CentOS ~]# vim /etc/sysconfig/network-scripts/route-tun0 192.168.1.0/24 via 172.16.1.2 [root@CentOS ~]# ifup tun0
Iptables 配置
# 安裝 iptables 管理服務(wù) [root@CentOS ~]# yum install iptables-services # 在 INPUT 方向要放行對(duì)端的公網(wǎng)地址 [root@CentOS ~]# iptables -I INPUT -s 100.1.1.1/32 -j ACCEPT # 配置源地址轉(zhuǎn)換 [root@CentOS ~]# iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j SNAT --to-source 200.1.1.1 # 端口映射 [root@CentOS ~]# iptables -t nat -A PREROUTING -d 200.1.1.1 -p tcp --dport 8080 -j DNAT --to-dest 192.168.1.10:8080 # 保存 iptables [root@CentOS ~]# service iptables save
開(kāi)啟 ipv4 轉(zhuǎn)發(fā)
[root@CentOS ~]# echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf [root@CentOS ~]# sysctl -p
華為防火墻
本次以華為 USG6300E 系列防火墻為例:
配置接口,并添加到安全區(qū)域
interface Tunnel0 ip address 172.16.1.2 255.255.255.0 tunnel-protocol gre source 100.1.1.1 destination 200.1.1.1 # 將接口添加到安全區(qū)域內(nèi) [USG6300E] firewall zone tunnel firewall zone name tunnel set priority 75 add interface Tunnel0
配置安全策略
在實(shí)際的實(shí)施中,可以將策略收緊一些,根據(jù)需求限制源和目的地址。
如果條件允許的話,可以先將默認(rèn)安全策略設(shè)置為 permit,待調(diào)通之后,再修改安全策略:
security-policy rule name tunnel_out source-zone trust destination-zone tunnel action permit rule name tunnel_in source-zone tunnel destination-zone trust action permit # 放行 tunnel 到 untrust 的流量 rule name tunnel_untrust source-zone tunnel destination-zone untrust action permit
配置策略路由
[USG6300E]policy-based-route # policy-based-route rule name PBR source-zone trust source-address 192.168.1.0 mask 255.255.255.0 action pbr egress-interface Tunnel0
配置 No-NAT
設(shè)置去往隧道的流量不使用源地址轉(zhuǎn)換:
[USG6300E-policy-nat]dis th nat-policy rule name SNAT source-zone tunnel destination-zone untrust source-address 192.168.1.0 mask 255.255.255.0 action no-nat
驗(yàn)證
主要有如下幾個(gè)測(cè)試方法:
鴻蒙官方戰(zhàn)略合作共建——HarmonyOS技術(shù)社區(qū)
在 CentOS 或 防火墻 ping 對(duì)端的隧道地址;
使用 192.168.1.0/24 網(wǎng)段內(nèi)的設(shè)備 traceroute 公網(wǎng)地址,查看經(jīng)過(guò)的路徑以確認(rèn)是否經(jīng)過(guò)隧道轉(zhuǎn)發(fā)。
Ubuntu 18 與 華為路由器建立 GRE 隧道
拓?fù)鋱D
實(shí)現(xiàn)目標(biāo)
Ubuntu 18 與華為路由器建立 GRE 隧道;
華為防火墻背后的內(nèi)網(wǎng)網(wǎng)段 192.168.1.0/24 通過(guò) GRE 隧道從 CentOS 到 Internet;
Ubuntu 配置端口映射,將 192.168.1.10 的 8080 端口映射到 CentOS 的公網(wǎng)地址 200.1.1.1 的 8080 端口。
配置
Ubuntu
netplan 配置
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 # 可以先執(zhí)行 netplan try 驗(yàn)證一下,如果沒(méi)有斷掉的話可以按 ENTER 確認(rèn)配置 # 如果和主機(jī) SSH 中斷,可以等待 120S 會(huì)自動(dòng)恢復(fù) root@ubunt18demo:~# netplay try
iptables 設(shè)置
Ufw 是 Ubuntu 的防火墻配置工具,底層還是調(diào)用 iptables 處理的:
# 啟用 ufw ufw enable # 放行 SSH ufw allow ssh # 放行 GRE 對(duì)端進(jìn)入的流量 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 設(shè)置為開(kāi)機(jī)自啟動(dòng) systemctl enable ufw
開(kāi)啟 ipv4 轉(zhuǎn)發(fā):
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 # 在內(nèi)網(wǎng)口調(diào)用流策略 [AR1200] interface gigabitethernet 1/0/1 [AR1200-GigabitEthernet3/0/0] traffic-policy togretunnel inbound
驗(yàn)證
驗(yàn)證方法同 CentOS 與 華為防火墻建立 GRE 隧道一致。
Juniper SRX 防火墻的 GRE 配置
SRX 防火墻的出接口如果使用了 route-instances,那么配置 tunnel 口時(shí),一定要注意增加 route-instance destination,如下所示:
set interfaces gr-0/0/0 unit 0 tunnel source 100.1.1.1 set interfaces gr-0/0/0 unit 0 tunnel destination 200.1.1.1 set interfaces gr-0/0/0 unit 0 tunnel routing-instance destination EXAMPLE-INSTANCE set interfaces gr-0/0/0 unit 0 family inet address 172.16.1.2/24
另外策略路由在 SRX 中稱為 FBF,還有 No-NAT的配置示例如下:
# 配置 firewall filter,匹配需要進(jìn)入隧道的流量 set firewall filter to-GreTunnel term 1 from source-address 192.168.1.0/24 set firewall filter to-GreTunnel term 1 then routing-instance EXAMPLE-INSTANCE set firewall filter to-GreTunnel term 3 then accept set routing-options rib-groups global import-rib EXAMPLE-INSTANCE.inet.0 # 配置去往 Gre Tunnel 的路由 set routing-instances EXAMPLE-INSTANCE instance-type forwarding set routing-instances EXAMPLE-INSTANCE routing-options interface-routes rib-group inet global set routing-instances EXAMPLE-INSTANCE routing-options static route 0.0.0.0/0 next-hop 172.16.1.1 # 在內(nèi)網(wǎng)口調(diào)用 firewall filter set interfaces reth3 unit 0 family inet filter input to-GreTunnel # 去往隧道口的流量不做 SNAT set security nat source rule-set Gre-snat from zone Trust set security nat source rule-set Gre-snat to zone EXAMPLE-INSTANCE set security nat source rule-set Gre-snat rule to-cn2-no-nat match source-address 192.168.1.0/24 set security nat source rule-set Gre-snat rule to-cn2-no-nat match destination-address 0.0.0.0/0 set security nat source rule-set Gre-snat rule to-cn2-no-nat then source-nat off
CentOS 的策略路由
如果有使用 Linux 作為中轉(zhuǎn)的場(chǎng)景,也就是說(shuō)華為防火墻和 Linux 建立 GRE 隧道,Linux 又和其他設(shè)備建立,由 Linux 做中轉(zhuǎn)流量,這種場(chǎng)景下,可以在 Linux 配置策略路由,如下所示:
# 臨時(shí)配置,重啟后會(huì)消失,可以作為調(diào)試使用 ip rule add from 192.168.1.0/24 table 100 pref 10 ip route add 0.0.0.0/0 via 200.1.1.254 table 100 # 將配置持久化 vim /etc/sysconfig/network-scripts/rule-eth0 from 192.168.1.0/24 table 100 pref 10 vim /etc/sysconfig/network-scripts/route-eth0 default via 172.16.1.1 dev tun0 # 驗(yàn)證命令 ip rule show ip route show table 100
以上是“Linux與網(wǎng)絡(luò)設(shè)備GRE如何配置”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
本文名稱:Linux與網(wǎng)絡(luò)設(shè)備GRE如何配置
網(wǎng)頁(yè)URL:http://sd-ha.com/article6/psooog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、軟件開(kāi)發(fā)、網(wǎng)站內(nèi)鏈、靜態(tài)網(wǎng)站、App開(kāi)發(fā)、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)