I changed my hostname, why is my bash PS1 prompt unchanged?

28,699

Solution 1

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.

    echo 'mynewhostname' | sudo tee /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.

Solution 2

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.

Solution 3

  • Capitalize the h in your PS1:

    PS1='${debian_chroot:+($debian_chroot)}\u@\H:\w\$ '
    

Solution 4

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:
28,699

Related videos on Youtube

Dizzley
Author by

Dizzley

Updated on September 18, 2022

Comments

  • Dizzley
    Dizzley almost 2 years

    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.

    • Keith
      Keith about 13 years
      What does echo $PS1 show?
    • Dizzley
      Dizzley about 13 years
      @Keith - I updated the question to show PS1.
    • tcoolspy
      tcoolspy about 13 years
      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!
  • Dizzley
    Dizzley about 13 years
    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
    Dizzley about 13 years
    I've updated the Q with a resolution and will answer my own question in a little while. Thanks Warren/Keith.
  • Dizzley
    Dizzley about 13 years
    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
    Dizzley about 13 years
    If this answer seems appropriate, an upvote from others would be appreciated as I can't vote for my own answer.
  • Dizzley
    Dizzley about 13 years
    That should have been 'I have also put the FQDN into /etc/hosts.'
  • berezovskyi
    berezovskyi over 3 years
    I was about to go crazy as every part of the system showed the new qualified hostname except for the PS1 prompt. Thank you!