Sub topic 11
Set hostname, locale, timezone and time sync
This chapter covers four identity and clock settings on a fresh server: its hostname, locale, timezone, and time synchronization. It assumes you can log in as an administrative user with sudo, as set up in earlier chapters. You should be comfortable editing files in a terminal editor and reading systemctl output.
These settings look cosmetic, but they affect logs, certificates, scheduled jobs, authentication, and clustering. A wrong clock breaks TLS validation and Kerberos, and it makes log correlation across machines unreliable. A vague hostname makes it hard to tell which server a shell prompt or a log line belongs to.
What each setting controls
| Setting | What it affects | Tool | Where it is stored |
|---|---|---|---|
| Hostname | Shell prompts, log entries, mail headers, monitoring labels | `hostnamectl` | `/etc/hostname` |
| Locale | Language of messages, sort order, character encoding | `localectl` | `/etc/locale.conf` (RHEL family), `/etc/default/locale` (Debian/Ubuntu) |
| Timezone | How local time is displayed, when cron jobs run | `timedatectl` | `/etc/localtime` (a symlink into `/usr/share/zoneinfo`) |
| Time sync | Accuracy of the system clock | `timedatectl`, `chronyc` or `timedatectl timesync-status` | Daemon configuration, see below |
All of these tools talk to systemd services (systemd-hostnamed, systemd-localed, systemd-timedated), so they behave the same way on Debian, Ubuntu, Rocky, Alma, and Fedora.
Set the hostname
Systemd tracks up to three hostnames. The static hostname is the one stored in /etc/hostname and used at boot. The transient hostname is assigned at runtime, for example by DHCP. The pretty hostname is a free-form label for humans. For a server, you normally set only the static name.
sudo hostnamectl set-hostname web01
sudo timedatectl set-timezone UTC
timedatectl status
hostnamectlThe change takes effect immediately. Open a new shell to see it in the prompt.
Choosing a name
- Use lowercase letters, digits, and hyphens. Avoid underscores and spaces, which are invalid in DNS host labels.
- Keep each label to 63 characters or fewer, and the full name to 253 or fewer.
- Pick a naming scheme that scales, such as role plus number (
web01,db02). Avoid names that encode details likely to change, such as an IP address or a project that may be renamed. - Decide whether to use a short name or a fully qualified domain name (FQDN). Both work. If you use an FQDN such as
web01.example.com, the short name is the first label.
Check name resolution
Some software resolves the machine's own hostname at startup. On Debian and Ubuntu, /etc/hosts usually contains a line for 127.0.1.1 that maps to the old name. After renaming, update it, or sudo may print unable to resolve host.
grep -n '127.0.1.1' /etc/hosts
getent hosts "$(hostname)"If the first command shows the old name, edit the line to use the new one. If your DNS publishes a record for the server, make sure it matches the hostname you chose.
Set the timezone
The common recommendation for servers is UTC. UTC has no daylight saving changes, so cron jobs never run twice or get skipped during a clock change. Logs from servers in different regions line up without conversion. Applications can still show local time to users.
Use local time only when a policy or an application requires it. If you do, remember that it affects every service on the machine.
timedatectl list-timezones | grep -i 'europe/ber'
sudo timedatectl set-timezone Europe/Berlin
timedatectlTimezone names follow the Area/City form from the IANA database. Abbreviations like CET or EST are ambiguous, so avoid them.
Hardware clock
The hardware clock (RTC) keeps time while the machine is off. Linux expects it to hold UTC. Windows dual-boot setups often store local time there, which causes an offset after reboot. On a server, check that the RTC in local TZ line in timedatectl status says no. If it says yes, correct it:
sudo timedatectl set-local-rtc 0Set the locale
A locale defines language, character encoding, number and date formats, and sort order. Pick a UTF-8 locale. The safest default for servers is en_US.UTF-8 or C.UTF-8, because scripts and tools that parse command output expect stable, English-language messages.
List what is installed and what is active:
locale -a
localectl statusOn Debian and Ubuntu, a locale must be generated before use. On RHEL-family systems, install the language pack if it is missing.
# Debian/Ubuntu
sudo locale-gen en_US.UTF-8
sudo update-locale LANG=en_US.UTF-8
# RHEL family
sudo dnf install glibc-langpack-en
sudo localectl set-locale LANG=en_US.UTF-8Log out and back in for the new value to reach your shell. Check it with echo $LANG.
Common locale errors
setlocale: LC_ALL: cannot change localemeans the requested locale is not generated or installed. Generate it as shown above.- SSH clients often forward their own
LANGandLC_*variables. If your laptop uses a locale the server lacks, you see warnings on login. Either generate that locale on the server or stop forwarding withSendEnvsettings on the client. - Setting
LC_ALLglobally overrides every other locale variable. Use it for a single command, not in system files.
Keep time accurate
Hardware clocks drift by seconds per day or more. A time synchronization daemon corrects the clock by talking to NTP servers. Two daemons are common:
systemd-timesyncdis a lightweight SNTP client. It is the default on many Debian and Ubuntu installs. It sets the clock but does not act as a time server.chronyis a full NTP implementation. It is the default on RHEL-family systems. It handles intermittent networks and virtual machines well, and it can serve time to other hosts.
Run only one of them. Check what is active:
timedatectl status
systemctl is-active chrony chronyd systemd-timesyncdIn timedatectl status, look for System clock synchronized: yes and NTP service: active. A freshly booted machine may take a minute to reach that state.
Enable synchronization
sudo timedatectl set-ntp trueThis starts whichever service systemd knows as the NTP service. If neither is installed, install one first.
# Debian/Ubuntu: install chrony (this replaces timesyncd)
sudo apt install chrony
# RHEL family: chrony is normally preinstalled
sudo dnf install chrony
sudo systemctl enable --now chronydOn Debian and Ubuntu the chrony service is named chrony. On RHEL it is chronyd.
Check sources and offset
With chrony, these commands show which servers are in use and how far off the clock is:
chronyc sources -v
chronyc trackingIn sources, a * marks the source currently selected, and + marks acceptable backups. In tracking, System time shows the current offset from the reference. A healthy server is usually within a few milliseconds.
Choosing time servers
The distribution's defaults point to its own pool, such as pool.ntp.org entries, and are fine for most servers. Change them if your network provides internal NTP servers, if outbound NTP (UDP port 123) is blocked, or if you run in a cloud that offers a local time source. For chrony, add a server or pool line to /etc/chrony/chrony.conf (Debian/Ubuntu) or /etc/chrony.conf (RHEL), then restart the service.
pool time.example.internal iburstThe iburst option sends several requests at startup so the clock syncs quickly.
Time sync in virtual machines and containers
Virtual machines can be paused, snapshotted, or migrated, and the guest clock jumps afterward. Keep an NTP daemon running in the guest even if the hypervisor offers time integration. Containers share the host clock and have no clock of their own, so configure time sync on the host only.
How the pieces fit together
The diagram shows the order to work in. Name and timezone are independent, while a working network is a precondition for NTP.
Verify the result
Run these checks together. Each one should match what you intended.
hostnamectl
timedatectl
localectl statusConfirm that Static hostname is your chosen name, Time zone is correct, System clock synchronized is yes, and System Locale shows a UTF-8 value. Then reboot once and run them again to confirm the settings persist.
Common problems
- Prompt still shows the old hostname. The shell reads the name when it starts. Log out and back in.
System clock synchronized: nofor a long time. Check that UDP port 123 is allowed outbound, that DNS resolves the pool names, and that exactly one NTP daemon is running.- Two time daemons running. They fight over the clock. Disable one with
sudo systemctl disable --now systemd-timesyncdafter installing chrony. The chrony package normally handles this on Debian and Ubuntu. - Large initial offset. If the clock is off by more than a few minutes, chrony may step it at boot. Applications that are already running can see time jump, so apply the settings before deploying services.
- Manual
date -sgets overwritten. With NTP active, manual changes are reversed. Turn NTP off withtimedatectl set-ntp falseonly for testing, then turn it on again.
Exercise
After running sudo timedatectl set-ntp true on a new Ubuntu server, timedatectl status shows NTP service: active but System clock synchronized: no five minutes later. Name two checks you would make and the command for each.
Show answer
First, confirm the network and DNS work and outbound UDP port 123 is allowed. For example, getent hosts pool.ntp.org tests name resolution, and a firewall rules listing such as sudo ufw status verbose shows whether outbound traffic is restricted. Second, confirm which daemon is running and whether it can reach a source: systemctl is-active chrony systemd-timesyncd shows whether more than one daemon is active, and chronyc sources -v (if chrony is installed) or timedatectl timesync-status (if timesyncd is in use) shows whether any source is reachable.
Exercise
Why is UTC the usual timezone for servers, and which line of timedatectl status tells you whether the hardware clock is stored in local time?
Show answer
UTC has no daylight saving changes, so scheduled jobs do not run twice or get skipped, and logs from servers in different regions line up without conversion. The line RTC in local TZ shows how the hardware clock is stored, and it should read no on a server.