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.
When I boot into ssh now I only type in ssh tilde into the terminal:
Host tilde Hostname tilde.town User USERNAME IdentityFile ~/.ssh/tilde.town
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.
#!/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
#!/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)"
This should be the same, or similar, on other IRC clients.
$ ./ssh_tunnel.sh
HexChat -> Network List
localhost/6667
$ ./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.
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 "$$")