Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
What does echo $PS1 show? –  Keith Jun 26 '11 at 5:11
    
@Keith - I updated the question to show PS1. –  Dizzley Jun 26 '11 at 8:18
    
Thanks for the update, that threw me off because I didn't remember dhcp could do that because I never let it. Thanks also for coming back and staying on top of the question and (eventually) adding your edit as an answer, welcome to unix.SE! –  Caleb Jun 26 '11 at 20:49

3 Answers 3

The file /etc/hostname holds the persistent copy of the hostname, and is used during bootup to set the run-time copy. To change the run-time copy without rebooting, say:

$ sudo hostname `cat /etc/hostname`

Or just pass the new host name as the parameter to the hostname(1) command.

share|improve this answer
    
My problem is in persistence. I used and verified the runtime hostname (see question). Also, "sudo hostname -F /etc/hostname" is an alternative form of the command (man hostname). However, I did as you suggested. The hostname is lost on reboot. :( I have also put the FQDN into /etc/host.conf and /etc/host.conf is set to only use bind. I am still at a loss. –  Dizzley Jun 26 '11 at 8:15
    
I've updated the Q with a resolution and will answer my own question in a little while. Thanks Warren/Keith. –  Dizzley Jun 26 '11 at 8:48
    
That should have been 'I have also put the FQDN into /etc/hosts.' –  Dizzley Jun 27 '11 at 4:47

I see you have /h in your PS1 and then mention FQDN, now, could you post your old & a new hostname you are trying to use?

  • /h shows hostname up to the first '.'
  • /H shows the full FQDN

give it a try

share|improve this answer
    
Hi njekto. NB it's \h not /h. I like to keep my hostnames private when it's a production server so "oldname" and "newname" (no dots). I'm an old hand at Unix but this had me stumped - I never saw dhcpcd being used like this. It looked like the PS1 prompt was wrong, but the real problem was /etc/hostname is overwritten by dhcpcd on boot. Dhcpcd is used because my machine image can be redeployed or duplicated onto another IP address setup so my hoster, Linode, generates default values using DHCP so each instance of my Ubunto image is unique. I am a cloud beginner, so this has been kind of fun. –  Dizzley Jun 26 '11 at 9:15
    
aha, ok thanks for sharing the solution –  njekto Jun 26 '11 at 9:32

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.

  1. Change the persistent hostname by editing the file /etc/hostname.

    sudo echo 'mynewhostname' > /etc/hostname
    
  2. 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:

    sudo hostname -F /etc/hostname
    

    or its equivalent:

    sudo hostname `cat /etc/hostname`
    
  3. Set the fully qualified domain name (FQDN) in /etc/hosts.

    Excerpt:

    127.0.0.1    mynewhostname.mydomainname.com    mynewhostname
    
  4. 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 the SET_HOSTNAME directive:

    #SET_HOSTNAME='yes'
    
  5. When possible, reboot the system and check the name has changed with:

    hostname
    

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.

share|improve this answer
    
If this answer seems appropriate, an upvote from others would be appreciated as I can't vote for my own answer. –  Dizzley Jun 27 '11 at 4:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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