Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have a problem understanding how IP resolving works on Linux systems. To demonstrate my problem I'll show an example on Windows and afterwards on Linux.

Windows: The Windows hosts file is empty. It only contains comments.

Running the following command in a Python shell:


I get which is my current IP in the network I am in. As I expected it to be.

Linux: On Linux my /etc/hosts looks as follows:       localhost.localdomain           localhost

The same Python command would give me errors that the resource is temporarily unavailable. I am guessing that it has to do with the absence of the my current Hostname from /etc/hosts. If I add it manually       localhost.localdomain           localhost    MyHostName

the python command returns Which is still not what I expected it to be. I can however add the IP and my hostname to the file       localhost.localdomain           localhost  MyHostName 

but this is surely not the correct way to go. If DHCP is used this IP would change.

How can I achieve the same result on Linux as on Windows? Where is my configuration (or my thinking?) wrong?

share|improve this question
which dhcp client do you use in linux? ps -ef|grep dhcp – LilloX Nov 27 at 13:13
@LilloX udhcpc. But this also has happended to me with all smaller devices I use. For example Raspberry Pi 2 (I don't knwo right now which dhcp client it uses by default). – ap0 Nov 27 at 13:17
what is wrong with using MyHostName? – user4668401 Nov 27 at 13:33
@user4668401 On this device is a webserver running where I also use websockets. They require to know the IP the device has in the network for communication. – ap0 Nov 27 at 13:41
ok I see, what about an ifconfig query? executed through shell? e.g. ipaddress=$(ip addr show eth0 | grep --color=always inet | awk '{ print $2; }' | sed -n '1p' | sed 's/\/[[:digit:]]*//g') – user4668401 Nov 27 at 13:45

1 Answer 1

up vote 0 down vote accepted

You can edit the file that contains the instructions to execute when assigned the IP to your network card (/etc/udhcpc/default.script).

A change that you can do is the following: in the renew|bound section add, under the ifconfig instruction :

echo " localhost.localdomain localhost"> /etc/hosts
echo "$i myHostName" > /etc/hosts
share|improve this answer
better yet, don't erase and re-create the entire hosts file, use sed to change only the line with myHostName: sed -i -e "/myHostName/ s/^\([0-9.]\+\)/$i/" /etc/hosts. or edit it if it exists, add it otherwise: if grep -q myHostName /etc/hosts ; then sed -i -e "/myHostName/ s/^\([0-9.]\+\)/$i/" /etc/hosts ; else echo "$i myHostName" >> /etc/hosts ; fi – cas 2 days ago
Thank you. This will work. But I am also interested in what the difference in Windows handling there is. Is there a configuration issue that Linux cannot resolve my Hostname to my network IP when DHCP is used or is this only achieveable with static IPs and enterening those into /etc/hosts? – ap0 8 hours ago

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.