Most SSH tutorials treat keys, port forwarding and file transfer as three separate topics. On a real server they are one workflow. You log in once with a password or a key the provider injected, you switch to key-only login, and from then on the same key and the same ~/.ssh/config entry carry your shell, your database tunnel and every file you copy.

This post walks that workflow in the order you actually do it on a fresh Linux VPS. Each section links to a longer guide if you want the full detail.

Throughout, 203.0.113.10 is the server and deploy is a normal sudo user. Use these commands only on servers you own or manage.

1. First login: check the host key before you type "yes"

ssh root@203.0.113.10 

The first connection shows a prompt like ED25519 key fingerprint is SHA256:.... Most people type yes without reading it. That's the one moment SSH can't protect you, because there is nothing in known_hosts to compare against yet.

If your provider gives you a web console that doesn't go through SSH, run this there and compare the two SHA256: strings:

ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub 

If they match, accept. If you rebuild the server later and SSH starts shouting REMOTE HOST IDENTIFICATION HAS CHANGED, check the new fingerprint the same way, then remove the old entry with ssh-keygen -R 203.0.113.10 (or ssh-keygen -R '[203.0.113.10]:2222' on a custom port). Don't make the warning go away with StrictHostKeyChecking no. That turns off the check entirely.

Our step-by-step guide to SSH into a server covers PuTTY, Windows Terminal and the common first-login errors.

2. Keys, then key-only

Generate one key per device

ssh-keygen -t ed25519 -C "laptop-2026" 

Set a passphrase. The private key then sits encrypted on disk, and ssh-agent means you type the passphrase once per session, not once per connection. If ssh-keygen asks whether to overwrite an existing file, answer n and pass -f ~/.ssh/id_ed25519_new instead. Overwriting locks you out of every server that trusts the old key.

Use one key per laptop rather than copying one private key around. If a machine is lost, you delete one line from authorized_keys and nothing else changes.

Install it

# Linux / macOS ssh-copy-id -i ~/.ssh/id_ed25519.pub deploy@203.0.113.10 

Windows ships the OpenSSH client but not ssh-copy-id, so pipe the key from PowerShell:

Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub | ssh deploy@203.0.113.10 "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys" 

If key login still fails, it's almost always permissions or ownership. ~/.ssh must be 700, authorized_keys must be 600, both must be owned by the user, and the home directory must not be group- or world-writable. ssh -v shows which keys the client offered. On the server, sudo journalctl -u ssh usually names the exact file sshd didn't like (the unit is sshd on RHEL-family systems).

Switch passwords off, safely

Keep your current session open. From a second terminal, confirm that key login works. Then create a drop-in file:

sudo tee /etc/ssh/sshd_config.d/00-keys-only.conf >/dev/null <<'EOF' PasswordAuthentication no KbdInteractiveAuthentication no EOF sudo sshd -t && sudo systemctl restart ssh 

The 00- prefix matters. sshd uses the first value it reads for each setting, and Ubuntu's sshd_config includes sshd_config.d/*.conf at the top. Some cloud images ship a 50-cloud-init.conf with PasswordAuthentication yes, and your 00- file is read before it. On OpenSSH older than 8.7, use ChallengeResponseAuthentication no in place of the second line.

Now prove it from the outside instead of trusting the config file:

# on the server: effective settings sudo sshd -T | grep -Ei '^(passwordauthentication|kbdinteractiveauthentication)' # from your laptop: force a password attempt ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password,keyboard-interactive deploy@203.0.113.10 

The first command should print two lines ending in no. The second should fail immediately with Permission denied (publickey). If it prompts for a password, something is overriding your file.

A note for Ubuntu 24.04: sshd is started through ssh.socket, which owns the listening port. Auth changes like the ones above apply with a normal restart. If you also change Port or ListenAddress, though, you need sudo systemctl daemon-reload and then sudo systemctl restart ssh.socket, or the old port keeps listening. Check with ss -tlnp. If you get locked out after a port change, our SSH connection refused guide works through each cause.

The full post-login hardening order (sudo user, root login, firewall) is in our Ubuntu VPS initial setup guide. For key types, agents on each OS and GitHub, see the SSH keys guide.

3. One ~/.ssh/config that makes everything one word

This is the part most people skip, and it is what makes tunnels and SFTP painless. Every OpenSSH tool (ssh, scp, sftp, and rsync through ssh) reads this file.

# Specific hosts first: for most options, the first value found wins Host web1 HostName 203.0.113.10 User deploy IdentityFile ~/.ssh/id_ed25519 IdentitiesOnly yes # Same server, but only for the database tunnel Host web1-db HostName 203.0.113.10 User deploy IdentityFile ~/.ssh/id_ed25519 IdentitiesOnly yes LocalForward 13306 127.0.0.1:3306 LocalForward 15432 127.0.0.1:5432 ExitOnForwardFailure yes SessionType none # A server with no public SSH, reached through web1 Host worker1 HostName 10.0.0.5 User deploy ProxyJump web1 # Defaults last Host * ServerAliveInterval 30 ServerAliveCountMax 3 AddKeysToAgent yes 

What each non-obvious line does:

  • IdentitiesOnly yes makes the client offer only the key you named. Without it, a client with six keys in the agent can hit the server's MaxAuthTries limit and fail with Too many authentication failures before it reaches the right key.
  • SessionType none is the config version of -N: no shell, forwarding only. It needs OpenSSH 8.7 or later on the client. On older clients, drop the line and run ssh -N web1-db.
  • ExitOnForwardFailure yes makes ssh exit with an error if port 13306 is already taken, instead of connecting with no working tunnel.
  • ProxyJump web1 makes ssh worker1 hop through web1. The connection to worker1 is end-to-end encrypted, and your private key never sits on web1.
  • ServerAliveInterval 30 sends a keepalive through the encrypted channel. After three missed replies, the client gives up instead of hanging on a dead connection.

Run ssh -G web1-db to print the fully resolved settings for a host. It's the fastest way to debug "why is it using the wrong key?".

With that file in place:

ssh web1 # shell ssh web1-db # both database tunnels, no shell sftp web1 # file session scp backup.sql web1:~/ # one-off copy rsync -avz site/ web1:/var/www/site/ ssh worker1 # via the jump host 

Optional: connection sharing

On Linux and macOS, add this to the Host * block to make repeated scp/rsync/ssh calls start almost instantly. They ride on one already-authenticated connection.

 ControlMaster auto ControlPath ~/.ssh/cm-%C ControlPersist 10m 

ssh -O check web1 shows whether a master is running, and ssh -O exit web1 closes it. The Windows build of OpenSSH doesn't support multiplexing, so leave these lines out there.

4. Tunnels: -L, -R and -D with real use cases

The rule that clears up most confusion: in -L local_port:host:host_port, the host is resolved on the server. 127.0.0.1 on the right-hand side means the server itself, not your laptop.

Flag Who listens Where traffic comes out Typical use
-L 13306:127.0.0.1:3306 Your machine The server, to its own port 3306 Database bound to localhost on the VPS
-L 16379:10.0.0.5:6379 Your machine The server, onward to a private host Redis or an admin panel on a private network
-R 8080:127.0.0.1:3000 The server (loopback) Your machine, port 3000 Show a local dev app to the server, reach a box behind NAT
-D 1080 Your machine (SOCKS proxy) The server, to any destination the client asks for Test a site as the server sees it, reach several internal hosts

The database through a tunnel

This is the reason most people learn tunnels: MySQL or PostgreSQL stays bound to 127.0.0.1, no firewall port is opened, and you still query it from your laptop.

ssh -f -N -o ExitOnForwardFailure=yes -L 13306:127.0.0.1:3306 web1 mysql -h 127.0.0.1 -P 13306 -u appuser -p ssh -f -N -o ExitOnForwardFailure=yes -L 15432:127.0.0.1:5432 web1 psql -h 127.0.0.1 -p 15432 -U appuser appdb 

Three things trip people up:

  1. Use 127.0.0.1, not localhost, with the MySQL client. -h localhost makes it use the local Unix socket and ignore -P, so you reach your laptop's MySQL (or nothing) instead of the tunnel.
  2. The database sees the connection coming from 127.0.0.1, not from your home IP. The MySQL user must be allowed from localhost/127.0.0.1. In PostgreSQL, the host ... 127.0.0.1/32 line in pg_hba.conf applies, not the local line.
  3. Pick a high local port (13306, 15432) so you don't need admin rights and don't clash with a database running on your own machine.

GUI tools such as DBeaver and TablePlus have an SSH tunnel tab that does exactly this. Point it at the same host, user and key. If you prefer the terminal, ssh web1-db from the config above opens both tunnels at once.

To close a backgrounded tunnel, pgrep -af "ssh -f -N" finds it and kill ends it. If you use ControlMaster, ssh -O exit web1 does the same job.

Reaching a private host

ssh -N -L 16379:10.0.0.5:6379 web1 redis-cli -h 127.0.0.1 -p 16379 

The leg from your laptop to web1 is encrypted. The last leg, from web1 to 10.0.0.5, is a plain TCP connection on the private network. That's normally fine on a private VLAN, but it isn't end-to-end encryption.

Reverse tunnel: a machine behind NAT

An office machine that can make outbound connections but accepts none can phone home to your VPS:

# on the office machine ssh -N -R 2222:127.0.0.1:22 deploy@203.0.113.10 # later, on the VPS ssh -p 2222 officeuser@127.0.0.1 

By default, sshd binds remote forwards to the server's loopback only, so port 2222 is not exposed to the internet. Keep GatewayPorts at its default of no unless you really mean to publish the port. For a tunnel that has to stay up through network drops, autossh -M 0 -N with the ServerAlive settings above restarts it for you.

SOCKS proxy

ssh -N -D 1080 web1 curl --socks5-hostname 127.0.0.1:1080 https://ifconfig.me 

--socks5-hostname (rather than --socks5) makes the DNS lookup happen on the server's side too. That matters when you are checking how a site resolves and responds from the server's network. Only TCP is carried, not UDP.

Lock tunnels down on the server

Any account that can log in can forward ports by default. For a key that should only ever open the database tunnel, restrict it in authorized_keys:

restrict,port-forwarding,permitopen="127.0.0.1:5432",command="/bin/false" ssh-ed25519 AAAA... tunnel-only 

restrict turns everything off (PTY, agent and X11 forwarding, port forwarding). port-forwarding turns forwarding back on, permitopen limits it to one destination, and the forced command means the key can't get a shell. It works with ssh -N, because no command is ever run. For SFTP-only accounts and the server-wide AllowTcpForwarding/PermitOpen settings, see our guide to SSH tunnels.

5. scp vs sftp vs rsync: which one, when

All three use the same SSH login, key and config, so the choice is about the job, not security.

scp sftp rsync over SSH
Best for One file or folder, once Browsing, renaming, fixing permissions Deploys, backups, anything repeated
Only sends changes No No Yes (delta transfer)
Resume a broken transfer No Yes (reget, reput) Yes (-P / --partial)
Needs on the server SFTP subsystem (since OpenSSH 9.0) SFTP subsystem rsync installed on both ends
Port flag -P 2222 -P 2222 -e 'ssh -p 2222'
Scriptable Yes Yes (sftp -b, key login only) Yes

Some details that save time:

  • scp uses SFTP underneath since OpenSSH 9.0. The command line hasn't changed, but you no longer need extra quoting for remote filenames with spaces, and adding it can now break the copy. If you hit an old server that only speaks the legacy protocol, add -O.
  • -P vs -p. scp and sftp take the port as capital -P, because lowercase -p in scp preserves times and modes. ssh uses lowercase -p for the port. With a Port line in ~/.ssh/config, you stop caring.
  • rsync trailing slash. site/ copies the folder's contents, while site creates site/ inside the destination. Always run --dry-run before adding --delete.
  • cPanel accounts. FTP accounts created inside cPanel can't log in over SFTP. SFTP uses the main cPanel username and the account's SSH access.
  • Many small files. For thousands of tiny files on a first copy, a tar stream is often faster than any of the three:
 tar czf - uploads | ssh web1 'tar xzf - -C /var/www/site' 

Interactive sftp, GUI clients (FileZilla's "SFTP" option, not "FTP over TLS", which is FTPS) and the FTP/FTPS/SFTP differences are in our SFTP and SCP guide.

The whole workflow on one screen

  1. First login: compare the host key fingerprint from a console.
  2. Create an ed25519 key with a passphrase, one per device, and install it with ssh-copy-id.
  3. Test key login from a second terminal, then add PasswordAuthentication no in a 00- drop-in, and verify with sshd -T and a forced password attempt.
  4. Write ~/.ssh/config with IdentitiesOnly, a separate tunnel host and ProxyJump for private machines.
  5. Keep databases on 127.0.0.1 and reach them with -L. Restrict tunnel-only keys with restrict,port-forwarding,permitopen.
  6. Use scp for one-offs, sftp for poking around, and rsync for anything you'll run twice.

If you want a server to practise this on, a Linux KVM VPS from Bigdomainhost (Mumbai or Delhi NCR) takes your SSH key at order time. It also has an out-of-band console in the control panel, which is where you check the host key fingerprint, and how you get back in if a firewall or sshd change goes wrong.

Disclosure: written by the team at Bigdomainhost, an Indian hosting provider. We sell the VPS linked above.