ssh
Connect to a remote machine as a user, with flags for the port, the key, and tunnels between the two machines.
ssh <user>@<host>Angle brackets are yours to fill in. Windows, Linux, macOS.
Examples for ssh
Generate an SSH key
ssh-keygen -t ed25519 -C "<email>"Makes a key pair in ~/.ssh: id_ed25519 is private and stays on this machine, id_ed25519.pub is the one you give to GitHub or a server. Give it a passphrase. The -C comment only labels the key.
ssh-keygentool-t ed25519the key type-C "<email>"a labelLog in without a password
ssh-copy-id <user>@<host>Adds your public key to ~/.ssh/authorized_keys on the server, asking for the password one last time. Windows has no ssh-copy-id: append the contents of the .pub file to that server file yourself.
ssh-copy-idtool<user>@<host>argumentConnect on a different port
ssh -p <port> <user>@<host>Lowercase -p for ssh. scp uses uppercase -P for the same thing.
sshtool-p <port>flag with value<user>@<host>argumentConnect with a specific key file
ssh -i <key-file> <user>@<host>Common for cloud servers that give you a .pem file. ssh refuses a key that other users can read: run chmod 600 on it first.
sshtool-i <key-file>the private key<user>@<host>argumentRun one command on a remote server
ssh <user>@<host> "<command>"Runs the command, prints its output here, and disconnects. Quote it, so that pipes and variables are run on the server and not by your shell.
sshtool<user>@<host>argument"<command>"runs on the serverReach a remote database through an SSH tunnel
ssh -N -L 5432:localhost:5432 <user>@<host>Connections to port 5432 on your machine go through the server to its own port 5432. localhost here means the server, not you. -N keeps the tunnel open without a shell; stop it with Ctrl+C.
sshtool-Ntunnel only-L 5432:localhost:5432your port : target as the server sees it<user>@<host>argumentConnect through a bastion host
ssh -J <user>@<bastion> <user>@<host>ssh connects to the bastion, then from there to the target. Your keys stay on your machine, which makes it safer than agent forwarding.
sshtool-J <user>@<bastion>go through this first<user>@<host>argumentSee why an SSH connection fails
ssh -v <user>@<host>Shows which keys ssh offers and which the server accepts or refuses. Permission denied (publickey) usually means the server does not have this key, or the key file permissions are too open.
sshtool-vverbose<user>@<host>argument
Flags
- -p <port>
- Connect to a port other than 22.
- -i <key-file>
- Use this private key instead of the default ones in ~/.ssh.
- -L <local-port>:<host>:<port>
- Forward a port on your machine through the server to <host>:<port>.
- -R <remote-port>:<host>:<port>
- Forward a port on the server back to <host>:<port> as seen from your machine.
- -J <jump-host>
- Connect through another server first, such as a bastion.
- -N
- Do not run a command or open a shell. Use it with -L or -R for a tunnel only.
- -v
- Print each step of the connection and the login. Add more v's for more detail.
- -A
- Forward your SSH agent, so the server can use your keys to connect onward.Anyone with root on that server can use your keys while you are connected. Prefer -J.