Vpn

通過 ppp0 啟用 Internet 路由

  • May 27, 2019

我的伺服器上執行著 Debian 8.0.0-64,它eth1作為與預設網關的介面。eth0是指向內部網路。

root@server:/home/user# ifconfig
eth0      Link encap:Ethernet  HWaddr 06:46:7e:88:72:d7  
         inet addr:10.168.118.205  Bcast:10.168.118.255  Mask:255.255.255.192
         inet6 addr: fe80::446:7eff:fe88:72d7/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:161 errors:0 dropped:0 overruns:0 frame:0
         TX packets:203 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1000 
         RX bytes:15215 (14.8 KiB)  TX bytes:79027 (77.1 KiB)

eth1      Link encap:Ethernet  HWaddr 06:70:65:5f:e9:89  
         inet addr:167.41.133.218  Bcast:167.41.133.223  Mask:255.255.255.240
         inet6 addr: fe80::470:65ff:fe5f:e989/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:697 errors:0 dropped:0 overruns:0 frame:0
         TX packets:282 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1000 
         RX bytes:46420 (45.3 KiB)  TX bytes:33486 (32.7 KiB)

lo        Link encap:Local Loopback  
         inet addr:127.0.0.1  Mask:255.0.0.0
         inet6 addr: ::1/128 Scope:Host
         UP LOOPBACK RUNNING  MTU:65536  Metric:1
         RX packets:706 errors:0 dropped:0 overruns:0 frame:0
         TX packets:706 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:0 
         RX bytes:86847 (84.8 KiB)  TX bytes:86847 (84.8 KiB)

我使用以下說明設置了 VPN (TotalVPN) 連接:http: //pptpclient.sourceforge.net/howto-debian.phtml#configure_by_hand

有了pon amsterdam我實際上可以打開一個隧道:

root@server:/home/user# pon amsterdam

root@server:/home/user# ifconfig
ppp0      Link encap:Point-to-Point Protocol  
         inet addr:10.126.0.29  P-t-P:10.126.0.1  Mask:255.255.255.255
         UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1496  Metric:1
         RX packets:6 errors:0 dropped:0 overruns:0 frame:0
         TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:3 
         RX bytes:60 (60.0 B)  TX bytes:66 (66.0 B)

到現在為止還挺好。現在我希望能夠ping -I ppp0 google.com通過隧道 ping,例如 google.com ( ),而不會失去eth1用作我的預設介面 ( ping google.com) 的可能性。如果特別要求,即通過定義要使用的介面,我如何設置一個route或任何需要的東西,以便流量(到網際網路)可以通過隧道ppp0

以下是一些資訊(VPN 連接的名稱是amsterdam):

root@server:/home/user# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         167.41.133.209  0.0.0.0         UG    0      0        0 eth1
10.0.0.0        10.168.118.193  255.0.0.0       UG    0      0        0 eth0
10.168.118.192  0.0.0.0         255.255.255.192 U     0      0        0 eth0
161.26.0.0      10.168.118.193  255.255.0.0     UG    0      0        0 eth0
167.41.133.208  0.0.0.0         255.255.255.240 U     0      0        0 eth1

root@server:/home/user# pon amsterdam

root@server:/home/user# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0           0.0.0.0         UG    0      0        0 eth1
10.0.0.0        10.168.118.193  255.0.0.0       UG    0      0        0 eth0
10.126.0.1      0.0.0.0         255.255.255.255 UH    0      0        0 ppp0
10.168.118.192  0.0.0.0         255.255.255.192 U     0      0        0 eth0
45.32.239.20    167.41.133.209  255.255.255.255 UGH   0      0        0 eth1
161.26.0.0      10.168.118.193  255.255.0.0     UG    0      0        0 eth0
167.41.133.208  0.0.0.0         255.255.255.240 U     0      0        0 eth1

root@server:/home/user# poff amsterdam

root@server:/home/user# route -n

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         167.41.133.209  0.0.0.0         UG    0      0        0 eth1
10.0.0.0        10.168.118.193  255.0.0.0       UG    0      0        0 eth0
10.168.118.192  0.0.0.0         255.255.255.192 U     0      0        0 eth0
45.32.239.20    167.41.133.209  255.255.255.255 UGH   0      0        0 eth1
161.26.0.0      10.168.118.193  255.255.0.0     UG    0      0        0 eth0
167.41.133.208  0.0.0.0         255.255.255.240 U     0      0        0 eth1

root@server:/home/user# 

我找到了兩個解決方案,它們適用於第一個視圖,但它們確實不起作用(我不知道為什麼)。

  1. 添加route add default metric 10 gw $PPP_REMOTE $PPP_IFNAME向上腳本,或
  2. 添加defaultroute到pptp配置

在這兩種情況下,呼叫curl --interface ppp0 ifconfig.coas都root有效並返回 VPN 連接的 IP 地址,但我無法以任何其他非 root 使用者身份執行該命令。該命令curl --interface eth1 ifconfig.co可以作為root普通使用者正常工作。

為了實現我想要實現的目標,我必須做以下事情:

第 1 步:為 Debian 項目安裝 PPTP 客戶端程序

第 2 步:設置 PPTP 連接

第 3 步:測試連接

第 4 步:添加路線

第 5 步:最終檢查

對於前三個步驟,我主要遵循http://pptpclient.sourceforge.net/howto-debian.phtml。這樣做之後,我發現了這篇很棒的文章 ( https://www.thomas-krenn.com/en/wiki/Two_Default_Gateways_on_One_System ),它解釋瞭如何添加兩個預設網關。第二種網關方法背後的主要思想是在(在我的例子中,我將其命名為)中創建第二個路由表。完成此操作後,將路由添加到此新表並定義規則:/etc/iproute2/rt_tables``ppp

ip route add 10.10.0.0/24 dev eth1 src 10.10.0.10 table ppp
ip route add default via 10.10.0.1 dev eth1 table ppp

ip rule add from 10.10.0.10/32 table ppp
ip rule add to 10.10.0.10/32 table ppp

經過測試,我添加了腳本,以便在建立VPN連接時添加和刪除路由,即

ip-up 腳本

#!/bin/sh

if [ "$PPP_IPPARAM" = "amsterdam" ] ; then

 /sbin/ip route add $PPP_LOCAL/24 dev $PPP_IFACE src $PPP_LOCAL table ppp
 /sbin/ip route add default via $PPP_REMOTE dev $PPP_IFACE table ppp

 /sbin/ip rule add from $PPP_LOCAL/32 table ppp
 /sbin/ip rule add to $PPP_LOCAL/32 table ppp
fi

ip-down 腳本

#!/bin/sh

if [ "$PPP_IPPARAM" = "amsterdam" ] ; then

 /sbin/ip route del $PPP_LOCAL/24 dev $PPP_IFACE src $PPP_LOCAL table ppp
 /sbin/ip route del default via $PPP_REMOTE dev $PPP_IFACE table ppp

 /sbin/ip rule del from $PPP_LOCAL/32 table ppp
 /sbin/ip rule del to $PPP_LOCAL/32 table ppp
fi

這非常有效,我可以在需要時選擇第二個網關。

引用自:https://unix.stackexchange.com/questions/315793