If you want to conduct some tasks on local host while connected to a remote SSH server, you actually do not have to terminate the current SSH connection, nor switch to another terminal. If you are using OpenSSH, there is a way to "escape" out of the current SSH session temporarily to obtain the shell prompt of local host. You can return the original SSH session later.
In this post, I will explain how to suspend an SSH session in Linux, much like you would suspend a running foreground process.
OpenSSH supports several escape sequences, each of which is essentially a special SSH-related command you can use while you are on a remote SSH connection. Each escape sequence starts with "~" character, and you can enter an escape sequence at the beginning of the remote SSH shell prompt. Using one of such escape sequences, you can suspend an existing SSH session.
To escape the current SSH session, type "~" and "Ctrl+Z" back to back at the beginning of the remote shell prompt. Note that when you type "~", the tilde character will not appear in the terminal. Once this escape sequence is entered, the current SSH session will be suspended immediately, and you will get the shell prompt of local host.
You can also unsuspend an SSH session later when you want to return to the escaped SSH session. To do so, simply type "fg", and press enter at the shell prompt of local host.
The following screenshot shows how an SSH session is being suspended and recovered.
If you would like to keep a suspended SSH session open indefinitely, there are server-side approach as well as client-side method to do so.
To keep a suspended SSH session open by using a server-side method, edit OpenSSH daemon configuration on a remote SSH server, and restart the SSH server, as shown below.
ClientAliveInterval 60 ClientAliveCountMax 1 TCPKeepAlive yes
user@remote_host:~$ sudo service sshd restart (CentOS, Fedora or RedHat)
To keep a suspended SSH session open by using a client-side method, use the following SSH option when connecting to a remote SSH sever.
If you want to know all the escape sequences supported by OpenSSH, type "~" and "?" characters back to back at the remote SSH shell prompt.
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 (SSH protocol 2 only) ~^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.)
Subscribe to Xmodulo
Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.
Did you find this tutorial helpful? Then please be generous and support Xmodulo!
Latest posts by Dan Nanni (see all)
- How to switch from NetworkManager to systemd-networkd on Linux - August 31, 2015
- How to set up a system status page of your infrastructure - August 25, 2015
- How to configure virtual hosts in Apache HTTP server - August 24, 2015