How to suspend ssh session in Linux

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
user@remote_host:~$ sudo service ssh restart (Ubuntu or Debian)
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.

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

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.

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of Xmodulo.com. He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.
Your name can also be listed here. Write for us as a freelancer.

5 thoughts on “How to suspend ssh session in Linux

  1. I think this won't work in case you lose your connection momentarily.
    I used to use tmux once I start my ssh session and can detach it easily with ctrl+b d , then I can disconnect, reconnect and reattach the detached session with: tmux attach

  2. What's the point. Most Linux distros default to several running ttys at start-up. Just switch to another tty. Or, if you're on a graphical shell, start another terminal session. or, Use a windowed (text based or otherwise) terminal.

    So many simpler options.

  3. you can also multiplex ssh connections (via multiple ssh client processes ) providing the multiple ssh connections are to the same username/server. the end-effect is better use of available bandwidth :)

Leave a comment

Your email address will not be published. Required fields are marked *

Current ye@r *