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

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

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

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

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

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


当前回答

在“~/.”中添加以下行。Bashrc“为我解决了这个问题。我使用的是Ubuntu 14.04桌面。

eval `gnome-keyring-daemon --start`
USERNAME="reynold"
export SSH_AUTH_SOCK="$(ls /run/user/$(id -u $USERNAME)/keyring*/ssh|head -1)"
export SSH_AGENT_PID="$(pgrep gnome-keyring)"

其他回答

在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

对于那些使用Fish shell的程序,你可以使用下面的函数,然后在~/.config/ Fish /config中调用它。Fish或在~/.config/ Fish /conf.d/loadsshkeys.fish中的单独配置文件中。它将把所有以id_rsa开头的密钥加载到ssh-agent中。

# Load all ssh keys that start with "id_rsa"
function loadsshkeys
  set added_keys (ssh-add -l)
   for key in (find ~/.ssh/ -not -name "*.pub" -a -iname "id_rsa*")
    if test ! (echo $added_keys | grep -o -e $key)
      ssh-add "$key"
    end
  end
end

# Call the function to run it.
loadsshkeys

如果您想在打开终端时自动启动ssh-agent,可以使用danhper/fish-ssh-agent来实现这一点。

我在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快速提示中提到的那样 https://help.ubuntu.com/community/QuickTips

What

不需要经常启动ssh-agent和ssh-add,可以使用keychain来管理ssh密钥。要安装keychain,只需点击这里,或使用Synaptic来完成工作或从命令行apt-get。

命令行

安装文件的另一种方法是打开终端(Application->Accessories-> terminal)并输入:

sudo apt-get install keychain

编辑文件

然后,您应该将以下行添加到${HOME}/。Bashrc或/etc/bash.bashrc:

keychain id_rsa id_dsa
. ~/.keychain/`uname -n`-sh

在“~/.”中添加以下行。Bashrc“为我解决了这个问题。我使用的是Ubuntu 14.04桌面。

eval `gnome-keyring-daemon --start`
USERNAME="reynold"
export SSH_AUTH_SOCK="$(ls /run/user/$(id -u $USERNAME)/keyring*/ssh|head -1)"
export SSH_AGENT_PID="$(pgrep gnome-keyring)"