Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

My bash prompt shows my hostname. If I change my hostname with the hostname command, in /etc/hosts, /etc/hostname and $HOSTNAME, the prompt still uses the old name. Is it possible to update this without logging out?

share|improve this question

2 Answers 2

up vote 5 down vote accepted

If you have \h or \H in your prompt setting, its expansion is initialized when the shell starts, so you'll have to restart bash (exec bash). If that bothers you too much, change your prompt setting to print $HOSTNAME.

share|improve this answer

If you want your bash prompt to update dynamically as the hostname (as reported by the /bin/hostname command) is updated, you can do this:

[root@foo ~]# PS1='[\u@`/bin/hostname` \W]\$ '
[root@foo ~]# hostname
foo
[root@foo ~]# hostname bar
[root@bar ~]# hostname baz
[root@baz ~]#

It's wasteful (runs a subshell every time you hit return -- not horrible but inelegant,) so I would only use this where the situation requires it. I use it when working on a base VM template that is used to generate multiple target VM instances where metadata about the instance is indicated in the hostname. And even then the custom PS1 is only set temporarily, as part of the initial login session while the role VM instance is being deployed.

share|improve this answer

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.