How to Change Hostname on Ubuntu: Complete Step-by-Step Guide (2025)

Alright, let's talk about changing hostname on Ubuntu. You know when you set up a fresh Ubuntu install and it gives your machine some generic name like "ubuntu" or "ubuntu-desktop"? Drives me nuts every time. Especially when you've got multiple servers humming away in a closet and you can't tell which is which from the command line. Been there, messed that up before during a late-night maintenance window. Not fun.

Maybe you need changing hostname on Ubuntu for security reasons (those default names scream "hack me"), or perhaps you're configuring a cluster and need clear naming conventions. Whatever your reason, this guide will walk you through every step without the jargon overload. I'll even throw in some troubleshooting tricks from my own facepalm moments.

What's a Hostname Anyway and Why Bother Changing It?

Think of your hostname like your computer's nickname on the network. When you're pinging devices or SSHing between machines, that's the name you use instead of typing IP addresses all day. The default "ubuntu" might work for a single laptop, but when you're dealing with multiple systems? Recipe for chaos.

Here's why changing hostname on Ubuntu matters more than you might think:

  • Network Clarity: Instantly identify machines in your terminal or network tools
  • Security Hygiene: Default names make you an easier target (automated scanners love 'ubuntu')
  • Service Configuration: Web servers, databases, and monitoring tools often rely on hostnames
  • Avoiding Conflicts: Ever had two machines answering to the same name? Network meltdown guaranteed

Funny story - once I skipped changing hostname on a backup server. Months later during a disaster recovery test, we couldn't figure out why commands were executing on the wrong box. Turns out both responded to "backup-server". Two hours of panic because I got lazy. Don't be me.

Hostname Rules You Can't Afford to Ignore

Before changing hostname on Ubuntu, know these non-negotiables. I learned them the hard way after breaking a perfectly good DNS setup:

Rule Allowed Forbidden Why It Matters
Characters Letters (a-z), numbers (0-9), hyphens (-) Underscores (_), spaces, symbols (@#$%) DNS and networking protocols choke on special characters
Length Up to 63 characters Longer names DNS limitations cause silent failures
Start/End Letters or numbers Hyphens at start/end Some tools reject names starting/ending with hyphens
Casing Lowercase recommended Mixed case (e.g., MyServer) Case sensitivity causes inconsistent behavior

Watch Out!

I made the mistake of using an underscore once. Took me three hours to figure out why Apache wouldn't start. The error logs just said "invalid config" - zero mention of the hostname. Save yourself the headache.

Hostname Naming Convention Tips

  • Use location or function (nyc-web01, backup-db02)
  • Stick to one naming pattern across all machines
  • Avoid cutesy names - "deathstar" gets old fast when troubleshooting
  • For FQDNs (like server.domain.com), the hostname is just "server"

The Right Way: Changing Hostname on Ubuntu Using hostnamectl

Modern Ubuntu systems (17.04 and later) make changing hostname on Ubuntu dead simple with hostnamectl. This handles everything in one go - no file editing, no service restarts. Here's the full walkthrough:

Step-by-Step Permanent Hostname Change

First, crack open your terminal. Check your current name so you have a reference:

hostnamectl

You'll see something like this:

   Static hostname: ubuntu-old
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 2a9e5b1c3d4f67g8h9i0j
           Boot ID: a1b2c3d4e5f6g7h8i9j0k
    Virtualization: kvm
  Operating System: Ubuntu 22.04 LTS
            Kernel: Linux 5.15.0-76-generic
      Architecture: x86-64

Now for the main event. Replace "new-server-name" with your actual name:

sudo hostnamectl set-hostname new-server-name

But wait - we're not done. This is where most tutorials stop, but I've seen this blow up enough times to know better. You must update your hosts file. Open it with nano:

sudo nano /etc/hosts

Find the line with your old hostname. It'll look like:

127.0.0.1 localhost
127.0.1.1 ubuntu-old

Change that second line to your new hostname:

127.0.1.1 new-server-name

Save the file (Ctrl+O, then Enter in nano), exit (Ctrl+X), and you're golden. No reboot needed in most cases, but I always run:

exec bash

This reloads your shell so the prompt updates immediately. Otherwise you might still see the old name taunting you.

Pro Tip

Want to verify everything took? Run hostnamectl again and check the "Static hostname" line. Also try hostname and uname -n - all three should match.

The Old-School Method: Manual File Editing

Sometimes you're on an ancient Ubuntu system (looking at you, 14.04 LTS holdouts) or just prefer editing files directly. Here's how changing hostname on Ubuntu works at the file level:

First, edit the hostname file:

sudo nano /etc/hostname

Delete everything in this file and type your new hostname. Just the name - no spaces, no extra lines. Save and close.

Next, the critical step everyone forgets until things break - edit your hosts file:

sudo nano /etc/hosts

Modify the lines containing your old hostname. Look for:

127.0.0.1       localhost
127.0.1.1       old-hostname

Change that second line to:

127.0.1.1       new-hostname

Now, here's where things get messy. Unlike the hostnamectl method, manual edits require either a reboot or some command gymnastics. You could try:

sudo service hostname restart

But honestly? This often fails or partially applies. I usually bite the bullet and reboot:

sudo reboot

After rebooting, verify with:

hostname
# Should return new-hostname
cat /etc/hostname
# Should show new-hostname

Why Method Matters

I used to always edit files manually until I realized hostnamectl does three things at once: updates systemd, syncs with the hosts file (if you pay attention), and avoids typos. Now I only use the manual method for legacy systems.

Hostname Change Troubleshooting Guide

Even when changing hostname on Ubuntu correctly, things can go sideways. Here's my field-tested troubleshooting list:

Problem #1: Sudo Takes Forever After Changing Hostname

Classic sign you messed up /etc/hosts. Open the file and ensure you have:

127.0.0.1       localhost
127.0.1.1       your-new-hostname  # Must match exactly!

Fix: Correct the hosts file, then run sudo systemctl restart systemd-hostnamed

Problem #2: Terminal Prompt Still Shows Old Name

Your bash prompt hasn't refreshed. Either:

  • Close and reopen all terminals
  • Run exec bash in each open window

Problem #3: Network Services Fail After Hostname Change

Some services bind specifically to hostnames. Check:

ss -tulpn | grep "your-new-hostname"

If nothing shows, they might still be using the old name. Try restarting problematic services.

Problem #4: Hostname Reverts After Reboot

Cloud platforms like AWS or Azure often override hostnames. Check for:

  • /etc/cloud/cloud.cfg (set "preserve_hostname: true")
  • DHCP client configs

Critical Check

After changing hostname on Ubuntu, always verify with these three commands:

hostname          # Shows current hostname
hostnamectl       # Confirms static hostname
cat /etc/hosts    # Checks for correct mapping

Inconsistencies here cause 90% of post-change issues.

Hostname Management FAQs

Can changing hostname on Ubuntu break my system?

It shouldn't if done properly, but I've seen it mess up:

  • SSL certificates tied to old hostnames
  • Database replication setups
  • Monitoring tools like Nagios or Zabbix

Always test critical services after changing hostname on Ubuntu.

Do I need to reboot after changing hostname on Ubuntu?

With hostnamectl? Usually not. With manual file edits? Absolutely. But even with hostnamectl, some applications (like MySQL or PostgreSQL) might need restarts to recognize the new name.

What's the difference between transient, static, and pretty hostnames?

Great question! Most people only care about static:

  • Static: The real deal, stored in /etc/hostname
  • Transient: Temporary name set by DHCP (usually ignored)
  • Pretty: Fancy display name (like "Web Server #1")

When changing hostname on Ubuntu, focus on static.

How do I set a Fully Qualified Domain Name (FQDN)?

Use hostnamectl with the --static flag:

sudo hostnamectl set-hostname --static server01.example.com

Then ensure /etc/hosts has:

127.0.1.1 server01.example.com server01

But honestly? I prefer setting FQDNs in DNS, not locally.

Why does my hostname appear differently in some apps?

Drives me bonkers too. Different tools use different sources:

Command Source Notes
hostname Kernel parameter Most reliable
uname -n Same as hostname POSIX standard
hostnamectl Systemd configuration Shows all three types
Prompt ($HOSTNAME) Shell environment Updates on shell restart

Special Scenarios: When Changing Hostname Gets Tricky

Changing Hostname on Ubuntu Servers in a Cluster

Did this for a MongoDB sharded cluster last year. Nerve-wracking! Key steps:

  1. Change one node at a time
  2. Update DNS records before changing hostnames
  3. Stop cluster services during the change
  4. Check configuration files for hardcoded hostnames

Missed step 4 once. Took the whole cluster down because configs still referenced old names. Team wasn't thrilled.

Changing Hostname on Ubuntu in the Cloud

AWS, Azure, and GCP have their own quirks:

  • AWS: Set "preserve_hostname: true" in /etc/cloud/cloud.cfg
  • Azure: Edit /etc/waagent.conf (set "Provisioning.Enabled=n")
  • Google Cloud: Disable guest OS hostname management via metadata

Cloud providers often reset hostnames on reboot if you don't override their settings. Found out the hard way during an outage.

Changing Hostname on Ubuntu Without Rebooting Production Systems

For critical systems where downtime isn't an option:

# Set temporarily (lasts until reboot)
sudo hostname temporary-name

# Set permanently with hostnamectl
sudo hostnamectl set-hostname permanent-name

# Update hosts file
sudo nano /etc/hosts

# Reload services without reboot
sudo systemctl restart systemd-hostnamed
sudo systemctl restart networking  # Or NetworkManager

But test this in staging first! Some services (looking at you, Oracle DB) really don't like live hostname changes.

Hostname Best Practices for Ubuntu Pros

After changing hostname on Ubuntu hundreds of times across various environments, here's what sticks:

  • Document Before Changing: Note original name, IP, and role
  • Use DNS Consistently: Forward and reverse records should match
  • Choose Descriptive Names: "nyc-web03" beats "server3" any day
  • Update Monitoring: Nothing worse than alerts referencing dead names
  • Test Sudo Immediately: Sluggish sudo means /etc/hosts is wrong

Changing hostname on Ubuntu seems trivial until it breaks something critical. I once spent three hours debugging an Apache failure only to discover I'd typed "prdduction" instead of "production" in /etc/hosts. Moral? Double-check your spelling. Trust me, your future self will thank you during that 2 AM outage call.

Got a hostname horror story or unique scenario? I've probably seen it - ask away!

Leave a Comments

Recommended Article