使用ssh手动加载私钥

13

是否可以在忽略默认的 .ssh 目录的情况下运行 ssh,并指定其他目录或更好地指定私钥?

例如:

ssh --private-key other_id_rsa login@host
3个回答

15
你可以使用-i选项。
来源:man ssh
-i identity_file
    Selects a file from which the identity (private key) for public key authentication is read.  The default is ~/.ssh/identity for protocol
    version 1, and ~/.ssh/id_dsa, ~/.ssh/id_ecdsa and ~/.ssh/id_rsa for protocol version 2.  Identity files may also be specified on a per-
    host basis in the configuration file.  It is possible to have multiple -i options (and multiple identities specified in configuration
    files).  ssh will also try to load certificate information from the filename obtained by appending -cert.pub to identity filenames.

是的,这是一个很好的答案。不确定是否建议这样做,但请确保文件权限正确。例如chmod 400 private_rsa_key.pem 然后 ssh -i private_rsa_key.pem user@host - Daniel

5
你可以为每个访问的主机添加特定的配置,这与持久化ssh中可用标志的使用基本相同。
有许多可用的标志,每个不同的服务专业化都提供了一些映射。在你的情况下,使用特定的id_rsa文件,你可以将其写入到~/.ssh/config文件中:
...

Host host_alias
    HostName host_name
    IdentityFile ~/.ssh/id_rsa_you_want

...

那么,您可以简单地使用:

ssh host_alias

而且将使用id_rsa_you_want,以及您可能应用于连接的任何进一步配置。有关可用指令的完整列表,请参见man ssh_config


3

另一种方法是在使用ssh之前手动使用ssh-agent和ssh-add命令。

ssh-agent (if not running already)
ssh-add /path/to/private_key

example:
ssh-agent
ssh-add ~/.ssh/id_rsa

1
我相信应该是 eval \ssh-agent -s``。这些是反引号。 - ccalvert

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接