Guides 11792 Published by

The guide explains why renaming a fresh Ubuntu install from its default “ubuntu” label is useful for clearer logs and smoother SSH connections. It shows how to use the modern `hostnamectl` command to change the static hostname, update `/etc/hostname`, and then adjust `/etc/hosts` so that local resolution matches the new name. For systems without `hostnamectl`, it walks through editing `/etc/hostname` manually, syncing the hosts file, and applying the change with the `hostname` command or a reboot to refresh services. The article also includes quick sanity‑check commands and tips for handling dynamic DHCP‑assigned names so that users can keep consistent hostnames on both static servers and cloud instances.



How to Change Hostname on Ubuntu 22.04 or 20.04

If you’re running a fresh install of Ubuntu and the machine keeps calling itself “ubuntu,” it’s time for a rename.

A clear hostname makes logs readable, keeps your devices organized, and stops those annoying “unknown host” messages when you try to SSH in.

Why a new hostname matters

I’ve seen people complain that their home router still lists the server as ubuntu‑01 after they’ve set up a home lab. The logs are full of “Connection from unknown host ubuntu‑01.” Changing it is cheaper than replacing hardware and fixes a ton of headaches.

Method 1 – The modern way: hostnamectl

1. Open a terminal

Hit Ctrl+Alt+T or launch your favorite shell.

2. Check the current name

   hostnamectl status

You’ll see something like Static hostname: ubuntu. That’s what you’re about to change.

3. Set the new static hostname

   sudo hostnamectl set-hostname my-cool-server

- The sudo is required; without it, you get “Permission denied.”

- The command writes to /etc/hostname for persistence and updates in‑memory values so services see the change immediately.

4. Verify

   hostnamectl status

Look for Static hostname: my-cool-server. If it’s there, you’re good.

5. Update /etc/hosts if needed

Some apps resolve localhost and the hostname to 127.0.1.1 by default. Open that file:

   sudo nano /etc/hosts

Replace the line that reads:

   127.0.1.1    ubuntu

with:

   127.0.1.1    my-cool-server

6. Reboot (or restart networking)

   sudo reboot

Rebooting ensures all daemons pick up the new name. On a headless server, you can also just run:

   systemctl restart systemd-logind

but a full reboot is cleaner.

Method 2 – Manual file edits (for older or minimal setups)

Sometimes hostnamectl isn’t available—think of an Ubuntu container or a stripped‑down server image. In that case, tweak the files directly:

1. Edit /etc/hostname

   sudo nano /etc/hostname

Replace whatever is on that line with your desired name and save.

2. Sync /etc/hosts

Follow the same pattern as in Method 1: ensure the line with 127.0.1.1 points to your new hostname.

3. Apply without rebooting

   sudo /sbin/hostname "$(cat /etc/hostname)"

This sets the current session’s name. If you’re running a script that relies on the old name, it might still see “ubuntu” until you restart those services.

Quick sanity check

After rebooting or reloading:

echo $HOSTNAME          # should print your new name
hostname -f             # fully qualified domain name (if set)
ping -c 1 localhost     # resolves to the new hostname if /etc/hosts was updated

If any of those fail, double‑check /etc/hosts and make sure you didn’t forget a space or typo in /etc/hostname.

What if I want a dynamic name?

On cloud machines that get IPs via DHCP, the hostname can be set by the DHCP server. If you keep running into automatic renames after each reboot, add Hostname= to your Netplan config (e.g., /etc/netplan/01-netcfg.yaml) or override the DHCP client’s behaviour with a client-hostname directive.

That’s all there is to it. Give your machine a proper name and enjoy cleaner logs, fewer SSH warnings, and a more professional look when you drop your laptop into the office chair.