How to Change Machine Name in Ubuntu Easily

How to Change Machine Name in Ubuntu

Changing the machine name, also known as the hostname, on an Ubuntu system is a common task for system administrators and users who want to personalize their environment or identify devices more clearly on a network.

The hostname is a unique identifier assigned to your computer and is used by various network services and tools.

This comprehensive guide explains multiple ways to change the machine name in Ubuntu, from using command-line tools to manually editing configuration files. Each method is detailed with examples and explanations, ensuring you can choose the approach that best suits your requirements.

What is a Hostname?

The hostname is a label assigned to a device on a network and is used to identify the device in various network communications. It can be a simple word or a fully qualified domain name (FQDN).

Ubuntu, like most Linux distributions, uses the hostname in system prompts and network identification.

Note: Changing the hostname does not affect your system’s IP address or any running services. However, some services might require a restart to recognize the new hostname.

Why Change the Hostname?

  • Personalization: To give your machine a custom name that reflects its purpose or owner.
  • Network Identification: Easier recognition when managing multiple machines on a network.
  • Compliance: Meet organizational or infrastructure naming standards.
  • Security: Avoid exposing default hostnames that might reveal information about your system.

Prerequisites

Before proceeding, ensure you have:

  • Access to an Ubuntu system (desktop or server).
  • Sudo or root privileges to edit system files and run commands.
  • A terminal or SSH access to execute commands.

Method 1: Using the hostnamectl Command

The hostnamectl command is part of systemd and is the recommended way to change the hostname in modern Ubuntu systems (16.04 and later). It allows you to easily set different types of hostnames.

Step-by-Step Instructions

  1. Open a terminal window.
  2. Check your current hostname by running:
    hostnamectl status
  3. Set the new hostname (replace new-hostname with your desired name):
    sudo hostnamectl set-hostname new-hostname
  4. Verify the change:
    hostnamectl status
  5. Optionally, reboot or log out and back in to see the hostname updated in your terminal prompt.

Tip: The hostnamectl command changes the static, transient, and pretty hostnames simultaneously, making it a one-stop solution.

Understanding Different Hostname Types

Hostname Type Description Example
Static hostname The traditional hostname stored in /etc/hostname. Persistent across reboots. myserver
Transient hostname The hostname assigned by the kernel, often via DHCP or mDNS. Temporary. myserver-1234
Pretty hostname A free-form UTF8 hostname for displaying purposes. Can contain spaces and special characters. My Server

You can set each hostname type individually using:

  • sudo hostnamectl set-hostname new-hostname --static
  • sudo hostnamectl set-hostname "Pretty Hostname" --pretty
  • sudo hostnamectl set-hostname new-hostname --transient (usually not recommended manually)

Method 2: Manually Editing System Files

For those who prefer or require manual control, Ubuntu stores hostname information in specific files. Editing these files directly can change the hostname.

Files Involved

File Purpose Location
/etc/hostname Stores the static hostname of the machine. /etc/hostname
/etc/hosts Maps hostnames to IP addresses for local resolution. /etc/hosts

Steps to Change Hostname Manually

  1. Open the /etc/hostname file with a text editor, for example:
    sudo nano /etc/hostname
  2. Replace the existing hostname with your new hostname.
  3. Save and exit the editor (in nano, press Ctrl+O, then Enter, then Ctrl+X).
  4. Edit the /etc/hosts file:
    sudo nano /etc/hosts
  5. Locate the line starting with 127.0.1.1. It will look similar to:
    127.0.1.1    old-hostname

    Replace old-hostname with your new hostname.

  6. Save and exit the editor.
  7. Apply the changes immediately without rebooting:
    sudo hostname new-hostname
  8. Verify the hostname:
    hostname

Warning: If you skip updating /etc/hosts, some network services might fail or behave unexpectedly.

Method 3: Using the hostname Command (Temporary Change)

The hostname command allows you to change the hostname temporarily until the next reboot. This method is useful for testing or temporary network configurations.

How to Use

sudo hostname new-hostname

This changes the hostname immediately but does not persist across reboots. To make the change permanent, update /etc/hostname and /etc/hosts as explained in Method 2.

Verifying the Hostname Change

After changing the hostname, it is important to verify the change to ensure it has been applied correctly.

Command Description Example Output
hostname Prints the current hostname. new-hostname
hostnamectl status Displays detailed hostname information.
Static hostname: new-hostname
Pretty hostname: New Hostname
Transient hostname: new-hostname
cat /etc/hostname Shows the static hostname stored in the file. new-hostname
cat /etc/hosts Check hostname mapping entries. 127.0.0.1 localhost
127.0.1.1 new-hostname

Common Issues and Troubleshooting

Changing the hostname is usually straightforward, but sometimes you may encounter issues. Below are common problems and how to resolve them.

Hostname Not Updating in Terminal Prompt

Many terminal prompts display the hostname. If the prompt does not update after changing the hostname:

  • Try logging out and logging back in.
  • Restart the terminal session.
  • Reboot the system if necessary.

Network Services Not Recognizing New Hostname

Some services cache the hostname at startup. To fix this:

  • Restart the affected services.
  • Reboot the system if restarting services is insufficient.

Permission Denied or Command Not Found

If you encounter permission errors, ensure you are using sudo for commands that modify system files or settings. For example:

sudo hostnamectl set-hostname new-hostname

If hostnamectl is not available, your system might be using an older init system. The manual method should work in any case.

Additional Tips

  • Use Simple Hostnames: Stick to alphanumeric characters and hyphens to avoid issues with network compatibility.
  • Avoid Spaces: Spaces in hostnames can cause problems with scripts and services.
  • Consistent Naming: Follow your organization’s naming conventions if applicable.
  • Backup Files: Always backup important configuration files before editing:
    sudo cp /etc/hostname /etc/hostname.backup

Summary of Commands

Purpose Command
Check current hostname hostnamectl status or hostname
Set new hostname (permanent) sudo hostnamectl set-hostname new-hostname
Set new hostname (temporary) sudo hostname new-hostname
Edit hostname file sudo nano /etc/hostname
Edit hosts file sudo nano /etc/hosts

Conclusion

Changing the machine name in Ubuntu is a straightforward but essential task for system identification and network management. Using hostnamectl is the recommended and easiest method for modern Ubuntu systems, providing flexibility and simplicity.

Alternatively, manual editing of /etc/hostname and /etc/hosts files remains effective and useful in environments where systemd tools are unavailable or unwanted.

Always verify hostname changes and ensure that network services recognize the new name to avoid disruptions. With this guide, you can confidently change and manage your Ubuntu system’s hostname as needed.

Photo of author

Emily Johnson

Hi, I'm Emily, I created Any Team Names. With a heart full of team spirit, I'm on a mission to provide the perfect names that reflect the identity and aspirations of teams worldwide.

I love witty puns and meaningful narratives, I believe in the power of a great name to bring people together and make memories.

When I'm not curating team names, you can find me exploring languages and cultures, always looking for inspiration to serve my community.

Leave a Comment

Share via
Copy link