We've got seperate environments at my workplace for development, testing, integration, and staging.

Within those envs, we've overloaded the hostnames in DNS - e.g. in the dev environment, the primary web machine is called web1.dev.example.com, and in the test environment, the primary web machine is web1.test.example.com.

To distinguish between machines in the different environments, I want to customise the bash prompts to display the FQDN rather than just the hostname. Well and good; I should be able to replace \h with \H in $PS1, right? Hmm. They show the exact same thing.

me@web1:~$ hostname
me@web1:~$ hostname -f
me@web1:~$ export PS1="\[\u@\h: \w\]\$ "
me@web1: ~$ export PS1="\[\u@\H: \w\]\$ "
me@web1: ~$ 

In /etc/hostname, I've got just the hostname (web1). hostname and hostname -f both return the correct results ("web1" and "web1.test.example.com" respectively), and I've got the correct entries in /etc/hosts.

What gives?

These are Ubuntu 10.04 hosts, if that makes a difference.


Try using an explicit call to hostname -f to get the fqdn of the system

export PS1="\[\u@$(hostname -f): \w\]\$ "


iain$ export PS1="\[\u@$(hostname -f): \w\]\$ "
iain@ub10-04-1.lan: ~$ 


Further research shows that the contents of /etc/hostname (Ubuntu) and /etc/sysconfig/network (CentOS) are relevant. If the FQDN is in the file then the \H works correctly.

The hostname(1) man page for Ubuntu does though say that you shouldn't put the FQDN in /etc/hostname but gives no reason as to why.

  • That's kinda ugly :( But it does work. I might leave this open for a bit longer, it'd be really nice to have a "clean" fix. – Ben Williams Aug 3 '11 at 7:13
  • This appears to be related to debian/ubuntu. It works as expected in centos. – user9517 Aug 3 '11 at 7:45
  • 1
    Yeah, it's definitely a Ubuntu problem. I've ended up using your suggested solution; I should really report this as a bug against Bash in Ubuntu. – Ben Williams Aug 4 '11 at 0:49
  • @BenWilliams: updated answer with what should make it less fugly. – user9517 Aug 4 '11 at 8:57
  • I've also found that same ambiguity in Ubuntu's hostname-setting docs. Not sure how to resolve it. – Dogweather Oct 8 '13 at 19:11

There is an insane amount of craziness about hostnames, short and long, and getting it right all the time is hard -- so I just give up and make everything use the FQDN as the hostname...

I do the same thing as you in my environments, but I chop down the FQDNs in the prompt because I know what site I'm on, and it saves space. I also colour-code my prompt based on environment so I've got a better warning of when I'm doing something somewhere "important". I also space-separate the path from everything else, to make it easy to copy-pasta pwd. A snippet from my stock /etc/profile:

if hostname -f | grep -q '\.stg\.example\.com'; then
  _ROOT_COLOR=33  # yellow
  _USER_COLOR=36  # cyan
  _ROOT_COLOR=31  # red
  _USER_COLOR=32  # green

if [ "`id -u`" -eq 0 ]; then
  PS1="\[\033[01;${_ROOT_COLOR}m\]$(hostname -f|sed 's/\.example\.com//')\[\033[01;34m\] \w #\[\033[00m\] "
  PS1="\[\033[01;${_USER_COLOR}m\]\u@$(hostname -f|sed 's/\.example\.com//')\[\033[01;34m\] \w $\[\033[00m\] "

And, as far as "ugliness" goes, who cares what the code to display the prompt looks like? It's write-only code most of the time, anyway.

  • 1
    +1 for the color usage. – Saad Masood Apr 12 '17 at 15:35

That is a bug with how Ubuntu manages /etc/hostname.

Bug filed: https://bugs.launchpad.net/ubuntu/+source/bash/+bug/1276796

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.