Hack 15. Toggle SSH Session using SSH Escape Character

When you’ve logged on to the remotehost using ssh from the localhost, you may want to come back to the localhost to perform some activity and go back to remote host again. In this case, you don’t need to disconnect the ssh session to the remote host. Instead, follow the steps below.

1. Login to remotehost from localhost:

localhost$ ssh -l jsmith remotehost

2. Now you are connected to the remotehost:

remotehost$

3. To come back to the localhost temporarily, type the escape character ~ and Control-Z.

When you type ~ you will not see that immediately on the screen until you press and press enter. So, on the remotehost in a new line enter the following key strokes for the below to work:

~

remotehost$ ~^Z
[1]+  Stopped  ssh -l jsmith remotehost

localhost$ 

4. Back to localhost

Now you are back to the localhost and the ssh remotehost client session runs as a typical UNIX background job, which you can check as shown below:

localhost$ jobs
[1]+  Stopped  ssh -l jsmith remotehost

5. Back to remotehost

You can go back to the remote host ssh without entering the password again by bringing the background ssh remotehost session job to foreground on the localhost.

localhost$ fg %1
ssh -l jsmith remotehost

remotehost$