关于arm:无法SSH进入运行dropbear sshd的QEMU虚拟机

Cannot SSH into QEMU virtual machine running dropbear sshd

我已经设置了一个模拟vexpress-a9机器的QEMU虚拟机。我使用buildroot来创建initrd系统,并且使用了buildroot下载的代码源交叉编译器作为编译Linux内核的一部分。这就是我调用qemu

的方式

qemu-system-arm -m 512 -M vexpress-a9
-dtb linux-4.4.192/arch/arm/boot/dts/vexpress-v2p-ca9.dtb
-kernel linux-4.4.192/arch/arm/boot/zImage
-initrd buildroot/output/images/rootfs.cpio
-append"console=ttyAMA0,ip=dhcp"
-net nic,model=lan9118
-net user,hostfwd=tcp::2222-:22
-nographic

它启动很好。与网络相关的引导消息,表明网络已经初始化,并且dropbear sshd已启动并正在运行:

Starting network: OK
Starting dropbear sshd: random: dropbear: uninitialized urandom read (32 bytes read, 1 bits of entropy available)
OK

但是eth0接口必须手动分配IP。我在与Linux主机相同的子网中为它分配了一个未使用的C类地址,如下所示:

# ifconfig eth0 192.168.160.150 up
smsc911x 4e000000.ethernet eth0: SMSC911x/921x identified at 0xa12a0000, IRQ: 31

所以ifconfig现在显示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# ifconfig
eth0      Link encap:Ethernet  HWaddr 52:54:00:12:34:56
          inet addr:192.168.160.150  Bcast:192.168.160.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
          Interrupt:31

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

netstat告诉我我们正在侦听端口22:

1
2
3
4
# netstat -tln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN

我什至可以像这样SSH到localhost:

1
2
3
4
5
6
7
8
9
10
11
# ssh [email protected]
random: ssh: uninitialized urandom read (32 bytes read, 24 bits of entropy available)
random: dropbear: uninitialized urandom read (32 bytes read, 24 bits of entropy available)
random: dropbear: uninitialized urandom read (32 bytes read, 24 bits of entropy available)
random: dropbear: uninitialized urandom read (32 bytes read, 24 bits of entropy available)

Host '127.0.0.1' is not in the trusted hosts file.
(ecdsa-sha2-nistp256 fingerprint sha1!! de:cf:f8:b3:ac:4d:96:47:22:d7:76:a1:ea:11:3d:bc:ec:c6:71:9a)
Do you want to continue connecting? (y/n) y
[email protected]'s password:
random: dropbear: uninitialized urandom read (32 bytes read, 26 bits of entropy available)

而当我尝试从linux主机通过SSH进入此VM时,从主机上看到的是这句话:

1
2
3
4
5
6
7
8
9
$ ssh -vvv [email protected]
OpenSSH_7.4p1 Debian-10+deb9u7, OpenSSL 1.0.2s  28 May 2019
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: resolving"192.168.160.150" port 22
debug2: ssh_connect_direct: needpriv 0
debug1: Connecting to 192.168.160.150 [192.168.160.150] port 22.
debug1: connect to address 192.168.160.150 port 22: No route to host
ssh: connect to host 192.168.160.150 port 22: No route to host

我很高兴能对此提出一些建议,因为我找不到其他可以尝试的方法

更新1:配置buildroot时包含dhcpd。现在,无论我使用dropbear还是openssh,日志消息都会一直显示到

1
2
3
4
5
6
7
Starting dhcpcd...
no interfaces have a carrier <--- the problem
forked to background, child pid 746
smsc911x 4e000000.ethernet eth0: SMSC911x/921x identified at 0xa12a0000, IRQ: 31
Starting dropbear sshd: random: dropbear: uninitialized urandom read (32 bytes read, 2 bits of entropy available)
OK
random: dhcpcd: uninitialized urandom read (120 bytes read, 2 bits of entropy available)

这时只是停止,我看不到登录提示。删除dhcpd并使用dropbear或openssh时,我可以再次看到登录提示(表示所有init脚本都运行完毕),但仍然没有任何形式的连接。我不明白为什么dhcpd认为世界上没有运营商(当它能够枚举和识别模拟的NIC卡时)

UPDATE2:我尝试手动调用udhcpc以通过DHCP获得地址,但它确实获得了一个地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# udhcpc -S -a100
udhcpc: started, v1.31.0
udhcpc: sending discover
udhcpc: sending select for 10.0.2.15
udhcpc: lease of 10.0.2.15 obtained, lease time 86400
deleting routers
adding dns 10.0.2.3
# ifconfig
eth0      Link encap:Ethernet  HWaddr 52:54:00:12:34:56
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:2360 (2.3 KiB)  TX bytes:1428 (1.3 KiB)
          Interrupt:31

但是仍然无法从主机访问计算机。我是否需要从主机向该VM添加手动路由?如果是,我该怎么做? QEMU是否在主机上创建虚拟接口(就像工作站一样)?


如果您正在使用QEMU的"用户"模式网络,则尝试在主机的网络中为访客分配IP地址是错误的。用户模式网络使用自己的DHCP服务器创建了一个模拟的网络-您可以在https://wiki.qemu.org/Documentation/Networking#User_Networking_.28SLIRP.29

上看到其示意图。

默认情况下,来宾所在的网络是10.0.2.x,但是您可以使用命令行参数对其进行更改。

配置来宾的最简单方法是让它运行DHCP客户端,该客户端应该能够找到QEMU提供的内置DHCP服务器,并以此方式在仿真网络上获取IP地址。

关于用户模式网络的重要一点是,它通常不允许您直接从主机连接到来宾VM。连接到它的唯一方法是,如果您的QEMU命令行指定了要从主机转发到来宾的特定端口,如果这样做,则可以连接到在命令行转发选项中指定的主机IP地址和端口。并将其转发到仿真网络上来宾的IP地址和端口。

如果这还不够,并且您需要VM出现在桥接到主机网络的真实虚拟接口上,则应停止使用用户模式网络,而应查看TAP网络选项。但是,TAP的设置更为复杂。


昨天我花了几个小时尝试使用qemu-system-arm的不同选项(即-net-netdev),但是最后一个可行的办法是使用这个选项:-nic user,hostfwd=::2222-:22,这是我从已删除功能页面的列表中了解到的(请参阅替换-redir选项)。我还必须在主机的SSH配置文件中添加以下3行:/etc/ssh/ssh_config

1
2
3
Host localhost
PreferredAuthentications=password
PubkeyAuthentication=no