Remote access: ssh
ssh opens a secure, encrypted shell on a remote machine.
Syntax
ssh user@host [-p port]ssh (Secure Shell) lets you log in to and run commands on another computer over an encrypted connection. It is how nearly everyone administers remote servers.
Connecting
The basic form is ssh user@host. Keys are safer than passwords: generate a key pair with ssh-keygen and copy the public key to the server with ssh-copy-id.
Example
# Log in to a remote server
ssh [email protected]
# Use a non-default port
ssh [email protected] -p 2222
# Set up key-based login (do this once)
ssh-keygen -t ed25519
ssh-copy-id [email protected]
# Run a single command remotely and return
ssh [email protected] 'uptime'When to use it
- A developer connects to a cloud VM with 'ssh -i ~/.ssh/deploy_key [email protected]' to deploy an application.
- A sysadmin uses SSH tunnelling with 'ssh -L 5432:db.internal:5432 jumphost' to securely access a database through a bastion server.
- A CI/CD pipeline uses SSH with a deploy key to pull code and restart services on a production server without a password.
More examples
Connect to a remote server
Shows the basic ssh connection forms: default user, explicit user@host, and a non-standard port with -p.
# Connect as the current user
ssh server.example.com
# Specify a different username
ssh [email protected]
# Connect on a non-default port
ssh -p 2222 [email protected]Use an SSH key for authentication
Covers generating an ed25519 key, copying it to the server with ssh-copy-id, and specifying a key with -i.
# Generate an SSH key pair
ssh-keygen -t ed25519 -C 'alice@laptop'
# Copy public key to the remote server
ssh-copy-id [email protected]
# Connect using a specific key file
ssh -i ~/.ssh/deploy_key [email protected]SSH local port forwarding
Creates a secure tunnel that forwards a local port through the SSH connection to reach an internal service.
# Tunnel local port 5432 -> remote postgres via jumphost
ssh -L 5432:db.internal:5432 [email protected]
# Now connect to the remote DB as if it were local
psql -h 127.0.0.1 -p 5432 -U admin mydb
Discussion