In Emacs 24, the function
system-name returns an FQDN. In Emacs 25,
system-name returns just the host name, and there doesn't seem to be a good way to get the FQDN.
How do I get an FQDN portably across Emacs versions?
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.Sign up to join this community
Emacs used to try and set
system-name to an FQDN. This has caused a fair bit of bug-reports and frustration over the years, since it caused Emacs to sometimes hang for somewhere between 30s and 2min at startup in case of a "misconfigured" DNS setup. Things have gotten worse nowadays since in many common situations (e.g. machine behind NAT) it's almost impossible to find such an FQDN, and it can change at any time during the Emacs session.
So in Emacs-25 we decided to stop trying. This is also indirectly the answer to your question: no, we don't know how to get an FQDN portably, so we don't try to find an FQDN any more.
For those situations where you do need an FQDN, you will generally have some extra knowledge about your machine's config which will make it possible for you to find the FQDN and/or to write a little chunk of code which can find this FQDN.
One easy way (in some situations) might be to change the machine's
localname to be this FQDN (so Emacs's
system-name will again be an FQDN).
My fix was to use this:
(car (split-string (shell-command-to-string "hostname -f")))
It's a bit of a hack, because I just use the
hostname -f shell command. This is because I don't think
hostname should return the FQDN. there's all sorts of things wrong with this, and anyways, I don't always control the
/etc/hosts file or the hostname myself
My use case is simply using the
smtpmail handler to send mail with Emacs. The remote SMTP server expects a FQDN to be given in the
HELO parameter. Before Emacs 25, this would work fine, but now it fails. Using
localhost or any static variation wouldn't work either, and some servers explicitly will deny
localhost.localdomain as well.
Therefore I now have yet another kludge in my
.emacs, like this:
;; most SMTP servers expect a FQDN on HELO and (system-name) returns ;; only the short hostname since Emacs25, fallback to hostname -f (when (>= emacs-major-version 25) (setq smtpmail-local-domain (car (split-string (shell-command-to-string "hostname -f")))))
Kind of unfortunate, if you ask me. I understand this was done to workaround broken configurations, but at least there should be a
system-name-fqdn macro or something to do the right thing for people that are properly configured... If you're going to break the API, at least provide a fallback.