Ubuntu Linux how to set the date and time via Network Time Protocol (NTP)

Q. How do I setup Ubuntu Linux system time using Network Time Protocol (NTP)? Do I need to install ntp server?

ADVERTISEMENTS

A. No you don’t have to install NTP server. All you have to do is install simple client called ntpdate. ntpdate sets the local date and time by polling the Network Time Protocol (NTP) server(s) given as the server arguments to determine the correct time. It must be run as root on the local host.

A number of samples are obtained from each of the servers specified and a subset of the NTP clock filter and selection algorithms are applied to select the best of these. Note that the accuracy and reliability of ntpdate depends on the number of servers, the number of polls each time it is run and the interval between runs.

Please note that ntpdate will decline to set the date if an NTP server daemon (ntpd) is running on the same host. When running ntpdate on a regular basis from cron as an alternative to running a daemon, doing so once every hour or two will result in precise enough timekeeping to avoid stepping the clock.

Install ntpdate client

$ sudo apt-get install ntpdate

ntpdate will automatically run when your network interface get activated by system (i.e. while booting Ubuntu Linux sever/desktop system) . Ubuntu Linux stores script at /etc/network/if-up.d/ntpdate location.

If you wish to just run script again just type command:
$ sudo /etc/network/if-up.d/ntpdate
OR
$ sudo ntpdate pool.ntp.org

Install as cronjob

To avoid stepping the clock you must run ntpdate every 1 or 2 hours using cronjob:
crontab -e
Now add hourly job:
#Setup NTPDATE
@hourly /etc/network/if-up.d/ntpdate

Save and close the file.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
10 comments… add one
  • Ashok.S Mar 5, 2007 @ 11:44

    i want java script for Disk space and disk memory and disk usage

  • Rupert Nov 20, 2007 @ 22:43

    Do I need to restart the cron service after editing the crontab to add ntpdate ?

  • 🐧 nixCraft Nov 21, 2007 @ 5:53

    Noop, if crond is already running it will pickup new changes.

  • James May 9, 2009 @ 14:14

    DO NOT DO THIS! You will put too much load on the ntp servers, BE NICE!

    If you RIGHT CLICK on your desktop clock and select “set date / time” there’s an option to automatically set the time, and select the ntp.ubuntu.com servers so you don’t load the pub servers, be nice and these services will continue.

    WHY? Because using ntp – NOT ntpdate every hour, will figure out what the drift of your clock is and set it on a regular basis and prevent too much load on the ntp server.

    Just set it by the feature provided.

    • Benjamin Jul 8, 2011 @ 21:52

      That would be great – if it worked. My laptop is losing 30 minutes every 3 days. Nothing else I’ve tried has fixed it.

      • David Apr 3, 2012 @ 5:10

        To anyone with the same issue, just replace the internal battery and it’ll stop happening.

  • steppa Jul 1, 2009 @ 17:24

    Using ntpdate daily or every few hours is just fine regarding load on ntp servers. The ntp daemon synchronizes every few seconds!

  • Vole Jan 13, 2010 @ 15:52

    Instead of ntpdate install ntpd.

    https://help.ubuntu.com/9.10/serverguide/C/NTP.html

  • anon Oct 5, 2010 @ 10:13

    > Using ntpdate daily or every few hours is just fine regarding load
    > on ntp servers. The ntp daemon synchronizes every few seconds!

    This is wrong information. ntpdate hourly is bad. ntpd running the whole time is good.

  • Thomas Tvivlaren Jul 18, 2011 @ 9:43

    Benjamin: I had the exact same problem. A Fujitsu Siemens Amilo Li 1705 that simply could not keep the time synced! It lagged about 1 second/minute… I set up a crontab that ran ntpdate every other hour or so. (NO! To any of you naysayers, the ntp daemon will not do it’s job under those circumstances…)

    After having the missus spilling coffee and milk on the keyboard of the laptop, rendering the keyboard useless, I instead started using it as a server. Then, of course, syncing every 2 hours simply would not cut it as the lag buildup could amount to 2 minutes.

    The solution was to use another server, well-behaved and well-synced (a trusty NSLU2 running Debian), as a local ntp server running ntpd. Then the misbehaving former-laptop-now-server could sync to that server instead and do it as often as needed without burdening the already burdened ntp servers around the globe. To have it never go beyond 5 seconds of lag I need to sync it with ntpdate as a cronjob every 5 minutes…

    Vivek: Keep up the good job! Perhaps a follow up on this post could be setting up ntp authentication?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.