Read original article by clicking here.

SSH Tricks: Escape Sequences

Written by Rocky Olsen|1 Minutes
SSH Tricks: Escape Sequences
Rocky Olsen

May 22, 2019

·
2 min read

SSH Tricks: Escape Sequences

Escape Sequences

SSH is one of my favorite tools, while it’s most common usage is simply and securely connecting to remote machines. Lurking beneath is a powerful swiss army knife that can help you navigate a multitude of situations.

One of the most powerful features that no one ever seems to know about, and was imparted on me by some one much, much smarter is SSH’s escape sequences.

All escape sequences must occur after a new line character — so hit that enter key a couple of times before inputing one.

Getting Help: Tilde-QuestionMark (~?)

To get a list of all the available supported escape sequences use the escape sequnce: ~?

(rocky@ragnar ~)% 
(rocky@ragnar ~)%
(rocky@ragnar ~)% ~?
Supported escape sequences:
~. - terminate connection (and any multiplexed sessions)
~B - send a BREAK to the remote system
~C - open a command line
~R - request rekey
~V/v - decrease/increase verbosity (LogLevel)
~^Z - suspend ssh
~# - list forwarded connections
~& - background ssh (when waiting for connections to terminate)
~? - this message
~~ - send the escape character by typing it twice
(Note that escapes are only recognized immediately after newline.)

Terminate Connection: Tilde-Dot (~.)

Tilde-Dot (~.) is above and beyond my favorite escape sequence. Why you might ask? Well simply because some times SSH gets stuck, your network disconnects, the process locks, who knows why but you lose all interactivity. When this happens the traditional cure-all is to kill your local terminal, your local process, or in general more destructive things.

However using (~.) you can kill just the SSH connection!

Command Line: Tilde-C (~C)

Tilde-C (~C) is also an amazing one, as it drops you to a ‘ssh>’ prompt, from there you can modify the running ssh command.

This is awesome because it means that say you are hacking along, hard at work, and you realize that you really wish you forwarded a port. Instead of dropping your current connection, spawning a new terminal, you can add them from here.

(rocky@ragnar ~)%
(rocky@ragnar ~)%
(rocky@ragnar ~)%
ssh> ?
Commands:
-L[bind_address:]port:host:hostport Request local forward
-R[bind_address:]port:host:hostport Request remote forward
-D[bind_address:]port Request dynamic forward
-KL[bind_address:]port Cancel local forward
-KR[bind_address:]port Cancel remote forward
-KD[bind_address:]port Cancel dynamic forward

Til next time,
EOF