You need to access multiple services on a remote EC2 instance locally — say, Chrome DevTools on port 18792 and an admin panel on port 8899. The naive approach is two terminals:
# Terminal 1
ssh -L 18792:localhost:18792 ec2-user@your-host -N
# Terminal 2
ssh -L 8899:localhost:8899 ec2-user@your-host -N
Two connections, two processes, easy to lose track of.
One command handles both:
ssh -L 18792:localhost:18792 -L 8899:localhost:8899 -o ServerAliveInterval=30 -o ServerAliveCountMax=3 -N ec2-user@your-host
The -N flag tells SSH not to execute a remote command (just forward ports). -o ServerAliveInterval=30 keeps the connection alive.
Add this to ~/.ssh/config:
Host ec2-dev
HostName your-ec2-ip-or-hostname
User ec2-user
IdentityFile ~/.ssh/your-key.pem
LocalForward 18792 localhost:18792
LocalForward 8899 localhost:8899
ServerAliveInterval 30
ServerAliveCountMax 3
Then:
ssh -N ec2-dev # forwards all listed ports, no typing
Or if you also want a shell:
ssh ec2-dev # opens shell + forwards ports simultaneously
| Flag | Behavior |
|------|----------|
| -N | No remote command; process stays in foreground |
| -f | Goes to background immediately after authenticating |
| -N -f | Background process, no shell — ideal for port forwarding daemons |
For interactive use, -N (foreground) is easier to kill with Ctrl+C. For long-running setups, -N -f lets you close the terminal.
# List all ssh processes doing port forwards
ps aux | grep "ssh -" | grep -v grep
# Check if a port is actually bound
lsof -i :18792