Use SSH local port forwarding to access as if ports are local

When you write a code, like a web development, you would access localhost:3000 to check the progress. Or when you write an OAuth app, so localhost is needed when you test this out. How do we deal with it? The answer is SSH local port forwarding. There is a lot of software to do SSH local port forwarding but simply, you can run the below on your local terminal.

ssh -NT -o "ServerAliveInterval 60" -o TCPKeepAlive=yes -o "ServerAliveCountMax 120"  -L 8000:localhost:8000 <username>@<hostname>

Using the above, the connection stops and won't automatically reconnect. For example, if you finish the work and on the next day, when you unlock the screen, the connection is gone. You need to run the process again. In order to avoid this case, use autossh which reconnects automatically when you come back.

Install autossh

You can search the binary on your platform.

$ bew install autossh # macOS
$ sudo apt install autossh # crostini on chromeOS

Then, run

autossh -M 50000 -NT -o "ServerAliveInterval 60" -o TCPKeepAlive=yes -o "ServerAliveCountMax 120"  -L 8000:localhost:8000 <username>@<hostname>

I wrote a simple bash script to run multiple local port forwarding.

$ cat
autossh -M 50000 -NT -o "ServerAliveInterval 60" -o TCPKeepAlive=yes -o "ServerAliveCountMax 120"  -L 3000:localhost:3000 &
autossh -M 50100 -NT -o "ServerAliveInterval 60" -o TCPKeepAlive=yes -o "ServerAliveCountMax 120"  -L 5000:localhost:5000 &
autossh -M 50200 -NT -o "ServerAliveInterval 60" -o TCPKeepAlive=yes -o "ServerAliveCountMax 120"  -L 9222:localhost:9222 &

Ok, now you never feel these ports actually exists remotely.