先说点没用的
什么是SSH?
简单说,SSH是一种网络协议,用于计算机之间的加密登录。
如果一个用户从本地计算机,使用SSH协议登录另一台远程计算机,我们就可以认为,这种登录是安全的,即使被中途截获,密码也不会泄露。
最早的时候,互联网通信都是明文通信,一旦被截获,内容就暴露无疑。1995年,芬兰学者Tatu Ylonen设计了SSH协议,将登录信息全部加密,成为互联网安全的一个基本解决方案,迅速在全世界获得推广,目前已经成为Linux系统的标准配置。
image需要指出的是,SSH只是一种协议,存在多种实现,既有商业实现,也有开源实现。
上图是SSH的加密原理图,此处关于原理不多说,我他么也不是很懂。
二. 配置SSH key
首先检查电脑是否有SSH key,打开终端
$ cd ~/.ssh
$ ls
如果有 id_rsa.pub或id_dsa.pub,说明本地已经有SSH key 可以直接跳过第二步。
创建一个SSH key
$ ssh-keygen -t rsa -C "your_email@example.com"
代码参数含义:
-t 指定密钥类型,默认是 rsa ,可以省略。
-C 设置注释文字,比如邮箱。
-f 指定密钥文件存储文件名。
以上代码省略了 -f 参数,因此,运行上面那条命令后会让输入一个文件名,用于保存刚才生成的 SSH key 代码,如:
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]`
可以选择不输入文件名,使用默认文件名(推荐)。
接着又会提示输入两次密码(该密码是push文件的时候要输入的密码,而不是github管理者的密码),
当然,也可以不输入密码,直接按回车。那么push的时候就不需要输入密码,直接提交到github上了,如:
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
然后会有如下提示:
Your identification has been saved in /c/Users/you/.ssh/id_rsa.
Your public key has been saved in /c/Users/you/.ssh/id_rsa.pub.
The key fingerprint is:
01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com
添加SSH key到Github或Coding上
1.首先需要拷贝 id_rsa.pub 文件的内容,你可以用编辑器打开文件复制,也可以用git命令复制该文件的内容,如:
$ clip < ~/.ssh/id_rsa.pub
2.登录Github或Coding,在账号设置里设置SSH key。
测试SSH key
如果是Github的话,打开终端,输入:
$ ssh -T git@github.com
然后提示:
The authenticity of host 'github.com (207.97.227.239)' can't be established.
# RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
# Are you sure you want to continue connecting (yes/no)?
这是正常的,输入 yes 回车即可。如果创建 SSH key 的时候设置了密码,接下来就会提示输入密码:
Enter passphrase for key '/Users/Administrator/.ssh/id_rsa':
当然如果密码输错了,会再要求输入,知道对了为止。
注意:输入密码时如果输错一个字就会不正确,使用删除键是无法更正的。
密码正确后会看到下面这段话:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
如果是Coding的话 在终端输入:
$ ssh -T git@git.coding.net
如果提示:
Coding 提示: Hello username, You've connected to Coding.net via SSH. This is a personal key.
username,你好,你已经通过 SSH 协议认证 Coding.net 服务,这是一个个人公钥
那么就设置成功了
一台电脑管理多个SSH key
使用环境:关于同一台电脑Linux系统下使用多个SSH key 切换使用(或者多用户使用ssh提交代码)
要求可以创建不同的 PUBLIC KEY ,根据下面步骤设置.
比如有aaa,bbb,ccc 三个帐号需要生成不同的 PUBLIC KEY
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):/root/.ssh/id_rsa_aaa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa_aaa.
Your public key has been saved in /root/.ssh/id_rsa_aaa.pub.
The key fingerprint is:
9b:92:f6:1f:d2:72:bd:72:19:45:42:5f:e4:65:33:64 root@AY140122145815620396Z
The key's randomart image is:
+--[ RSA 2048]----+
| .. .E=|
| ..o++|
| o. .|
| . |
| S . |
| . + .. |
| + = + .o |
| . o +..o. |
| ...o. |
+-----------------+
经过上面的shell 我们在 /root/.ssh/ 目录下创建 id_rsa_aaa 私钥 和 id_rsa_aaa.pub 公钥
注意这里
Enter file in which to save the key (/root/.ssh/id_rsa):/root/.ssh/id_rsa_aaa #设置路径,如果不设置默认生成 id_rsa 和 id_rsa.pub
照上面的步骤逐个生成 bbb 和 ccc 对应的公钥和私钥。
查看系统ssh-key代理,执行如下命令:
$ ssh-add -l
Could not open a connection to your authentication agent.
如果发现上面的提示,说明系统代理里没有任何key,执行如下操作
exec ssh-agent bash
如果系统已经有ssh-key 代理 ,执行下面的命令可以删除:
$ ssh-add -D
把 .ssh 目录下的3个私钥添加的 ssh-agent
$ ssh-add ~/.ssh/id_rsa_aaa
$ ssh-add ~/.ssh/id_rsa_bbb
$ ssh-add ~/.ssh/id_rsa_ccc
依次执行上面三条shell 把三个私钥添加到 ssh-key 代理里面
打开Github 或者 Coding ssh 管理页面把 对应的公钥提交保存到代码管理服务器 (.pub 结尾)
在 .ssh 目录创建 config 配置文件
nano ~/.ssh/config
输入如下配置信息
#aaa (github 配置)
Host aaa
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_aaa
#bbb (Coding 配置)
Host bbb
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_bbb
#ccc
........
然后可以测试下SSH key是否配置成功,参考步骤2.4
Sourcetree和 SSH key
Sourcetree生成的SSH key 在相关网站上配置后,需要在终端手动添加,不然电脑重启之后就悲剧了。
ssh-add -K id_rsa.github
参考链接
(http://yijiebuyi.com/blog/f18d38eb7cfee860c117d629fdb16faf.html)
(https://help.github.com/articles/connecting-to-github-with-ssh/)
网友评论