0

I've got a Raspberry Pi Zero V1.2 that is plugged into a powered USB hub that in turn is plugged into my router via Ethernet cable. (Basically like this.)

On my desktop computer, running Arch Linux, when I try running ssh pi@raspberrypi to connect to the Pi, I get this error: ssh: Could not resolve hostname raspberrypi: Name or service not known.

On the Pi, when I run hostname or cat /etc/hostname, the output is raspberrypi.

And here's the output of cat /etc/hosts:

127.0.0.1       localhost
::1             localhost ip6-localhost ip6-loopback
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters

127.0.1.1 raspberrypi

On my Arch Linux computer, I can connect to the Pi Zero using its IP address on my network by running ssh pi@192.168.1.7.

What must I do to SSH into my Pi using its hostname too?

update

The output of cat /etc/hosts on my Arch machine is this...

# Static table lookup for hostnames.
# See hosts(5) for details.
Username
  • 73
  • 1
  • 7
  • 1
    Can you get to the Pi via ssh pi@raspberrypi.local? Is the Arch box running Avahi - if not load that onto the Arch box. Does you router have a setting for the domain - if so check that is set to local. –  Aug 03 '20 at 19:05
  • Running ssh pi@raspberrypi.local on my Arch Linux computer gets the same error. I've got the avahi package already installed on my Arch Linux. I'm not sure exactly what you mean about my router. But when I navigate to 192.168.1.1 in my browser and log into the router, then click "Attached devices" I see a table showing the Pi's IP address, device name and MAC address, – Username Aug 03 '20 at 22:05
  • the quick and dirty method is to add an entry to the /etc./hosts file on the arch box for the pi. – Steve Robillard Aug 04 '20 at 11:30
  • or create an ssh config file on arch an use the server alias to call it by name – Steve Robillard Aug 04 '20 at 15:30
  • It sounds like the Arch Linux box is not "playing ball". If your router's table of DHCP leases contains a valid correct entry, and Arch is configured to check the router for DNS, it should work. Can you ssh from your RPi to the Arch box? – Seamus Aug 04 '20 at 19:35
  • My router lists what I believe to be my Arch Linux at 192.168.1.4, with Device name "--". When I ssh into my Pi and then run ssh 192.168.1.4, I get this error: ssh: connect to host 192.168.1.4 port 22: Connection refused. – Username Aug 05 '20 at 03:33
  • Do you have mDNS avahi-daemon installed on the Raspberry Pi? – John S Aug 08 '20 at 01:28
  • @JohnS avahi-daemon is already installed on the Pi if that's what you're asking. – Username Aug 08 '20 at 21:26
  • The Pi supports mDNS (multicast DNS). This only works if the client also supports mDNS. Basically instead of having to use a real DNS server or the /etc/hosts file to resolve the host name to an IP address, it does a broadcast query to say "who is raspberrypi.local" to all hosts on the LOCAL subnet (it will not cross a router boundary). The host whose name is 'raspberrypi' will answer saying "I am raspberrypi and my IP address is xx.xx.xx.xx". But if your Arch Linux doesn't have support for mDNS, this wont work. mDNS uses broadcast messages which are not propagated across subsets. – Tim Campbell Aug 08 '20 at 23:30
  • How do I see if both my Pi and Arch have mDNS? – Username Aug 09 '20 at 23:35

2 Answers2

2

Make sure your hostname files are setup correctly on BOTH ends:

Pi /etc/hosts:

127.0.0.1       localhost
::1             localhost ip6-localhost ip6-loopback
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters

127.0.1.1 raspberrypi

Arch (SSH Client) /etc/hosts:

127.0.0.1       localhost
::1             localhost ip6-localhost ip6-loopback
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters

[Pi WLAN/LAN IP] raspberrypi

Your Pi is likely to be 192.168.0.[something] or 10.0.1.[something]

This is only particularly useful if you have a static IP for your Pi.

What I recommend instead of Hostnames for SSH...

Is using the ~/.ssh/config file:

In the ~/.ssh/config file:

host RasPi
    user pi
    hostname 192.168.0.[Pi IP]

This will be more useful for the purpose of SSH, and it much more useful (in my opinion) when you start to do more fancy things with SSH (Proxy jump, Port Forward, Local/External IP matching)

Another thing that I have seen:

Some people recommend using Bonjour for Hostname resolution. So you can look into that if you want Hostname resolution on WLAN/LAN.

Hope this helps!

Awbmilne
  • 126
  • 1
  • 5
  • Updated my question to include output of cat /etc/hosts on my Arch machine. Will this affect how I implement your proposed solution? – Username Aug 08 '20 at 21:28
  • Sorry, I may have been a little unclear. The suggested Pi and SSH client files should be the /etc/hosts files, not the /etc/hostname files. I will edit my answer. – Awbmilne Aug 09 '20 at 12:01
  • Actually your answer worked. I went with the ~/.ssh/config solution – Username Aug 10 '20 at 02:35
-1

The normal (i.e. non Windows) operation of Zero-conf uses the .local "domain".

This is a function of your local network and source computer as Raspberry Pi OS has Zero-conf enabled by default.

Occasionally caching prevents Zero-conf resolution and the only cure I have found is to restart all network devices although pinging sometimes refreshes the arp cache.

Try raspberrypi.local

Milliways
  • 59,890
  • 31
  • 101
  • 209
  • I ran ssh pi@raspberrypi.local on my Arch Linux machine but got this error: Could not resolve hostname raspberrypi.local: Name or service not known – Username Aug 03 '20 at 22:27