I do not understand this:

Python 2.7.3 (default, Apr 14 2012, 08:58:41) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> socket.getfqdn()
>>> socket.gethostname()

What is wrong here?

According to the docs of socket.getfqdn() the "a fully qualified domain name" should get returned.


More infos:


sr-mw001:~ # grep -iP 'SR-MW001|localhost|foo-domain' /etc/hosts       localhost
::1             localhost ipv6-localhost ipv6-loopback   SR-MW001 foo-work


sr-mw001:~ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet brd scope host lo
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:50:56:a8:6e:eb brd ff:ff:ff:ff:ff:ff
    inet brd scope global eth0
       valid_lft forever preferred_lft forever


>>> socket.gethostbyaddr('sr-mw001.stadtwerke-gt.de')
('SR-MW001', ['foo-work'], [''])

More details:

> uname -n

> hostname

> domainname

> nisdomainname
-bash: nisdomainname: command not found

> dnsdomainname

> ypdomainname
-bash: ypdomainname: command not found

Related question: /etc/HOSTNAME on SuSE: short name or FQDN?

  • Did you modified your files (e.g. socket.py) in your Python installation? getfqdn should return the result of gethostname if anything goes wrong. Based on the outputs, you shouldn't get here using a pristine version.Could you paste the getfqdn implementation (you can find the file by: print(socket))? Any reason for using this ancient version? – CristiFati Jun 11 at 17:39
  • Also is it possible to share /etc/hosts content? Is this reproducible on one machine only or is it general? Your machine is in a domain? If yes what are you using? nis, ldap, ... ? Is the machine IPv6 or 4? – CristiFati Jun 11 at 17:46
  • @CristiFati I updated the question. You ask "Your machine is in a domain?" how can I check this? – guettli Jun 12 at 7:58
  • @guettli So your question now has become "Why does socket.gethostbyaddr() not return a domain name?" That sounds more like a DNS configuration issue than a Python question. And the returned values look suspiciously like what's defined in /etc/hosts. – Roland Weber Jun 13 at 9:31
  • 1
    Unfortunately I can't figure out why the functions behave the way they do. If I could reproduce the problem locally, maybe I could come up with an answer, but I imagine, you got tired of updating the question every time I need a new detail. As a workaround (to get the fqdn) you could call socket.getfqdn() and if that doesn't contain any dots, call socket.gethostname(). Or you could do subprocess.check_output(["hostname", "--fqdn"]).split("\n")[0]. – CristiFati Jun 13 at 12:13

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

The implementation of getfqdn() relies on that:

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:    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.

Ensure that these are working before checking from python.

  • Thank you very much for your answer. Is there a reference or name for this "best practice" guide? – guettli Jun 14 at 8:52
  • 2
    The hostname(1) man page vaguely suggests this route. I essentially learned this after many attempts. – Jonathon Reinhart Jun 14 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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