Linux中如何配置GRE隧道
配置拓撲
這里使用ens33接口地址來建立隧道,使用ens37接口地址做隧道接口標識,使用ens38接口地址來模擬私網業(yè)務地址。
實際場景中,用于建立隧道的地址是公網地址,而業(yè)務地址是私網地址,GRE隧道使得私網地址空間不會暴露于公網。
具體配置
1. node02
開啟路由轉發(fā),加載GRE協(xié)議模塊。
# 開啟ipv4路由轉發(fā) echo 1 > /proc/sys/net/ipv4/ip_forward # 查看GRE內核模塊信息 modinfo ip_gre # 查看GRE內核模塊是否安裝 lsmod | grep ip_gre # 加載GRE內核模塊 modprobe ip_gre modprobe --first-time ip_gre
創(chuàng)建隧道虛接口tunnel2并配置隧道接口ip(隧道標識)。
# 配置GRE隧道的source和destination ip(外部,公網地址) ip tunnel add tunnel2 mode gre local 192.168.91.129 remote 192.168.91.130 ttl 255 dev ens33 # 配置GRE tunnel接口的隧道標識(在數(shù)通設備上可以通過ip unnumber借用其他接口的primary ip) ip addr add 172.16.18.10 dev tunnel2 peer 172.16.20.20/24 # 開啟tunnel2接口 ip link set dev tunnel2 up # 查看tunnel接口 ifconfig ip a
添加指向隧道接口的路由以引導流量進入隧道。
# 添加私網路由指向隧道口 ip route add 10.10.20.20/32 dev tunnel2 ip route add 172.16.20.20/32 dev tunnel2
2. node03
開啟路由轉發(fā),加載GRE協(xié)議模塊。
# 開啟ipv4路由轉發(fā) echo 1 > /proc/sys/net/ipv4/ip_forward # 加載GRE內核模塊 modprobe --first-time ip_gre
創(chuàng)建隧道虛接口tunnel2并配置隧道接口ip(隧道標識)。
# 配置GRE隧道的source和destination ip(外部,公網地址) ip tunnel add tunnel2 mode gre local 192.168.91.130 remote 192.168.91.129 ttl 255 dev ens33 # 配置GRE tunnel接口的隧道標識(在數(shù)通設備上可以通過ip unnumber借用其他接口的primary ip) ip addr add 172.16.20.20 dev tunnel2 peer 172.16.18.10/24 # 開啟tunnel2接口 ip link set dev tunnel2 up
添加指向隧道接口的路由以引導流量進入隧道。
# 添加私網路由指向隧道口 ip route add 10.10.10.10/32 dev tunnel2 ip route add 172.16.18.10/32 dev tunnel2
3. 查看路由表
route -n # 推薦此法 或者 netstat -nr
4. 驗證
- 隧道建立
[root@node02 ~]# ping -c3 -I 10.10.10.10 10.10.20.20 PING 10.10.20.20 (10.10.20.20) from 10.10.10.10 : 56(84) bytes of data. 64 bytes from 10.10.20.20: icmp_seq=1 ttl=64 time=0.708 ms 64 bytes from 10.10.20.20: icmp_seq=2 ttl=64 time=0.523 ms 64 bytes from 10.10.20.20: icmp_seq=3 ttl=64 time=0.555 ms --- 10.10.20.20 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2001ms rtt min/avg/max/mdev = 0.523/0.595/0.708/0.083 ms [root@node02 ~]# ping -c3 -I 172.16.18.10 172.16.20.20 PING 172.16.20.20 (172.16.20.20) from 172.16.18.10 : 56(84) bytes of data. 64 bytes from 172.16.20.20: icmp_seq=1 ttl=64 time=1.12 ms 64 bytes from 172.16.20.20: icmp_seq=2 ttl=64 time=0.509 ms 64 bytes from 172.16.20.20: icmp_seq=3 ttl=64 time=0.469 ms --- 172.16.20.20 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2006ms rtt min/avg/max/mdev = 0.469/0.700/1.122/0.298 ms
- 隧道拆除
將tunnel2接口shutdown
ip link set dev tunnel2 down
再進行ping測試
[root@node02 ~]# ping -c3 -I 10.10.10.10 10.10.20.20 PING 10.10.20.20 (10.10.20.20) from 10.10.10.10 : 56(84) bytes of data. --- 10.10.20.20 ping statistics --- 3 packets transmitted, 0 received, 100% packet loss, time 2017ms [root@node02 ~]# ping -c3 -I 172.16.18.10 172.16.20.20 PING 172.16.20.20 (172.16.20.20) from 172.16.18.10 : 56(84) bytes of data. --- 172.16.20.20 ping statistics --- 3 packets transmitted, 0 received, 100% packet loss, time 2023ms
5. 配置多個環(huán)回地址
在上述的配置中可以發(fā)現(xiàn)啟用了多個物理網卡來提供本地IP,但在實際應用中可能不具備這樣的條件。配置多個環(huán)回口地址是一個解決思路。
操作步驟如下:
cd /etc/sysconfig/network-scripts/ cp ifcfg-lo ifcfg-lo:1 vim ifcfg-lo:1 ---------------------------------------------------- DEVICE=lo:1 IPADDR=10.199.18.16 NETMASK=255.255.255.255 ONBOOT=yes NAME=loopback1 ---------------------------------------------------- systemctl restart network ifconfig ---------------------------------------------------- lo:1: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 10.199.18.16 netmask 255.255.255.255 loop txqueuelen 1000 (Local Loopback) ---------------------------------------------------- ip a ---------------------------------------------------- 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 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 inet 10.199.18.16/32 brd 10.199.18.16 scope global lo:1 valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever ---------------------------------------------------- [root@node01 network-scripts]# ping -c3 10.199.18.16 PING 10.199.18.16 (10.199.18.16) 56(84) bytes of data. 64 bytes from 10.199.18.16: icmp_seq=1 ttl=64 time=0.045 ms 64 bytes from 10.199.18.16: icmp_seq=2 ttl=64 time=0.050 ms 64 bytes from 10.199.18.16: icmp_seq=3 ttl=64 time=0.053 ms
如果是非持久化的添加(重啟后失效),可以使用如下命令:
ip addr add 10.199.18.16/32 dev lo:1
使用上述命令添加的環(huán)回接口地址只能使用“ ip a ”命令才可以查看到,使用“ ifconfig ”命令是看不到的。
但是在系統(tǒng)中可以ping通。
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
linux下查看yum/rpm/dpkg某軟件是否已安裝的方法
這篇文章主要介紹了在linux下查看yum/rpm/dpkg某軟件是否已安裝的方法,文中給出了詳細的示例代碼,對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-03-03CentOS 7.x下的LEMP環(huán)境搭建詳細教程
我們常說的LNMP環(huán)境是指Linux/nginx/MySQL/PHP組合,LEMP包是由Linux、nginx、MariaDB/MySQL和PHP組成的,那么看來LEMP和LNMP是一樣的,而現(xiàn)在業(yè)內習慣性的稱作LEMP。這篇文章主要介紹了CentOS 7.x下的LEMP環(huán)境搭建詳細教程,需要的朋友可以參考下2016-10-10mac下配置和訪問阿里云服務器(Ubuntu系統(tǒng))的圖文教程
這篇文章主要介紹了mac下配置和訪問阿里云服務器(Ubuntu系統(tǒng))的圖文教程,非常不錯,具有參考借鑒價值,需要的朋友參考下2017-01-01linux 不改變目錄結構移動 home 目錄到新分區(qū)的操作方法
這篇文章主要介紹了linux 不改變目錄結構移動 home 目錄到新分區(qū)的操作方法,需要的朋友可以參考下2017-12-12Linux使用Cron+AT實現(xiàn)在某個確定的時間段內隨機執(zhí)行命令
寫了個腳本簽到,但是不想總是在確定的時間簽到,不然在數(shù)據庫里面的記錄太假了,所以需要在確定的時間段內,隨機選個時間執(zhí)行,最后想到了使用Cron+AT實現(xiàn),需要的朋友可以參考下2016-07-07