以mac下多个不同ssh key提交到不同github上为例:
1.安装 ssh
Apple$ ssh -v
usage: ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
[-D [bind_address:]port] [-E log_file] [-e escape_char]
[-F configfile] [-I pkcs11] [-i identity_file]
[-J [user@]host[:port]] [-L address] [-l login_name] [-m mac_spec]
[-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address]
[-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]]
[user@]hostname [command]
mac下已经默认安装好了ssh
2.生成两个不同账号的ssh key
ssh-keygen -t rsa -C "test1@email.com" // 一路enter即可...
ssh-keygen -t rsa -C "test2@email.com" // 同上
进入.ssh目录
:.ssh Apple$ ls
authorized_keys id_rsa known_hosts user2.pub
config id_rsa.pub user2
ssh默认只读取id_rsa,为了让SSH识别新的私钥,需将其添加到SSH agent中:
ssh-add ~/.ssh/user2
如果出现Could not open a connection to your authentication agent的错误,就试着用以下命令:
ssh-agent bash
ssh-add ~/.ssh/user2
接下来配置一下config文件,如果没有就创建一个
.ssh Apple$ touch config
:.ssh Apple$ cat config
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
Host frankwii.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/user2
这个时候注意一下,如果你的github上的仓库添加的是sshkey是user2.pub那么地址应该为
git@frankwii.com:frankwii/meiqi.git
至于你要访问github项目将你的.pub文件扔到要访问的github中即可
密钥添加完成后,你还可以测试一下:
ssh -T git@frankwii.com
Hi wii95! You've successfully authenticated, but GitHub does not provide shell access.