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.
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With