ssh

A collected notes page of my gathered ssh knowledge or things you might need to get started with ssh. Always remember to backup your ssh keys folder on multiple services/devices. If you lose your keys you might not be able to regain access.

Shortcuts

When I boot into ssh now I only type in ssh tilde into the terminal:

  1. Open ~/.ssh/config on your machine
  2. Paste and edit in the following:
Host tilde
Hostname tilde.town
User USERNAME
IdentityFile ~/.ssh/tilde.town
  1. Host - What you type in after ssh e.g "ssh tilde"
  2. IdentityFile - File location of your private ssh key for tilde.town
  3. User - Your tilde.town username

Tunnelling

I found out you can tunnel to tilde.town's IRC client via your own personal IRC whilst making it look like you're connecting from localhost.

These shell scripts manage an SSH tunnel to the IRC client on tilde.town, allowing you to connect as if it's running locally. The start_ssh_tunnel.sh script checks for an existing tunnel, ensures the necessary port is available, and then initiates the SSH tunnel, recording its process ID. The kill_ssh_tunnel.sh script safely terminates the SSH tunnel by referencing the stored process ID, ensuring no stale processes are left running. Note: I do have Powershell versions of these scripts as well.

./start_ssh_tunnel.sh

#!/bin/bash
        
        # Function to check if the SSH tunnel is already running
        is_tunnel_running() {
            if [ -e ssh_pid ]; then
                SSH_PID=$(cat ssh_pid)
                if ps -p $SSH_PID > /dev/null; then
                    echo "SSH tunnel is already running with PID: $SSH_PID"
                    exit 1
                else
                    echo "Found stale PID file. Cleaning up."
                    rm -f ssh_pid
                fi
            fi
        }
        
        # Function to check if the port is in use
        is_port_in_use() {
            if netstat -an | grep -q ":6667"; then
                echo "Port 6667 is already in use."
                exit 1
            fi
        }
        
        # Log the start time
        echo "Starting SSH tunnel: $(date)"
        
        # Check if the tunnel is already running
        is_tunnel_running
        
        # Check if the port is in use
        is_port_in_use
        
        # Start SSH tunnel and log output to the console
        # Use the correct path to the identity file
        ssh -N -i ~/ssh/tilde.town USERNAME@tilde.town -L localhost:6667:localhost:6667 &
        SSH_PID=$!
        sleep 1
        
        # Check if the SSH process is still running after a short delay
        if ps -p $SSH_PID > /dev/null; then
            echo $SSH_PID > ssh_pid
            echo "SSH tunnel started with PID: $SSH_PID"
        else
            echo "Failed to start SSH tunnel."
        fi
        

./kill_ssh_tunnel.sh

#!/bin/bash
        
        # Log the action of stopping the SSH tunnel
        echo "Stopping SSH tunnel: $(date)"
        
        # Check if the ssh_pid file exists
        if [ -e ssh_pid ]; then
            # Read the PID from the file
            SSH_PID=$(cat ssh_pid)
            
            # Kill the SSH process
            kill $SSH_PID
            
            # Check if the kill command was successful
            if [ $? -eq 0 ]; then
                echo "SSH tunnel (PID $SSH_PID) terminated successfully."
            else
                echo "Failed to terminate SSH tunnel (PID $SSH_PID)."
            fi
        
            # Remove the PID file
            rm -f ssh_pid
        else
            echo "No ssh_pid file found. SSH tunnel may not be running."
        fi
        
        # Log the completion of the action
        echo "Finished stopping SSH tunnel: $(date)"
        

Setting Up HexChat to use the tunnel

This should be the same, or similar, on other IRC clients.

  1. Start the SSH tunnel from your machine by running the start_ssh_tunnel.sh script:

    $ ./ssh_tunnel.sh

  2. Open HexChat and go to Network List:

    HexChat -> Network List

  3. Click on Add to create a new network and name it, for example, "tilde.town".
  4. With "tilde.town" selected, click on Edit:
  5. In the Edit window, add a new server with the address:

    localhost/6667

  6. Ensure the "Use SSL for all servers on this network" option is unchecked.
  7. Click Close to save the settings.
  8. Select the "tilde.town" network from the network list and click connect to join the IRC server through the SSH tunnel.
  9. To stop the SSH tunnel, run the ./kill_ssh_tunnel.sh script:

    $ ./kill_ssh_tunnel.sh

If all goes well you should be connected to tilde.town via localhost eg. username@localhost. These scripts are to be run on your local machine not on the tilde server itself.

Killing Unused Connections to the Town

I'm one to always forget to use the Logout/Quit command when closing a SSH session from my terminal and this ends up resulting in processes being used up on the server. This will kill all processes.

kill $(ps -u USERNAME | grep -E 'pts/|sshd|mosh-server|tmux' | awk '{print $1}' | grep -v "$$")

Back