Jus*_*tin 816 ssh openssh ssh-keys
我想使用多个私钥连接到不同的服务器或同一服务器的不同部分(我的用途是服务器的系统管理,Git的管理,以及同一服务器内的正常Git使用).我试过简单地将id_rsa文件中的密钥堆叠起来无济于事.
显然,一种直接的方法是使用该命令
ssh -i <key location> login@server.example.com
Run Code Online (Sandbox Code Playgroud)
这非常麻烦.
关于如何更轻松地做这件事的任何建议?
Ran*_*rtz 1161
从我的.ssh/config:
Host myshortname realname.example.com
HostName realname.example.com
IdentityFile ~/.ssh/realname_rsa # private key for realname
User remoteusername
Host myother realname2.example.org
HostName realname2.example.org
IdentityFile ~/.ssh/realname2_rsa # different private key for realname2
User remoteusername
Run Code Online (Sandbox Code Playgroud)
等等.
小智 322
您可以指示ssh在连接时连续尝试多个键.这是如何做:
$ cat ~/.ssh/config
IdentityFile ~/.ssh/id_rsa
IdentityFile ~/.ssh/id_rsa_old
IdentityFile ~/.ssh/id_ed25519
# ... and so on
$ ssh server.example.com -v
....
debug1: Next authentication method: publickey
debug1: Trying private key: /home/example/.ssh/id_rsa
debug1: read PEM private key done: type RSA
debug1: Authentications that can continue: publickey
debug1: Trying private key: /home/example/.ssh/id_rsa_old
debug1: read PEM private key done: type RSA
....
[server ~]$
Run Code Online (Sandbox Code Playgroud)
这样您就不必指定哪个键可以与哪个服务器一起使用.它只会使用第一个工作密钥.
如果给定服务器愿意接受密钥,您也只能输入密码.如上所示,.ssh/id_rsa即使有密码,ssh也没有尝试要求输入密码.
当然,它不像其他答案那样超出每服务器配置,但至少你不必为你连接的所有服务器添加配置!
per*_*ron 249
兰德尔施瓦茨的答案几乎帮助了我.我在服务器上有不同的用户名,所以我必须将User关键字添加到我的文件中:
Host friendly-name
HostName long.and.cumbersome.server.name
IdentityFile ~/.ssh/private_ssh_file
User username-on-remote-machine
Run Code Online (Sandbox Code Playgroud)
现在您可以使用友好名称进行连接:
ssh friendly-name
Run Code Online (Sandbox Code Playgroud)
可以在OpenSSH手册页上找到更多关键字.注意:列出的某些关键字可能已存在于/ etc/ssh/ssh_config文件中.
小智 99
foo:~$ssh-add ~/.ssh/xxx_id_rsa
Run Code Online (Sandbox Code Playgroud)
确保在添加之前测试它:
ssh -i ~/.ssh/xxx_id_rsa username@example.com
Run Code Online (Sandbox Code Playgroud)
如果您有任何错误问题,有时更改文件的安全性有助于:
chmod 0600 ~/.ssh/xxx_id_rsa
Run Code Online (Sandbox Code Playgroud)
obl*_*ion 96
之前的答案已正确解释了创建配置文件以管理多个ssh密钥的方法.我认为,还需要解释的重要事情是在克隆存储库时用别名替换主机名.
假设,贵公司的GitHub帐户的用户名是abc1234.假设您的个人GitHub帐户的用户名是jack1234
并且,假设您已创建了两个RSA密钥,即id_rsa_company和id_rsa_personal.因此,您的配置文件如下所示:
# Company account
Host company
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_company
# Personal account
Host personal
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_personal
Run Code Online (Sandbox Code Playgroud)
现在,当您从公司的GitHub帐户克隆存储库 (名为demo)时,存储库URL将类似于:
Repo URL: git@github.com:abc1234/demo.git
Run Code Online (Sandbox Code Playgroud)
现在,在执行操作时git clone,您应该将上述存储库URL修改为:
git@company:abc1234/demo.git
Run Code Online (Sandbox Code Playgroud)
请注意github.com现在如何替换为我们在配置文件中定义的别名"company".
同样,您必须根据配置文件中提供的别名修改个人帐户中存储库的克隆URL.
Saj*_*han 33
生成SSH密钥:
$ ssh-keygen -t rsa -C <email1@example.com>
Run Code Online (Sandbox Code Playgroud)生成another SSH key:
$ ssh-keygen -t rsa -f ~/.ssh/accountB -C <email2@example.com>
Run Code Online (Sandbox Code Playgroud)
现在,目录中应该存在两个公钥(id_rsa.pub,accountB.pub)~/.ssh/.
$ ls -l ~/.ssh # see the files of '~/.ssh/' directory
Run Code Online (Sandbox Code Playgroud)~/.ssh/config使用以下内容创建配置文件:
$ nano ~/.ssh/config
Host bitbucket.org
User git
Hostname bitbucket.org
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
Host bitbucket-accountB
User git
Hostname bitbucket.org
PreferredAuthentications publickey
IdentitiesOnly yes
IdentityFile ~/.ssh/accountB
Run Code Online (Sandbox Code Playgroud)从default帐户克隆.
$ git clone git@bitbucket.org:username/project.git
Run Code Online (Sandbox Code Playgroud)从accountB帐户克隆.
$ git clone git@bitbucket-accountB:username/project.git
Run Code Online (Sandbox Code Playgroud)Wah*_*Haq 22
我同意Tuomas关于使用ssh-agent的观点.我还想为工作添加第二个私钥,这个教程对我来说就像一个魅力.
步骤如下:
$ ssh-agent bash$ ssh-add /path.to/private/key 例如 ssh-add ~/.ssh/id_rsa$ ssh-add -l$ssh -v <host url>例如测试它ssh -v git@assembla.comAna*_*Pai 14
我曾经遇到过这个问题,当时我有两个Bitbucket帐户并且想要为两者存储单独的SSH密钥.这对我有用.
我创建了两个单独的ssh配置,如下所示.
Host personal.bitbucket.org
HostName bitbucket.org
User git
IdentityFile /Users/username/.ssh/personal
Host work.bitbucket.org
HostName bitbucket.org
User git
IdentityFile /Users/username/.ssh/work
Run Code Online (Sandbox Code Playgroud)
现在,当我不得不从我的工作帐户克隆存储库时 - 命令如下.
git clone git@bitbucket.org:teamname/project.git
Run Code Online (Sandbox Code Playgroud)
我不得不将此命令修改为:
git clone git@**work**.bitbucket.org:teamname/project.git
Run Code Online (Sandbox Code Playgroud)
同样,我的个人帐户中的克隆命令必须修改为
git clone git @ personal .bitbucket.org:name/personalproject.git
有关更多信息,请参阅此链接.
现在,使用最新版本的git,我们可以在存储库特定的git配置文件中指定sshCommand。
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
sshCommand = ssh -i ~/.ssh/id_rsa_user
[remote "origin"]
url = git@bitbucket.org:user/repo.git
fetch = +refs/heads/*:refs/remotes/origin/*
Run Code Online (Sandbox Code Playgroud)
这是我使用的解决方案,灵感来自sajib-khan 的答案。未设置默认配置;它是我在GitLab上的个人帐户,另一个指定的是我的公司帐户。这是我所做的:
ssh-keygen -t rsa -f ~/.ssh/company -C "name.surname@company.com"
Run Code Online (Sandbox Code Playgroud)
nano ~/.ssh/config
Run Code Online (Sandbox Code Playgroud)
Host company.gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/company
Run Code Online (Sandbox Code Playgroud)
ssh-add -D
Run Code Online (Sandbox Code Playgroud)
ssh -T git@company.gitlab.com
Run Code Online (Sandbox Code Playgroud)
欢迎来到 GitLab,@hugo.sohm!
ssh -T git@gitlab.com
Run Code Online (Sandbox Code Playgroud)
欢迎来到 GitLab,@HugoSohm!
公司账户
git clone git@company.gitlab.com:group/project.git
Run Code Online (Sandbox Code Playgroud)
个人/默认帐户
git clone git@gitlab.com:username/project.git
Run Code Online (Sandbox Code Playgroud)
这是我使用的来源。
对我来说,唯一可行的解决方案是简单地将其添加到文件中~/.ssh/config:
Host *
IdentityFile ~/.ssh/your_ssh_key
IdentityFile ~/.ssh/your_ssh_key2
IdentityFile ~/.ssh/your_ssh_key3
AddKeysToAgent yes
Run Code Online (Sandbox Code Playgroud)
your_ssh_key没有任何扩展名。不要使用.pub.
| 归档时间: |
|
| 查看次数: |
399921 次 |
| 最近记录: |