每次我使用git与遥控器交互时,比如拉或推时,我都会看到以下消息:

警告:永久添加'…' (RSA)到已知主机列表。

如何防止显示这个烦人的消息?这只是一个烦恼——一切都很正常。


该消息来自SSH,它警告您正在连接到一个您以前从未连接过的主机。我不建议关闭它,因为这意味着您可能会错过关于主机密钥更改的警告,这可能表明SSH会话受到MITM攻击。


就我所知,你提到的这个问题没有明确的解决办法。 之前建议的/dev/null重定向仍然会显示警告,它只是通过将输出重定向到/dev/null来禁用存储远程密钥的安全特性 所以ssh仍然会认为它写了一些实际上被丢弃的东西。

据我所知,唯一的选择是捕获消息并将其从标准输出中删除。

ssh/scp..... 2>&1 | grep -v "^Warning: Permanently added"

这是一个完整的例子,你可以使用包装器隐藏这样的警告:

#!/bin/bash
remove="^Warning: Permanently added" # message to remove from output

cmd=${0##*/}

case $cmd in
 ssh)
  binary=/usr/bin/ssh
 ;;
 *)
  echo "unsupported binary ($0)"
  exit
 ;;
esac
$binary "$@" 2>&1 | grep -v "$remove"

要安装它,您所需要做的就是为您希望修改的实际命令添加/修改“case”语句。(ssh, scp, git等)。 “ssh”意味着脚本必须命名为“ssh”(或者到脚本的链接命名为ssh)。 binary=/full/path是脚本应该包装的二进制文件的路径。 然后将带有您选择的名称的脚本放到/bin或其他地方。

该脚本还可以在$binary变量中使用-o "UserKnownHostsFile=/dev/null",这比将这样的安全风险放入全局ssh配置中要好得多,这会影响所有ssh会话,而不仅仅是那些您想要抑制消息的会话。

缺点: 这有点开销,不是一个完美的解决方案,并将stderr移动到stdout,这可能不是在所有情况下都很好。 但是它将消除您不希望看到的任何类型的警告消息,并且您可以使用一个脚本来包装您想要的所有二进制文件(通过使用文件系统链接)


在ssh配置文件($HOME/.ssh/config)中添加以下行:

LogLevel=quiet

如果从命令行运行ssh,在命令字符串中添加以下选项:

-o LogLevel=quiet

例如,下面打印在machine.example.org上安装的gcc版本(没有警告):

ssh -o UserKnownHostsFile=/dev/null \
    -o StrictHostKeyChecking=no \
    -o LogLevel=quiet \
    -i identity_file \
    machine.example.org \
    gcc -dumpversion

要屏蔽ssh的警告消息,可以在~/.ssh/config中添加以下行:

Host *
LogLevel error

这将禁用警告,但不会禁用错误消息。就像~/中的其他设置一样。如果你想要一个更细粒度的控制,你可以在每个主机上配置LogLevel。


创建一个~/。Ssh /config文件并插入行:

UserKnownHostsFile ~/.ssh/known_hosts

然后,您将在下次访问Github时看到该消息,但在此之后,您将不再看到它,因为主机已添加到known_hosts文件。这可以修复问题,而不仅仅是隐藏日志消息。

这个问题困扰了我很长时间。出现此问题是因为为Windows编译的OpenSSH客户端没有检查~/.ssh/known_hosts中的known_hosts文件

SSH -vvv git@github.com

debug3: check_host_in_hostfile: filename /dev/null
debug3: check_host_in_hostfile: filename /etc/ssh/ssh_known_hosts
debug3: check_host_in_hostfile: filename /dev/null
debug3: check_host_in_hostfile: filename /etc/ssh/ssh_known_hosts
Warning: Permanently added 'github.com,207.97.227.239' (RSA) to the list of known hosts.

如果你正在使用来自GitHub的存储库,可以考虑使用URL的HTTPS版本,以完全避免这个问题:

如果您从Windows GitHub应用程序中克隆存储库,这就是它用于远程URL的内容。也许他们知道一些我们不知道的事。


这主要意味着该主机的键发生了变化~/。ssh/known_hosts,它不会自动更新它。因此,每当您收到此警告消息时。

这种情况经常发生在连接到重新创建的虚拟机时,它会使用相同的IP地址更改密钥

解决方案

如果只有一个条目,那么可以删除~/。Ssh /known_hosts文件,并且在第一次连接之后,密钥将在那里,之后没有警告消息。

如果有多个条目,则可以使用下面的命令删除

$ ssh-keygen -R <hostname>

这对我来说很有效


在~/中设置LogLevel为ERROR(不是QUIET)。Ssh /config文件,以避免看到这些错误:

Host *
   StrictHostKeyChecking no
   UserKnownHostsFile /dev/null
   LogLevel ERROR

我有同样的问题,我发现没有一个.ssh文件在我的~。所以我只是在~路径下创建了.ssh目录,问题就解决了。


当我开始使用Windows电脑时,我也遇到了同样的问题。在我的例子中,这是因为我的SSH设置没有完成。Github有一个关于SSH设置的非常精确的文档。一旦解决了这个问题,问题就解决了。

https://help.github.com/articles/checking-for-existing-ssh-keys/ https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/


添加ssh密钥

ssh-keygen -t rsa -b 4096 -C "abc@abc.com"

eval "$(ssh-agent -s)"

ssh-add ~/.ssh/bitbucket_rsa

机箱配置文件

crate ~/.ssh/config

加到下面一行。

UserKnownHostsFile ~/.ssh/known_hosts

然后添加pub密钥和克隆您的存储库…做……


我在Linux/Cent操作系统虚拟机中也遇到过同样的错误,这是因为重启后IP发生了变化。为了解决这个问题,我在网络中定义了一个静态IP,并将该条目添加到/etc/hosts文件中。对于静态IP,请提到稍微高一些的范围值。例如,如果您当前的IP (ipconfig/ifconfig)是192.168.0.102,下次重启后可能会变成192.168.0.103。因此,在IPV4设置中定义您的静态IP为192.168.0.181,这应该可以做到这一点。


在我的例子中,这是因为设置服务器的管理员在~/.ssh/config中设置了这些选项

StrictHostKeyChecking no
UserKnownHostsFile /dev/null

在大多数情况下,不使用~/。ssh / known_hosts文件中。但是对于企业gitlab回购,每当它给出“警告:永久添加…”到已知的宿主名单。”

我的解决方案是注释掉UserKnownHostsFile /dev/null行,这允许创建~/.ssh/known_hosts。在那之后,它没有再给出任何警告。

在known_hosts中也可能有旧的/无效的条目。

# find entry in ~/.ssh/known_hosts
ssh-keygen -F <hostname>

# delete entry in ~/.ssh/known_hosts
ssh-keygen -R <hostname>

将您的私钥添加到ssh-agent:

ssh-add ~/.ssh/id_rsa

我的情况下,我只得到ssh警告时使用Gridengine qrsh远程shell登录。然而,正常的ssh会像预期的那样工作(第一次发出警告,然后在随后的时间里保持安静)。

我的解决方案是手动填充~/。ssh/known_hosts与Gridengine可以选择的所有可能的服务器名称(使用qhost列出服务器):

for p in server1 server2 server3 server4; do
  ssh-keyscan -H ${p}.company.com;
  ssh-keyscan -H $(getent hosts $p | perl -lane 'print $F[0]');
done >> ~/.ssh/known_hosts

背景:

Gridengine is a job scheduler which can use ssh to select the least loaded server. The reason for the warning is that qrsh seem to always specify a non-standard port for doing the ssh connection, causing known_hosts to be updated with an entry also containing a port number. Next time when qrsh selects the same server there would be a new port-number and known_hosts would get updated with a new port-specific entry. The reason for also adding the raw host IP address is that some hosts used ecdsa-sha2-nistp521. If a raw IP entry is not added I would get the warning:

ECDSA host key for IP address '10.1.2.3' not in list of known hosts.

您只需要这个命令。

如果是,使用GitHub:

ssh -T git@gitlab.com

如果你使用GitLab:

ssh -T git@gitlab.com