Skip to content

Product docsLinux VPS & VDS

Set the hostname, time zone and time sync on a Linux server

Give your Linux server a proper hostname, match it in /etc/hosts, pick a time zone and confirm that the clock stays in sync with chrony or systemd-timesyncd.

  • Beginner
  • 10 min read
  • Updated
On this page
  1. Before you start
  2. Step 1: Check the current settings
  3. Step 2: Set the hostname
  4. Step 3: Set the time zone
  5. Step 4: Make sure the clock is synchronised
  6. Troubleshooting
  7. Next steps

Two small settings cause surprisingly many problems when they are wrong: the hostname (mail servers greet each other with it, many tools write it into logs) and the clock (certificates, package repositories, two-factor codes and log timelines depend on it). This guide sets both on Ubuntu, Debian, AlmaLinux and Rocky Linux.

Before you start

Step 1: Check the current settings

Bash
hostnamectl
timedatectl

hostnamectl shows the Static hostname; timedatectl shows the local time, the Time zone, whether the System clock synchronized is yes and whether the NTP service is active.

Step 2: Set the hostname

Set the name in one step; hostnamectl hostname updates the static, transient and pretty names together:

Bash
sudo hostnamectl hostname srv1.example.com

Hostnames follow DNS rules: letters, digits and hyphens, labels separated by dots, at most 64 characters.

Then make the name resolvable on the server itself. Open /etc/hosts with sudo nano /etc/hosts and make sure there is one line with the server's public IP address, the full name and the short name. Debian's reference manual recommends the permanent IP address for servers that have one, instead of the 127.0.1.1 line used on workstations:

Text
127.0.0.1       localhost
203.0.113.10    srv1.example.com srv1

If a line with 127.0.1.1 and the old name exists, remove it or change it to the new name.

Verify: hostname -f prints srv1.example.com, and getent hosts srv1.example.com returns your IP address.

Step 3: Set the time zone

List the names and set the one you want. Etc/UTC is a common choice for servers:

Bash
timedatectl list-timezones | grep -i europe
sudo timedatectl set-timezone Europe/Istanbul

Verify: timedatectl shows the new Time zone, and date prints the local time. Services that were already running may keep the old zone in their logs until they restart.

Step 4: Make sure the clock is synchronised

Linux uses one of two time services. Since Ubuntu 25.10, including Ubuntu 26.04 LTS, the default is chrony; Ubuntu 24.04 LTS uses systemd-timesyncd, and AlmaLinux and Rocky Linux use chrony. See which one runs on your server:

Bash
systemctl status chrony chronyd systemd-timesyncd --no-pager

Turn synchronisation on (it usually already is) and check it:

Bash
sudo timedatectl set-ntp true
timedatectl

With chrony, look at the details:

Bash
chronyc tracking
chronyc sources -v

With systemd-timesyncd:

Bash
timedatectl timesync-status

Verify: timedatectl shows System clock synchronized: yes. With chrony, Leap status in chronyc tracking is Normal and chronyc sources marks one source with * (the one in use).

If neither service is installed, install one: sudo apt install chrony on Ubuntu and Debian, sudo dnf install chrony and sudo systemctl enable --now chronyd on AlmaLinux and Rocky Linux. Run only one time service at a time.

Troubleshooting

sudo: unable to resolve host srv1.example.com. The new name is not in /etc/hosts. Add it as in Step 2; the warning disappears at once.

System clock synchronized: no for a long time. The server cannot reach its time servers. Outbound NTP uses UDP port 123: check that your firewall allows outgoing traffic (UFW allows it by default), then restart the service with sudo systemctl restart chrony (Ubuntu and Debian), chronyd (AlmaLinux and Rocky Linux) or systemd-timesyncd.

apt reports Release file … is not valid yet. The clock is behind. Fix synchronisation as in Step 4, then run sudo apt update again; see fix apt and dpkg errors.

The clock is far off and chrony corrects it only slowly. chrony adjusts large offsets gradually. To step the clock at once, run sudo chronyc makestep.

Mail is rejected with HELO or hostname errors. Receiving servers compare your hostname with your reverse DNS. Use the same fully qualified name in both; see reverse DNS (PTR).

Next steps

Frequently asked questions

Which hostname should I use?

A fully qualified name you control, such as srv1.example.com, with an A record pointing to the server's IP address. Mail servers should use the same name in their reverse DNS (PTR) record.

Should a server run in UTC or in my local time zone?

Either works, because Linux keeps the clock in UTC internally. UTC makes logs from servers in different regions easy to compare and avoids daylight-saving jumps; choose local time if your team reads the logs that way.

My hostname changes back after a reboot. Why?

On images that use cloud-init, cloud-init can set the hostname at boot. Set preserve_hostname to true in its configuration, then set the name again.

Why does the clock matter for apt or TLS?

Package repositories and certificates have validity dates. A clock that is far off makes apt report a Release file that is not valid yet, and TLS connections fail with certificate date errors.

Generate Password

Please confirm