VirtualBox下三台Ubuntu虚拟机SSH互连失败问题求助
Hey there, let's troubleshoot this SSH connectivity issue together. You've already knocked out the foundational steps (installing SSH, enabling the service, whitelisting it in UFW), so let's dive into the common roadblocks when static IPs are set but VM-to-VM SSH still fails.
First, confirm each VM has a unique, valid static IP that's on the same subnet:
- Run
ip aon every VM and look for the IP under your primary network interface (usuallyenp0s3for VirtualBox). Make sure no two VMs share the same IP, and all use the same subnet mask (e.g.,255.255.255.0) and gateway (typically192.168.56.1for VirtualBox's Host-Only adapter). - If you edited
/etc/netplan/*.yamlor/etc/network/interfacesto set static IPs, double-check for typos (like missing colons in Netplan, or incorrect gateway addresses).
VM-to-VM communication relies on the right network adapter type:
- Ensure all three VMs are using a Host-Only Adapter (this is designed for VM-to-VM and VM-to-host communication without external network access). Go to each VM's settings > Network > Adapter 1:
- Check "Enable Network Adapter".
- Set "Attached to:" to "Host-Only Adapter".
- Pick the correct host-only interface (usually
vboxnet0).
- Avoid using NAT for VM-to-VM connections—NAT isolates VMs from each other by default.
Before debugging SSH, confirm the VMs can see each other:
- From one VM, run
ping <VM2-IP>andping <VM3-IP>. If ping fails, the problem is with network connectivity, not SSH itself.- If ping works but SSH doesn't, move to SSH-specific checks below.
- If ping fails, revisit your static IP subnet and VirtualBox adapter settings.
Let's make sure SSH is set up correctly on the target VM:
- Verify the SSH service is running:
sudo systemctl status ssh. You should see "active (running)" in the output. If it's stopped, start it withsudo systemctl start sshand enable it on boot again withsudo systemctl enable ssh. - Check the SSH daemon config for restrictive rules: Open
/etc/ssh/sshd_configwithsudo nano /etc/ssh/sshd_configand confirm:PasswordAuthentication yes(if you're using password login—skip this if you're using SSH keys).PermitRootLogin yesonly if you're trying to log in as root; otherwise, ensure your regular user has access.AllowUsersdoesn't list specific users unless your username is included (comment this line out if you're unsure).
- After editing the config, restart SSH to apply changes:
sudo systemctl restart ssh.
Even though you ran sudo ufw allow ssh, double-check:
- Run
sudo ufw statuson the target VM. You should see a rule allowing22/tcpfrom your network (or anywhere, for testing). - If you're using
iptablesdirectly, runsudo iptables -Lto ensure there are no DROP/REJECT rules blocking port 22.
Small mistakes here can cause failures:
- Make sure you're using the correct username for the target VM (it's easy to mix up usernames across multiple VMs).
- If you get a "Connection refused" error, check if SSH is listening on the right interface: Run
sudo ss -tulpn | grep sshon the target VM. You should see it listening on0.0.0.0:22(all interfaces) or your static IP.
Work through these steps in order—starting with the ping test will quickly rule out basic network issues. Let me know which step uncovers the problem!
内容的提问来源于stack exchange,提问作者Amr Eldeeb

