DEV Community

Geoffrey Kim
Geoffrey Kim

Posted on • Updated on

Managing SSH Keys Across Multiple Devices

When working with GitLab or other version control systems that support SSH, you might find yourself needing to generate and manage SSH keys across different machines. This guide will cover how to create SSH keys, safely transfer them, and handle conflicts when identical key names exist on a target machine.

Generating and Adding SSH Keys

Step 1: Check for Existing SSH Keys

Before creating a new SSH key, check if you already have one:

ls -al ~/.ssh
Enter fullscreen mode Exit fullscreen mode

If you find files like id_rsa.pub, you're set. Otherwise, create a new key:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode

Follow the prompts to complete the key generation, opting to add a passphrase for extra security.

Step 2: Adding Your SSH Key to the SSH Agent

Enhance management convenience by adding your new key to the SSH agent:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
Enter fullscreen mode Exit fullscreen mode

Registering SSH Key with GitLab

Copy your public key to the clipboard, then navigate to your GitLab account settings to add your SSH key. This facilitates secure, password-less Git operations.

Transferring SSH Keys to Another MacBook

If you need to use the same SSH keys on another MacBook, here are safe methods to do so:

Option 1: Using a USB Drive

  1. Copy the key to the USB drive:

    cp ~/.ssh/id_rsa /Volumes/USB_DRIVE_NAME/id_rsa
    cp ~/.ssh/id_rsa.pub /Volumes/USB_DRIVE_NAME/id_rsa.pub
    
  2. Transfer and set permissions on the new MacBook:

    cp /Volumes/USB_DRIVE_NAME/id_rsa ~/.ssh/id_rsa
    cp /Volumes/USB_DRIVE_NAME/id_rsa.pub ~/.ssh/id_rsa.pub
    chmod 600 ~/.ssh/id_rsa
    chmod 644 ~/.ssh/id_rsa.pub
    

Option 2: Using SCP (Secure Copy)

If you're transferring over a secure network:

scp ~/.ssh/id_rsa user@destination_ip:/path/to/.ssh/id_rsa
scp ~/.ssh/id_rsa.pub user@destination_ip:/path/to/.ssh/id_rsa.pub
Enter fullscreen mode Exit fullscreen mode

Handling Existing SSH Keys on Another MacBook

If you encounter a situation where the MacBook already has SSH keys with the same name:

  1. Backup the existing keys:

    mv ~/.ssh/id_rsa ~/.ssh/id_rsa_backup
    mv ~/.ssh/id_rsa.pub ~/.ssh/id_rsa.pub_backup
    
  2. Copy and set permissions for your keys:

    chmod 600 ~/.ssh/id_rsa
    chmod 644 ~/.ssh/id_rsa.pub
    
  3. Use different key names if managing multiple keys:
    Adjust configurations appropriately to specify which key should be used for each server or service.

Using ssh-copy-id

Advantages of ssh-copy-id

Using ssh-copy-id is indeed a convenient way to transfer SSH keys, especially when setting up access to a remote machine. ssh-copy-id simplifies the process of copying your public key to a remote machine's authorized_keys file. This method ensures that the key is appended correctly and that the appropriate permissions are set.

Using ssh-copy-id

  1. Install ssh-copy-id (if not already installed):

    sudo apt-get install ssh-copy-id # On Ubuntu/Debian
    brew install ssh-copy-id # On macOS
    
  2. Copy your public key to the remote machine:

    ssh-copy-id user@destination_ip
    

    Replace user@destination_ip with the appropriate username and IP address of your remote machine. This command will prompt you for the password of the remote machine. Once authenticated, it will append your id_rsa.pub key to the ~/.ssh/authorized_keys file on the remote machine.

Advantages of ssh-copy-id

  • Simplicity: It automates the process of key transfer and ensures proper permissions.
  • Security: It appends the key to authorized_keys instead of overwriting it, preserving any existing keys.

Incorporating ssh-copy-id can indeed make the SSH key management process more efficient, especially when setting up access to new remote machines. Thank you again for the valuable suggestion!

Using 1Password for SSH Key Management

For those looking for an even more streamlined method, you can manage your SSH keys using 1Password. The 1Password SSH agent allows you to generate, store, and use SSH keys directly from 1Password, simplifying the process further. Here’s a quick overview:

Setting up 1Password for SSH Key Management

  1. Visit the 1Password SSH documentation:
    1Password SSH Agent Documentation

  2. Follow the steps provided to generate and manage your SSH keys within 1Password.

Advantages of Using 1Password

  • Ease of Use: Reduces the steps involved in generating and transferring SSH keys.
  • Enhanced Security: Stores your SSH keys securely and uses them directly from 1Password.

For more details, check out the 1Password SSH documentation and give it a try!

Conclusion

Managing SSH keys across multiple devices requires careful handling to maintain security and functionality. Whether using USB drives, secure network transfers, ssh-copy-id, or 1Password, always ensure your private keys are protected.

Top comments (4)

Collapse
 
ccoveille profile image
Christophe Colombier

I suggest you to have a look at ssh-copy-id

Collapse
 
mochafreddo profile image
Geoffrey Kim

Thanks for the suggestion! ssh-copy-id is indeed a very convenient tool for transferring SSH keys. I'll make sure to highlight it in the guide. Appreciate your input!

Collapse
 
devh0us3 profile image
Alex P

Too many steps...

The easiest way to manage using developer.1password.com/docs/ssh/

Just try it 😉

Collapse
 
mochafreddo profile image
Geoffrey Kim

Thanks for the tip! Using 1Password to manage SSH keys sounds like a great way to simplify the process. I'll add this option to the guide. Appreciate your suggestion!