Skip to main content

Secure Shell is a protocol used to securely log onto remote systems. It can be used for logging or executing commands on a remote server.

# To connect to a remote server:
ssh <username>@<remote_host>

# To connect to a remote server with a specific identity (private key):
ssh -i <path/to/key_file> <username>@<remote_host>

# To connect to a remote server using a specific port:
ssh <username>@<remote_host> -p <2222>

# To run a command on a remote server with a [t]ty allocation allowing interaction with the remote command:
ssh <username>@<remote_host> -t <command> <command_arguments>

# To sSH tunneling: Dynamic port forwarding (SOCKS proxy on 'localhost:1080):
ssh -D <1080> <username>@<remote_host>

# To sSH tunneling: Forward a specific port ('localhost:9999' to 'example.org:80') along with disabling pseudo-[T]ty allocation and executio[N] of remote commands:
ssh -L <9999>:<example.org>:<80> -N -T <username>@<remote_host>

# To sSH jumping: Connect through a jumphost to a remote server (Multiple jump hops may be specified separated by comma characters):
ssh -J <username>@<jump_host> <username>@<remote_host>

# To agent forwarding: Forward the authentication information to the remote machine (see 'man ssh_config' for available options):
ssh -A <username>@<remote_host>

##
# Extra
##

# Compare a remote file with a local file
ssh user@host cat /path/to/remotefile | diff /path/to/localfile -

# Copies directory_or_file_name on the local machine
# to /path/to/destination/directory_or_file_name on
# a remote machine.
tar -czf - directory_or_file_name | ssh username@hostname \
    "cd /path/to/destination; tar -xzf -"

# Copies the directory called directory_name from
# /path/to/source/directory_name on a remote server
# to the current directory on the local machine.
ssh username@hostname "cd /path/to/source; \
    tar -czf - directory_name" | tar -xzf -

# ssh sock5 self closing tunnel and open Safari
ssh -f -q -D 8080 user@host sleep 60 && open /Applications/Safari.app
ssh -f -q -D 8080 jl sleep 60 && open /Applications/Safari.app

# self closing ssh tunnel for afp file sharing
SSHSERVER=80.100.10.4
ssh -f -q -L 1548:localhost:548 $SSHSERVER sleep 60 && open afp://localhost:1548

# show Host key fingerprint as image
ssh -o VisualHostKey=yes user@host

# regenerate a public ssh-key using private ssh-key
ssh-keygen -y -f id_rsa > id_rsa.pub

# ssh socks5 proxy (set proxy switcher in chrome to 127.0.0.1 port 8080, socks5)
ssh -D 8080 user@server.com

# ===================================================================
# ssh g switch for sharing ssh tunnel on the lan as a socks 5 proxy =
# ===================================================================

ssh -g -D 8001 -p 443 username@sshserver.com

# -g = share ssh tunnel on lan
# -D 8001 = localport
# -p 443 = remote ssh port

# search for ssh process id, if you want to kill the connection
ps aux | grep "[s]sh -D"

# You can now connect to the ssh tunnel on the lan as a socks 5 proxy
# Change your network setting to connect to the ip address and port of
# the computer running the ssh tunnel
# socks proxy
# ip: 10.0.0.1
# port: 8001

# =======================
# Socks over ssh tunnel =
# =======================

# Logic: check if network location is proxy and switch if it isnt:
LOCATION=Proxy # Network location
IPADDRESS=80.100.10.4 # ip address of computer running ssh

# To switch network locations and open an ssh tunnel:
scselect $LOCATION && ssh -N -p 22 -C -c 3des -D 1080 $IPADDRESS