How to Generate an SSH Key Pair on Linux, Windows, and macOS/iOS

Secure Shell (SSH) keys make logging into servers safer and easier by replacing passwords with cryptographic authentication. In this guide, we’ll walk through how to generate an SSH key pair on Linux, Windows (with PuTTY), and macOS/iOS, then show how to format and install the public key on a Linux server. Perfect for web developers new to server management, this step-by-step tutorial covers the essential commands, tools, and tips you need to get started.

Table of Contents

Secure Shell (SSH) keys are a cornerstone of modern web development. If you’re deploying code, managing servers, or working with version control systems like Git, you’ll often need an SSH key pair. In this article, we’ll explain what an SSH key pair is, why it matters, and how to generate one on common platforms: Linux, Windows (with PuTTY), and macOS/iOS. We’ll also cover how to format the key correctly for use on a Linux server.


What is an SSH Key Pair?

An SSH key pair is a set of two cryptographic files used for secure communication between your computer and a remote server:

  • Private Key: This stays on your local machine. Think of it like your password—never share it.
  • Public Key: This can be shared freely. You place this on the server you want to connect to.

When you connect via SSH, the server checks that your private key matches the public key it has on file. This allows you to log in securely without typing a password.


Generating an SSH Key on Linux

Most Linux distributions include the ssh-keygen tool. You can create a new key pair with the default filename, or specify a custom name if you need multiple keys.

Quick start (default filename)

  1. Open a terminal.
  2. Run:

    • -t ed25519 specifies the algorithm. Ed25519 is modern and recommended, but you can use rsa for compatibility.
    • -C adds a comment, usually your email.
  3. Press Enter to accept the default location (~/.ssh/id_ed25519).
  4. Optionally enter a passphrase for extra security.

This generates:

  • ~/.ssh/id_ed25519 → Private key
  • ~/.ssh/id_ed25519.pub → Public key

Specify a custom filename

Use the -f option to save the key with a custom name. This is helpful if you manage multiple servers or services.

  • The private key will be ~/.ssh/id_ed25519_prod
  • The public key will be ~/.ssh/id_ed25519_prod.pub

You can also set a passphrase at generation time:

Use -N "" to create a key without a passphrase.

Using a custom key

To connect with a custom key:

Or, make it easier by adding an entry to your SSH config (~/.ssh/config):

Now you can connect with:


Generating an SSH Key on Windows with PuTTY

Windows doesn’t come with ssh-keygen by default, but PuTTY provides a tool called PuTTYgen.

  1. Download PuTTY and install it.
  2. Open PuTTYgen.
  3. Under Parameters, choose Ed25519 (or RSA if needed).
  4. Click Generate and move your mouse around the blank area to create randomness.
  5. Once complete, save the two files:
    • Private key (id_ed25519.ppk) → keep safe on your computer.
    • Public key (copy from the box labeled “Public key for pasting into OpenSSH authorized_keys file”).

⚠️ Important: The text in the PuTTYgen box may look different than Linux’s .pub file. Copy the full key string and paste it into your server’s ~/.ssh/authorized_keys file as a single line.


Generating an SSH Key on macOS and iOS

On macOS

macOS comes with the same ssh-keygen tool as Linux. Use the same command:

The keys will be stored in ~/.ssh/.

On iOS

If you’re working from an iPhone or iPad, you can use a terminal app like Termius or iSH:

  1. Install Termius from the App Store.
  2. Inside the app, go to Settings → Keychain → Generate Key.
  3. Choose Ed25519, give it a name, and save.
  4. Termius will let you copy the public key to use on your server.

Formatting Your Public Key for a Linux Server

To use your key, the public key must be placed in a specific file on your server:

  1. Connect to your server (using a password this one time).
  2. Open the file:

    (Create the file and folder if it doesn’t exist: mkdir -p ~/.ssh && chmod 700 ~/.ssh)
  3. Paste your public key on a single line. It should look like this:
  4. Save and exit.
  5. Set correct permissions:

Now you can log in without a password.


Common Options and Tips

  • Algorithm: Use ed25519 when possible. RSA is still supported but less efficient.
  • Passphrases: Add one for stronger security. You’ll need to enter it each time unless you use an SSH agent.
  • Multiple Keys: You can create multiple keys for different servers or services by using the -f flag.
  • Checking Keys: Your public key should always be a single line starting with ssh-ed25519 or ssh-rsa.

Conclusion

SSH keys make connecting to servers more secure and more convenient. Whether you’re on Linux, Windows, or iOS, generating and installing an SSH key pair is straightforward once you understand the steps. As a web developer, learning this workflow will save you time and strengthen your security practices.

Have a project or a problem?

Talk with a senior engineer for practical recommendations—no obligation.

Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Categories

Get a free consultation from Reliable Penguin

Submit the form—or for immediate service call 866-649-7984.