brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: type ~C to add a port forward to a running SSH session

Network and access
~/tips/tip-ssh-escape-tilde-c-add-forward

[email protected]:~$ ~C

Opens ssh's own command line in a live session so you can add or cancel port forwards.

I forget the -L flag more often than I would like, and the session is already open and full of state. You can add the forward without reconnecting.

ssh has escape characters that work while a session is running. The manual lists ~C as “open command line”. From that prompt you can add -L, -R or -D forwardings, and cancel them again with -KL, -KR or -KD.

[email protected]:~$ ~C
ssh> -L 8080:127.0.0.1:80

After that, localhost:8080 on your machine reaches port 80 on the server. Press ~# to list the forwarded connections, and ~? to see all escape characters.

The rules for the tilde are strict, and this is where the tip goes wrong for most people. The manual says escapes are recognised only at the start of a line and only when a pseudo-terminal is in use, and that the escape character must follow a newline. So press Enter first, then type ~ and C. If you type it halfway through a command, the characters just land in your shell.

To remove a forward again, open the command line the same way and use the matching cancel option, -KL for a local forward (the manual’s escape section has the details).

The forwards you add this way live only as long as the session. When you want one permanently, write it as LocalForward in your ssh config or pass -L next time.

Further reading: ssh(1), escape characters, ssh_config(5)

More Network and access tips scp -3 alice@server-a:/var/backups/db.sql.gz bob@server-b:/srv/restore/ tailscale exit-node suggest tailscale lock sign nodekey:1abddef1 tlpub:abcdef12 wg genpsk > peer1.psk Table = off All Network and access tips (163) All topics

Link