Ubuntu: How to change the hostname

Occasionally during development activities, or even normal server operation, you’ll want to change the hostname of Ubuntu. Here’s how:

  1. In Ubuntu Desktop click the cog icon in the top right corner, then select About This Computer

    Ubuntu Desktop About This Computer

  2. Click in the Device name field and type a new name for your machine

    Ubuntu Desktop About This Computer

  3. Close the window to save the changes

  4. Open a new terminal window. The new hostname will be reflected in the window title and the prompt.

    Ubuntu Desktop About This Computer

  5. In the terminal verify that the /etc/hostname file has been updated

    cat /etc/hostname
    

    This command should output the new hostname, for example newmachinename.

  6. Lastly, check the file /etc/hosts and change the below line, where oldhostname is your old hostname.

    127.0.1.1 oldhostname

If you suddenly start getting a console error like sudo: unable to resolve host newmachinename then make sure both /etc/hostname and /etc/hosts reflect your new hostname.