为某些命令配置SSH配置文件



我的问题是以下。我希望配置.ssh/config这样,当我写

ssh examplex

就像我写

一样

ssh -i/path/to/key.pem用户@地址

请注意,上述命令有效。

按照此处的答案,我尝试将文件创建为

Host exampleX
    HostName address
    User user
    IdentityFile /path/to/key.pem

取自

ssh -i/path/to/key.pem用户@地址

但是,当我运行

ssh examplex

我得到错误

ssh:无法解析主机名examplex:名称或服务未知

但是,如果我手动运行命令

ssh -i/path/to/key.pem用户@地址

一切都起作用。我在哪里创建文件的错误?

编辑

如果我运行

sudo ssh exampleX -v

我得到输出

OpenSSH_7.2p2 Ubuntu-4ubuntu2.2, OpenSSL 1.0.2g  1 Mar 2016
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
ssh: Could not resolve hostname exampleX: Name or service not known

但是,如果我在没有sudo的情况下运行它,我会得到更长的流,那以

结束
debug1: Authentications that can continue: publickey
debug1: Trying private key: /path/to/key.pem
Load key "/path/to/key.pem": Permission denied
debug1: No more authentication methods to try.
Permission denied (publickey).

编辑2

由于有些混乱,我重述了我的问题

配置文件必须是什么样的,以便运行

ssh exampleX

将与运行

相同
ssh -i /path/to/key.pem user@address

通过sudo运行命令时,您使用的是与sudo运行的用户相对应的.ssh/config文件。如果您真的需要以root运行此SSH命令,则需要添加到~root/.ssh/config而不是~/.ssh/config的配置。

如果可能的话,将ssh作为普通用户运行,而不是root

(由于编辑了问题,我对我的答案进行了相应的编辑)

  1. 检查文件的权限〜/.ssh/config :它必须具有严格的权限:为用户读/写,而不是其他人访问,如<< MAN 页面。

  2. 还要检查您对文件/path/path/key.pem 的读取访问(作为用户)。您使用 ssh 使用的调试选项建议您没有。

最新更新