1、新建rsa文件:
cd .ssh或者cd ~/.ssh
ssh-keygen -t rsa -C lala@163.com
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/lala/.ssh/id_rsa):work_rsa(此处填写rsa文件的名字)
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in yes.
Your public key has been saved in work_rsa.pub.
The key fingerprint is:
fb:c4:b0:e0:47:fd:be:e0:fb:ea:73:ef:a8:29:d5:22 lala@163.com
The key's randomart image is:
+--[ RSA 2048]----+
|? ? ? ? ? ? ? ? |
|???????????????? |
|???????????????? |
|???????? .?????? |
|????? . S ..???? |
|???? . oE=o..??? |
|????? . +o+..??? |
|?????? ..+.+..?? |
|???????? oOB=+o? |
+-----------------+
2、添加密钥到ssh:ssh-add 文件名 ??需要之前输入密码。
'ssh-add~/.ssh/work_rsa
3、配置.ssh/config
我只需要在~/.ssh/config(如果没有就新建一个)里新增一个Host的别名,将不同帐号的区分开来就可以了。
在.ssh目录下新建config文件,将下面的内容粘贴进去
Host github.com
HostName github.com(第一个账号)
PreferredAuthenticationspublickey
IdentityFile~/.ssh/id_rsa
Host work.github.com (第二个GitHub账户)
HostName github.com
PreferredAuthenticationspublickey
IdentityFile~/.ssh/work_rsa、
4、测试GitHub连接是否成功:
ssh -T git@work.github.com(第二个账户的host名字)
Hi Jone Zhang(GitHub账户)! You've successfully authenticated, but GitHub does not provide shell access.
Connection to work.github.com closed.
5、获取公钥pub文件内容,并将此内容添加到GitHub账户里的SSH keys里
vim work_rsa.pub (用vim命令打开文件复制内容)或者cat work_rsa.pub命令(公钥),登陆github,进?Account setting之后,?面左侧?面有一项是SSH keys,点击后?面右侧会有add SSH keys的选项,最后将之前vim work_rsa.pub命令之后的内容粘贴过来保存就可以了。
6、连接远程仓库并创建一个叫origin_work的别名
git remote add origin_work git@work.github.com:GitHub账户名/test.git
将本地仓库提交到远程仓库别名origin_work的地址 master分支下
git push -u origin_work master
以后提交内容如果是第一个账户就使用origin 如果是第二个账户则使用origin_work