SSH 使用大小事

平常常常會使用 SSH 做一些特別的運用,今天想說就把它做一個紀錄吧!這樣之後要查也比較簡單 ~

Local port forwarding

ssh -L [bind_address:]<port>:<host>:<host_port> <SSH Server>

使用範例

假設我需要經過堡壘主機 ([email protected]) 連線到位於內網的資料庫 (3306:rds.ap-northeast-1.rds.amazonaws.com:3306)

ssh -L 13306:rds.ap-northeast-1.rds.amazonaws.com:3306 [email protected]

使用以上指令後即可使用 localhost:13306 連到資料庫

Remote port forwarding

ssh -R [bind_address:]<port>:<host>:<host_port> <SSH Server>

使用範例

架設我有一台伺服器在內網的 192.168.1.100:80 我希望使用者經過 [email protected] 的 8080 連到 192.168.1.100:80 的 Web 伺服器就可以使用此指令

ssh -R 0.0.0.0:8080:192.168.1.100:80 [email protected]

常用參數介紹

-N : 不執行任何命令
-f : 在背景執行