How to set up NTP Clock Synchronization on FreeBSD with ntpd

FreeBSD uses the Network Time Protocol (NTP) to synchronize computer systems’ clocks over packet-switched, variable-latency data networks. NTP uses UDP port 123. If you have one computer or a single server, you can easily synchronize time with other NTP servers. All you need is an ntp client called ntpdate. It is used to set the date and time via NTP servers. Let us see how set up NTP Clock Synchronization on FreeBSD with ntpd and ntpdate.

How to set up NTP Clock Synchronization on FreeBSD with ntpd

Advertisement

How to set up NTP Clock Synchronization on FreeBSD with ntpd and ntpdate

You need the following two components on your FreeBSD server or desktop:

  1. ntpdate – Set the date and time via NTP. Beneficial for the desktop system. However, ntpdate is to be retired from FreeBSD soon. Hence, avoid using it.
  2. ntpd – The ntpd utility is an operating system daemon which sets and maintains the system time of day in synchronism with Internet standard time servers. It acts as both server and client. This is now recommended method for all users.

Installing ntpd on FreeBSD

By default, ntpdate and ntpd ship with the latest version of FreeBSD. Hence, you no longer need to install ntpd or ntpdate on FreeBSD.

Pick the appropriate NTP server for your needs on FreeBSD

Visit public ntp timeserver list to pick up your NTP server.

Configuring NTP Clock Synchronization on FreeBSD

Edit the /etc/ntp.conf as follows:
# vim /etc/ntp.conf
Make sure you set pool statement. For example:

pool 0.freebsd.pool.ntp.org iburst

You can set up your country’s public NTP server when you want to sync. Then replace CC with the country’s abbreviation. For example, for the USA (us code):

pool 0.us.pool.ntp.org iburst

For Canada (ca code), try:

pool 0.ca.pool.ntp.org iburst

Are you located in Japan (jp code)? Then:

pool 0.jp.pool.ntp.org iburst

Here is how India (in code) specific host name looks using the dig command or host command:
$ host 0.in.pool.ntp.org
$ dig 0.in.pool.ntp.org

In simple words, just replace the us with your country specific code. Save and close the file by pressing the Esc followed by :wq.

Enabling ntpd service on FreeBSD 12/13/14

Edit the /etc/rc.conf and append the following line:

ntpd_enable="YES"

How to start/stop/restart ntpd service on FreeBSD?

Use the service command as follows:
# Start the ntpd on FreeBSD #
$ sudo service ntpd start
# Stop the ntpd on FreeBSD #
$ sudo service ntpd stop
# Restart the ntpd on FreeBSD #
$ sudo service ntpd restart
# Finding the ntpd service status on FreeBSD #
$ sudo service ntpd status

Outputs from the last command:

ntpd is running as pid 1570.

Open the UDP port 123 at firewall

If you are running FreeBSD ipfilter firewall, you need to open the UDP port 123. Just add following rule to your firewall script:

pass out quick on lnc0 proto udp from YOUR-SERVER to any port = 123
keep state

OR

pass out quick on lnc0 proto udp from YOUR-SERVER to
TIME-SERVER-IP port = 123 keep state

For example, my FreeBSD workstation IP is 192.168.1.16 and 61.246.176.131 is IP of NTP server then my rule is in ipf.conf file as follows:

pass out quick on lnc0 proto udp from 192.168.1.16
to 61.246.176.131 port = 123 keep state

FreeBSD test NTPD clock synchronization

First, set wrong date (Mon Dec 13 4:27 pm) on FreeBSD. For example, type the date command as follows:
# date 0412131627
Now set the correct date with ntp client:
# ntpdate -v -b in.pool.ntp.org

13 Dec 16:27:50 ntpdate[997]: ntpdate 4.2.0-a Thu Nov 3 07:34:22 UTC 2005 (1)
25 Jan 12:35:47 ntpdate[997]: step time server 61.246.176.131 offset 35237275.965726 sec

Or start or restart the ntpd service if not running:
$ sudo service ntpd start
You can verify that correct data is set up for date on your FreeBSD machine via NTPD:
$ date
Output:

Wed Jan 25 12:36:21 IST 2006

How to verify NTP is working Or not (Check Status of NTP)

Type the ntpq command as follows:
# ntpq -pn
Sample outputs:

     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 0.freebsd.pool. .POOL.          16 p    -   64    0    0.000   +0.000   0.000
+178.215.228.24  237.17.204.95    2 u 1073 1024  377  228.491   +1.913   2.695
-192.46.210.39   237.17.204.95    2 u  133 1024  377  135.911   -0.083   3.222
*64.227.167.110  210.23.25.77     2 u  651 1024  377  116.969   +2.343   0.940
-162.159.200.123 10.23.8.4        3 u  746 1024  377  110.824   -1.675   3.016
+157.245.102.2   129.134.25.123   2 u  500 1024  377  117.682   +5.679   2.486

Enable date and time/ clock synchronization at boot time on FreeBSD

WARNING! The following config is no longer needed as ntpdate will retire soon on the latest version of FreeBSD. However, I kept it here for historical reasons and if anyone is still using the older version of FreeBSD.

You need to set ntpdate via /etc/rc.local file.
# vi /etc/rc.conf
Append following line to it:

ntpdate_enable="YES"
ntpdate_hosts="asia.pool.ntp.org"

Save and close the file. Make sure you have correct ntpdate_hosts server entry.

Summing up

Over time, a FreeBSD system’s clock is prone to drift. The Network Time Protocol (NTP) is one way to ensure your clock stays accurate. Hence, this is important for both desktop and server systems to avoid issues. See the following manual pages using the man command or help command:
$ man ntpd
$ man ntpdate

See also:

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

8 comments… add one
  • fink Oct 26, 2006 @ 18:32

    how can i tell what date the tutorial was written, or what version of of software was used?

  • 🛡️ Vivek Gite (Author and Admin) nixCraft Oct 26, 2006 @ 18:46

    This is one is written on FreeBSD 6.0 but it should work on any version. This entry was written on Jan-25-2006.

    Hope this helps.

  • batso Dec 19, 2007 @ 11:43

    i have problem. i can’t configuration & check NTP server.
    Help Me. My config:
    install /usr/ports/net/ntp
    make install
    install finish
    my server ip address:192.168.0.1/24
    1./etc/rc.conf
    ntpdate_enable=”YES”
    ntpdate_program=”/usr/local/bin/ntpdate”
    ntpdate_flags=”-b”
    ntpd_enable=”YES”
    ntpd_program=”/usr/local/bin/ntpd”
    2./etc/ntp.conf
    server 0.asia.pool.ntp.org
    server 1.asia.pool.ntp.org
    server 2.asia.pool.ntp.org
    server 3.asia.pool.ntp.org
    server 192.168.0.1
    broadcast 192.168.0.255
    driftfile /var/db/ntp.drift
    logfile /var/log/ntp.log

  • JB Jun 18, 2008 @ 3:22

    Are you sure this is correct?

    # vi /etc/rc.local

    and that it’s not rc.conf instead?

  • 🛡️ Vivek Gite (Author and Admin) nixCraft Jun 18, 2008 @ 9:49

    Are you sure this is correct?
    No, it was a typo and the post has been updated to fix this issue. Thanks for the heads up.

  • SIFE Oct 4, 2009 @ 13:45

    Salamo Alikom
    is there any way to prevent gnome changing sys clock ?

  • Fred Finster WB7ODY Thanks Vivek Gite Jul 23, 2023 @ 3:34

    missing restart and stop, simple typo

    # Start the ntpd on FreeBSD #
    sudo service ntpd start
    # Stop the ntpd on FreeBSD #
    sudo service ntpd start
    # Restart the ntpd on FreeBSD #
    sudo service ntpd start

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.