Easy Steps To Change Hostname on Ubuntu 22.04 – OrcaCore

Posted on

Easy Steps To Change Hostname on Ubuntu 22.04 - OrcaCore

Easy Steps To Change Hostname on Ubuntu 22.04 – OrcaCore

In this guide, we want to teach you to Change Hostname on Ubuntu 22.04. hostname is used to either set or display the system’s current host or domain name. Many networking programs use this name to identify the machine, and NIS/YP also uses it.

By default, a system’s hostname is set during the OS installation. Even if we install a virtual machine, the system dynamically assigns it. However, there may be some conditions whenever we want to change the hostname. The hostname command will let us do so. Follow the guide steps below on the Orcacore website to Change Hostname on Ubuntu 22.04.

To complete this guide, log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide on Initial Server Setup with Ubuntu 22.04.

In this guide, we will show you how to change your Ubuntu hostname by using the following methods:

  • Command Line
  • GUI

1. Ubuntu 22.04: Change Hostname via Command Line

First, you need to check your current hostname on your server by using the following command:

sudo hostname
**Output**
ubuntulinux

In my case, it is ubuntulinux.

At this point, you can use the following syntax to change your hostname:

sudo hostname <your-hostname>

For example:

sudo hostname jammy.jelly

Again check your hostname, you should see that it has been changed.

sudo hostname
**Output**
jammy.jelly

2. Ubuntu 22.04: Change Hostname via GUI

From your Ubuntu desktop, click on the top right corner where the battery icon, sound icon, and internet icon are displayed. A drop-down menu will pop up. In that menu click on the settings options.

Change hostname ubuntu 22
Change hostname ubuntu 22

From the settings menu, click on About from the first section, and click on Device Name.

Then, a message box will open up displaying your previous hostname. Erase that name and insert your new desired hostname and press “Rename”.

Rename Ubuntu Device
Rename Device

That’s it, you are done.

Conclusion

At this point, you have learned to Change Hostname on Ubuntu 22.04. You can easily use the command line interface or from your desktop settings, you can change your hostname.

Hope you enjoy it. Please subscribe to us on Facebook, Twitter, and YouTube.

You may also like these articles:

Install and Use Sysdig on Ubuntu 22.04

How To Install OpenSSL 3 on Ubuntu 20.04

Alternative Methods to Change Hostname on Ubuntu 22.04

While the hostname command and the GUI settings provide straightforward ways to modify the hostname, two other persistent methods ensure the hostname remains consistent across reboots. These methods involve directly editing configuration files.

Method 1: Editing /etc/hostname

The /etc/hostname file stores the system’s hostname. Editing this file directly provides a persistent solution.

Explanation:

This method involves using a text editor to modify the /etc/hostname file. This file contains the hostname of the system. After changing the hostname in this file, you need to update the /etc/hosts file as well to ensure proper name resolution. Finally, restarting the systemd-hostnamed service or rebooting the system applies the changes.

Steps:

  1. Open /etc/hostname with root privileges:

    sudo nano /etc/hostname
  2. Replace the existing hostname with the new desired hostname. For example, change ubuntulinux to new-hostname. Save the file and exit the editor.

  3. Edit /etc/hosts:

    sudo nano /etc/hosts
  4. Find the line that contains 127.0.1.1 followed by the old hostname. Change the old hostname to the new hostname. It should look something like this:

    127.0.1.1       new-hostname

    Save the file and exit the editor.

  5. Restart the systemd-hostnamed service:

    sudo systemctl restart systemd-hostnamed
  6. Verify the change:

    hostname

    The output should now display the new hostname.

Code Example:

Let’s say we want to change the hostname to jupiter.

  1. Edit /etc/hostname:

    sudo nano /etc/hostname

    (Replace ubuntulinux with jupiter in the file.)

  2. Edit /etc/hosts:

    sudo nano /etc/hosts

    (Modify the line 127.0.1.1 ubuntulinux to 127.0.1.1 jupiter.)

  3. Restart the service:

    sudo systemctl restart systemd-hostnamed

Method 2: Using hostnamectl

The hostnamectl command is part of systemd and provides a more modern and comprehensive way to manage the system hostname. This is a preferred way to Change Hostname on Ubuntu 22.04.

Explanation:

The hostnamectl command allows you to set the hostname, including the static, transient, and pretty hostnames. The static hostname is stored in /etc/hostname and is the one used by default. The transient hostname is a temporary hostname that is lost on reboot. The pretty hostname is a human-readable hostname. This method is preferred because it handles updating related configuration files automatically, reducing the risk of errors.

Steps:

  1. Use hostnamectl set-hostname to set the new hostname:

    sudo hostnamectl set-hostname <new-hostname>

    For example:

    sudo hostnamectl set-hostname nebula
  2. Verify the change:

    hostnamectl

    The output will show the static hostname as the new hostname. You can also just run hostname to verify.

Code Example:

To change the hostname to nebula using hostnamectl:

sudo hostnamectl set-hostname nebula

The hostnamectl command simplifies the process and ensures that all relevant system components are updated correctly. This approach is less prone to manual errors compared to directly editing configuration files. This method provides a reliable way to Change Hostname on Ubuntu 22.04.

By understanding these alternative methods, you gain more control over managing the hostname on your Ubuntu 22.04 system. Whether you prefer the simplicity of hostnamectl or the directness of editing configuration files, these options provide flexibility in managing your system’s identity.

Leave a Reply

Your email address will not be published. Required fields are marked *