SSH管理多密钥

生成密钥对

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

# 默认情况下在~/.ssh目录下生成id_rsa和id_rsa.pub两个文件
# id_rsa是密钥,id_rsa.pub是公钥

# 生成a密钥对和b密钥对,-f参数设置密钥文件的文件名
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/a_id_rsa
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/b_id_rsa

连接a机器用密钥a,连接b机器用密钥b

# 在~/.ssh目录下编辑config文件
Host a
	HostName 192.158.5.201
	User root
	Port 1234
	IdentityFile ~/.ssh/a_id_rsa
Host b
	HostName 192.158.5.202
	User root
	Port 1234
	IdentityFile ~/.ssh/b_id_rsa
	
# 下面两条命令就可以连上a,b两台机器
ssh a
ssh b

访问github用密钥a,访问gitee用密钥b

# 在~/.ssh/config配置文件中添加如下配置
Host github.com
	IdentityFile ~/.ssh/a_id_rsa
Host gitee.com
	IdentityFile ~/.ssh/b_id_rsa

# 验证, 如果失败可用-vT参数查看失败原因
git -T git@github.com
git -T git@gitee.com

访问多个github账号

  • 设置不同Host对应同一个HostName但密钥不同
  • 取消全局用户名/邮箱设置,为每个仓库独立设置用户名/邮箱
# 在~/.ssh/config配置文件中添加如下配置
Host account1.github.com
	HostName github.com
	IdentityFile ~/.ssh/a_id_rsa
Host account2.github.com
	HostName github.com
	IdentityFile ~/.ssh/b_id_rsa

设置邮箱和用户名

# 设置全局的
git config --global user.email "your_email@example.com"
git config --global user.name "your_name"

# 取消全局的
git config --global --unset user.email
git config --global --unset user.name

# 为每个库设置单独的
git config user.email "your_email@example.com"
git config user.name "your_name"

scp的简化

# 未配置ssh-config之前
scp -P 1234 root@xxx.xxx.xxx.xxx:/abc/def .

# 配置ssh-config之后
scp a:/abc/def .

ssh-agent

ssh-agent bash命令解释: ssh-agent是专为既令人愉快又安全的处理RSA和DSA密钥而设计的特殊程序,不同于ssh,ssh-agent是个长时间持续运行的守护进程(daemon),设计它的唯一目的就是对解密的专用密钥进行高速缓存。ssh包含的内建支持允许它同ssh-agent通信,允许ssh不必每次新连接时都提示您要密码才能获取解密的专用密钥。对于ssh-agent,您只要使用ssh-add把专用密钥添加到ssh-agent的高速缓存中。这是个一次性过程;用过ssh-add之后,ssh将从ssh-agent获取您的专用密钥,而不会提示要密码短语来烦您了。 如果出现如下提示信息说明没有SSH Key在代理中

补:遇到的一个错误,提示信息如下

/root/.ssh/config: line 5: Bad configuration option: identifyfile
/root/.ssh/config: terminating, 1 bad configuration options

# 原因是拼写错误
# IdentifyFile  -->   IdentityFile

参考

posted @ 2018-05-21 04:18  okokabcd  阅读(764)  评论(0编辑  收藏  举报