Connecting
Connect to a server (default port 22)
$ ssh [email protected]Connect on a specific port
$ ssh [email protected] -p 6222Connect via pem file (0400 permissions)
$ ssh -i /path/file.pem [email protected]See: SSH Permissions
This quick reference cheat sheet provides various for using SSH.
Connect to a server (default port 22)
$ ssh [email protected]Connect on a specific port
$ ssh [email protected] -p 6222Connect via pem file (0400 permissions)
$ ssh -i /path/file.pem [email protected]See: SSH Permissions
Executes remote command
$ ssh [email protected] 'ls -l'Invoke a local script
$ ssh [email protected] bash < script.shCompresses and downloads from a server
$ ssh [email protected] "tar cvzf - ~/source" > output.tgzCheck this machine's public IP
$ curl myip.dogCheck a remote server's public IP (requires curl on the server)
$ ssh user@server 'curl myip.dog'More IP tools: myip.dog
Copies from remote to local
$ scp user@server:/dir/file.ext dest/Copies between two servers
$ scp user@server:/file user@server:/dirCopies from local to remote
$ scp dest/file.ext user@server:/dirCopies a whole folder
$ scp -r user@server:/dir dest/Copies all files from a folder
$ scp user@server:/dir/* dest/Copies from a server folder to the current folder
$ scp user@server:/dir/* .| File Path | Description |
|---|---|
/etc/ssh/ssh_config | System-wide config |
~/.ssh/config | User-specific config |
~/.ssh/id_{type} | Private key |
~/.ssh/id_{type}.pub | Public key |
~/.ssh/known_hosts | Known Servers |
~/.ssh/authorized_keys | Authorized login key |
| Options | Description |
|---|---|
scp -r | Recursively copy entire directories |
scp -C | Compresses data |
scp -v | Prints verbose info |
scp -P 8080 | Uses a specific Port |
scp -B | Batch mode (Prevents password) |
scp -p | Preserves times and modes |
Host server1
HostName 192.168.1.5
User root
Port 22
IdentityFile ~/.ssh/server1.keyLaunch by alias
$ ssh server1See: Full Config Options
$ ssh -J proxy_host1 remote_host2$ ssh -J user@proxy_host1 user@remote_host2Multiple jumps
$ ssh -J user@proxy_host1:port1,user@proxy_host2:port2 user@remote_host3$ ssh-copy-id user@serverCopy to alias server
$ ssh-copy-id server1Copy specific key
$ ssh-copy-id -i ~/.ssh/id_rsa.pub user@server$ ssh-keygen -t rsa -b 4096 -C "[email protected]"-t | Type of key | |
-b | The number of bits in the key | |
-C | Provides a new comment |
Generate an RSA 4096 bit key with email as a comment, or use SSH Key Generator online
Generate a key interactively
$ ssh-keygenSpecify filename
$ ssh-keygen -f ~/.ssh/filenameGenerate public key from private key
$ ssh-keygen -y -f private.key > public.pubChange comment
$ ssh-keygen -c -f ~/.ssh/id_rsaChange private key passphrase
$ ssh-keygen -p -f ~/.ssh/id_rsarsa
ed25519
dsa
ecdsa
Search from known_hosts
$ ssh-keygen -F <ip/hostname>Remove from known_hosts
$ ssh-keygen -R <ip/hostname>PEM
PKCS8
| Option | Description |
|---|---|
-L | Local port forwarding |
-R | Remote port forwarding |
-D | Dynamic port forwarding |
-f | Run in background |
-N | Do not execute a remote command |
-g | Allow remote hosts to connect to local forwarded ports |
more details on flag above with man ssh
# Forward a local port to a remote server
ssh -L local_port:remote_host:remote_port user@ssh_server
# Example: Forward local port 8080 to remote port 80 on example.com
ssh -L 8080:example.com:80 user@ssh_server# Create a SOCKS proxy on a local port
ssh -D local_port user@ssh_server
# Create a SOCKS proxy on local port 1080 in the background
ssh -f -N -D 1080 user@ssh_server# Create a local port forwarding tunnel in the background
ssh -f -N -L local_port:remote_host:remote_port user@ssh_server# Create multiple tunnels in a single SSH command
ssh -L 8080:example.com:80 -L 3306:db.example.com:3306 user@ssh_server# In background, no command, allow remote host, local port forward, identity_file
ssh -f -N -g -L 8080:example.com:80 -i ~/.ssh/custom_key user@ssh_serverOpenSSH Config File Examples (cyberciti.biz)
ssh_config (linux.die.net)
Adapted from Fechin/referenceGPL-3.0 licenseMarkdown sourceChanges and attribution