Page 1 of 1

host name only resolves to localhost IP addess

Posted: 2007/01/23 00:07:56
by johnPote
Sorry if this is a rather basic question but am new to Linux.

In my python script the lines (which call the 'C' functions of same name):

thisHostNm = socket.gethostname()
thisHostIP = socket.gethostbyname( thisHostNm )

return the computer name and the IP address (eg 192.168.0.3) assigned by DHCP to the network card at boot time by my router. This is on my win XP box.

On my newly installed Centos 4.4 box things are different,

'Linux1' is the host name I set in SystemSettings:network:DNS tab:Hostname input field. 'Linux1' is what I get back from 'gethostname()'. So far so good.

But
'gethostbyname()' throws an exception with the message 'Name or service not known'.

If I now add 'Linux1' to /etc/hosts as in:
127.0.0.1 localhost.localdomain localhost Linux1

then 'gethostbyname()' returns -
127.0.0.1
which I suppose is not unreasonable, but different to the winXP box answer.

(it also stops Gnome complaining about 'Could not look up internet address for Linux1' at logon time)

What I'm after is the IP address given to the network card, 'eth0', by my router via DHCP. How can this be done? Can it be done in a way that would also work on my XP box?

I not getting the link between all the network elements in a Linux box. i.e the relation between computer name (a la XP), hostname, domain name as in localhost.localdomain and the network cards 'eth0' etc. A tutorial or good book reference on this and Linux in general would be most helpfull.

Appreciate any help you can give,

John