Git 说“警告:永久添加到已知主机列表”
每次我使用 git 与远程交互时,例如拉或推时,都会显示以下消息:
警告:已将“...”(RSA) 永久添加到已知主机列表中。
如何防止显示这条烦人的消息?这只是一个烦恼——一切都运转正常。
Every time I use git to interact with a remote, such as when pulling or pushing, I am shown the following message:
Warning: Permanently added '...' (RSA) to the list of known hosts.
How can I prevent this annoying message from displaying? It is only an annoyance—everything functions properly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
创建一个
~/.ssh/config
文件并插入以下行:下次访问 Github 时您将看到该消息,但之后您将不再看到它,因为主机已添加到
known_hosts
文件。这解决了问题,而不仅仅是隐藏日志消息。这个问题困扰了我很长一段时间。出现此问题的原因是针对 Windows 编译的 OpenSSH 客户端未检查
~/.ssh/known_hosts
中的known_hosts 文件ssh -vvv [电子邮件受保护]
Create a
~/.ssh/config
file and insert the line:You will then see the message the next time you access Github, but after that you'll not see it anymore because the host is added to the
known_hosts
file. This fixes the issue, rather than just hiding the log message.This problem was bugging me for quite some time. The problem occurs because the OpenSSH client compiled for Windows doesn't check the known_hosts file in
~/.ssh/known_hosts
ssh -vvv [email protected]
将以下行添加到您的 ssh 配置文件 ($HOME/.ssh/config):
如果从命令行运行 ssh,请将以下选项添加到命令字符串:
例如,以下内容打印出 machine.example 上安装的 gcc 版本.org(并且没有警告):
Add the following line to your ssh config file ($HOME/.ssh/config):
If running ssh from the command line add the following option to the command string:
For example, the following prints out the gcc version installed on machine.example.org (and no warning):
在
~/.ssh/config
文件中将LogLevel
设置为ERROR
(而不是QUIET
)以避免看到这些错误:Set
LogLevel
toERROR
(notQUIET
) in~/.ssh/config
file to avoid seeing these errors:该消息来自 SSH,它警告您正在连接到以前从未连接过的主机。我不建议将其关闭,因为这意味着您可能会错过有关主机密钥更改的警告,这可能表明您的 SSH 会话受到 MITM 攻击。
That message is from SSH, which is warning you that you are connecting to a host which you've never connected to before. I wouldn't recommend turning it off, since it would mean that you might miss a warning about a host key changing, which can indicate a MITM attack on your SSH session.
要抑制
ssh
的警告消息,您可以将以下行添加到~/.ssh/config
中:这将禁用警告,但不会禁用错误消息。与
~/.ssh/config
中的其他设置一样,如果您想要更细粒度的控制,您可以在每个主机的基础上配置LogLevel
。To suppress warning messages for
ssh
you can add the following lines to~/.ssh/config
:That will disable warnings but not error messages. Like the other settings in
~/.ssh/config
you can configure theLogLevel
on a per-host basis if you want a more finegrained control.这主要意味着该主机
~/.ssh/known_hosts
的密钥发生了变化,并且不会自动更新它。因此,每次您收到此警告消息时。这种情况经常发生在连接到重新创建的虚拟机时,它会更改具有相同 IP 地址的密钥
解决方案
如果您只有一个条目,那么您可以删除
~/.ssh /known_hosts
文件,并且在第一次连接后,密钥将在那里,并且此后不会出现警告消息。如果您有多个条目,那么您可以使用下面的命令来删除
它对我来说效果很好
It mainly means there are changes for the key for that host
~/.ssh/known_hosts
, and it will not automatically UPDATE it. Therefore every time you get this warning message.This happens often for the connecting to the re-created virtual machines, which changes the key with the same IP address
Solution
If you only have one entry, then you can delete the
~/.ssh/known_hosts
file, and after first connection, that the key will be there, and no warning messages after that.If you have multiple entries, then you can use command below to remove
It works fine for me
添加 ssh 密钥
箱配置文件
添加以下行。
然后添加公钥并克隆您的存储库...完成......
Add ssh key
crate config file
add below line.
Then add pub key and clone your repository... Done.....
将您的私钥添加到 ssh-agent 中:
add your private key to the ssh-agent with:
如果您使用的是 GitHub 中的存储库,请考虑使用 URL 的 HTTPS 版本,以完全避免此问题:
如果您从 Windows GitHub 应用程序中克隆存储库,这就是它用于远程 URL 的内容。也许他们知道一些我们不知道的事情。
If you are using a repository from GitHub, consider using the HTTPS version of the URL instead, to sidestep this problem entirely:
If you clone your repository from within the Windows GitHub application, this is what it uses for the remote URL. Maybe they know something we don't know.
当我开始使用 Windows 机器时,我遇到了同样的问题。就我而言,这是因为我的 SSH 设置尚未完成。 Github 有关于 SSH 设置的非常精确的文档。一旦解决了这个问题,问题就解决了。
https://help.github.com/articles/checking-for -现有-ssh-密钥/
https ://help.github.com/articles/generate-a-new-ssh-key-and-adding-it-to-the-ssh-agent/
I got into the same issue when I started using a Windows machine. In my case it was because my SSH setup was not done. Github has a very precise documentation on the SSH setup. Once that's taken care, the issue was resolved.
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/config
中设置了这些选项,在大多数情况下,不使用
~/.ssh/known_hosts 效果很好。
文件。但对于企业版gitlab仓库,每次都会给出“警告:永久添加......到已知主机列表”。我的解决方案是注释掉
UserKnownHostsFile /dev/null
行,这允许创建~/.ssh/known_hosts
。之后就没有再发出任何警告。您的
known_hosts
中可能还存在旧的/无效的条目。In my case, it was because the admin who set up the server set these options in
~/.ssh/config
Which worked fine for most cases by not using the
~/.ssh/known_hosts
file. But for the enterprise gitlab repo, every time it gave the "Warning: Permanently added ... to the list of known hosts."My solution was to comment out the
UserKnownHostsFile /dev/null
line, which allowed the creation of~/.ssh/known_hosts
. Then it didn't give any more warnings after that.You might also have a old/invalid entries in your
known_hosts
.就我而言,我仅在使用 Gridengine
qrsh
远程 shell 登录时收到 ssh 警告。而普通的 ssh 会按预期工作(第一次发出警告,然后随后安静)。我的解决方案是手动填写 Gridengine 可以选择的所有可能的服务器名称(使用 qhost 列出服务器):
背景:
Gridengine 是一个作业调度程序可以使用 ssh 选择负载最少的服务器。发出警告的原因是
qrsh
似乎总是指定一个非标准端口来进行 ssh 连接,导致known_hosts
使用也包含端口号的条目进行更新。下次当qrsh
选择同一服务器时,将会有一个新的端口号,并且known_hosts
将使用新的特定于端口的条目进行更新。还添加原始主机 IP 地址的原因是某些主机使用了 ecdsa-sha2-nistp521。如果未添加原始 IP 条目,我会收到警告:I my case I only got the ssh warning when using Gridengine
qrsh
remote shell login. Whereas a normalssh
would work as expected (warning first time, then quiet subsequent times).My solution was to manually fill
~/.ssh/known_hosts
with all the possible server names that Gridengine could choose (useqhost
to list the servers):Background:
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, causingknown_hosts
to be updated with an entry also containing a port number. Next time whenqrsh
selects the same server there would be a new port-number andknown_hosts
would get updated with a new port-specific entry. The reason for also adding the raw host IP address is that some hosts usedecdsa-sha2-nistp521
. If a raw IP entry is not added I would get the warning:我在 Linux/Cent OS VM 中遇到了同样的错误,这是因为重启后 IP 发生了变化。为了解决这个问题,我在网络中定义了一个静态 IP 并将该条目添加到
/etc/hosts
文件中。对于静态 IP,请提及稍高的范围值。例如,如果您当前的 IP (ipconfig/ifconfig) 是 192.168.0.102,下次重新启动后可能会变为 192.168.0.103。因此,在 IPV4 设置中将静态 IP 定义为 192.168.0.181 即可。I had faced the same error in Linux/Cent OS VM and it was because the IP was changing after restart. In order to get around this problem, I defined a static IP in the network and added that entry to
/etc/hosts
file. For static IP mention a slightly higher range value. For example if your current IP (ipconfig/ifconfig) is 192.168.0.102, next time after restart this may become 192.168.0.103. So define your static IP in IPV4 settings as 192.168.0.181 which should do the trick.你只需要这个命令。
如果是,请使用 GitHub:
如果您使用 GitLab:
you just need this command.
If it is, use GitHub:
If you use GitLab:
据我所知,对于您提到的问题没有干净的解决方案。
之前建议的 /dev/null 重定向仍然会显示警告,它只是通过将输出重定向到 /dev/null 来禁用存储远程密钥的安全功能。
所以 ssh 仍然会认为它写了一些实际上被丢弃的东西。
据我所知,唯一的选择是捕获消息并将其从标准输出中删除。
这是一个完整的示例,您可以将其用作包装器来隐藏此类警告:
要安装它,您所需要做的就是添加/修改您要修改的实际命令的“case”语句。 (ssh、scp、git 等)。
“ssh)”表示脚本必须命名为“ssh”(或者脚本的链接名为 ssh)。
binary=/full/path 是脚本应包装的二进制文件的路径。
然后将具有您选择的名称的脚本放入 /bin 或其他位置。
该脚本还可以在 $binary 变量中使用 -o "UserKnownHostsFile=/dev/null" ,这比将此类安全风险放入全局 ssh 配置中要好得多。影响您所有的 ssh 会话,而不仅仅是那些您想要抑制消息的会话。
缺点:
这是一个有点开销,不是一个完全干净的解决方案,并将 stderr 移到 stdout 中,这可能并不在所有情况下都很好。
但它将消除您不希望看到的任何类型的警告消息,并且您可以使用单个脚本来包装您想要的所有二进制文件(通过使用指向它的文件系统链接)
There is no clean solution for the problem you noted as far as I am aware.
The previously suggested /dev/null redirection will still display the warning, it just disables the security feature of storing the remote keys by redirecting the output into /dev/null.
So ssh would still think it writes something which is actually discarded.
As I know the only option is to catch the message and remove it from stdout.
Here is a complete example that you can use as wrapper to hide such warnings:
To install it all you need to do is add/modify the "case" statement for the actual command you wish to modify. (ssh, scp, git etc).
the "ssh)" means the script has to be named "ssh" (or a link to the script is named ssh).
The binary=/full/path is the path to the binary the script should wrap.
Then put the script with a name of your choice into /bin or somewhere else.
The script also the place where you can use a -o "UserKnownHostsFile=/dev/null" to the $binary variable, that's a lot better than putting such a security risk into the global ssh configuration which will affect all your ssh sessions and not just those you want to supress the message.
Disadvantages:
It's a bit overhead, not a perfectly clean solution and moves stderr into stdout which might not be good in all cases.
But it will get rid of any sort of warning messages you don't wish to see and you can use a single script to wrap all binaries you want (by using filesystem links to it)