How To Change The Hostname On Ubuntu

Ubuntu Sign in to your XDA account If you’ve ever set up a Linux distribution like Ubuntu, you may have noticed the installer prompts you to enter a name for … Read more

Taylor Bell

Taylor Bell

Published on Jul 11, 2024

How To Change The Hostname On Ubuntu
Ubuntu

4

4

Sign in to your XDA account

PC specs screen in Ubuntu

If you’ve ever set up a Linux distribution like Ubuntu, you may have noticed the installer prompts you to enter a name for your machine. Commonly called the hostname, it’s the name that identifies your Linux system every time you connect to another device over a network or Bluetooth connection.

After reviewing all the settings, toggles, and options during the installation procedure, you may have accidentally entered the wrong hostname. But don’t worry — Ubuntu lets you change your system’s hostname via GUI and a variety of terminal commands.

These steps will be valid for Ubuntu 23.04, but they should be similar to other versions of Ubuntu.

best-laptops-for-linux

Related

Best laptops for Linux in 2024

Looking for a new laptop that can run Linux? Here’s our take on the best Linux laptops that you can buy right now.

How to change the hostname on Ubuntu with the Settings app

Similar to the process on Windows 11, if you want to change the hostname of an Ubuntu system, you can just head into the Settings app. The GUI makes it easy, which is the method we suggest most people use.

  1. Click on the power button and choose Settings (or a cog wheel).
    A screenshot of Ubuntu depicting the procedure to open the Settings app

  2. Click System.
  3. Click About.
  4. Enter a new device name in the top field.

You don’t need to restart your system after changing the hostname. The change should take effect right away. Some apps might not see the name change, though, and if this is the case, simply reboot your PC.

How to change the hostname on Ubuntu with the terminal

More advanced Ubuntu users might want to change the hostname using the terminal. This method requires you to enter a few commands. You won’t have to reboot your system, either.

  1. Open the terminal (Ctrl+Alt+T).
  2. Set a new hostname using the following command:
    hostnamectl set-hostname new-hostname

    (Be sure to replace “new-hostnamewith the actual hostname you want to use.)

    Changing the hostname on Ubuntu with the terminal

  3. Confirm the hostname was changed:
    hostnamectl
MacBook-Air-13

Related

How to run an Ubuntu virtual machine on Apple Silicon for free

You can run a Ubuntu Virtual Machine on Apple Silicon for free, and here’s one of the ways to do so.

How to change the hostname on Ubuntu via configuration files

Since Ubuntu uses the hostname and host configuration files to store the name of your PC, you can rename it by replacing the old hostname with the name you wish to give your system.

  1. Open the terminal.
  2. Open the hostname file using the nano editor:
    sudo nano /etc/hostname
    The command used to open the hostname file on Ubuntu

  3. Overwrite the older name with the new hostname.
    The hostname file on Ubuntu depicting the hostname string

  4. Press Ctrl+X to exit the file. Be sure to tap Y when prompted to save the changes.
  5. Open the hosts config file:
    sudo nano /etc/hosts
    The command to open the hosts file on Ubuntu

  6. Rename the string adjacent to the 127.0.1.1 IP address to the new hostname.
    The hosts file on Ubuntu with the hostname string and its associated IP address highlighted

  7. Hit Ctrl+X followed by Y to save and exit the hosts file.
  8. (Optional) If you’re running Ubuntu as a cloud instance with the help of the cloud-init utility, you’ll need to modify a third config file called cloud.cfg. Simply open the file with this command and ensure the preserve_hostname string has the value true assigned to it.
    sudo nano /etc/cloud/cloud.cfg

After using this method, you’ll need to restart your system for Ubuntu to reflect the hostname changes.

How to temporarily change the hostname on Ubuntu

It’s also possible to temporarily change the hostname on Ubuntu. This is handy if you don’t wish the hostname to remain changed.

  1. Open the terminal.
  2. Change the hostname temporarily:
    sudo hostname new-hostname 

    (Replace “new-hostname” with the actual hostname you want to use.)

    Temporarily changing the hostname in Ubuntu

  3. Enter your password when prompted by Ubuntu.

Once you finish, you can confirm the hostname was changed with the hostname command.

What hostname did you pick for your Ubuntu PC?

Those are all the ways you can change the hostname on Ubuntu. Apart from the last method, every other procedure results in a permanent change in the hostname, though you can modify it as many times as you wish.

If you’re curious about Ubuntu, be sure to check out our detailed setup guide for the OS. We also have other articles that may interest developers, such as those on configuring environment variables and installing Anaconda and AWS CLI on Ubuntu.

Partager cet article

Inscrivez-vous à notre newsletter