Tech Tutorial: 108.1 Maintain System Time and Synchronize Clock via NTP #
Introduction #
Maintaining accurate system time is crucial for many system tasks and processes including logging, scheduling, and network communication. For Linux administrators, it’s essential to know how to manage and synchronize the system clock with network time protocol (NTP) servers to ensure consistent timekeeping across systems and networks.
In this tutorial, we will cover how to properly maintain the system time and synchronize the clock using NTP on a Linux system. We’ll explore various utilities and commands involved in time management and provide detailed examples for practical application.
Key Knowledge Areas #
- Understanding of system and hardware clocks
- Configuring timezone settings
- Usage of date and timedatectl commands
- Installation and configuration of NTP using chrony
Utilities #
date
timedatectl
chronyd
chronyc
Step-by-Step Guide #
1. Checking Current Time #
Before making any changes, it’s a good practice to check the current system time along with the timezone.
Using date
Command
#
# Display current date and time
date
# Output might be:
# Mon Mar 6 15:21:45 UTC 2023
Using timedatectl
Command
#
# Display detailed system time information
timedatectl
# Output might include:
# Local time: Mon 2023-03-06 15:21:45 UTC
# Universal time: Mon 2023-03-06 15:21:45 UTC
# RTC time: Mon 2023-03-06 15:21:45
# Time zone: America/New_York (EST, -0500)
# System clock synchronized: yes
# NTP service: active
# RTC in local TZ: no
2. Setting Time and Date Manually #
Sometimes, you may need to set the time and date manually. This is not recommended for servers that can use NTP, but it’s good to know how to do it.
# Set date and time manually
sudo date --set="2023-03-06 16:25:00"
# Verify the change
date
3. Configuring Timezone #
To ensure that your server operates in the correct time zone, use the timedatectl
command.
# List all timezones
timedatectl list-timezones
# Set system timezone
sudo timedatectl set-timezone America/New_York
# Verify the change
timedatectl
4. Installing and Configuring NTP Using Chrony #
Chrony is a versatile NTP client and server that is designed to work well in a variety of network conditions.
Installing Chrony #
# Install chrony on Ubuntu
sudo apt update && sudo apt install chrony
# Install chrony on CentOS
sudo yum install chrony
Configuring Chrony #
The main configuration file for chrony is /etc/chrony/chrony.conf
.
# Edit chrony configuration
sudo nano /etc/chrony/chrony.conf
# Add server lines to use public NTP servers
server 0.pool.ntp.org iburst
server 1.pool.ntp.org iburst
server 2.pool.ntp.org iburst
server 3.pool.ntp.org iburst
Starting and Enabling Chrony #
# Start chrony service
sudo systemctl start chronyd
# Enable chrony to start at boot
sudo systemctl enable chronyd
Checking Chrony Status #
# Checking chrony synchronization
chronyc tracking
# Check chrony sources
chronyc sources -v
Conclusion #
In this tutorial, we covered the essentials of maintaining system time and synchronizing the clock via NTP on a Linux system. We went through checking, setting, and configuring system time, as well as installing and setting up Chrony for NTP synchronization. Accurate timekeeping is critical for the smooth functioning of IT systems, and understanding how to manage it effectively is a valuable skill for any system administrator.
Remember, while you can set the time manually, using NTP is generally the best practice for maintaining accurate system time across networked computers.