ssh【Secure Shell】
ssh 的介绍
secure shell 应用层上实现安全交流的协议
- ssh 连接方式
- 基于口令【帐号密码】
- 基于密钥【也就是我们连上[email protected]时所使用的方法】
ssh 的原理
- SSH:
- server create public key
- receiver get the key and create its’ public and private key.
- resent the ‘receiver’public key to server
server will send the data which is encrypted (use the receiver’s public key encrypt). Receiver get the data and decrypt it with receiver’s private key.
ssh 安全问题
- 通过公钥进行伪装攻击怎么办?
现在普遍的方法是通过将公钥存于证书中,而用户通过去相信一些大型的、可靠的证书机构 GlobalSign、Comodo、Go Daddy、 Digicert 等等
连接事项
系统
- windows:
- 注意使用 git bash 来进行连接操作,cmd 下将无那些指令
- 而用 git bash 默认生成时,文件存在于~/.ssh/id_rsa,需要用指令获取密钥
- Linux:
- linux 的话同 git bash 一步,按流程走即可
具体步骤
- How to connect to github by ssh:
# 这里回车两次,不设置密令,同时这里可以设置生成路径以及密钥名字
ssh-keygen -t rsa -C "yourEmail"
# eval $(ssh-agent -s)等同于下面两句
ssh-agent -s
eval `ssh-agent`
# Linux: you need to make this file touchable
# chmod 600 id_rsa
# 若需要清除之前的key,可以执行ssh-add -D 清除所有key
ssh-add id_rsa
# (you need to add the id_rsa.pub to the github's deployer key)
ssh -T [email protected]
Bug【无法添加】
- 有多个 key 需要添加
通过设置 config,进行详细配置【在~/.ssh 目录下 执行touch config,然后添加以下代码】
# github Host github.com HostName github.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa # gitlab 以此类推进行添加 Host gitlab.....
- 有多个 key 需要添加
生成 key 给谁
- 给对应的仓库 repository,则添加到对应的 deploy key
- 但如果是像我们开发者这种,可以添加到 user settings 里面的 deploy key,这样就可以一劳永逸地上传,更新库了
ssh 的指令解释
ssh-key add id_rsa
添加私钥
ssh -T [email protected]
激活密钥
ssh-add -l
显示所有 key
ssh-add -D
清除所有 key
如何不再重复设置 ssh-key
# 本地的ssh目录
cd ~/.ssh
# 生成ssh key
ssh-keygen -t rsa -C "[email protected]"
# 打印公钥 将公钥添加到github中
cat id_rsa.pub
# 连接github
ssh -T [email protected]