Updated: 10 May 2025
Generate a key pair
ssh-keygen -t rsa
Copy ssh key to clipboard
cat ~/.ssh/id_rsa.pub | xclip -sel clip
Freelance software engineer United Kingdom
Updated: 10 May 2025
Generate a key pair
ssh-keygen -t rsa
Copy ssh key to clipboard
cat ~/.ssh/id_rsa.pub | xclip -sel clip
Updated: 10 May 2025
Restart the ssh deamon
sudo systemctl restart sshd
Updated: 14 April 2025
Mount directory on remote server over ssh
sshfs user@example.com:/remote/folder/path /local/folder/path \
-o IdentityFile=/full/path/to/private/key/file \
-o allow_other
Unmount directory mounted with sshfs above
fusermount -u PATH
Updated: 04 April 2024
scp — OpenSSH secure file copy.
Copy a file from B to A while logged into B
scp /path/to/file username@a:/path/to/destination
Copy a file from B to A while logged into A
scp username@b:/path/to/file /path/to/destination
Copy a directory and the contents
scp -r username@the_host:/etc/nginx /home/chris/nginx-conf-backup
Updated: 10 May 2025
Prefer password authentication over public key
ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no user@example.com
Use one specific private key
ssh -o "IdentitiesOnly=yes" -i /home/me/path/to/private/key/id_rsa user@host
Use a non-standard port
ssh user@192.168.1.1 -p 26
Kill a ssh session
ps -A | grep ssh
kill PID # the process id from previous command
Kill a ssh session
ps -aux | grep ssh
kill PID # the process id from previous command
# -D: Tells SSH that we want a SOCKS tunnel on the specified port number
# -f: Forks the process to the background
# -C: Compresses the data before sending it
# -q: Uses quiet mode
# -N: Tells SSH that no command will be sent once the tunnel is up
ssh -D 8123 -f -C -q -N user@example.com
Verify that the tunnel is up and running
ps aux | grep ssh
If using -f, close the tunnel when finished
kill <process id>
-R |
Specifies that connections to the given TCP port or Unix socket on the remote (server) host are to be forwarded to the local side. |
-L |
Specifies that connections to the given TCP port or Unix socket on the local (client) host are to be forwarded to the given host and port, or Unix socket, on the remote side. |
See What’s the difference between ssh local and remote port forwarding
See https://iximiuz.com/ssh-tunnels/ssh-tunnels.png
Step 1. Forward traffic connecting to port 8000 on your local machine to port 8123 on the remote machine
ssh -L 127.0.0.1:8000:127.0.0.1:8123 the_user@remote_machine
Step 2. On the remote machine, start a python http server which is listening for connections on port 8123 of it’s own localhost
python3 -m http.server 8123
Step 3. On the local machine, visit http://127.0.0.1:8000 and you should see the directory listing for the remote directory where the python http server was started.