ArchLinux 虚拟机通过 virt-manager 登录后才可以通过 SSH 登录

ArchLinux 虚拟机通过 virt-manager 登录后才可以通过 SSH 登录

我在 CentOS 服务器上用 KVM 上的 virt-manager 安装了两个 ArchLinux 虚拟机。ip 地址分别是 192.168.1.138 和 192.168.1.183。安装后,我安装了 OpenSSH 并复制了 SSH 密钥。我尝试登录并成功。

然后我重启虚拟机并用 virsh 命令启动它们。这时我尝试使用 ssh 登录时收到“连接被拒绝”错误。对虚拟机执行 ping 操作显示网络运行正常。

255 hans@archie ~ % ssh root@arch1                                                                                            
ssh: connect to host arch1 port 22: Connection refused

hans@archie ~ % ssh -vv root@arch1                                                        
OpenSSH_7.8p1, OpenSSL 1.1.1  11 Sep 2018
debug1: Reading configuration data /home/hans/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug2: resolving "arch1" port 22
debug2: ssh_connect_direct
debug1: Connecting to arch1 [10.20.60.138] port 22.
debug1: connect to address 10.20.60.138 port 22: Connection refused
ssh: connect to host arch1 port 22: Connection refused

255 hans@archie ~ % ping arch1                                                                                             
PING arch1 (10.20.60.138) 56(84) bytes of data.
64 bytes from arch1 (10.20.60.138): icmp_seq=1 ttl=64 time=0.286 ms
64 bytes from arch1 (10.20.60.138): icmp_seq=2 ttl=64 time=0.288 ms
64 bytes from arch1 (10.20.60.138): icmp_seq=3 ttl=64 time=0.292 ms

实际情况是,如果我在 virt-manager 上“打开”虚拟机并在那里登录,那么我就可以 ssh 到虚拟机。

SSH 密钥在我的另外 2 台 CentOS 服务器上运行良好,其 IP 地址分别为:192.168.1.101 和 192.168.1.202。

答案1

我在使用 Arch VM 时遇到了同样的问题,并且发现我要么等待 5 分钟,要么通过 virt-manager 登录。

问题在于随机数生成器减慢了启动速度:

# journalctl  --since 13:23  | grep crng
Oct 06 13:23:04 apsis kernel: random: get_random_bytes called from start_kernel+0x95/0x535 with crng_init=0
Oct 06 13:28:03 apsis kernel: random: crng init done

https://bbs.archlinux.org/viewtopic.php?id=236696了解详情。

我按照建议安装哈格德现在可以重启后立即登录。crng init 现在只需一秒钟,而不是 5 分钟。

希望这可以帮助。

答案2

我使用 journalctl 检查了 OpenSSH 日志,发现 OpenSSH 服务器启动 22 分钟后才开始监听端口:

journalctl -u sshd | tail -100

以下是一些摘录:

-- Reboot --
Sep 23 00:28:47 arch3 systemd[1]: Started OpenSSH Daemon.
Sep 23 00:32:12 arch3 sshd[242]: Server listening on 0.0.0.0 port 22.
Sep 23 00:32:12 arch3 sshd[242]: Server listening on :: port 22.

-- Reboot --
Sep 23 00:46:03 arch1 systemd[1]: Started OpenSSH Daemon.
Sep 23 00:51:16 arch1 sshd[249]: Server listening on 0.0.0.0 port 22.
Sep 23 00:51:16 arch1 sshd[249]: Server listening on :: port 22.

相关内容