Cloud Computing

How to Safely Store SSH Private Keys in 1Password or Apple Keychain Instead of ~/.ssh

SSH keys make it easy to connect to remote servers, GitHub repositories, and cloud platforms without entering a password every time. However, storing private…

D
DevByteTechnical Author
Published
16 min read3 views
How to Safely Store SSH Private Keys in 1Password or Apple Keychain Instead of ~/.ssh

If you work with GitHub, AWS servers, VPS hosting, or remote Linux machines, you've probably used SSH keys at some point.

The usual setup is straightforward. Generate an SSH key, save it inside ~/.ssh, add the public key to GitHub or your server, and use the private key whenever you connect.

For example, a typical SSH directory might look like this:

~/.ssh/
├── id_ed25519
├── id_ed25519.pub
├── known_hosts
└── config

This setup works perfectly well for many developers. In fact, storing SSH keys in ~/.ssh is a standard and supported approach.

But there's another option worth considering, especially if you're using a Mac for development: store your SSH private keys in a dedicated password manager or use Apple's Keychain to manage their availability.

Tools such as 1Password can manage SSH keys and provide an SSH agent, while macOS Keychain can securely store certain credentials and passphrases and can integrate with the built-in OpenSSH tools.

The important distinction is that these approaches don't all work the same way. Apple Keychain can manage an SSH key's passphrase without necessarily storing the private-key file itself, whereas 1Password can store SSH private keys and provide them through its SSH agent.

In this article, we'll explore both approaches, configure them on macOS, and look at how to use them with GitHub, remote servers, and everyday development workflows.

We'll also cover the security trade-offs, common errors, and situations where keeping a key file in ~/.ssh is still a reasonable choice.

1. Why Do Developers Store SSH Keys in ~/.ssh?

Before changing anything, let's understand how the standard SSH setup works.

SSH uses public-key cryptography to authenticate users and systems.

When you generate an SSH key pair, you receive two related files:

  • Private key: The secret key that must remain under your control.

  • Public key: The key you can share with services or servers that need to recognize your identity.

For example, you can generate an Ed25519 key pair using:

ssh-keygen -t ed25519 -C "manjeet@example.com"

The command asks where you want to save the key and whether you want to protect it with a passphrase.

If you accept the default location, the private key is usually saved as:

~/.ssh/id_ed25519

The corresponding public key is saved as:

~/.ssh/id_ed25519.pub

You can add the public key to GitHub or a remote server.

When you connect, SSH uses your private key to prove that you control the identity associated with the public key.

Is storing SSH keys in ~/.ssh unsafe?

Not necessarily.

A properly protected private-key file can be a perfectly reasonable security choice. The standard OpenSSH tools are mature, widely supported, and easy to automate.

The problems usually come from how the keys are managed.

For example:

  • You reuse the same private key across unrelated systems.

  • You leave an old key on a laptop you no longer use.

  • You forget to protect the private key with a passphrase.

  • You accidentally upload the key to a Git repository.

  • You copy private keys between multiple computers.

  • You leave keys accessible to more users or processes than necessary.

A password manager or operating-system credential store can help address some of these problems, depending on how you configure it.

The goal isn't to assume that ~/.ssh is insecure. The goal is to reduce unnecessary exposure and make SSH identity management easier.

2. Understanding the Difference Between 1Password and Apple Keychain

Before configuring either option, it's important to understand what each one actually stores.

Option 1: 1Password

1Password supports storing SSH keys as items and using its SSH agent to provide access to those keys.

Instead of keeping a usable private-key file permanently available in ~/.ssh, you can select a key from 1Password and use it through the agent.

The private key remains managed by 1Password, and your SSH client communicates with its agent when authentication is required.

This can be useful if you manage multiple identities for GitHub, work servers, personal projects, and cloud infrastructure.

For example:

1Password
├── GitHub Personal
├── GitHub Work
├── Production Server
└── Staging Server

These are illustrative item names. You can organize your actual keys according to your workflow.

1Password can also provide an SSH agent integration that lets you control which keys are available and how SSH clients interact with them.

Official documentation: 1Password SSH agent.

Option 2: Apple Keychain

Apple Keychain is built into macOS and provides a secure way to manage passwords, certificates, and other supported secrets.

OpenSSH on macOS can integrate with Keychain to store and retrieve the passphrase used to unlock an encrypted SSH private key.

For example, you might keep the private-key file at:

~/.ssh/id_ed25519

while using Keychain to avoid repeatedly typing its passphrase.

This is convenient because the private key remains a standard OpenSSH key file, but its passphrase can be managed through macOS.

Important: Using Apple Keychain this way does not mean the private-key file has been moved into Keychain. The file still exists in ~/.ssh.

This distinction matters if your goal is specifically to avoid storing private-key files on disk.

Which option should you choose?

Requirement

1Password SSH agent

Apple Keychain

Store and manage SSH private keys as password-manager items

Yes

Not the standard SSH workflow

Use SSH without permanently exporting a key file

Yes, through supported agent integration

Generally no; the key file remains on disk

Store SSH private-key passphrases

Depends on the workflow

Yes

Use with GitHub and remote servers

Yes

Yes

Built into macOS

No

Yes

Share or synchronize key items across supported devices

Depends on your 1Password setup and policies

Depends on your Apple Keychain configuration; SSH private-key files are not automatically moved into Keychain

Control access through a dedicated SSH agent

Yes

Uses the available SSH agent and OpenSSH integration

For this guide, we'll configure 1Password first and then configure Apple Keychain.

3. Before You Start: Check Your Existing SSH Setup

If you already use SSH for GitHub or remote servers, check your current configuration before making changes.

Open Terminal and run:

ls -la ~/.ssh

You might see:

id_ed25519
id_ed25519.pub
known_hosts
config

Your actual files will depend on your setup.

Next, check the SSH client version:

ssh -V

You can also check which SSH agent is currently configured:

echo "$SSH_AUTH_SOCK"

This prints the socket path used by SSH clients to communicate with an agent, if one is configured.

Don't assume that every SSH client uses the same agent. A terminal session, an IDE, a Git GUI, and an automated script can run under different environments.

Before changing your setup, identify which keys you currently use and which services depend on them.

For example, if you use one key for GitHub and another for a production server, make sure you don't accidentally replace both with a single identity.

4. Method One: Store SSH Private Keys in 1Password

Let's start with 1Password.

This approach is particularly useful when you want your SSH private keys managed by a dedicated password manager rather than relying on private-key files in your home directory.

Step 1: Install and configure 1Password

Install the official 1Password application for macOS and sign in to your account.

If you're already using 1Password, make sure your application is up to date and that your account's security settings are configured appropriately.

You can find the official application and documentation at 1Password.

Step 2: Create or import an SSH key

There are two common approaches.

You can generate a new SSH key using 1Password's supported SSH-key workflow, or import an existing private key into an appropriate SSH-key item if your version and account configuration support that workflow.

If you already use a private key for important production systems, don't delete or replace it before confirming that your new configuration works.

A safer migration process is:

  1. Identify the existing key and its public key.

  2. Add the key to 1Password using the supported import workflow.

  3. Enable the SSH agent integration.

  4. Test authentication with a non-critical service.

  5. Confirm that the expected identity is being used.

  6. Remove unnecessary local copies only after you have verified the migration.

If you're generating a new key, make sure you add its public key to the appropriate GitHub account or remote server.

Remember that moving a key into a password manager doesn't automatically register the key with GitHub or update your servers' authorized_keys files.

Step 3: Enable the 1Password SSH agent

Open the 1Password application and locate the SSH agent settings.

Enable the SSH agent according to the current instructions for your version of the application.

1Password can expose an agent socket that SSH clients use to request authentication.

The exact socket path and configuration can depend on your installation and operating system. Follow the current official instructions rather than assuming that the path is identical across every Mac.

Once enabled, inspect your SSH agent configuration:

echo "$SSH_AUTH_SOCK"

The output should identify the socket used by your current shell.

If it points to another agent, your terminal might not be using 1Password yet.

Step 4: Configure SSH to use the appropriate agent

SSH normally discovers an agent through the SSH_AUTH_SOCK environment variable.

If your shell or IDE starts with a different environment, it may continue using another agent.

For example, a terminal launched before you enabled the integration may need to be restarted.

You can inspect the available identities with:

ssh-add -l

This command lists identities available through the active agent.

Depending on your 1Password configuration, keys may be presented through its SSH agent rather than being loaded as ordinary local key files.

If the command reports that no identities are available, check the 1Password agent settings, key item permissions, and terminal environment.

Don't work around the issue by exporting the private key to a temporary file unless your workflow specifically requires that behavior and you understand the security implications.

Step 5: Test your GitHub connection

If you've added your public key to GitHub, test the connection:

ssh -T git@github.com

On the first connection, SSH may ask you to verify GitHub's host key. Verify it against GitHub's official published SSH host-key information before accepting it.

If authentication succeeds, GitHub should identify the account associated with the key.

GitHub doesn't provide an interactive shell through this test, so a message indicating that shell access isn't provided can be normal.

If you receive a permission error, inspect the SSH agent and confirm that GitHub recognizes the public key associated with the identity you're using.

Official documentation: GitHub SSH connection testing.

Step 6: Configure a specific identity for a repository

If you use multiple GitHub accounts, you may want different keys for different repositories.

You can configure SSH host aliases in ~/.ssh/config without storing the private-key contents in that file.

For example:

Host github-personal
    HostName github.com
    User git
    IdentitiesOnly yes

Host github-work
    HostName github.com
    User git
    IdentitiesOnly yes

This is a starting point, not a complete multi-key configuration.

When using an external SSH agent, you must ensure that the intended identity is selected for each alias. Depending on the agent and your SSH version, that may involve specifying an identity file or public-key identity that the agent can match, as described in the relevant agent documentation.

Then configure a repository to use the appropriate alias:

git remote set-url origin git@github-work:organization/repository.git

Replace the organization and repository names with your actual values.

This changes the repository's remote URL; it doesn't change the private key stored in 1Password.

For multiple identities, test each alias separately before using it in your regular development workflow.

5. Method Two: Use Apple Keychain for SSH Passphrases

If you prefer the built-in macOS tools, Apple Keychain is another practical option.

This setup is useful when you want to continue using standard OpenSSH private-key files but avoid repeatedly entering the passphrase.

Let's walk through the configuration.

Step 1: Generate an encrypted SSH key

If you don't already have an appropriate key, generate an Ed25519 key:

ssh-keygen -t ed25519 -C "manjeet@example.com"

When prompted for the file location, you can use:

/Users/your-username/.ssh/id_ed25519

When prompted for a passphrase, choose a strong passphrase.

The private key remains encrypted on disk, and the passphrase protects it against someone who obtains the file without also obtaining the passphrase.

If you already have a suitable private key, you don't necessarily need to generate another one just to use Keychain.

Step 2: Add the private-key passphrase to Keychain

On macOS versions that support the relevant OpenSSH integration, run:

ssh-add --apple-use-keychain ~/.ssh/id_ed25519

Enter the key's passphrase when prompted.

The command asks the SSH agent to load the key and use Apple's Keychain integration to remember the passphrase.

The precise behavior depends on the macOS and OpenSSH versions installed on your system.

If your system reports that the option is unsupported, check your installed OpenSSH version and Apple's current documentation rather than substituting an unrelated ssh-add option.

Step 3: Configure SSH

Open your SSH configuration file:

nano ~/.ssh/config

Add:

Host *
    AddKeysToAgent yes
    UseKeychain yes

These settings tell compatible macOS OpenSSH clients to add keys to the agent and use Keychain for passphrase management.

If you have a default identity, you can also specify it:

Host *
    AddKeysToAgent yes
    UseKeychain yes
    IdentityFile ~/.ssh/id_ed25519

If you already have a configuration file, merge the settings into your existing configuration rather than overwriting host-specific rules.

For example, you may already have different identities configured for production servers and GitHub.

Also note that UseKeychain is a macOS-specific OpenSSH option. It isn't portable to every Linux distribution or SSH client.

Step 4: Restrict private-key file permissions

Even though Keychain manages the passphrase, the private-key file remains on disk.

Restrict access to it:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub

The private key should not be readable by other ordinary users.

The public key isn't secret, so it can generally have less restrictive permissions.

Avoid storing private keys in shared directories, project repositories, or folders that are automatically published or uploaded to public services.

Step 5: Test the connection

Try connecting to GitHub:

ssh -T git@github.com

Or connect to a remote server:

ssh username@your-server.example.com

If the key has already been loaded and the Keychain integration is working, you shouldn't need to enter the passphrase every time.

You may still be prompted depending on your agent state, macOS security settings, and the specific SSH client you're using.

Remember: Keychain stores the passphrase for an existing key file. It doesn't automatically move the private-key file out of ~/.ssh.

6. Which Approach Is Better for Developers?

Both approaches can be useful, but they solve slightly different problems.

Choose 1Password when you want centralized SSH-key management

1Password is worth considering if:

  • You manage multiple SSH identities.

  • You work across multiple devices.

  • You want SSH private keys stored as password-manager items.

  • You want an agent-based workflow without keeping ordinary private-key files permanently in your home directory.

  • You want a dedicated interface for managing keys.

Its effectiveness depends on how you configure the SSH agent and control access to your vault.

Choose Apple Keychain when you want a simple macOS workflow

Apple Keychain is a good option if:

  • You're comfortable using standard OpenSSH.

  • You want to avoid repeatedly entering private-key passphrases.

  • You prefer built-in macOS functionality.

  • You're happy to keep the encrypted private-key file in ~/.ssh.

For many developers, this is the simplest improvement to an existing SSH setup.

My recommendation

For a developer who regularly switches between GitHub repositories, VPS servers, cloud infrastructure, and production environments, I'd consider 1Password when the goal is to manage private keys through a dedicated SSH agent.

If your main problem is repeatedly entering a passphrase on your Mac, Apple Keychain may be enough.

Neither approach eliminates the need to protect your device, control access, verify server identities, and manage old keys.

The right choice depends on your threat model and how you work.

7. What About SSH Agent Forwarding?

There's another SSH feature worth mentioning: agent forwarding.

Agent forwarding allows a remote server to communicate with your local SSH agent while you're connected to that server.

For example, you might SSH into a deployment server and then use your local key to connect to GitHub from that machine.

This can be convenient, but it introduces risk.

If the remote server is compromised while forwarding is enabled, an attacker may be able to use the forwarded agent to authenticate as you while the forwarding session remains active.

The attacker generally doesn't obtain the private-key file itself through agent forwarding, but the ability to request authentication can still be dangerous.

Avoid enabling forwarding globally:

Host *
    ForwardAgent yes

Instead, enable it only for trusted hosts that genuinely require it.

For example:

Host deployment-server
    HostName deployment.example.com
    User deploy
    ForwardAgent yes

Only use this configuration if you understand why the remote server needs access to your agent and trust the server accordingly.

For many deployment workflows, using a dedicated deployment identity or a more appropriate CI/CD authentication mechanism is safer than forwarding your personal SSH agent.

8. Common SSH Key Management Mistakes

Even with a password manager or Keychain, a few mistakes can undermine the setup.

Mistake 1: Reusing the same key everywhere

Using one key for every GitHub account, VPS, production server, and automation task makes access management harder.

If the key needs to be revoked, you may have to update many systems.

Consider separate identities for personal projects, work infrastructure, and privileged production access.

Mistake 2: Assuming a passphrase makes every exposure harmless

A passphrase helps protect an encrypted private-key file at rest.

However, once the key is loaded into an agent, applications may be able to request authentication through that agent according to its configuration.

A passphrase doesn't replace the need to secure your running device and agent.

Mistake 3: Accidentally committing private keys

Never commit private keys to Git repositories.

Add suitable exclusions to your .gitignore:

.ssh/
*.pem
*.key

These exclusions are only a precaution. They won't protect a private key stored outside the repository, and they won't automatically remove a key that has already been committed.

If a private key has been exposed, treat it as compromised, remove its authorization from the relevant services, and replace it.

Simply deleting the file in a later commit doesn't erase it from the repository's history.

Mistake 4: Deleting a key before testing the replacement

When migrating from a file-based key to 1Password, don't immediately delete the original key.

First, confirm that the new identity works with every important service.

If you need to revoke or replace a key, make sure you have another working authentication method before removing the old one.

This is especially important for production servers, where a failed migration can lock you out.

Mistake 5: Ignoring old public keys

Your public keys may remain registered with GitHub or remote servers long after you stop using the corresponding private keys.

Review authorized keys and remove identities that are no longer required.

For servers, check the appropriate user's authorized_keys file. For GitHub, review the SSH keys registered with your account.

Removing an unused public key reduces the number of identities that can authenticate to the service.

9. A Practical Security Checklist

Before considering your SSH setup complete, review these points:

  • Private keys are encrypted with strong passphrases where appropriate.

  • Private-key files have restrictive permissions.

  • SSH identities are separated according to their purpose.

  • Private keys are never committed to Git.

  • Your SSH agent is the one you intended to use.

  • GitHub and server connections use the correct identity.

  • Unused public keys have been removed from services.

  • Agent forwarding is disabled unless genuinely required.

  • Your password manager and Mac account are protected.

  • A backup or recovery plan exists for important credentials.

  • Key replacement procedures are tested before old access is removed.

These checks are simple, but they help prevent common mistakes when managing SSH access across multiple systems.

Conclusion

SSH keys are an essential part of everyday development, especially when you're working with GitHub, cloud servers, deployment pipelines, and production infrastructure.

Keeping private keys in ~/.ssh is not inherently unsafe, but it does require proper permissions, passphrase protection, and careful management.

If you want a more centralized way to manage SSH private keys, 1Password offers SSH-key storage and an SSH agent that can provide access to keys without requiring you to keep ordinary private-key files permanently available in your home directory.

Apple Keychain offers another convenient option for macOS users. It can store SSH private-key passphrases and integrate with OpenSSH, although the private-key files themselves generally remain in ~/.ssh.

For my own development workflow, I'd choose the approach based on the problem I'm trying to solve. If I need centralized management for several SSH identities, I'd consider 1Password. If I simply want macOS to remember the passphrase for my existing SSH key, Keychain is a straightforward choice.

The most important thing is to understand what each tool protects, how your SSH agent behaves, and what happens if a key or device is compromised.

A secure SSH setup isn't just about where you store the key. It's about controlling when the key can be used, which systems trust it, and how quickly you can revoke access when something goes wrong.

D
DevByteAuthor

Technical author and software engineer at DevByte. Writing deep-dive tutorials on distributed systems, modern full-stack web architectures, and cloud deployments.

Keep Exploring

Related Engineering Dispatches

More guides and architectural patterns you might find valuable.

Browse All Articles↘