pythonfqdn

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


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()
'SR-MW001'
>>> socket.gethostname()
'sr-mw001.foo-domain.de'

What is wrong here?

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

Update

More infos:

/etc/hosts

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

IPs

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 127.0.0.1/8 brd 127.255.255.255 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 10.189.217.11/24 brd 10.189.217.255 scope global eth0
       valid_lft forever preferred_lft forever

socket.gethostbyaddr()

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

More details:

> uname -n
sr-mw001.foo-domain.de

> hostname
sr-mw001.foo-domain.de

> domainname
(none)

> nisdomainname
-bash: nisdomainname: command not found

> dnsdomainname
foo-domain.de

> ypdomainname
-bash: ypdomainname: command not found

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


Solution

  • /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.