Dashboard

What Is an SSH Key? Setting One Up for an AI Agent

An SSH key pair lets a machine prove its identity without a password. Here is what each half does and how to give an AI agent a key of its own.

Steve Jefferson
Steve Jefferson
Developer Advocate
29 October 20261 min read

An SSH key is a pair of files that lets one computer prove its identity to another without sending a password. One file is public and can be shared freely. The other is private and must never leave your machine. A server that has your public key can confirm you hold the matching private one, and let you in.

You will meet SSH keys the moment you connect to a server, push code to a Git host, or let an AI coding agent do either on your behalf. That last case is where care matters.

The two halves

  • The public key (usually a .pub file) goes on the server or Git host. It is safe to paste into a settings page or email.

  • The private key stays on your computer. Whoever has this file can act as you, everywhere the public half is installed.

Authentication works by challenge. The server sends something to be signed, your machine signs it with the private key, and the server checks the signature against the public key. The private key never travels.

Creating a key pair

The OpenBSD ssh-keygen manual documents every option. For almost everyone, this one command is enough:

bash
ssh-keygen -t ed25519 -C "laptop-2026" -f ~/.ssh/id_ed25519

It asks for a passphrase. Set one. The passphrase encrypts the private key on disk, so a copied file is useless without it. GitHub's guide to generating a new SSH key walks through the same steps and adds the key to the SSH agent so you do not retype the passphrase constantly.

You now have two files: id_ed25519 (private) and id_ed25519.pub (public). Mix them up and you have published a secret, so check the extension before you paste anything.

The mistake: lending your agent your key

An AI coding agent running in your terminal inherits your environment. If your key is loaded in the SSH agent, the AI tool can use it, with everything you can do. That means pushing to any repository you can write to, and connecting to any server your key opens.

This is the principle of least privilege in a concrete form. The agent needs access to one thing, so give it a key that reaches one thing. The idea of scoping damage in advance is covered in AI agent blast radius.

Give the agent its own key

  1. Generate a separate key just for the agent, with a clear name.

bash
ssh-keygen -t ed25519 -C "agent-myproject" -f ~/.ssh/agent_myproject
  1. Add the public half as a deploy key on one repository. GitHub's documentation on managing deploy keys explains that a deploy key grants access to a single repository, and that you choose whether it can write. Leave write access off unless the agent must push.

  1. Tell SSH to use that key only for that host alias. In ~/.ssh/config:

Host github-agent
  HostName github.com
  User git
  IdentityFile ~/.ssh/agent_myproject
  IdentitiesOnly yes

IdentitiesOnly yes stops SSH from offering every other key it can find. Without it, your personal key may be tried first and succeed.

  1. Point the repository's remote at the alias. Use git@github-agent:you/project.git as the URL.

  1. Rotate it. Delete the deploy key from the host when the project ends. Because it is a separate key, nothing else changes.

If something goes wrong, you revoke one key, not your identity.

Things to check when an agent asks for SSH access

  • Does it need a server at all? Many tasks only need the Git remote. Do not open a production login for a job that edits files.

  • Is it asking you to paste a private key into a chat? Never do this. A private key in a prompt is a leaked credential, as pasting an API key into an AI chat explains for the same reason. If it already happened, treat the key as burned and replace it.

  • Is the key in a repository? Private keys in a project folder end up committed. The guide to what to do if an agent commits a secret covers the cleanup, and the gitignore review helps prevent it.

  • Is it safe to let it run commands on a server? That is a bigger question than a key, and letting an agent deploy to production covers it.

For the wider set of things that go wrong, see the guide to AI risks.

Frequently asked questions

What is the difference between a public key and a private key?

The public key can be shared and installed on servers. The private key stays secret on your machine and is what proves your identity. Anyone holding the private key can impersonate you.

Is an SSH key safer than a password?

Generally yes. There is no password to guess or phish, and the private key never crosses the network. It is only as safe as the file and the passphrase protecting it.

Where is my SSH key stored?

By default in the .ssh folder in your home directory, with names like id_ed25519 and id_ed25519.pub.

What should I do if I leaked my private key?

Remove its public half from every server and Git host that trusts it, generate a new pair, and install the new public key. Deleting the file from your own machine is not enough.

How did this land?

About the author

Steve Jefferson
Steve Jefferson

Developer Advocate

Steve builds something with Swarmz every week and writes up what worked, what broke, and what he'd do differently. Tutorials and hands-on guides are his lane.

Share

Get the next post in your inbox

One email a month. Product updates, engineering posts, and the best of Built with Swarmz.

I agree to receive emails about AI building tips and Swarmz product news. Unsubscribe any time.