Add and configure an SSH Key to connect to remote source code repositories (like GitHub) and servers.
βοΈ You can use an automated script to add an SSH Key to your Mac.
I use a GitHub repository mac-setup for my basic Mac setup scripts including one to add a new SSH Key.
If you'd like, you can use my repository to add your new SSH Key.
Just follow the PREREQUISITES and then Add an SSH Key of my GitHub repository
π Once you add your new SSH Key to your Mac, you will still need to determine how to add the new public SSH Key to your remote server yourself.
Here are the steps if you prefer to create and add a new SHH Key manually.
π This is pretty much from the GitHub Documentation on Connecting to GitHub with SSH
The overall process is...
- Determine if you already have an SSH Key that you want to use
- If not, generate a new SSH Key
- Add your SSH Key to your SSH
Config
- Add your SSH Key to the SSH Agent
- Add your public SSH Key to the host where you want to connect
- Test the SSH connection
You will then need to add your public portion of your SSH Key to where you are connecting like GitHub or your servers.
- Generate the key...
ssh-keygen -t ed25519 -C "[email protected]"
- Add your SSH Key to your
~/.ssh/config
file...Host github.com AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/id_ed25519
- Add your SSH Key to the SSH Agent
env APPLE_SSH_ADD_BEHAVIOR=macos ssh-add -K ~/.ssh/id_ed25519
Your SSH Keys should be located (even if by softlink) in your
~/.ssh
directory. To see what keys you have, simply list the
files in this directory.
ls -al ~/.ssh
They look something like this...
-rw------- 1 someuser staff 464 Sep 3 14:41 id_ed25519
-rw-r--r-- 1 someuser staff 103 Sep 3 14:41 id_ed25519.pub
π§ You will need the email address that you want to be associated with your new SSH Key
On Mac, you can generate the following types ( -t
) of SSH Keys
(these are the encryption algorithms)...
- dsa
- ecdsa
- ecdsa-sk
- ed25519
- ed25519-sk
- rsa
The ed25519
algorithm is recommended.
You can specify the bit level of encryption using the -b
option
(e.g. -b 4096
)
Here the ed25519
algorithm is being used...
-
In a terminal window, run the
ssh-keygen
command substituting your email for"[email protected]"
ssh-keygen -t ed25519 -C "[email protected]"
-
When prompted to "Enter a file in which to save the key," You can press Enter to accept the default file location
-
When prompted to "Enter passphrase (empty for no passphrase)," You can press Enter to select no passphrase
-
When prompted to "Enter same passphrase again:," You can press Enter to again select no passphrase
-
When the command finishes, you can verify that it was created by listing your
~/.ssh
directoryls -al ~/.ssh
-
Ensure that you have an
~/.ssh/config
filetouch ~/.ssh/config
-
Edit your
~/.ssh/config
file using your preferred editor -
Add the following lines replacing
github.com
on theHost
line to the hostname where you are connecting and~/.ssh/id_ed25519
with the filename of yourIdentityFile
(which is your new generated SSH Key)...Host github.com AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/id_ed25519
π macOS Sierra 10.12.2 or later, requires specifying in your
~/.ssh/config
file to automatically load keys into thessh-agen
t and store passphrases in your keychain -
Save your
~/.ssh/config
file with these changes
You need to add your new SSH Key to the SSH Agent and your passphrase to the macOS Keychain (if you added a passphrase to your SSH Key)...
-
In a terminal window, run the
ssh-add
command substituting your new SSH Key file for~/.ssh/id_ed25519
env APPLE_SSH_ADD_BEHAVIOR=macos ssh-add -K ~/.ssh/id_ed25519
β¨ The
env APPLE_SSH_ADD_BEHAVIOR=macos
environment variable suppresses the warning messages about the-K
option being deprecated in newer versions of macOS. The-K
option stores the passphrase in your MacOS Keychain
π [Adding your SSH key to the ssh-agent](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/ generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#adding-your-ssh-key-to-the-ssh-agent)
You will need to add your SSH Key to the remote host
where you want to connect, for example github.com
.
You will use the contents of the *.pub
version of your new
SSH Key file but the process for adding it to the remote host
will be unique to that host and/or organization.
- To connect your SSH Key to GitHub, see their documentation Adding a new SSH key to your account
If you are connecting to GitHub and have added your public SSH Key to your GitHub account, here's how you can test your SSH connection...
-
In a terminal window, run the
ssh
command substituting your remote host for[email protected]
ssh -T [email protected]
You should see something like this as a response...
Hi someuser! You've successfully authenticated, but GitHub does not provide shell access.