SSH 如何忽略 IdentityFile not found 错误?

bma*_*acs 8 ssh ssh-keys

我目前在我的 .ssh/config 文件中有这个:

Host *
AskPassGUI no
IdentityFile ~/.ssh/%r@%h
IdentityFile ~/.ssh/%h
IdentityFile ~/.ssh/id_dsa
Run Code Online (Sandbox Code Playgroud)

当我通过 ssh 连接到我没有密钥文件的主机时,登录可以正常工作,但我也会收到以下错误:

no such identity: /Users/user/.ssh/user@example.com: No such file or directory
no such identity: /Users/user/.ssh/example.com: No such file or directory
Run Code Online (Sandbox Code Playgroud)

理想情况下,我希望 ssh 检查文件,但如果找不到任何文件,则不会抛出错误。这个想法是能够将名为“user@example.com”或“example.com”的私钥放入我的 .ssh 目录中,并让 ssh 在使用该用户/主机组合登录时使用它们,但不要抱怨和如果文件丢失,则正常登录。我不想使用这个答案中Host描述的指令,因为我有很多关键文件,我不想将它们都添加到文件夹中,然后编辑配置文件并为每个文件添加主机指令。

这样的事情可能吗?

bma*_*acs 9

查阅了openssh的源码后,似乎答案如下:

OpenSSH 认为 ~/.ssh/config 中的 IdentityFile 行是“用户提供的”。如果找不到用户提供的 IdentityFile,它会向控制台记录警告。请参阅 sshconnect2.c 中的“load_identity_file”函数。

所以不幸的是,不可能完全按照我的意愿去做,但存在一些解决方法:

一种方法是LogLevel ERROR将这一行添加到您的 ~/.ssh/config 文件中。这比默认日志级别低一级INFO。我没有选择这个,因为我不确定它会抑制什么其他警告。

我选择的选项是将以下几行添加到我的 /etc/ssh_config 文件中:

Host *
    IdentityFile ~/.ssh/%r@%h
    IdentityFile ~/.ssh/%h

    # The lines below maintain ssh's default behavior:
    IdentityFile ~/.ssh/identity
    IdentityFile ~/.ssh/id_rsa
    IdentityFile ~/.ssh/id_dsa
Run Code Online (Sandbox Code Playgroud)

然后IdentityFile我从 ~/.ssh/config 文件中删除了这些行。

这些行在 /etc/ssh_config 中时不被视为“用户提供”,因此在找不到文件时不会记录任何内容。

  • 您会因查看来源获得 +1 (3认同)