建立新的SSH密钥,并添加到ssh-agenthtml
建立密钥git
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
github
输入保存密钥的绝对路径和文件名,如/Users/Steve/.ssh/new_id_rsashell
Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
app
两次输入确认密钥的密码ssh
Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]
gitlab
把密钥添加到ssh-agent测试
把ssh-agent在后台启动spa
eval "$(ssh-agent -s)"
htm
一个帐号不用配置config
配置~/.ssh/config文件,若是没有该文件,经过touch config命令建立。注意HostName github.com
Host github.com
HostName github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa
把私钥添加到ssh-agent并存入keychain,执行命令会要求你输入密码
ssh-add -K ~/.ssh/id_rsa
把公钥复制到剪贴板
pbcopy < ~/.ssh/id_rsa.pub
进入Github帐户找到Settings,点击进入后选择SSH and GPG keys,点击New SSH key。在Title框内填入标题,在``框粘贴刚才复制的公钥。最后点击Add SSH key。
测试命令
ssh -vT git@github.com
假设如今有两个GitHub帐户,对应两个SSH密钥old_id_rsa和new_id_rsa。若是尚未密钥,分别按上面的步骤建立和添加。
修改config文件以下
# 配置多个id_rsa
# github
Host github.com # Host是别名
HostName github.com # HostName是远程仓库的域名
IdentityFile ~/.ssh/github_rsa
AddKeysToAgent yes
# PreferredAuthentications publickey
User yourname@gmail.com
# 公司项目
Host labs.oa.com
HostName labs.oa.com
IdentityFile ~/.ssh/id_rsa
AddKeysToAgent yes
User yourname@qq.com
# fastlane打包证书
Host app.cert.com
HostName labs.oa.com
IdentityFile ~/.ssh/app_rsa
AddKeysToAgent yes
User yourname@163.com
# gitlab仓库备份
Host gitlab.com
HostName gitlab.com
IdentityFile ~/.ssh/gitlab_rsa
AddKeysToAgent yes
User yourname@live.com
注意:labs.oa.com.app 和 labs.oa.com.cert 在新版本macOS中添加秘钥时会所有识别为labs.oa.com,建议让HostName彻底不一样
配置完~/.ssh/config后添加秘钥到ssh-agent中,能够不用再输入密码
ps -ef|grep ssh 查找已经启动的ssh-agent进程,若是进程存在则干掉 kill -9 pid
删除~/.ssh/known_hosts文件,rm -rf ~/.ssh/known_hosts
依次测试可否连通,ssh -T 会自动将秘钥添加到ssh-agent中
ssh -T github.com
ssh -T gitlab.com
ssh -T labs.oa.com
ssh -T app.cert.com
若是报错了,能够使用下面命令查看详细的报错信息
ssh -vT git@new.github.com
使用须要注意,git@后要改成对应帐户的别名。
如new_id_rsa密钥对应的GitHub帐户上有个仓库test.git,且你的GitHub用户名是username,使用下面命令克隆
git clone git@app.cert.com:username/test.git 设置了别名就不能用原来域名labs.oa.com
避免git错用密钥,把git全局的用户名和邮箱删除
git config --global --unset user.email
git config --global --unset user.name
删除后,之后进入每一个仓库都要指定该仓库局部的user.mail和user.name。
git config user.email "you@example.com"
git config user.name "Your Name"
还没完~
若是想要每次启动电脑都自动启动ssh-agent,就不用输入密码了
自动操做神器登场~~~~~~
一、新建应用程序
二、选择 shell 脚本类型
三、记得先在控制台执行一下这些脚本,由于须要输入密码,不然会报错
点击右上角的运行按钮执行脚本保证正确性
四、保存为应用程序
五、设置开机启动运行脚本
系统-偏好设置-用于与群组,点击 + 添加应用程序
完美~
Connecting to GitHub with SSH
Generating a new SSH key and adding it to the ssh-agent
Adding a new SSH key to your GitHub account
Multiple GitHub Accounts & SSH Config