Home Beginner's Guide How to Easily Change the Hostname on Your Linux System

How to Easily Change the Hostname on Your Linux System

Changing the hostname in Linux can help personalize your system or resolve network issues. This comprehensive guide provides a clear, step-by-step approach to updating your hostname, including necessary commands and tips for ensuring the change is applied correctly.

by Arun Kumar
change hostname linux

Changing the hostname on a Linux system is a common task for system administrators and enthusiasts alike. This guide will walk you through the process on an Ubuntu system. As a system admin who enjoys the flexibility and power of Linux, I find this task both straightforward and satisfying.

What is a hostname?

A hostname is a label that identifies your machine on a network. It’s like your computer’s name, which can be used by other devices to communicate with it. Having a meaningful hostname can help you manage your network more efficiently.

Why change the hostname?

There are several reasons you might want to change your hostname:

  • To better reflect the machine’s purpose (e.g., “webserver” instead of “ubuntu-12345”).
  • To comply with your organization’s naming conventions.
  • For personal preference and better organization.

Checking the current hostname

Before making any changes, let’s check the current hostname. Open a terminal and type:

hostname

You will see the current hostname displayed. For example:

ubuntu-12345

Changing the hostname temporarily

If you want to change the hostname temporarily (until the next reboot), use the hostname command followed by the new name:

sudo hostname new-hostname

Replace new-hostname with your desired hostname. For example:

sudo hostname my-new-host

To verify the change, type:

hostname

You should see the new hostname:

my-new-host

However, this change will not persist after a reboot. To make it permanent, follow the steps below.

Changing the hostname permanently

To permanently change the hostname, you need to update several configuration files.

1. Edit the /etc/hostname file

Open the /etc/hostname file in your favorite text editor. Personally, I prefer nano for its simplicity:

sudo nano /etc/hostname

Replace the existing hostname with your new desired hostname. Save and close the file (in nano, you do this by pressing CTRL+X, then Y to confirm, and Enter).

2. Edit the /etc/hosts file

Next, you need to update the /etc/hosts file to map the new hostname to the localhost IP address. Open the file with nano:

sudo nano /etc/hosts

Find the line that looks like this:

127.0.1.1   old-hostname

Replace old-hostname with your new hostname:

127.0.1.1   my-new-host

Save and close the file.

3. Apply the changes

To apply the changes without rebooting, you can use the hostnamectl command:

sudo hostnamectl set-hostname my-new-host

Then, restart the systemd-hostnamed service to apply the changes:

sudo systemctl restart systemd-hostnamed

Verify the new hostname

To verify that the hostname has been changed successfully, run the following command:

hostname

You should see the new hostname:

my-new-host

Additionally, you can check the hostnamectl status:

hostnamectl

This command provides detailed information about your system, including the new hostname.

Conclusion

Changing the hostname on an Ubuntu system is a simple yet important task that can help you keep your network organized. Personally, I enjoy these little customization tasks in Linux. They give me a sense of ownership and control over my system. Plus, it’s always nice to see a machine named something meaningful rather than a random string of characters.

If you have any questions or run into any issues, feel free to ask in the comments.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.