日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

通过ssh建立点对点的隧道,实现两个子网通信

發(fā)布時(shí)間:2025/7/14 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 通过ssh建立点对点的隧道,实现两个子网通信 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

查看ssh幫助文檔是發(fā)現(xiàn)有個(gè)-w的參數(shù),說明如下:

-w local_tun[:remote_tun]Requests tunnel device forwarding with the specified tun(4) devices between the client (local_tun) and theserver (remote_tun).The devices may be specified by numerical ID or the keyword “any”, which uses the next available tunneldevice. If remote_tun is not specified, it defaults to “any”. See also the Tunnel and TunnelDevice direc?tives in ssh_config(5). If the Tunnel directive is unset, it is set to the default tunnel mode, which is“point-to-point”.

?

可以建立“point-to-point”的隧道,類似于pptp的作用

假設(shè)現(xiàn)在有北京和上網(wǎng)兩個(gè)組網(wǎng),內(nèi)外網(wǎng)IP如下:

?準(zhǔn)備工作:

服務(wù)端需要開啟隧道參數(shù)(上圖中的上海節(jié)點(diǎn))

vim /etc/ssh/sshd_config; ------------------------------------------------------------------------------ PermitRootLogin yes PermitTunnel yes ------------------------------------------------------------------------------
service ssh restart#重啟sshd服務(wù)(ubuntu)

?

?

登錄北京的機(jī)器操作:

?

ssh -o TCPKeepAlive=yes -o ServerAliveInterval=60 -f -w 10:10 2.2.2.2 true

?

?TCPKeepAlive ServerAliveInterval 兩個(gè)參數(shù)是為了保持連接,防止網(wǎng)絡(luò)抖動(dòng)是ssh隧道斷掉

-f表示后臺(tái)運(yùn)行

-w 10:10 表示兩邊用于隧道通信的網(wǎng)卡名字為 tun10 ,如果0:0,那兩邊網(wǎng)卡就是tun0

注意:不要混淆了Linux下面名為tunl0的預(yù)設(shè)Tunnel界面,請(qǐng)用 ip addr show 命令檢查。

執(zhí)行完成這一步之后連邊會(huì)同時(shí)增加一個(gè)tun10名的網(wǎng)卡

此時(shí)隧道已經(jīng)建立,但是兩邊暫時(shí)還不能通信,需要制定兩邊網(wǎng)卡的ip和啟動(dòng)網(wǎng)卡

#北京 ip addr add 10.1.1.1 peer 10.1.1.2 dev tun10 ip link set tun10 up#上海ip addr add 10.1.1.2 peer 10.1.1.1 dev tun10 ip link set tun10 up

?

執(zhí)行完成上面命令之后 兩邊已經(jīng)可以相互ping通了,

如果需要兩個(gè)子網(wǎng)通信需要指定路由和利用iptables做snat

例如上海訪問北京:

上海: route add -net 192.168.10.0/24 dev tun10#網(wǎng)段為對(duì)端(北京)的網(wǎng)段 172.16.10.4上執(zhí)行
如果需要上海的同一子網(wǎng)的其他機(jī)器訪問的話需要以下操作
iptables -t nat -A POSTROUTING -s 172.16.10.0/24 -d 192.168.10.0/24 -j SNAT --to 10.1.1.2 # 172.16.10.4上執(zhí)行 子網(wǎng)內(nèi)其他機(jī)器增加路由 route add -net 192.168.10.0/24 gw 172.16.10.4 北京: iptables -t nat -A POSTROUTING -s 10.1.1.2 -d 192.168.10.0/24 -j SNAT --to 192.168.10.4

?

?執(zhí)行完成后在上海的子網(wǎng) 已經(jīng)可以和北京的子網(wǎng)內(nèi)其他機(jī)器通信了

?

如果需要北京子網(wǎng)也訪問上海子網(wǎng)的話

根據(jù)上面修改即可;

ssh 隧道還有個(gè)有點(diǎn)就是只需要服務(wù)端有公網(wǎng)IP就行,客戶端不需要有公網(wǎng)IP,只要可以訪問服務(wù)端的公網(wǎng)IP即可

總結(jié)

以上是生活随笔為你收集整理的通过ssh建立点对点的隧道,实现两个子网通信的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。