zl程序教程

您现在的位置是:首页 >  后端

当前栏目

ssh config多账户/多域名配置

ssh域名配置 账户 config
2023-06-13 09:11:05 时间

ssh config多账户/多域名配置

作者:matrix 被围观: 2,880 次 发布时间:2019-06-18 分类:Linux | 无评论 »

这是一个创建于 1170 天前的主题,其中的信息可能已经有所发展或是发生改变。

测试环境:ubuntu

客户端连接远程ssh/git服务的时候可以在本地配置SSH config,用于简化多参数使用操作或者修改默认的ssh命令使用的配置。

我这里需要使用gitee的ssh密钥来管理远程仓库代码,方便以后可以免密码提交/更新代码。然而本地已经存在一个~/.ssh/id_rsa私钥,且还设置了用来保护私钥的passphrase密码。如果用之前的私钥连接gitee会造成每次都要求输入passphrase密码,亦或不能单独区分使用。 这个问题可以使用配置文件~/.ssh/config来解决

新建新的密钥对

$ ssh-keygen -t rsa -C "user"

新建的时候设置新密钥的保存路径,避免把之前的覆盖掉

配置

config文件默认是不存在的,直接新建即可

$ vi ~/.ssh/config

使用下面配置:

# gitee账户
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rrsa_gitee

说明: Host类型昵称,可以简化登录的输入地址,比如Host ccl,则可以用ssh ccl直接连接 HostName表示连接的远程主机地址 IdentityFile表示指定私钥文件路径 还有其他参数 Port指定端口 User指定用户名

这种配置可以让ssh来根据远程host地址来使用不同的私钥,设置了User还可以让ssh工具不同用户名来读取配置,也可以使用相同host地址哟~

比如都是github的不同账户,类似配置:

Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/user1_rsa
User user1


Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/user2_rsa
User user2

参考: https://gitee.com/help/articles/4229 http://vra.github.io/2017/07/09/ssh-config/ https://gitee.com/help/articles/4181 https://daemon369.github.io/ssh/2015/03/21/using-ssh-config-file

PEACE~