User avatar
peppy.player
Posts: 191
Joined: Mon Mar 07, 2016 6:10 am

Server starts on wrong IP

Tue Aug 20, 2019 4:26 am

I'm trying to start web server (Tornado) on Raspbian Buster. I suspect that the order of 'lo' and 'eth0' causes the issue during server startup. I don't specify IP address explicitly. So I guess server takes the first from the top of the list. As a result it starts on address 127.0.1.1 and I cannot access it from outside. Is there any way to change the order? Here is the current one:

Code: Select all

pi@raspberrypi:~ $ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether b8:27:eb:49:1f:bc brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.11/24 brd 10.0.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 2601:648:8500:41e0::88ac/128 scope global dynamic noprefixroute
       valid_lft 604745sec preferred_lft 604745sec
    inet6 2601:648:8500:41e0:eadb:5904:98f4:b6df/64 scope global dynamic mngtmpaddr noprefixroute
       valid_lft 260931sec preferred_lft 260931sec
    inet6 fe80::865a:3657:fae5:88d6/64 scope link
       valid_lft forever preferred_lft forever
3: wlan0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
    link/ether b8:27:eb:1c:4a:e9 brd ff:ff:ff:ff:ff:ff

User avatar
DougieLawson
Posts: 36322
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Server starts on wrong IP

Tue Aug 20, 2019 6:46 am

It doesn't matter.

All addresses from 127.0.0.1 to 127.255.255.254 work as loopback.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

hortimech
Posts: 323
Joined: Wed Apr 08, 2015 5:52 pm

Re: Server starts on wrong IP

Tue Aug 20, 2019 8:01 am

Problem is that '127.0.1.1' doesn't appear in your output from 'ip a'

Try looking in /etc/hosts, I think you will find something like this:

Code: Select all

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

127.0.1.1	raspberrypi
If you are using dhcp, just comment the '127.0.1.1' and it will start to use the dhcp supplied IP

User avatar
peppy.player
Posts: 191
Joined: Mon Mar 07, 2016 6:10 am

Re: Server starts on wrong IP

Tue Aug 20, 2019 3:24 pm

Thank you for replies!

Another part of my code caused the confusion. After starting web server I print to console host an port:

Code: Select all

logging.debug("Web Server Started at %s:%s", host, port)
I get host this way:

Code: Select all

host = socket.gethostbyname(socket.gethostname())
That returned 127.0.1.1 but server was actually started on 10.0.0.11 as expected.
I just need to find a way to get real host from the server.
Thanks!

hortimech
Posts: 323
Joined: Wed Apr 08, 2015 5:52 pm

Re: Server starts on wrong IP

Tue Aug 20, 2019 4:54 pm

I just told you how to, comment '127.0.1.1' out in /etc/hosts, it will start using the correct IP supplied by dhcp, this comes up fairly frequently on the Samba mailing list.

User avatar
peppy.player
Posts: 191
Joined: Mon Mar 07, 2016 6:10 am

Re: Server starts on wrong IP

Wed Aug 21, 2019 3:18 am

As I mentioned the problem was in this line:

Code: Select all

host = socket.gethostbyname(socket.gethostname())
It returned 127.0.1.1 which was wrong/confusing as Tornado server actually started on 10.0.0.11

The server starts on the same address 10.0.0.11 no mater if I have that line in /etc/hosts or not. If I delete that line the code above throws exception as hostname 'raspberrypi' doesn't exist anymore.

Andyroo

Re: Server starts on wrong IP

Wed Aug 21, 2019 8:59 am

The name raspberrypi is only part of a system name and should have a domain as well as the machine name.

Raspbian uses avahi so by default will use the domain ‘.local’ so technically the machine is known to DNS as raspberrypi.local and often with home networks it will register itself with the DHCP server as that performs the DNS function.

By putting an entry in the HOSTS file you are bypassing the DNS and getting away with not using the domain name :D

hortimech
Posts: 323
Joined: Wed Apr 08, 2015 5:52 pm

Re: Server starts on wrong IP

Wed Aug 21, 2019 11:15 am

peppy.player wrote:
Wed Aug 21, 2019 3:18 am
As I mentioned the problem was in this line:

Code: Select all

host = socket.gethostbyname(socket.gethostname())
It returned 127.0.1.1 which was wrong/confusing as Tornado server actually started on 10.0.0.11

The server starts on the same address 10.0.0.11 no mater if I have that line in /etc/hosts or not. If I delete that line the code above throws exception as hostname 'raspberrypi' doesn't exist anymore.
And I told you to comment the '127.0.1.1' line in /etc/hosts and this will fix your problem, with '127.0.1.1 raspberrypi' in /etc/hosts

Code: Select all

pi@raspberrypi:~ $ hostname -i
127.0.1.1
Change it to '#127.0.1.1 raspberrypi'

Code: Select all

pi@raspberrypi:~ $ hostname -i
192.168.0.146

User avatar
peppy.player
Posts: 191
Joined: Mon Mar 07, 2016 6:10 am

Re: Server starts on wrong IP

Wed Aug 21, 2019 3:16 pm

I'm using the latest upgraded/updated Raspbian Buster OS. If I have the line

Code: Select all

127.0.1.1       raspberrypi
in /etc/hosts:

Code: Select all

pi@raspberrypi:~ $ hostname -i
127.0.1.1
If I comment out or delete that line:

Code: Select all

pi@raspberrypi:~ $ hostname -i
hostname: Name or service not known
Python code throws the exception with the same message: Name or service not known

User avatar
RaTTuS
Posts: 10493
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: Server starts on wrong IP

Wed Aug 21, 2019 3:21 pm

you'll need to reboot after editing hosts
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

User avatar
peppy.player
Posts: 191
Joined: Mon Mar 07, 2016 6:10 am

Re: Server starts on wrong IP

Wed Aug 21, 2019 4:17 pm

RaTTuS wrote:
Wed Aug 21, 2019 3:21 pm
you'll need to reboot after editing hosts
Sure, I did that.

hortimech
Posts: 323
Joined: Wed Apr 08, 2015 5:52 pm

Re: Server starts on wrong IP

Wed Aug 21, 2019 7:00 pm

I am also running the latest Raspbian and it works for me, so there must be something else going on here. How is the ipaddress being set ?
Is it a static ip or is it set via dhcp. If the latter, it should work, but if is a static IP, then don't comment the line, but change '127.0.1.1' to the static IP.

Return to “Networking and servers”