Long hostname placeholder for PS1 (\H) fails in Bash

Bug #1276796 reported by Rodney Beede
42
This bug affects 8 people
Affects Status Importance Assigned to Milestone
bash (Ubuntu)
Confirmed
Undecided
Unassigned

Bug Description

When using "\H" in the PS1 prompt value bash is not displaying the FQDN but instead only the short form hostname. This is the same as "\h".

Issue has also been noted at
http://serverfault.com/questions/296970/bash-prompt-on-ubuntu-fqdn-h-same-as-hostname-h
https://answers.launchpad.net/ubuntu/+source/bash/+question/200900

This appears related to /etc/hostname only having the host and not the FQDN.

Bash should obtain the \H value from somewhere other than /etc/hostname.
---
ApportVersion: 2.12.5-0ubuntu2.2
Architecture: amd64
DistroRelease: Ubuntu 13.10
InstallationDate: Installed on 2014-01-16 (20 days ago)
InstallationMedia: Ubuntu-Server 13.10 "Saucy Salamander" - Release amd64 (20131016)
MarkForUpload: True
Package: bash 4.2-5ubuntu3
PackageArchitecture: amd64
ProcEnviron:
 TERM=xterm
 PATH=(custom, no user)
 XDG_RUNTIME_DIR=<set>
 LANG=en_US.UTF-8
 SHELL=/bin/bash
ProcVersionSignature: Ubuntu 3.11.0-15.23-generic 3.11.10
Tags: saucy
Uname: Linux 3.11.0-15-generic x86_64
UpgradeStatus: No upgrade log present (probably fresh install)
UserGroups: adm cdrom dip lpadmin plugdev sambashare sudo

Revision history for this message
Jörg Frings-Fürst (jff-de) wrote :

Hello,

we need some more information. From a terminal window please run:

apport-collect 1276796

and then change the status of the bug to 'Confirmed'.

Jörg

Changed in bash (Ubuntu):
status: New → Incomplete
Revision history for this message
Rodney Beede (business2008+launchpad) wrote : Dependencies.txt

apport information

tags: added: apport-collected saucy
description: updated
Changed in bash (Ubuntu):
status: Incomplete → Confirmed
Revision history for this message
Jörg Frings-Fürst (jff-de) wrote : Re: bash fails to provide FQDN for PS1 with \H

Hi Rodney,

do you change the hostname over dhcp? Or how?

Jörg

Changed in bash (Ubuntu):
status: Confirmed → Incomplete
Revision history for this message
Rodney Beede (business2008+launchpad) wrote :

There is no DHCP at all.

The OS was installed with a static IP configuration during the install setup. I specified the hostname at that time by typing it into the installer prompt.

[rbeede@ubuntu13:/home/rbeede] $ cat /etc/hostname
okdev1383

[rbeede@ubuntu13:/home/rbeede] $ cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
        address 10.9.156.83
        netmask 255.255.252.0
        network 10.9.156.0
        broadcast 10.9.159.255
        gateway 10.9.156.1
        # dns-* options are implemented by the resolvconf package, if installed
        dns-nameservers 10.9.0.25 10.10.0.25
        dns-search example.org

Revision history for this message
Jörg Frings-Fürst (jff-de) wrote :

Hi,

I take a look to the bash source.

The hostname was only read on startup.

So you must relogin after changing your hostname.

It was tested on 13.10 and 14.04.

Jörg

Changed in bash (Ubuntu):
status: Incomplete → Invalid
Revision history for this message
Rodney Beede (business2008+launchpad) wrote :

Rebooted. No change to hostname. No DHCP. Problem still exists.

Should see a prompt of:
[<email address hidden>:/home/rbeede] $

but instead I only get:
[rbeede@okdev1381:/home/rbeede] $

[rbeede@okdev1383:/home/rbeede] $ echo $PS1
[\u@\H:/home/rbeede] $

[rbeede@okdev1383:/home/rbeede] $ cat /etc/os-release
NAME="Ubuntu"
VERSION="13.10, Saucy Salamander"

The bash man page states:

     \h
     The hostname, up to the first ‘.’.

     \H
     The hostname.

This would imply that the difference between \h and \H is
    \h is only the hostname
    \H is the FQDN

The source code however shows:

     case 'h':
     case 'H':
       temp = savestring (current_host_name);
       if (c == 'h' && (t = (char *)strchr (temp, '.')))
  *t = '\0';
       goto add_string;

This shows that \h is always the short form of the hostname while \H may be the FQDN. In most cases, however, \H just resolves so the short form hostname and not the FQDN (aka domain name).

This is an ambiguity in the bash man page documentation. The bash man page should be updated to say:

     \h
     The hostname, up to the first ‘.’.

     \H
     The hostname possibly with the parts after the '.' (domain name or FQDN but usually not)

Changed in bash (Ubuntu):
status: Invalid → Incomplete
Revision history for this message
Jörg Frings-Fürst (jff-de) wrote :

cat /etc/hostname: >trusty-virtbox.jff.de<

prompt with h: >jff@trusty-virtbox:~$<

prompt with H: ><email address hidden>:~$<

then

cat /etc/hostname: >trusty-virtbox<

prompt with h: >jff@trusty-virtbox:~$<

prompt with H: >jff@trusty-virtbox:~$<

Thus makes bash exactly what it should do.

If the contents of /etc/hostname is not FQDN, bash cannot show an FQDN.

But this is a problem of your setup und not of bash.

If you want to change the function then contact the maintainer direct via http://tiswww.case.edu/php/chet/bash/bashtop.html#Maintainer

Jörg

Changed in bash (Ubuntu):
status: Incomplete → Invalid
Revision history for this message
Rodney Beede (business2008+launchpad) wrote :

The default install of Ubuntu fills in /etc/hostname with only the host name and not any of the DNS domain name parts.

For Ubuntu 13.10 the following is noted:
http://manpages.ubuntu.com/manpages/saucy/man1/hostname.1.html

/etc/hostname Historically this file was supposed to only contain the
       hostname and not the full canonical FQDN. Nowadays most software is
       able to cope with a full FQDN here. This file is read at boot time by
       the system initialization scripts to set the hostname.

For Ubutnu 12.04 the following is noted:
http://manpages.ubuntu.com/manpages/precise/man1/hostname.1.html

   /etc/hostname This file should only contain the hostname and not the
       full FQDN.

So the issue is how /etc/hostname is treated and that bash uses the value provided by how the distro set it up.

Ubuntu 13.10 still leaves out the FQDN in /etc/hostname when you install.

It'd be useful if the bash man page stated something like:

     \h
     The hostname, up to the first ‘.’.

     \H
     The hostname. Depending on the distro this may or may not contain the domain name or FQDN.

Revision history for this message
Rodney Beede (business2008+launchpad) wrote :

I've submitted a bug report up-stream. A copy of that bug report is attached.

To: <email address hidden>,<email address hidden>
Subject: bash fails to provide FQDN for PS1 with \H

Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64' -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-pc-linux-gnu' -DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H -I. -I../bash -I../bash/include -I../bash/lib -D_FORTIFY_SOURCE=2 -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -Wall
uname output: Linux okdev1383 3.11.0-15-generic #23-Ubuntu SMP Mon Dec 9 18:17:04 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Bash Version: 4.2
Patch Level: 45
Release Status: release

Description:
        The bash man page offers two options for the PS1 prompt: \h and \H. \h is suppose to be for the short form of the host name whereas \H can be the longer form. The text reads:

\h
The hostname, up to the first ‘.’.

\H
The hostname.

The difference would imply that using \H would give you the FQDN version of the host name, but that is not the case. It is distro specific.

See https://bugs.launchpad.net/ubuntu/+source/bash/+bug/1276796 for a discussion thread on this issue in Ubuntu.

Repeat-By:
        Tested on Ubuntu 12.04 and 13.10. Set PS1 with \H. Because Ubuntu's installer creates /etc/hostname without the FQDN (as recommended in their man pages) bash never gets a host name other than the short version.

Fix:
        Option 1: use a function other than gethostname when the \H option is supplied. If the system doesn't have a FQDN this may not change anything, but it has a better chance at avoiding disto specific behavior.

        Option 2: Update the documentation as follows:

\h
The hostname, up to the first '.'.

\H
The hostname. May or may not include the domain name or FQDN.

Revision history for this message
Display Name (user340562791542-deactivatedaccount) wrote :

I confirm the problem. Bash uses gethostname which seems to be relying on the content of /etc/hostname. I have patched Bash to use a better API, getaddrinfo, the same used by the hostname utility when provided with the -f/--long option. Rodney, I could not find your report in the mailing list archives. I have subscribed to it and sent the report with patch attached and reference to this bug, but it is not showing up there still. It should appear here soon: http://lists.gnu.org/archive/html/bug-bash/2014-06/index.html.

In the case they don't feel like accepting it, I have created a Bazaar branch to handle this, it can be easily merged into the Ubuntu version. If we have a long hostname, then there's no sense in providing two placeholders for the same thing. This patch makes Bash more effectively differentiate between short and long hostname, by using a proper API to retrieve the value for the \H placeholder. This is enough reason for applying the change.

Changed in bash (Ubuntu):
status: Invalid → Confirmed
Revision history for this message
Display Name (user340562791542-deactivatedaccount) wrote :
Revision history for this message
Jörg Frings-Fürst (jff-de) wrote :

Hi,

please upload only you patch files and not your binaries.
If you want you can build it in a ppa.
Binary deleted.

Jörg

summary: - bash fails to provide FQDN for PS1 with \H
+ Long hostname placeholder for PS1 (\H) fails in Bash
Revision history for this message
Henk-Jan (henk-jan) wrote :

This problem doesn't occur in Ubuntu 16.04 LTS

Revision history for this message
Marc Bihlmaier (78luphr0rnk2nuqimstywepozxn9kl19tqh0tx66b5dki1xxsh5mkz9gl21a5rlwfnr8jn6ln0m3jxne2k9x1ohg85w3jabxlrqbgszpjpwcmvkbcvq9-launchpad-a811i2i3ytqlsztthjth0svbccw8inm65tmkqp9sarr553jq53in4xm1m8wn3o4rlwaer06ogwvqwv9mrqoku2x334n7di44o65qze67n1wneepmidnu) wrote :

Same problem here with 16.04 LTS, 17.04 and 17.10 , when not specifying the FQDN in /etc/hostname:

marc@marcubuntu:~$ hostname --fqdn
marcubuntu.tesdomain.intern
marc@marcubuntu:~$ hostname
marcubuntu
marc@marcubuntu:~$ head -n 2 /etc/hosts
127.0.0.1 localhost
127.0.1.1 marcubuntu.tesdomain.intern marcubuntu
marc@marcubuntu:~$ grep PS1 .bashrc
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\H\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
    PS1='${debian_chroot:+($debian_chroot)}\u@\H:\w\$ '
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\H: \w\a\]$PS1"
marc@marcubuntu:~$ bash
marc@marcubuntu:~$ cat /etc/hostname
marcubuntu

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers