I changed my hostname by editing /etc/hostname and can see the new hostname using the hostname
and hostname -F
commands.
But the shell prompt is still showing the old hostname.
This is Ubuntu 11.0.4 by the way. The prompt is set in my .bashrc
which I have not edited. Logging out and even rebooting has no effect.
Relevant section of the standard Ubuntu .bashrc
:
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
($debian_chroot is unset as I login…)
I guess the hostname is picked up by the special character \h.
Here's the PS1 setting as reported in the shell:
PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
And here is what PS1 shows:
username@oldhostname:~$
I repeated the process using the exact command in Warren's answer. It turns out that the hostname works until reboot but then it is lost, even though /etc/hostname
contains the new hostname.
Best Answer
I'm answering my own question, in light of previous answers by Keith and Warren, and the actual resolution. The perceived problem was "I changed my hostname, why is my bash PS1 prompt unchanged?" The actual problem was "Why has my system reverted to its old hostname on reboot?"
The answer in this particular case was: DHCP is configured to override local settings.
An Effective Way to Change the Hostname
The following is applicable to Ubuntu, ymmv.
Change the persistent hostname by editing the file
/etc/hostname
.To change the hostname for the running system use the hostname command. Without Step 1 this would be reset on reboot. It makes sense to use the value you just set:
or its equivalent:
Set the fully qualified domain name (FQDN) in
/etc/hosts
.Excerpt:
Check if the machine is running a DHCP client. In addition to IP address, a DHCP server may well override settings like hostname and DNS resolution. A "cloud" hosting service might do this so the image of a machine on disk can be reused several times without editing configuration files.
If it exists, edit the DHCP client configuration file
/etc/default/dhcpcd
to comment out theSET_HOSTNAME
directive:When possible, reboot the system and check the name has changed with:
Step 4 was news to me and caught me out. I thought it would be useful to document the whole process in this answer. That step is courtesy of (Linode) my hosting service's instructions which I really should have read properly.