Escape Characters - Session Control Commands

What are Escape Characters

In the case that you have established SSH session between Client and Server all commands that you are pasting in to opened sessions are forwarded to Server. According to this you are not able to change the SSH Client application. For example in the cause that you session is hanging and you are not able to exit it as you cannot send any command to Server it is useful to use escape characters as you will be able to maintain open SSH session on client site.

In general SSH Client is waiting for Escape character “ ~ ” followed with the command.
To see all available escape possibilities run this command “ ~? “
Example:

# ~?
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.)

If you would like to change the escape character it is possible to do as well:

[root@DD2 ~]# ssh -e "%" root@10.0.12.111                    # With -e "%" I'm changing the escape character to " % "
root@10.0.12.111's password:
Last login: Sun Jan 12 17:51:46 2014 from 10.0.12.110
 [root@DD1 ~]# %?                                            # When I will ask for help I'll to use " %? "
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.)//

Suspended SSH session

Command: “~ ^Z
fg <ID>
bg

This will move the SSH Client application to Background. It mean that the Client will not replay to server and it is possible that the server will drop the session. On another hand it is useful when you need to run just short command on local server so you can suspend the session and start it again.
With command “ ~ ^Z “ you can suspend the SSH session on client site and with command “ fg <ID>“ you can start the session again.

Example:

[root@DD1 ~]# ~^Z [suspend ssh]                                # Suspending the sunning session
[1]+  Stopped                 ssh root@10.0.12.111             # Suspended session has ID 1

[root@DD2 ~]# bg                                               # Checking for the suspended process ID
[1]+ ssh root@10.0.12.111 &                                    # Suspended process ID is 1

[root@DD2 ~]# fg 1                                             # Restart suspended process with ID 1
ssh root@10.0.12.111

[root@DD1 ~]#                                                  # The connection was reestablished

Terminate SSH session

Command: “~ .

This command will immediately terminate / drop the session from Client site. It is useful command in the situation when the SSH session is hanging and you do not have another possibility to terminate it.

Example:

[root@DD1 ~]# ~.                                             # Sending the " ~. " to a SSH session
Connection to 10.0.12.111 closed.                            # Client is closing the SSH sesion

[root@DD2 ~]#                                                # We are back in Client's Shell

List forwarded connections

Command: “ ~#


In the cause that you are forwarding TCP ports it is possible that SSH Client will provide you all currently established / used TCP port forwarding. It will not provide any information about currently not in use TCP port forwarding session.

Example:

[root@DD2 ~]# ssh -R 3333:127.0.0.1:22 root@10.0.12.111                                      # Open SSH connection with TCP port Forwarding
root@10.0.12.111's password:
Last login: Sun Jan 12 17:21:39 2014 from 10.0.12.110

[root@DD1 ~]# ~#                                                                             # Check active TCP port forwarding sessions
The following connections are open:
  #0 client-session (t4 r2 i0/0 o0/0 fd 4/5 cfd -1)                                          # Active is just the main session

[root@DD1 ~]# netstat -nap | grep 3333                                                       # Check if the server is listening on 3333/TCP
tcp        0      0 127.0.0.1:3333              0.0.0.0:*           LISTEN      2120/sshd    # Yes it is
tcp        0      0 ::1:3333                    :::*                LISTEN      2120/sshd    # Yes it is
-------------------------------------------------------------------------------------------------------------------------------------------
In Parallel Console of server DD1:

       [root@DD1 ~]# ssh -p 3333 root@127.0.0.1                                              # Open active connection to TCP fowarding port
       root@127.0.0.1's password:
       Last login: Sun Jan 12 17:23:06 2014 from localhost
       [root@DD2 ~]#
-------------------------------------------------------------------------------------------------------------------------------------------

[root@DD1 ~]# ~#                                                                             # Check active TCP port forwarding sessions
The following connections are open:
  #0 client-session (t4 r2 i0/0 o0/0 fd 4/5 cfd -1)                                          # Active is the main session
  #1 127.0.0.1 (t4 r3 i0/0 o0/0 fd 7/7 cfd -1)                                               # Active is as well the TCP Forwarding session

[root@DD1 ~]# netstat -nap | grep 3333                                                       #Check all 3333/TCP sessions on server
tcp        0      0 127.0.0.1:3333              0.0.0.0:*           LISTEN      2120/sshd
tcp        0      0 127.0.0.1:47893             127.0.0.1:3333      ESTABLISHED 2143/ssh     # This is our open session
tcp        0      0 127.0.0.1:3333              127.0.0.1:47893     ESTABLISHED 2120/sshd    # This is our open session
tcp        0      0 ::1:3333                    :::*                LISTEN      2120/sshd

Background ssh (when waiting for connections to terminate)

Command: “~&

This command will suspend the active session like “ ~^Z ” but until there is not any TCP port forwarding active it will immediately close the session like command “ ~. “ . In the case that we have still open TCP port forwarding session this will wait until the TCP forwarding session will finish and then the SSH connection will be closed.

Example:

[root@DD2 ~]# ssh -R 3333:127.0.0.1:22 root@10.0.12.111                    # Open SSH connection with TCP port forwarding
root@10.0.12.111's password:
Last login: Sun Jan 12 17:24:13 2014 from 10.0.12.110
-----------------------------------------------------------------------------------------------------------------------------------
In Parallel Console of server DD1:

       [root@DD1 ~]# ssh -p 3333 root@127.0.0.1                            # Open active connection to TCP fowarding port
       root@127.0.0.1's password:
       Last login: Sun Jan 12 17:23:06 2014 from localhost
       [root@DD2 ~]#
-----------------------------------------------------------------------------------------------------------------------------------
[root@DD1 ~]# ~&                                                           # Suspend to background the SSH connection
[1] 2223
-bash: /root: is a directory
[root@DD1 ~]# bg
-bash: bg: job has terminated
[1]+  Exit 126                ~                                            # The SSH connection has finished with error
[root@DD1 ~]# bg                                                           # Just after closing of TCP forwarding session
-bash: bg: current: no such job

Request rekey (SSH protocol 2 only)

Command: “ ~R
This command will force to generate new key used for this SSH connection.

This is possible only with SSH version 2.

Navigation
Print/export
QR Code
QR Code wiki:infrastructure_tools:ssh:ssh-escape-characters-session-control-commands (generated for current page)