5

I have a virtual machine running Ubuntu. That VM runs locally in Virtualbox on my Ubuntu machine. I can connect via ssh giving the IP of the VM. But I would like to use the hostname, because the IP of the VM can change. So i tried "ssh [email protected]" but it gives me "name or service not known" That same method works for a raspberry in the network: "ssh [email protected]" connects w/out issues.

What is the difference here? Why doesn't that work for my local VM? The VM's network adapter is in bridged mode, it gets its IP from my router via DHCP.

EDIT some more information: The hostname is in the list of connected devices on my router. "ping hostname.local" also fails, but "ping hostIP" works. Must be a DNS issue somehow.

Thanks for any hint! Hoebsi

1
  • What does nslookup <IP> return?
    – FelixJN
    Jan 7, 2021 at 11:04

1 Answer 1

3

You need name resolution for this to work. It could be DNS, but if you ran your own DNS server, you would ask a different question. So I guess you don't. No DNS server, no DNS-based name resolution.

Another name resolution method is /etc/hosts. You could simply add hostname.local to the /etc/hosts file on the hypervisor host. Probably the easiest solution, especially if you don't access the VM from other computers.

The third commonly used method is mDNS. It relies on a service running on the VM that responds to queries of the type "who has the name hostname.local?". On Linux, this service is typically named "Avahi". Perhaps your Raspberry Pi runs it, and your VM doesn't. Or perhaps mDNS queries don't reach the VM.

To find out which name resolution methods are enabled, see the hosts clause in the /etc/nsswitch.conf file.

3
  • 1
    Great advice! On Ubuntu Server the NSS service is not installed by default. So installing libnss-mdns "sudo apt-get install libnss-mdns" and editing the /etc/nsswitch.conf file solved the issue. In that file the hosts: line now is "hosts: files mdns4_minimal [NOTFOUND=return] dns"
    – Hoebsi
    Jan 7, 2021 at 11:26
  • Just for clarification: Adding hostname.local to /etc/hosts would only work if the IP of hostname didn't change, right? I copy and run the VM on different hosts in different environments, so the IP will change due to DHCP. Regards and thank you for your help
    – Hoebsi
    Jan 7, 2021 at 11:40
  • 1
    Yes, if the VM's IP address is not static, you need to update /etc/hosts every time the address changes. DNS has the same problem, except if you configure DDNS (dynamic DNS configuration updates). Perhaps Avahi would be the best solution then, but it only works when the VM is on the same network as the SSH client. Jan 7, 2021 at 23:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .