通過ssh遠端埠轉發使外網能訪問到內網機器
阿新 • • 發佈:2018-12-07
家裡有兩臺樹莓派平時在外面想訪問下
首先啟用伺服器端GatewayPort來使遠端網絡卡可以繫結,設定連線保持
vim /etc/ssh/sshd_config
#修改以下屬性
GatewayPorts yes
ClientAliveInterval 60
ClientAliveCountMax 3
#然後退出vim編輯器,重新載入sshd配置
service sshd reload
建立遠端埠轉發隧道
ssh -N -f -p 遠端ssh埠 -R 遠端網絡卡:遠端要暴露的埠:本機網絡卡:本機要轉發的埠 [email protected]遠端host
如果不想手動輸入密碼可以用expect或者ssh-keygen
expect如下
#!/usr/bin/expect
spawn ssh -N -f -p 遠端ssh埠 -R 遠端網絡卡:遠端要暴露的埠:本機網絡卡:本機要轉發的埠 [email protected]遠端host
expect "password:"
send "密碼\r"
expect eof
ssh-keygen如下
ssh-keygen -t rsa
#然後將公鑰傳到遠端機器
ssh-copy-id -p 遠端ssh埠 -i ~/.ssh/id_rsa.pub [email protected]遠端host
然後還可以用autossh來自動重連
autossh -p 遠端埠 -M 自動重連監聽埠 -NR 代理埠:127.0.0.1:被代理埠 [email protected]
然後如果有防火牆還得開啟下埠
#開啟防火牆
systemctl start firewalld.service
#開機啟動防火牆
systemctl enable firewalld.service
#開啟埠
firewall-cmd --zone=public --add-port=埠/tcp --permanent
#重新載入配置
firewall-cmd --reload
#檢視埠是否開啟
firewall-cmd --zone=public --query-port=埠/tcp