In trying to change the hostname in the bash prompt of a running system (without rebooting):

  1. I tried changing the hostname of my system using the command hostname NEW_HOST_NAME. For good measure, I also did /etc/rc.d/hostname restart as suggested in the forums. Now when I issue the command hostname it shows me the NEW_HOST_NAME. But when I do echo $HOSTNAME it still shows the old hostname.

  2. So when 1 didn't work, I edited the entry against hostname in /etc/rc.conf and ran /etc/netstart to restart rc.conf as suggested in mailing lists.

Changing the entry in rc.conf does not change the output of hostname. And when I do echo $HOSTNAME it still shows the old hostname. And hostname of course shows the new name.

So I am rather lost on what is going on behind the scenes here. Any clarification/explanation would be most appreciated. And more (or maybe less) importantly how I can change the hostname of the running system so that the bash prompt gets updated (I did try source ~/.bashrc after changing things but that obviously doesn't work since echo $HOSTNAME still outputs the old hostname).

  • 2
    I suspect you need to relogin to make your environment change. It is impossible for rc script to alter env of your shell.
    – arrowd
    Commented Jan 3, 2017 at 9:28
  • Yes, of course. Although a relogin wasn't sufficient, a reboot did the trick..but that's not a problem. I was only half expecting to be able to change the hostname of a running system. I am just really curious what happens under the hood here..where does the name actually get set, and where does $HOSTNAME get its value from. Because running /etc/rc.d/hostname restart and /etc/netstart really does restart things as evidenced on the messages on the screen. So what do those really do..
    – ahron
    Commented Jan 4, 2017 at 12:40

1 Answer 1


$HOSTNAME is a bash variable that is set by the bash shell when it starts (using struct utsname.nodename) or that you override at some point. Changing the actual host name will not rerun your login scripts or start a new shell. If you want it changed, too, just change it. Or after changing the host name you could log out and back in to get the setup to use the new host name.

Note also that what you did to change the host name will probably be undone when the machine reboots. There's probably a way to configure how it gets set at boot time (but I don't presently have a FreeBSD machine to check).

  • The questioner is using a third-party shell. The Bourne Again shell sets up variables like HOSTNAME by itself, at process startup. And the questioner actually mentioned in the question how to set the hostname persistently across a reboot.
    – JdeBP
    Commented Jan 4, 2017 at 12:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .