我有一个用密码保护的私钥,可以通过SSH访问服务器。

我有2台linux (ubuntu 10.04)机器,ssh-add命令的行为在它们中都是不同的。

在一台机器上,一旦我使用“ssh-add .ssh/identity”并输入密码,密钥就被永久添加了,即每次我关闭计算机并再次登录时,密钥就已经添加了。

在另一个系统中,我必须在每次登录时添加密钥。

在我的记忆中,我对两个都做了同样的事情。唯一的区别是,密钥是在永久添加的密钥上创建的。

有人知道如何将它永久地添加到另一台机器吗?


当前回答

我在Ubuntu 16.04上也遇到了同样的问题:一些密钥是永久添加的,对于其他密钥,我必须在每次会话中执行ssh-add。我发现永久添加的密匙同时具有位于~/中的私钥和公钥。SSH和在每个会话上被遗忘的密钥在~/中只有私钥。ssh dir。因此解决方案很简单:您应该将私钥和公钥复制到~/。执行SSH -add前,请先执行SSH命令。

附注:据我从Gnome wiki了解,我的方法是通过Gnome -keyring工具工作的,这是Gnome桌面环境的一部分。因此,我的方法可能只适用于使用Gnome或基于Gnome的DE。

其他回答

在Mac OS X Lion系统下,这并没有为我解决同样的问题。最后我补充道:

ssh-add ~/.ssh/id_rsa &>/dev/null

到我的.zshrc(但.profile也可以),这似乎已经修复了它。

(建议地址:http://geek.michaelgrace.org/2011/09/permanently-add-ssh-key-ssh-add/)

解决方案是通过将密钥文件添加到~/中,强制永久保存密钥文件。ssh /配置文件:

IdentityFile ~/.ssh/gitHubKey
IdentityFile ~/.ssh/id_rsa_buhlServer

如果在~/. conf文件中没有'config'文件。SSH目录,那么您应该创建一个。它不需要根权限,所以很简单:

nano ~/.ssh/config

...并按您的要求输入上面的行。

要做到这一点,文件需要chmod 600。可以使用chmod 600 ~/.ssh/config命令。

如果您希望计算机上的所有用户都使用该密钥,请将这些行放到/etc/ssh/ssh_config中,并将密钥放在所有人都可以访问的文件夹中。

此外,如果您想设置特定于一台主机的键,可以在~/中执行以下操作。ssh / config:

Host github.com
    User git
    IdentityFile ~/.ssh/githubKey

当您有许多身份时,这样做具有优势,服务器不会因为您首先尝试了错误的身份而拒绝您。只会尝试特定的身份。

我使用两个id_rsa密钥运行Ubuntu。(工作用一个)。 Ssh-add每次都会记住一个密钥(个人密钥),而忘记公司密钥。

查看两者之间的差异,我发现我的个人密钥有400个权限,而公司密钥有600个权限。(u + w)。 从公司密钥中删除用户写入权限(u-w或设置为400)解决了我的问题。Ssh-add现在记住两个键。

我在Mac OSX(10.10)上通过使用-K选项ssh-add解决了这个问题:

ssh-add -K ~/.ssh/your_private_key

对于macOS 10.12及以上版本,您需要额外编辑您的ssh配置,如下所示:https://github.com/jirsbek/SSH-keys-in-macOS-Sierra-keychain

在Ubuntu 14.04(也许更早,也许仍然如此)你甚至不需要控制台:

start seahorse or launch that thing you find searching for "key" create an SSH key there (or import one) no need to leave the passphrase empty it is offered to you to even push the public key to a server (or more) you will end up with an ssh-agent running and this key loaded, but locked using ssh will pickup the identity (i.e. key) through the agent on first use during the session, the passphrase will be checked and you have the option to automatically unlock the key on login this means the login auth will be used to wrap the passphrase of the key note: if you want to forward your identity (i.e. agent-forwarding) invoke your ssh with -A or make that the default otherwise you can't authenticate with that key on a machine you login to later to a third machine