【问题标题】:Load private key manually with ssh使用 ssh 手动加载私钥
【发布时间】:2012-11-26 13:23:42
【问题描述】:

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

例如:

ssh --private-key other_id_rsa login@host

【问题讨论】:

    标签: bash ssh private-key


    【解决方案1】:

    您可以使用-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
    【解决方案2】:

    您还可以为您访问的每个主机添加特定配置,这与持久使用 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
      

      【讨论】:

        猜你喜欢
        • 2023-03-21
        • 2013-02-12
        • 2017-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-24
        • 2016-09-15
        • 2013-03-24
        相关资源
        最近更新 更多