How to Change the Hostname and Time Zone on a Linux Server

Two small settings that matter more than they look. The hostname is the name that appears in your prompt and on the network; a sensible one stops you running a command on the wrong machine. The time zone decides what time your logs, backups and scheduled jobs think it is. Both take one command.
Quick reference
| Task | Command | |
|---|---|---|
| See the current name | hostnamectl | |
| Rename the server | sudo hostnamectl set-hostname homeserver | |
| See the date, time and zone | timedatectl | |
| List time zones | `timedatectl list-timezones | grep London` |
| Set the time zone | sudo timedatectl set-timezone Europe/London |
1. Change the hostname
sudo hostnamectl set-hostname homeserver
Use lowercase letters, numbers and hyphens only — no spaces or underscores. The prompt updates next time you log in.
Then edit the hosts file so the name resolves locally:
sudo nano /etc/hosts
Change the line that has the old name (usually the second line, starting 127.0.1.1) to the new one:
127.0.1.1 homeserver
Skip this and you'll get "sudo: unable to resolve host" warnings — harmless, but annoying.
2. Set the time zone
Check what it's set to now:
timedatectl
Cloud servers are often on UTC, which is fine for a server, but your cron jobs will run at UTC times and your log timestamps won't match your watch. To use UK time:
sudo timedatectl set-timezone Europe/London
Not in the UK? Find your zone with:
timedatectl list-timezones | grep -i york
3. Make sure the clock is syncing
In the timedatectl output, look for System clock synchronized: yes and NTP service: active. If either says no:
sudo timedatectl set-ntp true
A server whose clock drifts causes strange failures — HTTPS certificates look invalid, logins fail, backups run at the wrong time — so this line is worth checking once.
Troubleshooting
The prompt still shows the old name. Log out and back in. Some programs cache the name until a reboot.
"Failed to set time zone: Permission denied." You forgot sudo.
Time is right but the date in logs looks an hour out. Logs from Docker containers often use UTC inside the container. Pass TZ=Europe/London as an environment variable to the container.
timedatectl says "NTP service: inactive" and set-ntp doesn't fix it. Install the time service: sudo apt install systemd-timesyncd and try again.
Wrapping up
hostnamectl set-hostname, fix /etc/hosts, timedatectl set-timezone, check NTP is on. Two minutes now, and every log line and cron job on the server will make sense for the rest of its life.
