Oliver Servín

March 6, 2026

Generating SSH keys server-side with Laravel

I was exploring the Laravel Cloud public repository when something caught my eye. They were generating SSH keys server-side.

Wait, what?

Laravel Cloud generates unique SSH keys for each user server-side. These keys are later used to connect to user remote servers programmatically. When you need to SSH into user infrastructure on their behalf, you need a unique key per user, but users shouldn't have to generate and manage these keys themselves.

So, how complex would this be to implement?

Turns out, not complex at all. Laravel's Process facade makes it surprisingly elegant.
CleanShot 2026-03-06 at 13.39.02@2x.png

Here's how it works. Create a service class with a `generate` method that generates a random key path, runs `ssh-keygen` with ed25519 encryption, accepts an optional passphrase, reads and returns both public and private keys, and cleans up temporary files.

The key insight is using Laravel's Process facade instead of Symfony's Process library directly. It's not just cleaner syntax (though that helps). You get trivial testing with Process::fake(), and you're using framework-native tools.

I built a working demo at ssh-keygen.antihq.com where you can try it yourself. Enter your email as the key comment, optionally add a passphrase, and generate your keys through a simple web interface. The source code is available at github.com/antihq/ssh-keygen if you want to see the complete implementation.

When does server-side SSH key generation make sense? When you need to connect to user remote servers programmatically. This pattern lets you generate unique keys per user automatically, connect to user infrastructure on their behalf, avoid forcing users to manage SSH keys themselves, and standardize key formats across your platform.

Server-side SSH key generation isn't just possible. With Laravel, it's elegant and testable.

About Oliver Servín

Working solo at AntiHQ, a one-person software company.