Ssh

Linux QEMU - 來賓可以通過 SSH 連接到主機,但主機可以連接到來賓

  • December 12, 2019

我在通過 SSH 進行 Host-Guest 網路時遇到了一些問題。我在這裡查看了許多問題以及 QEMU 的網路文件(https://wiki.qemu.org/Documentation/Networking#How_to_get_SSH_access_to_a_guest),但未能找到答案。

我的來賓 QEMU 實例能夠像往常一樣通過 SSH 連接到主機。但是,當我嘗試從主機通過 SSH 連接到 QEMU 實例時出現ssh_exchange_identification: read: Connection reset by peer錯誤。

這是我用來啟動 QEMU 的命令:

qemu-system-aarch64 -M virt -m 32768 -cpu cortex-a72 \
 -kernel $VMLINUZ \
 -initrd $INITRD \
 -append 'root=/dev/vda2' \
 -drive if=none,file=$COW,format=qcow2,id=hd \
 -device virtio-blk-pci,drive=hd \
 -netdev user,id=mynet \
 -device virtio-net-pci,netdev=mynet \
 -nographic \
 -device e1000,netdev=net0 \
 -netdev user,id=net0,hostfwd=tcp::5555-:22

啟動 QEMU 後,我嘗試使用以下命令 SSH 並獲得以下輸出:

<*user*>@<*hostname*>:~$ ssh localhost -p 5555 -vvv
OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n  7 Dec 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: resolving "localhost" port 5555
debug2: ssh_connect_direct: needpriv 0
debug1: Connecting to localhost [127.0.0.1] port 5555.
debug1: Connection established.
debug1: identity file /home/<*user*>/.ssh/id_rsa type 0
debug1: key_load_public: No such file or directory
debug1: identity file /home/<*user*>/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/<*user*>/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/<*user*>/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/<*user*>/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/<*user*>/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/<*user*>/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/<*user*>/.ssh/id_ed25519-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
ssh_exchange_identification: read: Connection reset by peer

非常感謝您對此的任何幫助!

編輯:

兩台機器都執行 Ubuntu 18.04。

主持人:

Linux trace5 5.0.0-37-generic #40~18.04.1-Ubuntu SMP Thu Nov 14 12:06:39 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

客人:

Linux qemu-trace 5.2.0-050200-generic #201907072331 SMP Sun Jul 7 23:48:00 UTC 2019 aarch64 aarch64 aarch64 GNU/Linux

來賓正在執行sshd

ps -ef | grep '[s]shd'
root 616 1 0 Dec11 ? 00:00:00 /usr/sbin/sshd -D

/var/log/auth.log如果有幫助,這是對客人的摘錄。我正在閱讀身份驗證失敗,但不確定這意味著什麼,因為時間戳與我的主機 ssh-ing 的時間不一致:

Dec 11 18:43:17 qemu-trace sshd[1547]: Server listening on 0.0.0.0 port 22.
Dec 11 18:43:17 qemu-trace sshd[1547]: Server listening on :: port 22.
Dec 11 18:43:17 qemu-trace sudo: pam_unix(sudo:session): session closed for user root
Dec 11 18:45:30 qemu-trace systemd-logind[610]: New seat seat0.
Dec 11 18:45:33 qemu-trace sshd[617]: Server listening on 0.0.0.0 port 22.
Dec 11 18:45:33 qemu-trace sshd[617]: Server listening on :: port 22.
Dec 11 18:46:18 qemu-trace login[620]: pam_unix(login:session): session opened for user trace by LOGIN(uid=0)
Dec 11 18:46:19 qemu-trace systemd-logind[610]: New session 1 of user trace.
Dec 11 18:46:19 qemu-trace systemd: pam_unix(systemd-user:session): session opened for user trace by (uid=0)
Dec 11 19:17:01 qemu-trace CRON[675]: pam_unix(cron:session): session opened for user root by (uid=0)
Dec 11 19:17:02 qemu-trace CRON[675]: pam_unix(cron:session): session closed for user root
Dec 11 19:20:13 qemu-trace systemd-logind[605]: New seat seat0.
Dec 11 19:20:17 qemu-trace sshd[616]: Server listening on 0.0.0.0 port 22.
Dec 11 19:20:17 qemu-trace sshd[616]: Server listening on :: port 22.
Dec 11 19:22:33 qemu-trace login[621]: pam_unix(login:auth): check pass; user unknown
Dec 11 19:22:33 qemu-trace login[621]: pam_unix(login:auth): authentication failure; logname=LOGIN uid=0 euid=0 tty=/dev/ttyAMA0 ruser= rhost=
Dec 11 19:22:37 qemu-trace login[621]: FAILED LOGIN (1) on '/dev/ttyAMA0' FOR 'UNKNOWN', Authentication failure
Dec 11 19:22:49 qemu-trace login[621]: pam_unix(login:session): session opened for user trace by LOGIN(uid=0)
Dec 11 19:22:49 qemu-trace systemd-logind[605]: New session 1 of user trace.
Dec 11 19:22:50 qemu-trace systemd: pam_unix(systemd-user:session): session opened for user trace by (uid=0)

我能夠找到問題;這是我的 QEMU 命令的問題。啟動 QEMU 的新命令:

qemu-system-aarch64 -M virt -m 32768 -cpu cortex-a72 \
 -kernel $VMLINUZ \
 -initrd $INITRD \
 -append 'root=/dev/vda2' \
 -drive if=none,file=$COW,format=qcow2,id=hd \
 -device virtio-blk-pci,drive=hd \
 -netdev user,id=mynet \
 -device virtio-net-pci,netdev=mynet,hostfwd=tcp::2222-:22 \
 -nographic

然後我可以從主機 SSH:

<user>@<host>:~$ ssh <host>@localhost -p 2222
<user>@localhost's password: 
Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 5.2.0-050200-generic aarch64)

* Documentation:  https://help.ubuntu.com
* Management:     https://landscape.canonical.com
* Support:        https://ubuntu.com/advantage

Last login: Thu Dec 12 14:31:28 2019
<user>@<guest>:~$

引用自:https://unix.stackexchange.com/questions/556788