Bug 386621

Summary: hostname -s in /etc/profile causes DNS timeouts
Product: [openSUSE] openSUSE 11.0 Reporter: Ludwig Nussel <lnussel>
Component: OtherAssignee: Dr. Werner Fink <werner>
Status: RESOLVED FIXED QA Contact: E-mail List <qa-bugs>
Severity: Normal    
Priority: P5 - None CC: b.eckenfels, mmeeks
Version: Beta 1   
Target Milestone: ---   
Hardware: Other   
OS: Other   
Whiteboard:
Found By: --- Services Priority:
Business Priority: Blocker: ---
Marketing QA Status: --- IT Deployment: ---

Description Ludwig Nussel 2008-05-05 09:44:50 UTC
/etc/profile contains a call to 'hostname -s'. That causes DNS timeouts on systems where the hostname is not resolvable such as laptops.
Is that call actually necessary ie could it be removed or rewritten in a way that doesn't require DNS?

See also bug 383336 for a lengthy discussion about unecessary dns timeouts.
Comment 1 Dr. Werner Fink 2008-05-05 09:56:54 UTC
Bad idea.  We could use /proc/sys/kernel/hostname but nevertheless
the resolver should prefer local data instead of running into timeouts.
This discussion was hold a few years ago and the result was to use
/etc/hosts before running into DNS timeout.

Why this has changed now?
Comment 2 Petr Baudis 2008-05-05 10:02:46 UTC
The point is that having hostname resolving to localnet address in /etc/hosts is now regarded as ugly hack and we'd like to get rid of it. (Well, I'm personally not sure if this is the best approach but there's nothing harmful about it either.)

Still, why is hostname -s even _calling_ resolver? It doesn't for me on 10.3 and I can see no reason why should it.
Comment 3 Petr Baudis 2008-05-05 10:04:41 UTC
(Sorry, I meant that there's nothing harmful about getting rid of useless self-resolving; I still think getting rid of the hostname record in /etc/hosts is risky since too many apps seem to depend on it.)
Comment 4 Dr. Werner Fink 2008-05-05 10:18:35 UTC
Beside the called resolver from `/bin/hostname -s' the bash does use the
variable HOSTNAME which is the full qualified domain name of the node. 
That may also provide a second timeout problem as the bash uses gethostname(2).
Comment 5 Petr Baudis 2008-05-05 10:25:03 UTC
gethostname(2) does not trigger resolver at all, there's no reason for it to be so - it just extracts the hostname from struct coming from the uname(2) syscall.
Comment 6 Ludwig Nussel 2008-05-05 11:11:26 UTC
The intention of this bug report is to get rid of a useless DNS roundtrip. It caught my attention due to the mentioned other bug report but is independent of that. HOST=${HOSTNAME%%.*} instead of that hostname -s call should suffice.

This is what /bin/hostname -s does for comparison:

gethostname(hname, sizeof(hname));
...
if (!(hp = gethostbyname(hname))) {
    herror(program_name);
    exit(1);
}
...
if (!(p = strchr(hp->h_name, '.')) && (c == 'd'))
	return;
...
switch (c) {
...
    case 's':
    if (p != NULL)
	*p = '\0';
    printf("%s\n", hp->h_name);
    break;

ie it tries to resolve the own hostname and then remove anything after the first dot.
Comment 7 Petr Baudis 2008-05-05 11:31:07 UTC
Oh, indeed, I must've overlooked it before but it does this in 10.3 too... Any idea why is it doing the self-resolve step?
Comment 8 Ludwig Nussel 2008-05-05 12:08:15 UTC
No idea. It makes a difference if your hostname is actually the CNAME of another hostname. I don't know whether or not that is a useful configuration :-)
Anyways, I still don't think /etc/profile should call hostname -s
Comment 9 Dr. Werner Fink 2008-05-05 12:11:20 UTC
What is wrong with

       -s, --short
              Display the short host name. This is the host name  cut  at  the
              first dot.

of /bin/hostname?
Comment 10 Ludwig Nussel 2008-05-05 12:34:12 UTC
Nothing is wrong with that. It just keeps secret that -s causes a dns lookup. RedHat has therefore added a patch to make that clear:
http://cvs.fedoraproject.org/viewcvs/rpms/net-tools/devel/net-tools-1.60-hostname_man.patch?rev=1.2&view=auto
Comment 11 Ludwig Nussel 2008-05-05 12:38:27 UTC
Ok, an alternative fix would be to patch away that dns lookup in /bin/hostname :-)
Comment 12 Dr. Werner Fink 2008-05-05 13:28:01 UTC
Use now `/bin/uname -n'
Comment 13 Bernd Eckenfels 2013-08-30 10:10:29 UTC
(In reply to comment #12)
> Use now `/bin/uname -n'

Just a short note: instead of "uname -n" you can use "hostname". When you not specify a parameter it dies no lookup. For BOTH commands it might return a qualified name (containing dots).

(we currently discussing net-tool's hostname -s behaviour on net-tools-devel@sf because it differs from debian/fedore hostname command which has removed the gethostbyname() call for -s).

https://sourceforge.net/p/net-tools/mailman/net-tools-devel/