"Unable to resolve hostname" - how to cleanly change hostname without rebooting

10,931

Solution 1

I would suggest using nss-myhostname. It is a nss plugin which just always resolves your current hostname, so there is no need to modify /etc/hosts.

As the issue you are describing is just a resolver issue you can fix it by having a proper DNS setup, i.e. ourwebsiteLIVE.$DOMAIN just resolves to the ip of the machine.

Solution 2

I know I'm bring this question back from the dead but currently ( at least in Ubuntu Server 16.04 ) you can do the following to update the hostname with out rebooting.

First edit your /etc/hosts and /etc/hostname with your new hostname

The run the commands

systemctl restart systemd-logind.service
hostnamectl --static --transient --pretty set-hostname YOURHOSTNAME

Your new hostname is now active without a reboot and of course will persist after a reboot.

Share:
10,931

Related videos on Youtube

Mark Amery
Author by

Mark Amery

Email address: [email protected]. No spam, please. I work for Curative Inc: https://curative.com/ I license you to use any of my Stack Overflow contributions in any way you like. If you find something wrong in one of my posts, feel free to edit it. I'm a frequent visitor and can always roll back if I think a change you've made is wrong or stupid, so you may as well be bold - it's better, here, to ask for forgiveness than permission.

Updated on September 18, 2022

Comments

  • Mark Amery
    Mark Amery almost 2 years

    At work, I work on a small website using Ubuntu servers hosted on Amazon EC2 instances. We have two webservers, and at any given time one of them is 'live' and one is 'testing', but when we've got our new features or bugfixes working on the 'testing' server, we run a script that uses the AWS API to swap the IP addresses assigned to the two boxes so that the domains being served by the two servers are swapped.

    Consequently, at any given moment, one of these servers can be either our 'live' server or our 'testing' server, but these are not permanent states.

    To avoid confusion when SSHing into either of these servers, especially if some silly person (like me) leaves an SSH session open for a long time and gets confused about which server they're on, I want to modify the hostnames of the two servers so that the prompt and title in the terminal window will read ubuntu@ourwebsiteLIVE or ubuntu@ourwebsiteTESTING depending upon whether the server is currently live or testing.

    I know I can do this by running hostname ourwebsiteLIVE etc. in our IP-swapping script, and have already implemented this. It works, but now every time I sudo anything on either of our servers it prints e.g.

    sudo: unable to resolve host ourwebsiteLIVE

    which doesn't seem to break anything, but is fairly irritating. I figure it also might end up breaking stuff in future if I try to chain commands that pipe output to each other, so I guess it's technically not just an aesthetics issue.

    All the solutions I've seen to the 'unable to resolve hostname' message involve modifying /etc/hosts or some similar file and then rebooting the server. I can see that this would be fine for many use cases, but is far from ideal for us since we're modifying the hostname routinely and programmatically; we don't want to have to wait for a reboot every time we run our swap script.

    Is there a way I can make the error message go away without having to reboot?

    • vonbrand
      vonbrand over 11 years
      The title shown by your terminal window would need to be reset, and I doubt that ssh checks routinely if the hostname has changed (why should it, a hostname is supposed to be permanent). And I find the "switch test/live" a strange idea. Just have a test machine, and copy data over to the live one whenever it is OK doesn't work because?
    • Mark Amery
      Mark Amery over 11 years
      @vonbrand Changing the IP kills all open ssh sessions anyway, so modifying the title of currently open sessions is not an issue. As for why we don't just push to live when stuff is working, it's because your way we have the ability to screw up any time we change something on the servers that isn't in our source control repo and therefore isn't part of our normal change-pushing process (e.g. modifying config files or installing new software). Our way, there's no risk we'll break the site by screwing up deployment to the live site (and reverting server admin mistakes is easy; just swap back).
  • Mark Amery
    Mark Amery over 11 years
    As I explained already in response to a comment, the reason we use hotswapping is not simply to be able to push changes to our code to live. That would already be easy with two servers and source control. The hotswapping lets us install or update third party software, or make changes to things like our Apache server config that require us to restart it, in a way that is immediately reversable and doesn't cause any downtime on the live site. What you've proposed isn't an alternate solution, it's a substantial step backwards from just having hotswappable servers without changing the hostname.
  • Mark Amery
    Mark Amery over 11 years
    Also, hosting both websites on one server seems potentially dangerous to me since if we push something to the staging site that causes it to consume all the server's memory or CPU capacity, then it breaks the live site too. Finally, can you describe what non-obvious malfunctions changing the hostname might cause? You've asserted the existence, but not provided examples.
  • Mark Amery
    Mark Amery over 11 years
    Can you describe the changes I need to make? Do I need to modify only /etc/hosts or also /etc/hostname?
  • Mark Amery
    Mark Amery over 11 years
    It's probably my lack of basic knowledge at fault, but I don't understand your final paragraph at all. Can you clarify? As for your first paragraph, I don't fully understand what the tools does (I don't really have any grasp of the implications of changing hostname to begin with) but it looks like the linked tool will solve our problem. Thank you. I will test it on Monday and accept if it works.
  • xx4h
    xx4h over 11 years
    you edit all ourwebsiteOFF in the file /etc/hosts to ourwebsiteLIVE and execute command on command-line hostname ourwebsiteLIVE then re-login.
  • DocSalvager
    DocSalvager over 11 years
    In addition to my edit above, there are a number of related questions listed in the right column that might shed more light on this.