π Introduction
Keeping the correct time on a Linux system is essential for logging, system tasks (cron), authentication, backups, and more. In this post, we'll cover:
- How to view and change system time
- Tools like
date
,timedatectl
, andhwclock
- What NTP is and how to configure it
- Important packages:
chrony
,ntp
, andsystemd-timesyncd
- Date/time formats and timezone handling
ποΈ 1. Working with date
Command
β
View Current Date and Time:
date
Example Output:
Thu Jul 17 10:42:15 IST 2025
π οΈ Set Date and Time Using date
πΈ Option 1: Use -s
(String Format β Simple!)
sudo date -s "17 Jul 2025 12:30:00"
sudo date -s "12:00"
sudo date -s "2025-07-17"
πΈ Option 2: Use Numeric Format
sudo date 071712302025.30
# Format: MMDDhhmmYYYY.ss
β
Verify change:
date
π§ 2. timedatectl
: Systemd Time Manager
π View Current Status
timedatectl
β±οΈ Set System Date and Time
sudo timedatectl set-time "2025-07-17 12:45:00"
π Set Timezone
sudo timedatectl set-timezone Asia/Kolkata
π List All Available Timezones
timedatectl list-timezones
timedatectl list-timezones | grep Europe
π Enable or Disable NTP Sync
sudo timedatectl set-ntp true
sudo timedatectl set-ntp false
π°οΈ 3. hwclock
β Hardware Clock
π§Ύ Commands:
sudo hwclock --show
sudo hwclock --systohc
sudo hwclock --hctosys
π 4. NTP (Network Time Protocol)
β What is NTP?
A protocol that keeps system clocks in sync with global time servers (UTC-based). Important for clusters, servers, and security.
β Popular Tools for NTP
Tool | Package | Notes |
---|---|---|
ntpd |
ntp |
Classic, older NTP client/server |
chronyd |
chrony |
Fast, modern replacement |
systemd-timesyncd |
Built-in | Lightweight, good for desktops |
βοΈ Chrony Setup (Recommended)
sudo dnf install chrony # RHEL/CentOS
sudo apt install chrony # Debian/Ubuntu
sudo systemctl enable --now chronyd
Edit config:
sudo nano /etc/chrony.conf
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
Sync Immediately:
chronyc makestep
Check Sync Sources:
chronyc sources -v
π§© systemd-timesyncd (For Minimal Setups)
sudo systemctl enable --now systemd-timesyncd
timedatectl status
π§Ύ 5. Date/Time Formatting Examples
Format | Description |
---|---|
%Y | Year |
%m | Month |
%d | Day |
%H | Hour |
%M | Minute |
%S | Second |
date +"%Y-%m-%d %H:%M:%S"
π 6. UTC vs Local Time
date -u
TZ='America/New_York' date
π§ͺ 7. Troubleshooting Tips
journalctl -u chronyd
chronyc makestep
timedatectl status
β Summary Cheat Sheet
Task | Command |
---|---|
View system time | date |
Set time manually (simple) | sudo date -s "12:30" |
Set full date/time | sudo timedatectl set-time "YYYY-MM-DD HH:MM:SS" |
View timezone list | timedatectl list-timezones |
Set timezone | sudo timedatectl set-timezone Region/City |
Enable NTP sync | sudo timedatectl set-ntp true |
Install and configure chrony | sudo dnf install chrony |
View hardware clock | sudo hwclock --show |
π¬ Final Thoughts
Whether you're setting up a home server or managing enterprise infrastructure, time accuracy is vital. Learning to use tools like timedatectl
, date
, and chrony
helps ensure reliability across your systems.
Top comments (0)