Configuring the hostname, time zone and time synchronisation on a Linux server

Linux 3 min read

When setting up a new VPS, one of the first things you should do is configure the server name and the time. The correct hostname is important for email and logs, whilst the accurate time and correct time zone are essential for scheduled tasks (cron), interpreting logs, and the functioning of SSL and two-factor authentication.

Hostname

What should the server be called?

We recommend that the hostname be a fully qualified domain name (FQDN) which is a subdomain of a domain you manage, for example szerver1.example.hu. Do not use the website’s main domain (example.hu) or the www subdomain, as this may cause problems with email and in certain control panels.

Settings

# Az aktuális név
hostnamectl

# Új név beállítása
sudo hostnamectl set-hostname szerver1.example.hu

The change takes effect immediately; no restart is required; the old name at the start of the command line will only be updated the next time you log in.

Add the name to the /etc/hosts file as well, using the server’s own IP address:

203.0.113.10   szerver1.example.hu szerver1

DNS and reverse DNS (PTR)

To ensure that the domain name points to the server even when accessed externally:

  1. In the domain’s DNS zone, create an A record (or an AAAA record for IPv6) with the name szerver1.example.hu, pointing to the server’s IP address. About DNS records: Types of DNS records.
  2. If you send emails from the server, the reverse DNS (PTR) record must also point to this name; otherwise, many mail servers will treat the emails as spam. The PTR record can be set by the owner of the IP address, i.e. us: please contact our support team, providing the IP address and the hostname.

Check:

dig +short szerver1.example.hu A
dig +short -x 203.0.113.10

Time zone

# Az aktuális idő, időzóna és a szinkronizálás állapota
timedatectl

# A magyar időzóna beállítása
sudo timedatectl set-timezone Europe/Budapest

The available time zones can be listed using the timedatectl list-timezones command. The Europe/Budapest time zone automatically handles daylight saving time.

Some providers run their servers in the UTC time zone to ensure that logs are unambiguous. If your website is aimed at Hungarian visitors and you wish to schedule cron jobs according to local time (see: Crontab syntax and scheduled tasks on Linux), it is simpler to use the Europe/Budapest time zone.

After changing the time zone, restart any services that write timestamps (e.g. cron/crond, rsyslog, the web server and the database), or restart the server.

PHP and database: PHP uses its own time zone setting (date.timezone in the php.ini file), and MariaDB/MySQL can also handle its own time zone. If the time displayed on the website is incorrect, please check these settings as well.

Time synchronisation (NTP)

The server’s clock may drift over time, so it needs to be synchronised with a time server. Check the output of timedatectl:

System clock synchronized: yes
              NTP service: active

If synchronisation is not active, please enable it:

sudo timedatectl set-ntp true

AlmaLinux, Rocky Linux: chrony

On these systems, the chronyd service handles synchronisation.

sudo dnf install chrony
sudo systemctl enable --now chronyd

# A szinkronizálás állapota és az eltérés
chronyc tracking

# A használt időszerverek
chronyc sources -v

The default setting for AlmaLinux 10 is pool 2.almalinux.pool.ntp.org iburst; in testing, this synchronised correctly (System clock synchronized: yes). To use Hungarian time servers, replace the pool line in the /etc/chrony.conf file:

pool hu.pool.ntp.org iburst

Then restart it: sudo systemctl restart chronyd.

Ubuntu, Debian: systemd-timesyncd

On Ubuntu and Debian, synchronisation is handled by systemd-timesyncd by default (the command timedatectl set-ntp true enables this). In the test, Ubuntu 24.04 used the server ntp.ubuntu.com, whilst Debian 12 used 2.debian.pool.ntp.org. The status is:

timedatectl timesync-status

You can specify the time server in the /etc/systemd/timesyncd.conf file (NTP=hu.pool.ntp.org), then run sudo systemctl restart systemd-timesyncd.

Back: Linux articles (62)