我从网上的无数来源了解到(粗略地),为了在没有密码的情况下登录远程服务器:生成一个 ssh 密钥,将 pub 版本放在远程系统上的authorized_keys中;将私有版本放在本地 ~/.ssh/ 目录中;chmod 它到 0600 然后噗,你就进去了。虽然这基本上是正确的,但我发现密钥(对)必须命名为 id_rsa (id_rsa.pub) 或 id_dsa (id_dsa.pub) 以便 SSH 将其提供给远程服务器。
让我退后一点。SurnameG我在本地 Mac 上有一个登录名。我在远程系统(其他服务器)上有一个 SurnameG 帐户。
我将内容复制~/.ssh/surnameg.pub到该系统的/home/surnameg/.ssh/authorized_keys。我已经-i使用 Mac 上的 ssh 选项进行了测试,效果很好。
我有一个~/.ssh/id_rsa(我生成的用于github.com)。
当然,我什至还有一些其他密钥,当我尝试通过以下方式登录otherserver.com~/.ssh/surnameg时,这些密钥没有被“尝试” :
ssh 1.2.3.4
Run Code Online (Sandbox Code Playgroud)
在这里,我尝试使用SurnameG(当前本地登录用户)登录到我在otherserverSurnameG上的帐户。我希望 openssh 在尝试连接时提供,但它没有 - 让我们仔细看看详细选项:~/.ssh/surnameg
BOX:~ SurnameG$ ssh -v 1.2.3.4
OpenSSH_5.9p1, OpenSSL 0.9.8y 5 Feb 2013
debug1: Reading configuration data /Users/SurnameG/.ssh/config
debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 20: Applying options for *
debug1: Connecting to 1.2.3.4 [50.112.132.124] port 22.
debug1: Connection established.
debug1: identity file /Users/SurnameG/.ssh/id_rsa type 1
debug1: identity file /Users/SurnameG/.ssh/id_rsa-cert type -1
debug1: identity file /Users/SurnameG/.ssh/id_dsa type -1
debug1: identity file /Users/SurnameG/.ssh/id_dsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.9
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: RSA ax:64:3e:4a:e3:2c:e4:30:dd:36:a4:a0:9x:fa:ba:6b
debug1: Host '1.2.3.4' is known and matches the RSA host key.
debug1: Found key in /Users/SurnameG/.ssh/known_hosts:88
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/SurnameG/.ssh/id_rsa
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic
debug1: Trying private key: /Users/SurnameG/.ssh/id_dsa
debug1: No more authentication methods to try.
Run Code Online (Sandbox Code Playgroud)
奇怪的是,openssh 只提供 ~/.ssh/id_rsa,但这还不是全部。它也“尝试”〜/.ssh/id_dsa?我不确定两者之间的区别(提供与尝试)无论如何,如果我正确阅读,openssh 永远不会尝试我在~/.ssh/*.
好的,我知道我可以~/.ssh/config用类似的内容明确描述每个服务器
Host otherserver
HostName 1.2.3.4
User surnameg
IdentityFile ~/.ssh/surnameg
Run Code Online (Sandbox Code Playgroud)
然后登录
ssh otherserver
Run Code Online (Sandbox Code Playgroud)
这很好,很花花公子,而且效果很好。但实际上,我的 ~/.ssh/config 变得越来越笨拙。cd ~/.ssh && git init可悲的是我很久以前没有。但我离题了。
我的问题是:是否有一种更简单、更快、更自动化的方法让 ssh 在尝试登录时动态尝试 ~/.ssh 目录中的更多密钥,或者为您需要的每个服务器编辑 ~/.ssh/config连接到配置ssh的唯一方法?我是否误解了上面关于 SSH 应该如何工作的任何内容?
如果您想为所有主机提供相同的密钥,请使用 将它们加载到 SSH 代理中ssh-add。许多 Linux 发行版都会自动启动一个 - 尝试ssh-add -l检查它是否正在运行,然后加载您的密钥:
ssh-add ~/.ssh/id_rsa ~/.ssh/surnameg etc.
Run Code Online (Sandbox Code Playgroud)
如果代理没有自动启动,请将以下内容放入您的~/.profile:
agent_running() {
[ "$SSH_AUTH_SOCK" ] && { ssh-add -l >/dev/null 2>&1 || [ $? -eq 1 ]; }
}
env=~/.ssh/agent.env
if ! agent_running && [ -s "$env" ]; then
. "$env" >/dev/null
fi
if ! agent_running; then
ssh-agent >"$env"
. "$env" >/dev/null
ssh-add ~/.ssh/id_*
fi
unset env
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
702 次 |
| 最近记录: |