# SSH Port Forwarding aka SSH Tunneling ## Local Forwarding ```shell ssh -L 80:intra.example.com:80 gw.example.com ``` ## Remote Forwarding ```shell ssh -R 8080:localhost:80 public.example.com ``` アクセスされる側のマシンが Mac の場合は、 `Sharing > Remote Login: On` にしておく ### Use Case #### autossh ```shell brew install autossh ``` .ssh/config ``` StreamLocalBindUnlink yes ``` 踏み台の /etc/ssh/sshd_config ssh が切れた後に port が開きっぱなしになっても timeout してくれる ``` ClientAliveInterval 30 ``` アクセスされる側のマシン(`8888` は何でもよい) ```shell ssh hoge -R 8888:localhost:22 ``` ```shell autossh -f -N -M 0 -R 8888:localhost:22 hoge ``` 踏み台上で確認 ```shell sudo netstat -plant | grep 8888 ``` 踏み台から自宅マシンへ ```shell ssh -p 8888 127.0.0.1 ``` #### Google Cloud Compute Engine の VM インスタンスを踏み台にして自宅マシンに ssh 接続 アクセスされる側のマシン(`8888` は何でもよい) ```sh gcloud compute ssh --project "hibi-prj" --zone "asia-northeast2-a" "hibi" -- -R 8888:localhost:22 ``` アクセスする側のマシンからいったん VM インスタンスに入る ```shell gcloud compute ssh --zone "asia-northeast2-a" "hibi" ``` そこから自宅マシンへ ```shell ssh -p 8888 127.0.0.1 ``` ## Guide [Visual guide to SSH tunnels](https://robotmoon.com/ssh-tunnels/)