ssh 报Connection closed by 127.0.0.1 port 的解决办法

起了一个虚拟机,想要通过ssh从host连接。

起虚拟机时加上如下命令行:

-netdev user,id=net0,hostfwd=tcp::2222-:22 \
        -device virtio-net-pci,netdev=net0 \

这样就将host的2222端口转发给虚拟机的22端口。

ssh root@localhost -p 2222

得到Connection closed by 127.0.0.1 port 2222的错误。可能的问题在于:

1 虚拟机里的sshd服务没有正常运行,可通过命令查看systemctl status ssh, 如果没有开启就使用命令:systemctl start ssh

2网络端口没有正常监听,在host上使用命令:netstat -nlt 查看是不是存在0.0.0.0:2222的类似条目。

3虚拟机的ssh配置问题。

我在虚拟机中运行systemctl status ssh,得到一大堆error

Jan 10 03:49:56 ubuntu sshd[1195]: error: key_load_private: invalid format
Jan 10 03:49:56 ubuntu sshd[1195]: error: key_load_public: invalid format
Jan 10 03:49:56 ubuntu sshd[1195]: error: Could not load host key: /etc/ssh/ssh_
Jan 10 03:49:56 ubuntu sshd[1195]: error: key_load_private: invalid format
Jan 10 03:49:56 ubuntu sshd[1195]: error: key_load_public: invalid format
Jan 10 03:49:56 ubuntu sshd[1195]: error: Could not load host key: /etc/ssh/ssh_
Jan 10 03:49:56 ubuntu sshd[1195]: error: key_load_private: invalid format
Jan 10 03:49:56 ubuntu sshd[1195]: error: key_load_public: invalid format
Jan 10 03:49:56 ubuntu sshd[1195]: error: Could not load host key: /etc/ssh/ssh_
Jan 10 03:49:56 ubuntu sshd[1195]: fatal: No supported key exchange algorithms [

这是我在host连接虚拟机的时候sshd端报出的错误日志。这些文件都存在于/etc/ssh/ 下面,

参照Google的办法:

sudo rm /etc/ssh/ssh_host_*
sudo dpkg-reconfigure openssh-serve

 注意这是在虚拟机中的操作。

接着就可以在host上ssh上虚拟机了。

另外ssh -v ip是一个不错的调试工具,可以打出ssh连接的日志。

猜你喜欢

转载自blog.csdn.net/wujianyongw4/article/details/86223256