socket.getfqdn() returns no domain, but socket.gethostname() does?

Check what socket.gethostbyaddr('sr-mw001.foo-domain.de') returns on your machine.

The implementation of getfqdn() relies on that:
https://github.com/python/cpython/blob/2.7/Lib/socket.py#L128-L151

If gethostbyaddr() returns a hostname without domain, and no aliases, then that hostname is returned by getfqdn().


The updated information in the question indicates that my guess was close. It's the entries without domain in your /etc/hosts that cause this behavior. The following Q&A shows a way to fix it, I believe: https://unix.stackexchange.com/a/77924


Also consider to upgrade your Python installation. Version 2.7.3 is from 2012, the latest fixlevel for 2.7 is 2.7.16. There's no change in getfqdn(), but I haven't checked gethostbyaddr() or what other functions might get called.


/etc/hostname should have the short (unqualified) name (sr-mw00). The name from file is pushed into the kernel at boot, and should be seen in uname.

Then /etc/hosts should have an entry like this:

127.0.1.1    sr-mw001.foo-domain.de sr-mw001

This sets sr-mw001.foo-domain.de as the canonical name with sr-mw001 being an alias.

hostname should output the short name. hostname --fqdn should output the full canonical name.

Using 127.0.1.1 is the convention used by the Debian installer when the system has a DHCP-assigned IP address.

If the system has a static IP address, you should use that address instead. That will ensure the system can also determine its FQDN from its IP address (a reverse lookup).

Ensure that these are working before checking from python.

Tags:

Python

Fqdn