Bash prompt on Ubuntu - FQDN (\H) same as hostname (\h)

Solution 1:

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

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

e.g.

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

EDIT:

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.

Solution 2:

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
else
  _ROOT_COLOR=31  # red
  _USER_COLOR=32  # green
fi

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

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.

Tags:

Bash

Ubuntu

Fqdn