Unplugging ethernet cable causes ENETUNREACH when using "ip address" instead of "localhost"

Hi
I have made a small setup on a Raspberry pi 1B (RPI), where I read a temperature from a DS18B20 and using Node-red, I store it in an Influx DB. I have also installed Grafana to display the collected data from a computer.
I have set a static ip for my RPI.
Inside the influxdb node, I had entered the database address as the static IP I have set for my RPI.
Everything was working well until I disconnected the ethernet cable. After connecting again a day later, I realised that no data had been stored while the ethernet cable had been disconnected.
In the RPI cmdLine I checked node-red-log and it generates the following error when unplugging the ethernet cable:
10 Sep 13:00:56 - [error] [influxdb out:543e9e17.2a674] Error: connect ENETUNREACH 10.0.0.200:8086 - Local (0.0.0.0:0)
I have now replaced the ip address in the Influxdb node with "localhost" and everything works.

My newbie question here is: Should I avoid filling in the static IP and always just use localhost for InfluxDB, Node-red and Grafana? Or is the IP the "correct" way to go (except in the Influxdb-node :wink:

Thanks for any advice
/Palle

If you use the "external" IP address (I guess something like 192.168....) then, this IP will answer only if the ethernet interface is up (so with a cable and something making the interface up), which is what you saw.
If you use 127.0.0.1 (the loopback address), it will work, as soon as your RPi is up.
If there is a risk (or the choice) of your ethernet interface going down again, there in no real negative impact in using 127.0.0.1 for the address for the Rpi to talk to himself. Let's say it doesn't harm using 127.0.0.1 for this kind of setup.

1 Like

@greengolfer is right, you must use localhost or 127.0.0.1.
The reason is that you have set a static ip address on a particular ethernet interface (probably eth0) but if there is no cable plugged in then the interface is not 'up' so it does not have an ip address at all. Run ifconfig with it connected and not and you will see that the address disappears.

1 Like

Better still is to use "localhost" and avoid the IP address. Interestingly, that should let you use URL's like "red.localhost" or "pi.localhost" which you may find convenient during testing.

1 Like

That's debatable :slight_smile:
127.0.0.1 is the localhost IP address since the dinosaurs.
So, being almost as old as the dinosaurs, I like to use 127.0.0.1. red.localhost or pi.localhost is a fairly recent addition in the /etc/hosts.

1 Like

There are subtleties actually, even cases where 127.0.0.2 is the usable address on some versions of some systems. Of course, on most systems you can use the whole address space if you want to.

With localhost, it is easier to type (well I think so anyway), you don't have to care about the address and you get to use the prefixes if you like.

On my dev machine, it is really useful to use the prefixes because the browser remembers the rest of the address. I will have multiple localhost entries in use on my dev device but if I use red.localhost:1880 and graf.localhost:3000 etc, then I just have to type the first part "red" into the address bar and the rest comes up automatically.

The prefixes aren't dependent on the hosts file though, it is a browser feature. You've been able to do it in the hosts files for years but they are typically a pain to edit as they are secured.

3 Likes

Once again a gem found by opening up a curious looking topic. Had never heard of this but can come in handy!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.