$ ssh-keygen -t rsa -C "your_email@example.com" Generating public/private rsa key pair. Enter file in which to save the key (/Users/your_user_directory/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again:
your_email@example.com
改为本身的邮箱。git
密码输入成功后出现以下提示,表示建立成功:github
Your identification has been saved in /Users/your_user_directory/.ssh/id_rsa. Your public key has been saved in /Users/your_user_directory/.ssh/id_rsa.pub. The key fingerprint is: fingerprint值 your_email@example.com The key's randomart image is: +--[ RSA 2048]----+ | .+ + | | =oO. | ...
注解:这里第一次输入的是文件名,若是直接按回车则会自动生成私钥和公钥:id_rsa
、id_rsa.pub
。后面跟着的是密码和确认密码。vim
上方的命令执行屡次则会生成多个 SSH KEY。bash
$ cat ~/.ssh/id_rsa.pub ssh-rsa 公开密钥的内容 your_email@example.com
若是建立时输入了文件名,上方的id_rsa
替换成文件名。dom
此处返回的这段内容可用于使用 SSH KEY 的网站,这里不做过多阐述。ssh
像 github
或者 gitlab
等网站都会要求验证身份。一般状况下配置一个全局信息就能够了,针对一些特殊状况,若是须要配置多个身份信息,能够为项目单独配置。ide
$ git config --global user.name "Firstname Lastname" $ git config --global user.email "your_email@example.com"
这个命令会在~/.gitconfig
填入如下信息:gitlab
[user] name = Firstname Lastname email = your_email@example.com
若是须要修改信息,直接修改这个文件便可。网站
$ cd your_project $ git config user.name "Firstname Lastname" $ git config user.email "your_email@example.com"
这个命令会在项目目录下输出文件:/.git/.config
.net
这里设置的姓名和邮箱地址会用在 Git 的提交日志中。
在 ~/.ssh
目录下建立 config
文件:
$ vim ~/.ssh/config
输入如下信息:
Host github.com HostName github.com User git IdentityFile ~/.ssh/id_rsa_a Host git.oschina.net HostName git.oschina.net User git IdentityFile ~/.ssh/id_rsa_b ...
再把对应的公钥添加至对应的网站上面。
注解:未加入配置文件的网站会自动应用id_rsa
。
至此,多个 SSH KEY 就能够同时使用了。
原文地址:一台电脑配置多个SSH KEY,多个用户身份
文章做者:何启邦
版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证) 转载请注明出处