Working with SSH: the basics

SSH is how you talk to a rented server: an encrypted terminal, a file channel and a tunnel in one. Twenty minutes with these basics pays off for years.

What is SSH?

Secure Shell is a protocol that gives you a command line on a remote machine over an encrypted connection. Your server runs the OpenSSH service on TCP port 22 from the moment it is delivered. Your computer runs a client that connects to it, proves who you are with a key or password, and then carries your keystrokes and the server's output in both directions. Everything, including the login, is encrypted.

How do I connect?

Open a terminal and type ssh followed by the user and the server address. macOS and Linux have the client built in; Windows 10 and 11 include it in PowerShell and Windows Terminal. PuTTY remains an option on Windows if you prefer a separate program.

ssh root@192.0.2.40
ssh -p 2222 deploy@srv1.example.net     # when SSH listens on another port

The very first time, your client prints a fingerprint of the server's host key and waits for you to approve it. Compare it with the fingerprint printed by ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub on the server if you can; once accepted, the client remembers it in ~/.ssh/known_hosts and warns loudly if it ever changes.

Should I use a password or a key?

A key. Passwords can be guessed, and every public server sees guessing attempts within minutes of going online. A key pair cannot practically be guessed, and an agent on your computer means you unlock it once per session. The Create SSH keys guide shows how to make one and add it to your account so new servers arrive ready for it.

Can I run a single command without opening a shell?

Yes: put the command after the address. The output comes back to your terminal and the connection closes. This is handy for quick checks and for scripts that look after several machines.

ssh admin@192.0.2.40 uptime
ssh admin@192.0.2.40 'df -h / && free -m'
for h in fra1 waw1 bhs1; do ssh "$h" 'hostname; cat /proc/loadavg'; done

How do I keep a long job running if my connection drops?

Start it inside tmux or screen, which keep running on the server when you disconnect. Reattach later from anywhere and the job, its output and your scrollback are still there.

tmux new -s upgrade      # start a named session
# run the long job, then press Ctrl+b followed by d to detach
tmux attach -t upgrade   # reconnect later

To stop idle connections from being cut by home routers, add ServerAliveInterval 30 to your client configuration.

What is a client config file?

A file at ~/.ssh/config that stores the details of each server under a short name. After that, ssh fra1 replaces a long command, and scp, rsync and sftp understand the same names.

Host fra1
    HostName 192.0.2.40
    User admin
    IdentityFile ~/.ssh/work_ed25519
    ServerAliveInterval 30

Host waw1
    HostName 203.0.113.17
    User admin
    ProxyJump fra1

ProxyJump connects to waw1 through fra1, which is useful when the second server only accepts SSH from the first.

How do I reach a service that only listens on localhost?

Forward a port through the SSH connection. A database or admin panel bound to 127.0.0.1 on the server stays invisible to the internet, while you reach it on your own computer through the encrypted tunnel.

ssh -N -L 15432:127.0.0.1:5432 fra1     # PostgreSQL on the server, port 15432 on your laptop
ssh -N -L 8080:127.0.0.1:9090 fra1      # a web dashboard bound to localhost

While the command runs, point your local tool at localhost:15432. This pattern keeps databases and panels off the public port entirely, which the Hardening a new dedicated server article recommends.

How do I copy files?

Use scp for single files, sftp for browsing, and rsync for folders or large transfers. All three run over the same SSH connection and use the same keys and host names from your config file. Transfer files with SFTP and Copy data between servers with rsync cover each in detail.

TaskCommand
Upload one filescp report.pdf fra1:/srv/files/
Download a folderrsync -a fra1:/var/log/nginx/ ./nginx-logs/
Browse interactivelysftp fra1

What should I do next?

Turn off password logins once your key works, create a personal user with sudo instead of working as root, and enable a firewall. The First steps with a new server guide walks through those changes in order, with commands for every system we install.

Related pages

Create SSH keys

Generate an Ed25519 key pair on Windows, macOS or Linux, add it to your account before ordering, and manage keys on a running server.

Read more

First steps with a new server

The first hour after delivery: log in, check the hardware matches the order, update, add a user, lock down SSH and set a firewall.

Read more

Transfer files with SFTP

Upload and download files over SSH with sftp, scp or a graphical client, and give outside users a locked upload folder.

Read more

Hardening a new dedicated server

The first hour on a fresh machine: SSH keys, a firewall, automatic updates, brute force blocking and a habit of checking what listens.

Read more

Questions and answers

Which port does SSH use?

TCP port 22 by default. You can move it in the server configuration, then connect with ssh -p and the new port.

Do I need PuTTY on Windows?

No. Windows 10 and 11 include the OpenSSH client in PowerShell and Windows Terminal. PuTTY is optional.

What is the host key warning on first connection?

The client shows the server fingerprint so you can confirm you reached the right machine. It is stored and checked on every later connection.

How do I keep a job running after I disconnect?

Run it inside tmux or screen. Detach, close the connection and reattach later to the same session.

How can I reach a database that listens only on localhost?

Use SSH local port forwarding, for example ssh -N -L 15432:127.0.0.1:5432 with your server, then connect to localhost:15432.