ESC

Start typing to search across invoices, services, domains, tickets, and more...

Search... Ctrl+K
Linux Server

Linux Time Zone, NTP Time Sync, Hostname and DNS Settings (timedatectl, chrony, hostnamectl)

6 steps 14 min read 5 views 0
On this page

New servers often run on UTC, so log timestamps do not match your local time, and a drifting clock breaks HTTPS certificate checks, cron jobs and two-factor codes. This guide shows how to change the Linux time zone with timedatectl, enable NTP time sync with chrony, change the hostname and /etc/hosts, and set DNS resolvers correctly whether the system uses systemd-resolved, NetworkManager or a plain resolv.conf. It covers Debian/Ubuntu and Rocky Linux/AlmaLinux.

Step 1: Check and Change the Time Zone with timedatectl

Check the current status, then pick a zone name from the list: Asia/Hong_Kong for Hong Kong, Asia/Tokyo for Japan, Europe/Moscow for Russia. If the server serves users in many regions, staying on UTC is a perfectly good choice. Changing the zone does not change the actual time, only how it is displayed; restart long-running services such as PHP-FPM or MySQL so they pick up the new zone.

timedatectl                                   # current time, time zone, NTP status
timedatectl list-timezones | grep -E 'Asia|UTC'
timedatectl set-timezone Asia/Hong_Kong       # e.g. Asia/Tokyo, Asia/Shanghai, Europe/Moscow, UTC
timedatectl set-local-rtc 0                   # keep the hardware clock in UTC
date

Step 2: Enable NTP Time Sync with chrony

chrony keeps accurate time even on unstable networks. It is the default on Rocky/AlmaLinux, and on Debian/Ubuntu it replaces systemd-timesyncd when installed. In the output, the line marked ^* is the source in use, and chronyc tracking shows the current offset.

# Debian / Ubuntu (replaces systemd-timesyncd)
apt install -y chrony
systemctl enable --now chrony

# Rocky Linux / AlmaLinux
dnf install -y chrony
systemctl enable --now chronyd

timedatectl set-ntp true
chronyc sources -v        # "^*" marks the server currently in use
chronyc tracking          # "System time" shows the current offset

Step 3: Use Custom NTP Servers

The distribution's default pool is usually fine. To choose specific servers, edit the pool/server lines in the chrony config. makestep lets chrony jump the clock when the offset is large right after boot. Outbound UDP port 123 must be allowed by your firewall.

# Config file: /etc/chrony/chrony.conf (Debian/Ubuntu) or /etc/chrony.conf (Rocky/AlmaLinux)
# Replace or add the server lines, for example:
pool pool.ntp.org iburst
server time.cloudflare.com iburst
makestep 1.0 3

# Apply and step the clock immediately if it is far off
systemctl restart chrony        # chronyd on Rocky/AlmaLinux
chronyc makestep
KVM guests usually keep good time, but the clock may jump after a snapshot restore or host migration. chrony corrects this automatically, so there is no need for an ntpdate cron job.

Step 4: Change the Linux Hostname and /etc/hosts

hostnamectl applies the change immediately and keeps it after reboot. Then add the new name to /etc/hosts; otherwise sudo may warn "unable to resolve host" and some mail or database software starts slowly. On images with cloud-init, set preserve_hostname so a reboot does not revert it. The shell prompt shows the new name after you reconnect over SSH.

hostnamectl set-hostname web01.example.com
hostnamectl

# /etc/hosts - make the new name resolve locally
127.0.0.1     localhost
127.0.1.1     web01.example.com web01      # Debian/Ubuntu convention
# or map it to the server's public IP:
# 203.0.113.10  web01.example.com web01

# Images with cloud-init: stop it from resetting the hostname on reboot
grep -n preserve_hostname /etc/cloud/cloud.cfg
sed -i 's/^preserve_hostname:.*/preserve_hostname: true/' /etc/cloud/cloud.cfg

Step 5: Find Out What Manages DNS

Distributions manage DNS differently, and editing /etc/resolv.conf directly may be overwritten at the next reboot. Check whether the file is a symlink or a regular file and whether it starts with a "Generated by" comment before deciding how to change it.

ls -l /etc/resolv.conf
# -> ../run/systemd/resolve/stub-resolv.conf  : managed by systemd-resolved (Ubuntu)
# -> regular file "Generated by NetworkManager": managed by NetworkManager (Rocky/AlmaLinux)
# -> regular file without such comment         : edited by hand (common on Debian)
resolvectl status 2>/dev/null | head -n 20

Step 6: Set DNS Resolvers

Pick one method based on the previous step: a drop-in for systemd-resolved on Ubuntu, nmcli on Rocky/AlmaLinux (use the connection name shown by nmcli con show), or edit resolv.conf directly on classic Debian. Finally, test name resolution with getent.

# A) systemd-resolved (Ubuntu and some Debian setups)
mkdir -p /etc/systemd/resolved.conf.d
cat > /etc/systemd/resolved.conf.d/dns.conf <<'EOF'
[Resolve]
DNS=1.1.1.1 8.8.8.8 2606:4700:4700::1111
FallbackDNS=9.9.9.9
EOF
systemctl restart systemd-resolved
resolvectl status

# B) NetworkManager (Rocky Linux / AlmaLinux)
nmcli -t -f NAME,DEVICE con show --active
nmcli con mod "System eth0" ipv4.dns "1.1.1.1 8.8.8.8" ipv4.ignore-auto-dns yes
nmcli con up "System eth0"

# C) Plain /etc/resolv.conf (Debian with ifupdown)
cat > /etc/resolv.conf <<'EOF'
nameserver 1.1.1.1
nameserver 8.8.8.8
options timeout:2 attempts:2
EOF

# Test
getent hosts www.imidc.com
nmcli con up briefly reloads the network interface, and a mistake can drop your SSH session. Double-check the connection name and, if possible, work from the console provided in the client area.

FAQ

chronyc sources shows "?" and never syncs?

Usually UDP 123 is blocked by a firewall, or DNS cannot resolve the NTP server names. Check DNS first, then allow outbound UDP 123 or switch to reachable NTP servers.

resolv.conf on Ubuntu only contains 127.0.0.53?

That is the systemd-resolved local stub and is expected. Run resolvectl status to see the real upstream servers, and change them with method A in Step 6.

Will changing the hostname break my website or control panel?

Normally not, but update any mail server, aaPanel or database grants that reference the old name. A mail server's hostname should also match its rDNS (PTR) record; see our rDNS/PTR tutorial.

Still stuck after following these steps? Open a support ticket and the IMIDC 24/7 technical team will help. Include the server IP, OS version, the commands you ran and the full error output so we can pinpoint the issue faster.

Was this answer helpful?

Related Tutorials