1. 程式人生 > >ssh的tunnel設置+autossh設置

ssh的tunnel設置+autossh設置

會話 init 版本號 lan 反向 方法 apt ubunt 作者

tunnel設置


一、說明

用於通過ssh轉發數據


二、設置

編輯ssh server的‘2Fetc/ssh/sshd_config

加入下面:

#反向遂道
GatewayPorts yes
#正向遂道,註:該設翮會斷開ssh的22斷開

AllowTcpForwarding true


三、重新啟動ssh

sudo /etc/init.d/ssh restart 或 sudo service ssh restart


四、實例

A地址:10.1.1.1

B地址:10.1.1.2

1.反向遂道

在B機器上執行

ssh -R 8001:10.1.1.2:8002 [email protected]

/* */ -f -g -N -C -o TCPKeepAlive=yes

效果為訪問10.1.1.1:8001 ==> 10.1.1.2:8002

2.正向遂道

在A機器上執行

ssh -L 8001:10.1.1.1:8002 root@10.1.1.2 -f -g -N -C -o TCPKeepAlive=yes

效果為訪問10.1.1.1:8001 ==> 10.1.1.2:8002


註:因為以上方法長時間執行可能會斷開連接,因此推薦下面方法

autossh設置


一、說明

autossh 是一款開源工具。能夠幫助管理SSH會話、自己主動重連和停止轉發流量。


二、安裝
sudo apt-get install autossh


三、實例

1.可替換ssh方式:

autossh -M 5678 -R 8001:10.1.1.2:8002 [email protected] -f -g -N -C

2.開機自執行

輯/etc/rc.local文件,加入:

/usr/lib/autossh/autossh -M 5678 -fgNCR 8001:10.1.1.2:8002 [email protected]

如以xxx用戶執行:

/bin/su - xxx -c "/usr/lib/autossh/autossh-M 5678-fgNC

R 8001:10.1.1.2:8002 [email protected]"


作者測試環境說明:

系統: ubuntu 14.04 LTS

ssh版本號: 6.6.1p1

autossh版本號: 1.4c


ssh參數詳細參考ssh命令+免password登入

ssh的tunnel設置+autossh設置