How to suspend SSH session in Linux

Last updated on July 23, 2020 by Dan Nanni

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.

user@remote_host:~$ ~^Z

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.

$ fg

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.

user@remote_host:~$ sudo vi /etc/ssh/sshd_config
ClientAliveInterval 60
ClientAliveCountMax 1
TCPKeepAlive yes

To restart SSH service:

user@remote_host:~$ sudo service ssh restart (for old Ubuntu or Debian)
user@remote_host:~$ sudo service sshd restart (for old CentOS, Fedora)
user@remote_host:~$ sudo systemd restart ssh (for systemd users on Debian-based system)
user@remote_host:~$ sudo systemd restart sshd (for systemd users on Red Hat based system)

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.

$ ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=1 user@remote_host

If you want to know all the escape sequences supported by OpenSSH, type ~ and ? characters back to back at the remote SSH shell prompt.

user@remote_host:~$ ~?
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.)

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean