← Back to Articles Technology & IT Infrastructure

Understanding SSH, Public Keys, and Private Keys

A guide to SSH and key pairs: how to create and install them, then use them for automated website updates with read-only deploy keys and restricted access.

SSH (Secure Shell) is a protocol for accessing another computer over a network through an encrypted channel. Administrators use it to run commands on servers, transfer files, and automate application updates. Tatu Ylönen created the first version in 1995, partly to replace Telnet, which sent passwords unencrypted.

The basic command looks like this:

ssh deploy@203.0.113.10

It logs in as the user deploy on the server at 203.0.113.10. Addresses in this article come from ranges reserved for documentation, so substitute your own.

What the public and private keys do

SSH can use passwords, but passwords can be guessed through repeated attempts and are awkward in scripts. The alternative is a cryptographic key pair. The private key stays on the user’s computer and must be kept secret. The public key goes on the server, in ~/.ssh/authorized_keys, and may be known to anyone.

At login, your computer creates a digital signature over data tied to that session, including the session identifier and user name. The server checks the signature against the stored public key. The private key is never sent, and a signature from one session cannot be reused in another.

Keys remove the risk of weak passwords as long as password login is also turned off. The risk of a stolen private key file remains, so storage matters.

Creating and installing a key

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

This creates two files in ~/.ssh: id_ed25519, the private key, and id_ed25519.pub, the public key. Ed25519 is also the default type in current OpenSSH. The text after -C is only a comment, so choose something that identifies the owner.

Enter a passphrase when asked. The private key is then encrypted on disk, and a stolen file cannot be used straight away. A lost passphrase cannot be recovered; the remedy is to create a new key. Use ssh-agent to avoid typing it repeatedly.

Copy the public key to the server with:

ssh-copy-id deploy@203.0.113.10

If ssh-copy-id is unavailable, as on some Windows installations, append the contents of the .pub file to authorized_keys by hand. File permissions must be right, because sshd ignores a file that other users can write to:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Checking the server’s identity

On the first connection, SSH shows the fingerprint of the server’s key and asks whether you trust it. Compare it with the fingerprint shown in your hosting provider’s console before answering yes. Once accepted, the key is recorded in known_hosts.

If a warning later says the server’s identity has changed, find out why first. The server may have been reinstalled, or your connection may be reaching a different machine. Turning the check off with StrictHostKeyChecking no removes that protection.

Using it for website updates

Because key-based login needs no typed password, a script can run an update:

ssh deploy@203.0.113.10 "cd /var/www/website && git pull --ff-only && npm ci && npm run build"

The command logs in, fetches the latest code, installs the dependencies pinned in the lock file, and builds the site. The --ff-only option stops the process when the code history on the server has diverged from the repository. The && operators ensure that each step runs only if the previous one succeeded.

Continuous integration and delivery (CI/CD) services such as GitHub Actions follow the same pattern. The private key is stored as a secret and used during deployment. Automation keys usually have no passphrase, which is why their access should be narrow.

Read-only deploy keys

The example contains two separate relationships: laptop to server, and server to Git repository. They should use different keys. For the second one, create a dedicated key on the server:

ssh-keygen -t ed25519 -f ~/.ssh/website_deploy -C "deploy-website"

Register the contents of website_deploy.pub as a deploy key in the repository. On GitHub, a deploy key applies to one repository and is read-only by default. The server can fetch code but cannot push changes. If the server is compromised, the impact is smaller than if it held a developer’s personal key with access to every repository.

Point Git at the key in ~/.ssh/config:

Host github-website
  HostName github.com
  User git
  IdentityFile ~/.ssh/website_deploy
  IdentitiesOnly yes

The repository address then becomes git@github-website:account-name/repo-name.git. Deploy keys do not expire on their own, so keep a record of them and remove them when a server is retired.

Restricting access on the server

After confirming from another session that key login works, passwords can be disabled in /etc/ssh/sshd_config:

PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no

Check the file with sudo sshd -t, then reload the service. Keep your current session open until a new login succeeds, and make sure you can reach the provider’s emergency console.

Automation keys can be limited further with options in authorized_keys:

restrict,from="198.51.100.20",command="/usr/local/bin/deploy-website" ssh-ed25519 AAAA... ci-deploy

Among other things, restrict disables port forwarding and terminal allocation, from limits where connections may originate, and command fixes the single command that runs. Use one key per person or purpose so that revoking access means deleting one line. A firewall and login rate limiting such as fail2ban remain useful as extra layers, in keeping with the practices in the article on Linux servers.

The same approach applies to database servers. PostgreSQL’s port should not be open to the internet, and administrative access can go through an SSH tunnel. Connection handling is covered in the introduction to PgPool.

References

← All Articles
Contact Us