SSH on Mac OS

Create SSH key

First, create ssh key:

1
ssh-keygen -t rsa -C "yourmail@mail.com"

Then add the created private key to ssh-agent, and store it in keychain:

1
ssh-add -K ~/.ssh/id_rsa

-K can make sure that the ssh key will always be valid after restart. You can use ssh-add -l to check added ssh keys

Config

We can use different ssh keys to connect different servers.

The configuration file is ~/.ssh/config

Here is an example config file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# Use id_rsa_github to connect to git@github.com
Host myhost
	HostName github.com
	User git
	PreferredAuthentications publickey
	IdentityFile ~/.ssh/id_rsa_github
  
# Use default ssh key to connect to git@gitlab.com
Host myhost2
	HostName gitlab.com
  User git
  PreferredAuthentications publickey
	IdentityFile ~/.ssh/id_rsa
  
# Use id_rsa_myserver to connect root@12.34.56.78
Host myhost3
	HostName 12.34.56.78
  User root
  PreferedAuthentications publickey
  IdentityFile ~/.ssh/id_rsa_myserver

If the server is not in config file, the default ssh key is ~/.ssh/id_rsa

Github setting

Please refer to Github Help

updatedupdated2024-05-102024-05-10