SSH Tricks - Making your life simple

If you have ever been disconneted from a server while in an ssh session, you probably have realized how annoying it is to be stuck in an SSH Session until the client realizes that the server is gone and returns control ofo the terminal to you. After having accepted this problem for many years, I recently came accross a solution.

If you login into the next best serevr via ssh, you can enter the following three characters: Enter, ~, and ?. The result should look similar to this:

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.)

Turns out, you can control SSH using escape sequences that aren’t executed on the server like all the other input, but used to control the local client. To solve the issue described above we can just enter: Enter, ~, and . and terminate the current connection.

I will describe the purpose of the escape sequence below:

~. - Ends the current connection right away.

~C - Allows you to edit forwarded connections

~R - Should be done automatically by current SSH versions anyways.

~V/v - Chnage log levels on the fly.

~^Z - Suspends the current ssh connection and puts you back into a local shell. Allows you to return to the ssh session using fg.

~# - Lists all forwarded connections

~& - Puts SSH into background and white for all processes to exit gracefully.

~? - Print the help message you can see above.

~~ - Allows you to actually send the ~ character after a newline.

I am still trying to find a good explanation for ~B. Let me know, if you can point me in the right direction.

These escape characters are describes in the man pages of ssh.

By Cecil Wöbker


I do science during the day and develop or design at night. If you like my work, hire me.

Feel free to follow me on Twitter or email me with any questions.

Comments