Skip to content

Instantly share code, notes, and snippets.

@palaueb
Last active September 4, 2024 19:53
Show Gist options
  • Save palaueb/1514dbca34d94325e1449b2208ff71c8 to your computer and use it in GitHub Desktop.
Save palaueb/1514dbca34d94325e1449b2208ff71c8 to your computer and use it in GitHub Desktop.

Revisions

  1. palaueb renamed this gist Sep 4, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. palaueb created this gist Sep 4, 2024.
    57 changes: 57 additions & 0 deletions ssh-close-stuck-session
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    ## Managing SSH Sessions: Essential Commands and Tips

    ### How to Safely Exit a Stuck SSH Session

    #### Step 1: Press `Enter`
    - **Description**: Ensures you are at the beginning of a new line, necessary for the commands to be recognized.
    - **Usage**: Hit the `Enter` key.

    #### Step 2: Type `~`
    - **Description**: Uses the tilde key as a special escape sequence initiator for SSH.
    - **Usage**: Type the tilde key `~`.

    #### Step 3: Type `.`
    - **Description**: Completes the escape sequence to terminate the connection.
    - **Usage**: Type the dot `.`.

    ### Basic SSH Client Commands

    #### **Terminate the session: `~.`**
    - **Description**: Close the current SSH connection, especially useful when the session is stuck or unresponsive.
    - **Usage**: Simply type `~.` on a new line in your SSH session.

    #### **Suspend the SSH session: `~^Z` or `~CTRL+Z`**
    - **Description**: Suspends your SSH session, allowing you to return to your local shell without disconnecting.
    - **Usage**: Type `~^Z` or `~CTRL+Z`. Use `fg` to resume the session.

    #### **Send the session to the background: `~&`**
    - **Description**: Sends the current SSH session to the background immediately, combining the steps of suspending and then backgrounding the session.
    - **Usage**: Type `~&` after suspending the session.

    #### **Display the help menu: `~?`**
    - **Description**: Displays a list of all supported escape sequences, providing a quick reference.
    - **Usage**: Type `~?` to see the help menu.

    #### **Check the connection status: `~#`**
    - **Description**: Shows information about the current SSH tunnel and forwarded connections, useful for debugging or checking the status of port forwarding.
    - **Usage**: Type `~#` during your SSH session.

    #### **Forward a port: `~C`**
    - **Description**: Opens a command line to add port forwarding rules dynamically during an active SSH session.
    - **Usage**: Type `~C` and then enter the port forwarding rule.

    ### Advanced Usage

    #### **Escape character for literal `~`: `~~`**
    - **Description**: Allows you to send a literal `~` at the beginning of a line, useful when `~` is part of a command or data.
    - **Usage**: Type `~~` when needed.

    #### **Verbose mode**
    - **Description**: Provides verbose output with detailed debug messages about the connection setup and process, invaluable for troubleshooting.
    - **Usage**: Start SSH with the `-v` flag, e.g., `ssh -v user@host`.

    ### Tips for Effective SSH Use

    - **Customizing SSH configurations**: Create custom configurations for each host in the `~/.ssh/config` file, including usernames, identities, port numbers, and more.
    - **Using key-based authentication**: Enhance security and convenience by using SSH keys instead of passwords, using public-private key cryptography for authentication.
    - **Multiplexing connections**: Utilize SSH's support for connection multiplexing to speed up subsequent connections by reusing a single connection.