# 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.

Difficulty: Beginner

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

- You are connected over SSH as a user with `sudo`; see [connect to your Linux server](/guides/connect-to-linux-server-ssh).
- The server already has a name from the order form; see [configure a server in the order form](/guides/order-form-configure-server). Change it only if you want your own name, for example one under your domain.
- For your own name, create an **A record** (and an AAAA record for IPv6) for it at your DNS host; see [point a domain to your server](/guides/point-domain-to-server).

## 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.

> **Note**
>
> **Does the name change back after a reboot?** Then the image runs cloud-init, which sets the hostname at boot. Check for `/etc/cloud/cloud.cfg`; set `preserve_hostname: true` in it (or in a file under `/etc/cloud/cloud.cfg.d/`), then repeat Step 2. If `manage_etc_hosts: true` is set there too, cloud-init rewrites `/etc/hosts` at every boot from `/etc/cloud/templates/hosts.tmpl`: make your change in that template instead, or set the option to `false`.

## 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](/guides/fix-apt-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)](/guides/reverse-dns-ptr).

## Next steps

- Finish the basics: [secure a new Linux server](/guides/secure-a-new-linux-server).
- Running a mail server? Set [reverse DNS (PTR)](/guides/reverse-dns-ptr) to the same name.

## 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.

---

Source: <https://hyperdc.com/guides/products/linux-hostname-time-settings>\
Updated: 2026-10-15
