SARPi Project - Slacking on a Raspberry Pi


Setup a NTP server on Slackware Linux on a Raspberry Pi

This SARPi Project will enable you to setup and configure a Network Time Protocol (NTP) server running on Slackware Linux on a Raspberry Pi. This tutorial will take you through; configuring and setting up a NTP server, getting the internal network IP address of your Raspberry Pi, running the NTP daemon (starting and stopping it), querying the NTP server for a time reference from another computer connected to your network - which includes automating the process by writing your own Bash script (a.k.a. 'cron job') on a Linux system.

If you have a spare Raspberry Pi laying around, or not doing much, then this project would be an ideal purpose for it. You can leave it running on your network, connected to the Internet, and can be confident that when you use it to synchonise date/time on your other networked systems/devices it will be pretty much spot-on accurate. Even if your Internet connection goes down, NTP will continue to run and synchronise itself once your Internet connection is re-established.

Quick Links

What you will need for this project
What is NTP?
Configuring the NTP server
Getting the network IP address of the Raspberry Pi
Starting the NTP daemon
Querying the NTP server to synchronise time on other systems
Synchronise time on a Linux system
Automate synchronised time on a Slackware Linux system
Synchronise time on a Windows system
Concluding Remarks

What you will need for this project

• A Raspberry Pi running Slackware Linux.
• Your Raspberry Pi connected to the Internet.
• The internal network IP address of your Raspberry Pi (e.g. 192.168.1.50).

SARPi Project muse views ... For the purpose of this tutorial we are running Slackware AArch64 on a Raspberry Pi 4 [2GB] with the hostname 'kron'. We will be using this system for our NTP server. It's not important which version of Slackware Linux is used in order to be successful.

What is NTP?

Network Time Protocol (NTP) is an official Internet standard protocol that's used to synchronize the clocks of computer systems over a network to a common timebase. The term 'NTP' applies to both the protocol and any client/server programs that run on computers, laptops, tablets, mobile phones, etc.

NTP is implemented via the User Datagram Protocol (UDP) using port 123, and can operate in broadcast and multicast modes, or by direct queries. NTP can synchronise to multiple time sources, making allowances for the network delays and apparent reliability of the clock(s) it synchronises to.

SARPi Project muse views ... Accurate time between computers across a network can be essential, for a multitude of reasons, and NTP effectuates that. In some cases a few seconds difference between two or more computers on a network can have adverse consequences. For example, air-traffic control, where split-second accurate timing is crucial. Or at sporting events where one 10th, 100th, or even 1000th, of a second matters a lot. Incidentally, we aren't going to be using our Raspberry Pi for anything so pivotal, and probably neither are you.

NB: You do not require a real time clock (RTC) installed to run a NTP server. 'NTP' uses other (remote) NTP servers to synchronise with your system, and maintain accurate time, over the Internet. No additional hardware is required.

Configuring the NTP server

So, log in as 'root' user and check the date. It's always a good habit to check the current system time on a Raspberry Pi.

login

The first thing you need to do is make a backup copy of the '/etc/ntp.conf' file. Just in case things go horribly wrong, you can easily copy this file back to its original filename. We've named the backup 'ntp.conf.bak' here. You can do the same or choose your own filename as a backup.

root@kron:~# cp -a /etc/ntp.conf /etc/ntp.conf.bak

Next you're going to make the NTP daemon (ntpd) executable. This is so you can run the NTP server, stop and start it, and also, so it will run automatically when (re)booting the Raspberry Pi (i.e. so you don't have to start it manually each time). Do this with the following command:

root@kron:~# chmod +x /etc/rc.d/rc.ntpd

You can use 'ls -lah' to list the file and verify it is now executable. See the screenshot below.

login

Getting the network IP address of the Raspberry Pi

So, you're going to need the internal network IP address of your Raspberry Pi. This is not your Internet IP address. This is the internal IP address that your network uses for your Raspberry Pi computer - the one that you intend to use as the NTP server. To find out what it is (if you don't already know) use this command:

root@kron:~# hostname -I

You can also use:

root@kron:~# ifconfig | grep -m 1 "inet"

You can also use:

root@kron:~# ip route get 1.2.3.4 | awk '{print $7}'

The results will be something similar to the screenshot below.

ip route

In the example above, '192.168.1.242' is the internal network IP address of our Raspberry Pi. Yours will be different. Make a note of your Raspberry Pi's network IP address because it's significant and you will need it later in this tutorial.

Now you're going to edit the '/etc/ntp.conf' file using a text editor for this. We use 'nano'. The 'ntp.conf' file contains the configuration settings for NTP.

root@kron:~# nano -w /etc/ntp.conf

What you're looking for is a section titled "# NTP server (list one or more) to synchronize with:". This is highlighted in the screenshot below.

nano

What you need to do is remove the hash '#' from the start of the server address line for at least two NTP servers. Just one NTP server is mandatory. But the more NTP servers you have listed, the better. So, we have done it for all of them in the example above. You can also specify your own NTP server addresses here. The NTP servers listed here are what you will be synchronising your own system with.

SARPi Project muse views ... After each NTP server address you see 'iburst', which is a mode, and it means; when the server is unreachable, and at each poll interval, send a burst of eight packets instead of the usual one. As long as the server is unreachable, the meantime between packets is approx. 16 seconds. Once the server is reachable, the meantime between packets is approx. 2 seconds. After the first minute, iburst mode effectively synchronizes the clock so that queries will be sent at intervals of 64 seconds, or more, thereafter.

The iburst mode is entirely optional and you don't have to include it. Sometimes you need to omit it because it's considered "aggressive" by some public NTP server hosts. When a NTP server is unresponsive, iburst mode will continue to send queries until the server responds and time synchronization resumes. It's a good idea to check that iburst mode is ok to use with the remote NTP server hosts before using it.

Next you need to add a 'broadcast' line to the 'ntp.conf' file. This will tell the system to allow other computers/devices to query a time reference from the NTP server running on your Raspberry Pi. We have chosen to put this line underneath '#broadcastdelay' but you can put it just about anywhere within the file. 'broadcast' should be followed by the internal network IP address of your Raspberry Pi. Our own IP address is '192.168.1.242' which we made a note of earlier for this part of the tutorial.

So, the line that needs to be added is this: broadcast <YOUR RASPBERRY PI IP ADDRESS>

Example: broadcast 192.168.1.242

As seen in the following screenshot:

nano

Now scroll down until you see a section titled "# Don't serve time or stats to anyone else by default (more secure)". The two lines below this, beginning with 'restrict' (see highlighted area in screenshot below), need to be edited.

nano

You need to remove the word "limited" from both of these lines beginning with 'restrict' so they end up looking the same as what is shown below.

nano

What the 'restrict' lines mean:
• 'restrict default' is for IPv4
• 'restrict -6 default' is for IPv6
• 'Kod' – Kiss-of-death packet will be sent when access is denied to unwanted host/subnet queries.
• 'nomodify' prevents any changes to the ntpd configuration.
• 'notrap' prevents ntpdc control message protocol traps.
• 'nopeer' prevents a peer association being formed.
• 'noquery' prevents ntpq and ntpdc queries from being answered, but not time queries.

To only allow other systems on your internal network to synchronize with your NTP server (e.g. on a closed network), you need to replace the 'restrict' lines with the following line, and adjust the IP (192.168.1.0) and subnet mask (255.255.255.0) to suit your own class A/B/C network:

restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

Once that's been done, you have finished editing. If you're using 'nano', press + keys together and then press the key when asked to save the '/etc/ntp.conf' file and then press the key to confirm and quit.

Starting the NTP daemon

Now you need to start the NTP daemon. A daemon is just another name for a program which runs in the background. To start it type the following command:

root@kron:~# /etc/rc.d/rc.ntpd start

Next type this command to show a list (and status) of your chosen NTP servers:

root@kron:~# ntpq -p

That should produce something similar to the following:

ntpq

You can see that the 'remote' IP address (192.168.1.242) of our Raspberry Pi is shown with a '.BCST.' refid and server type 'B' which means it's 'broadcasting', on that IP address. We also have a '*' next to one of the remote servers which tells us that time synchronisation is currently happening with that specific NTP server. If you can see the something similar from your own results then you have done everything correctly and are now running a NTP server on Slackware Linux that's synchronising with other NTP servers across the Internet.

What the 'ntpq -p' column headings mean:
• 'remote' is the address of the NTP server.
• 'refid' is the reference ID (or address) of the server that the 'remote' server synchronises with.
• 'st' is the stratum of the server.
• 't' is the type of server (local, unicast, multicast, or broadcast).
• 'poll' is how long between queries to the server (in seconds).
• 'when' is how long since the last poll (in seconds).
• 'reach' is the octal bitmask of success/failure of the last 8 queries (377 = all ok).
• 'delay' is the network round trip communication time to the server (in milliseconds).
• 'offset' is the difference between the local clock and remote clock (in milliseconds).
• 'jitter' is the mean difference of deviation in successive time values from the server (in milliseconds).

If things aren't going to plan and the results you're getting aren't similar to what you see here then it's possible you've missed something or made an error. Check back over what you've done and verify that it's all correct. Remember, you made a backup of '/etc/ntp.conf'. So, if all else fails you can copy this backup file back to its orignal filename and start over.

At some point, for whatever reason, you may want to stop the NTP daemon from running. To do this use the following command:

root@kron:~# /etc/rc.d/rc.ntpd stop

To start the NTP daemon again use the same command you used initially to start it.

root@kron:~# /etc/rc.d/rc.ntpd start

SARPi Project muse views ... One thing to note: Now that your NTP server is running, the 'ntpdate' and 'sntp' commands will no longer work while the NTP socket (port 123) is in use. Running a NTP server replaces the need for them anyway.

If/when you edit the '/etc/ntp.conf' file, no new changes will be implemented until you restart the NTP daemon. Instead of stopping and starting it with two commands, you can do it using just one command: /etc/rc.d/rc.ntpd restart

Querying the NTP server to synchronise time on other systems

You can now go to a computer connected to the same network, that the NTP server is running on, and use the internal network IP address of your Raspberry Pi as a time server address. The method by which you synchronise time on your computer/device may be different between platforms and operating systems. Just keep in mind that instead of a NTP server address (e.g. '0.pool.ntp.org') you will be using the IP address of your Raspberry Pi (e.g. '192.168.1.242' in our case).

Let's go through a couple of straight-forward examples to show how this process works. First in Linux, and then in Windows.

Synchronise time on a Linux system

To synchronise time, using our NTP server, on another Linux system that's connected to our network, we changed computers (to a Slackware Linux system with the hostname 'iana') and typed this at the command prompt:

root@iana:~# ntpdate 192.168.1.242

Which resulted in the screenshot below.

ntpdate

SARPi Project muse views ... You can also use the following commands, as 'root' user, to synchronise the system clock on Slackware Linux:

sntp -Ss 192.168.1.242
ntpd -p 192.168.1.242

Obviously, use your own Internet NTP server address or local network IP address with these commands.

It doesn't matter which distribution or version of Linux you are running. This procedure is pretty much the same across the board, although on some Linux distributions 'ntpdate' may be a package you need to install. Just remember to use your own IP address and not the example one we've shown here.

Automate synchronised time on a Slackware Linux system

'ntpdate' is a one-time command and only updates the system date/time whenever you run it. It doesn't update automatically on its own. However, there is a system process called 'crontab' which automatically performs tasks (i.e. runs a script or command) based on, and according to, a set schedule. Each of these tasks is known as a 'cron job'. There's another utility called 'run-parts' which runs scripts (i.e. 'cron jobs') found in cron.daily, cron.hourly, cron.monthly, and cron.weekly directories at a specified time. So, for example, if you leave your Linux system running 24/7, it might be beneficial to create a Bash script to run 'ntpdate' and put that in /etc/cron.daily directory so it will run automatically, once every day. Or put the script in the /etc/cron.hourly, or /etc/cron.weekly, directory. Then 'run-parts' can be called from root's 'crontab' to run any script(s) found in these directories.

SARPi Project muse views ... Bash is a command language interpreter or shell on Linux and is one of the easiest types of scripting to learn. The name 'Bash' is an acronym for the 'Bourne-Again SHell'. If you'd like to know more about Bash, and Bash scripting, take a look at the 'How To' cli manual, and Bash Guide For Beginners, and also Starting off with a Sha-bang, guides.

To create a simple Bash script which automatically runs 'ntpdate' once every day, (as 'root' user) first create the file in the cron.daily directory using 'nano'. Like this:

root@stal:~# nano -w /etc/cron.daily/ntp-update.sh

We've named our script 'ntp-update.sh' but you are free to choose any filename that suits you.

So, in any Bash script the first line always starts with the 'Sha-bang' which is basically this: #!/bin/bash

SARPi Project muse views ... The 'Sha-bang' tells your system that this file is a set of commands to be fed to the command interpreter (i.e. Bash).

The '#!' is actually a two-byte magic number, a special marker that designates a file type, or in this case an executable shell script. After the Sha-bang comes the script code which has an infinite number of permutations.

In our case, we're going to invoke 'ntpdate' within the Bash script and tell it to update from the Raspberry Pi (NTP server) IP address. We can achieve this using only two lines of code. Like this:

#!/bin/bash

/usr/sbin/ntpdate 192.168.1.242 &> /dev/null

You should put these two lines into your Bash script, substituting our example IP address (192.168.1.242) for your own.

"So, what's going on here exactly?" you might wonder.

• '#!/bin/bash' = tells the system that this is an executable Bash script.
• '/usr/sbin/ntpdate' = tells the system to run 'ntpdate', using an absolute path to the command.
• '192.168.1.242' = tells 'ntpdate' to query the network IP address of our Raspberry Pi for a time reference.
• '&> /dev/null' = tells the system to suppress any output from our 'cron job'.

SARPi Project muse views ... Using an absolute path (a.k.a. full path) is always good policy. Relative paths work too but it's much better to use full paths when possible. To find out where a command is located type 'whereis <command>' at the command prompt.
Example: whereis ntpdate

'&> /dev/null' is, basically, an IO redirection (stderr to stdout) trick to execute a program quietly, forcibly hiding any output from a 'cron job'. What it effectively does here is to take any output (regular and error), from the results of running the 'ntpdate' command, and send it to '/dev/null'. It's standard practice to do this in a Bash script, unless you are debugging or want notifying every time the script is executed by the 'crontab' process.

NB: '&> /dev/null' redirection is more commonly written as '> /dev/null 2>&1' in Bash.

You can also add a comment to remind you what this script, and/or command, does. Comments always begin with a '#' (hash) and are usually followed by a space. Comments are not executed by the system when the script is run. It's not required to add comments but is another 'good practice habit'. We will add a comment as an example. So, what you should see in your Bash script is something very similar to the screenshot below.

ntp update

So, now press + keys together and then press the key when asked to save the file and then press the key to confirm and quit.

The next thing you need to do is make this script executable. We do this in exactly the same that we made the 'rc.ntpd' (NTP daemon) executable, earlier in this tutorial.

root@stal:~# chmod +x /etc/cron.daily/ntp-update.sh

You should 'ls -lah' (list) the script file to make sure it has been made executable.

chmod

To test that the Bash script is working as expected, you can run it at any time by typing:

root@stal:~# /etc/cron.daily/ntp-update.sh

And that's basically it! Now this Bash script should run once every day. If you had placed this script in the /etc/cron.hourly directory then the system would run it once per hour. Likewise, if you had placed this script in the /etc/cron.weekly directory then the system would run it once per week. Scripts which are put in these 'run-parts' directories will be executed according to 'crontab' settings. To view the 'run-parts' settings in 'crontab' type the following command:

root@stal:~# crontab -l

It's also possible to edit these settings to suit your own requirements. You can achieve this by typing:

root@stal:~# crontab -e

SARPi Project muse views ... If you are not familiar with editing/modifying 'crontab' settings then some required reading should be done before attempting to make any changes. A very easy to read and informative "Intro to cron" guide is a great place to start. Wikipedia's "Cron" page is another good resource.

Synchronise time on a Windows system

On a Windows system, in the 'Internet Time Settings' tab (Control Panel - Date and Time) there's a 'Server:' text field where you can enter an Internet time server address. You need to type the internal network IP address of your Raspberry Pi in that text field and click the 'Update Now' button.

We did this, on a Windows 11 system connected to our network, and the results can be seen in the screenshots below.

windows windows

SARPi Project muse views ... On a Windows system there is no need to automate the time synchronisation process because the software has been designed to run this process automatically by default, whether you want it that way or not.

Concluding Remarks

As you can see, both our Linux and Windows computers, running totally different operating systems, have had their system date/time set accurately to each other by querying our Slackware Linux NTP server on the Raspberry Pi, over the network, and using it as a standard for time reference. This same procedure also works on other operating systems such as Mac OS and Android. We're not going to cover all of them in this tutorial. That's what Google is for.

Now that you have your own NTP server running, there are even more possibilities to what can be achieved. For example, everybody who's connected to your internal network can make use of the NTP server (wife, husband, kids, work colleagues). Or you can have your Internet router(s) query your NTP server for a time reference. Or you can open up port 123 on your router/firewall, forward it to your Raspberry Pi's internal network IP address, and then use your Internet IP address as a time server address from a remote location. Additionally, if you have your own domain name (e.g. www.your-domain.com), and manage your own DNS records, you can create an A record (e.g. time.your-domain.com) and point it to your Internet IP address, and then the entire Internet could query your NTP server for a time reference, using the full domain name instead of your IP address (because it's never a good idea to give out your Internet IP address to all and sundry). You can tell your greater family, friends, and neighbours about it and allow them to use it. If you have a static IP address and a reasonable Internet connection you might even consider donating your NTP server to an established NTP server pool.

For as long as you keep your Raspberry Pi powered on, with the NTP server running while connected to the Internet, it will keep itself in perfect time, constantly. Even if/when your Internet connection goes down the system will compensate and still keep pretty much accurate time, and certainly for the period that your Internet is down - which hopefully isn't very long!

Of course, 'accurate time' is only as accurate as the NTP server(s) time that you're synchronising with. If they are inaccurate yours will be as well. It may be prudent to check out which NTP servers are available and best suited for you to use. In general, ISPs run their own NTP servers and that's usually a reliable service to use in the beginning. Later you may find other NTP servers which are more suitable and they can be added and/or removed as required, in the '/etc/ntp.conf' file, at any time. Just remember to restart the ntpd daemon after doing so.

root@kron:~# /etc/rc.d/rc.ntpd restart

Any changes you've made to the configuration and settings within the '/etc/ntp.conf' file will now be implemented.

Thanks!

Thank you for reading and taking part in this SARPi mini-project. We hope you enjoyed it and found it interesting and educational.

Thanks also to Patrick Volkerding, and the entire Slackware Team, for producing a truly wonderful OS. Without you, and the work you do, the SARPi Project would not exist.

If you have any questions or need help, visit the Slackware Linux Forum on Linux Questions. Or get in touch on the #SARPi IRC channel on irc.libera.chat.

Back to Top


Updated: 2024-03-07 14:40:09 UTC

Disclaimer: The SARPi Project website is for non-commercial and general information purposes only. The content is provided by Penthux.NET and while we endeavour to keep information up to date and correct, we make no representations or warranties of any kind, express or implied, about the completeness, accuracy, reliability, suitability or availability with respect to the website or any information, software, products, services, or related graphics which is available on the website for any purpose. Any reliance you place on such information is therefore strictly at your own risk. In no event will Penthux.NET be liable for any loss or damage including without limitation, indirect or consequential loss or damage, or any loss or damage whatsoever arising from loss of data or profits arising out of, or in connection with, the use of this website or any of its contents. Through this website you are able to visit other websites which are not under our control. Penthux.NET has no influence over the nature, content or availability of any external URLs. The inclusion of any URLs does not necessarily imply a recommendation or endorsement of any content therein. Every effort is made to ensure the SARPi Project website remains accessible. However, Penthux.NET takes no responsibility for, and will not be liable for, the SARPi Project website being temporarily unavailable due to technical issues beyond our control. SARPi Project is in no way affiliated with Slackware Linux, Inc, or the Linux Foundation, or Raspberry Pi Ltd., or any of their respective members, trustees, partners, or associates.


Accept!

SARPi Project uses cookies for website traffic data analytics purposes only. Cookies from this website do not collect or store any of your personal data.

Please read the SARPi Project website [ Cookie Policy ] for more details.