How To Synchronize System Time In Linux?


Time is important for systems because all work done according to time and logs get their timestamps. Normally server hardware provides time but it can be false over time because the hardware clock is not precise. Ntp is a protocol designed to get time info from the network. NTP can also provide date and time information to other servers as a daemon. NTP full name is Network Time Protocol.

Install Ntpdate

Ntpdate is simple and fast ntp client used to get information about time.

CentOS, Fedora, Red Hat

$ yum install ntpdate -y

Ubuntu, Debian, Mint, Kali

$ sudo apt-get install ntpdate

Get Time From Ntp Server

We can use ntpdate client to get the current time from the NTP server. We will specify the NTP server as a parameter to the ntpdate command.

$ sudo ntpdate ntp.ubuntu.com 
16 Nov 07:06:12 ntpdate[4551]: adjust time server 91.189.89.199 offset -0.000030 sec

We need root privileges to use ntpdate to sync clock

Check Current Time From Local Server

We can list current time in the server by using date command.

$ date 
Wed Nov 16 07:07:42 +03 2016

Enable Firewall For Ntp Related Ports

We may need to open ports about NTP like below by using iptables

$ iptables -A output -p udp -s 192.168.1.1 --sport 1024:65535 -d 0/0  --dport 123 -m state --state NEW,ESTABLISHED -j ACCEPT
$ iptables -A input -p udp -s 0/0 --sport 123 -d 192.168.1.1  --dport 1024:65535 -m state --state ESTABLISHED -j ACCEPT

Get Current Time Info

ntpdate is an old tool and deprecated we can use datetimectl according to ntpdate

$ timedatectl status
Get Current Time Info
Get Current Time Info

We see that our time is synchronized with ntp with “NTP synchronized” line.

LEARN MORE  What Is Nettime and How To Use Nettime?

How To Synchronize System Time In Linux? Infographic

How To Synchronize System Time In Linux? Infographic
How To Synchronize System Time In Linux? Infographic

 

Leave a Comment