Manage SSH keys on a Mac (OSX)

The generated key consists of a pair of files, one private and one public. It’s very important that you never share the private key, ever. While you will use the public key to authenticate with the external service.

Creating a key

To generate the key using the Terminal:

# Run the command below to generate an SSH key, then follow the prompts.
# Enter file location, or press enter to use the default location.
# Enter a passphrase, or press enter to for no passphrase. If you opt to use a passphrase you will need to enter it to use the key.
ssh-keygenCode language: PHP (php)

Now that the key can be copied to the clipboard ready for use:

pbcopy < ~/.ssh/id_rsa.pubCode language: JavaScript (javascript)

Bonus: Add the following to your .bash_profile so you only need to type your passphrase when opening the terminal.

eval $(ssh-add)Code language: JavaScript (javascript)

Removing a key

# Remove the private and public key, assuming the keys are in the default location
rm ~/.ssh/id_rsa && rm ~/.ssh/id_rsa.pubCode language: PHP (php)

Bonus

Save servers for quick connection using:

ssh myServer

Edit: ~/.ssh/config

Host alias
    HostName hostname
    User user

Host myServer
    HostName 172.168.1.1
    User root
    IdentityFile ~/.ssh/id_rsaCode language: JavaScript (javascript)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.