Index

Table of contents

SSH

SSH connections keep alive:
sudo vi /etc/ssh/ssh_config
ServerAliveInterval 60
connect to remote server
ssh [user]@example.com
connect on a different port
ssh -p 62775 [user]@example.com
invoke a script located on the remote server
ssh user@server '/path/to/script.sh'
invoke a local script on the remote server
ssh user@server bash < [script]
execute remote command
ssh user@server "sudo /opt/scripts/doSomething.sh magic"

tunneling / proxying

use gateway server to ssh into another server
ssh -J [gateway] [destination]
use ssh connection as a socks proxy
ssh -D [local_port] [user]@[host]
use ssh connection as a socks proxy no shell
ssh -N -D [local_port] [user]@[host]
forward local port to remote port on remote host through gateway
ssh -L [local_port]:[destination_host]:[detination_port] [gateway_user]@[gateway_host]
forward local port to remote port on remote host through gateway without shell
ssh -N -L [local_port]:[destination_host]:[detination_port] [gateway_user]@[gateway_host]
map local port directly onto a port on the destination
ssh -L [local_port]:localhost:[destination_port] [destination_user]@[destination_host]
example: map local port 8888 to port 80 on example.com through proxy
ssh -L 8888:example.com:80 [user]@[host]
firefox localhost:8888
example: map port 8888 to ssh into [target] through [gateway]
ssh -L 8888:[target_host]:22 [gateway_user]@[gateway_host]
ssh -p 8888 [target_user]@localhost
expose a local port to a remote server through ssh
ssh -R [remote_port]:[local_address]:[local_port] [gateway_user]@[gateway_host]
tunneling tutorial
https://www.howtogeek.com/168145/how-to-use-ssh-tunneling/

ssh keys

generate private and public key
ssh-keygen
copy local ssh key to server for key based authentication
ssh-copy-id [user]@[host]
or manually paste public key into remote config file
vi ~/.ssh/authorized_keys

secure copy (scp)

copy file to remote
scp /tmp/file.zip yourdomain.com:/path/to/file.zip
preserve last modified time
scp -p [from] [to]
copy remote directory
scp -r [user]@example.com:/data/upload /data/
connect to a different port
scp -P 62775 [from] [to]
download a file from host [target] tunnelling the call through host [gateway]
ssh -L [port]:[target]:22 [gateway_user]:[gateway]
scp -P [port] [target_user]@[gateway]:[file] .

mounting remote filesystems

mounting a remote directory onto the local filesystem
sshfs [user]@[host]:[remote_path] [mountpoint]
unmounting the remote directory
umount [mountpoint]

sshuttle - poor mans VPN over SSH

route all traffic over an ssh connection
sshuttle -r [user]@[host] 0/0

hostfile

config file location
~/.ssh/config
sample entry
Host [alias]
	HostName example.com
	User [user]
	IdentityFile ~/.ssh/[key]
	Port 22
	ForwardX11 no
	ForwardAgent no
	ProxyJump 10.10.10.10
launching an entry from this file
ssh [alias]
multiple aliases for one entry
Host [alias1] [alias2] ...
show launch menu containing all aliases
rofi -show ssh
more examples
https://www.cyberciti.biz/faq/create-ssh-config-file-on-linux-unix/
man page
https://linux.die.net/man/5/ssh_config

running ssh commands on multiple hosts at once (untested & needs arguments)

parallel-ssh