LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

SSH

Connecting

Connect as a user on a remote host, optionally on a non-standard port, or run a single command and return.
$ ssh [user]@[host]
copy
$ ssh -p 2222 [user]@[host]
copy
$ ssh [user]@[host] [command]
copy
End the session with exit, logout, or Ctrl+d.First connection to a host? SSH shows its key fingerprint and stores it in ~/.ssh/known_hosts. A sudden "host key changed" warning later means the server was reinstalled, or someone is intercepting the connection.If a session hangs (network drop), type Enter ~ . to force-close it.

Keys & Passwordless Login

Generate a key pair; ed25519 is the modern default (use -t rsa -b 4096 only when a legacy server requires RSA). The private key stays on your machine, the public key goes to servers.
$ ssh-keygen -t ed25519
copy
$ ssh-keygen -t rsa -b 4096
copy
Install your public key on a server, then log in without a password.
$ ssh-copy-id [user]@[host]
copy
$ ssh -i [keyFile] [user]@[host]
copy
The agent keeps decrypted keys in memory, so a passphrase-protected key only has to be unlocked once per session.
$ ssh-add -l
copy
Always protect private keys with a passphrase, and keep ~/.ssh/id_ed25519 mode 600. The .pub file is the one that is safe to share.

Client Config

Connection settings live in ~/.ssh/config, one block per host. After that, ssh myserver replaces the whole user/host/port/key incantation, and tab completion picks up the alias.
$ Host myserver
    HostName server.example.com
    User admin
    Port 2222
    IdentityFile ~/.ssh/id_ed25519
copy
$ ssh myserver
copy

Transferring Files

scp copies files and directories (-r); rsync does the same but resumes and transfers only differences, which makes it better for anything large or repeated.
$ scp [localFile] [user]@[host]:[remotePath]
copy
$ scp [user]@[host]:[remoteFile] .
copy
$ scp -r [localDir] [user]@[host]:[remoteDir]
copy
$ rsync -avz [localDir]/ [user]@[host]:[remoteDir]/
copy
sftp gives an interactive session with get, put, ls, and cd; sshfs mounts a remote directory like a local disk.
$ sftp [user]@[host]
copy
$ sshfs [user]@[host]:[remoteDir] [localDir]
copy
$ fusermount -u [localDir]
copy

Port Forwarding

-L makes a remote service reachable locally: the example exposes the database running on the server's localhost:5432 at your own localhost:5432. -N opens the tunnel without starting a shell.
$ ssh -L 5432:localhost:5432 [user]@[host]
copy
$ ssh -N -L 8080:internal-host:80 [user]@[gateway]
copy
-R is the reverse: a port on the remote server forwards to your machine.
$ ssh -R 8080:localhost:3000 [user]@[host]
copy
-D turns the connection into a SOCKS5 proxy; point your browser at localhost:9999 to route its traffic through the server.
$ ssh -D 9999 [user]@[host]
copy

Jump Hosts & X11

Reach a machine that is only accessible through a bastion with -J, and run graphical programs over the connection with -X.
$ ssh -J [user]@[bastion] [user]@[internalHost]
copy
$ ssh -X [user]@[host]
copy

Keeping Sessions Alive

Send keep-alive packets so idle connections survive NAT timeouts; set it per host or globally in ~/.ssh/config.
$ Host *
    ServerAliveInterval 60
copy
For work that must survive a disconnect, run tmux on the server: reattach after reconnecting and your programs are still running (see the Tmux basics page).
Copied to clipboard
Kai