Setting up NTP on Ubuntu 16.04 is a really quick post, as most Linux (or M$ Windows) installations will use/need an NTP server to keep the time in sync with other machines on the LAN. Services such as DNS, DHC, LDAP, email, Mythtv, syncing data between machines to name just a few.
Update and Install
As always, start with an up to date install. Installing the software is just two packages the server, ntp, and some utilities, ntpdate.
sudo apt-get update sudo apt-get dist-upgrade sudo apt-get install ntp ntpdate
If you want to set up a machine dedicated to service time to your LAN take a look at A minimal Ubuntu server running 16.04.. You will need to use real hardware for the production server as Virtual machines tend to sync their time from the underlying host.
Which NTP pool to use
If you only have the one PC then you should to do nothing more you are all set up. Take a look at the testing section below. This default setup will sync your time to the Ubuntu pool. I told you setting up NTP on Ubuntu 16.04 was quick. 🙂
If you have more than one machine and want your time to sync more accurately, by a few milliseconds 🙂 you will want to use a pool of time servers closer to where you are. I live in the UK so I will be looking at the settings in https://www.ntppool.org/en/use.html there you will find the names to use for the UK. There are lists of pool servers from all over the world just find where you are in the world and take a look.
Setup the server pool
Edit the ntp configuration file.
sudo nano /etc/ntp.conf
Look for the lines similar to these and either remove or comment them out. There is also a fallback line just under these server lines for a pool that Ubuntu provide. That can be removed too.
server 0.ubuntu.pool.ntp.org server 1.ubuntu.pool.ntp.org server 2.ubuntu.pool.ntp.org server 3.ubuntu.pool.ntp.org # Use Ubuntu's ntp server as a fallback. server ntp.ubuntu.com
Replace the server lines above with the pool servers of your choice, I will be using the UK pool. You need 3 servers to form a quorum for ntp, so always configure at least 4 servers. Take a look at the support pages regarding Upstream time quality.
server 0.uk.pool.ntp.org server 1.uk.pool.ntp.org server 2.uk.pool.ntp.org server 3.uk.pool.ntp.org
It is also worth adding the following lines in case your internet connection goes down. These lines can go right at the end of the configuration file.
server 127.127.1.0 fudge 127.127.1.0 stratum 10
Those lines let your machine use its own clock. It is also set to a low reliability (stratum) level. The NTP service will only fall over to it when no other time servers can be found. When the internet is not contactable.
Having changed the config file we need to restart the NTP service.
sudo systemctl restart ntp.service
Take a look at the testing section below. To see your time server pickup and sync itself to the time everyone else is using 🙂
That is all that is involved in setting up NTP on Ubuntu 16.04, unless you want to sync all of your LAN machines to one server so they are all in time with each other.
Seting up local NTP server
If you have many machines on your LAN and use services that need accurate time such as email, DNS, DHCP, LDAP or sync data between machines. We can setup one machine as above to act as a time server for your LAN. This will cut down on the network traffic on your internet connection. (A few hundred bytes) 🙂
So, If you have a machine that is running all the time or for long periods, you can use this setup for your own local time server. Use the setup above with one change. Which will mean your time server can broadcast time to your LAN. Your LAN IP addresses will, most liekly, be setup to use 192.168.1.0/24 or a range of IP addresses from 192.168.1.0 – 192.168.1.255. If which case the broadcast address with be 192.168.1.255. The last IP address in your range. This gives a setting as shown below.
broadcast 192.168.1.255
Having changed the config file we need to restart the NTP service.
sudo systemctl restart ntp.service
Take a look at the testing section below, when one of the server you are getting time from has a leading ‘*’ then you are beginning to sync your time correctly. Now you can begin to add some local machines so they get their time from your own time server.
Local clients
For local NTP clients you use the default setup without any changes.that is do not remove the Ubuntu NTP pool machines as before.
sudo nano /etc/ntp.conf
Leave these lines in place in case your own time server goes down or you turn it off fo some reason. The default settings already allow any other machine to grab your time and sync to it.
server 0.ubuntu.pool.ntp.org server 1.ubuntu.pool.ntp.org server 2.ubuntu.pool.ntp.org server 3.ubuntu.pool.ntp.org # Use Ubuntu's ntp server as a fallback. server ntp.ubuntu.com
Now we can add our new NTP server. The line which will tell NTP to look at to our own NTP server in preference to others. Lets say you called it ntp01.example.com. At the end of the config file add the following line.
server ntp01.example.com prefer iburst
This line tells the ntp server to prefer the server at ntp01.example.com, change the hostname to point at your main NTP server. The iburst sends requests quicker when the client machine first starts up so it can get established quicker.
Having changed the config file we need to restart the NTP service.
sudo systemctl restart ntp.service
Testing NTP
After the server has been running for 10-20 minutes it will get synced up and will be keeping your NTP server in time with the rest of the world. Run the command below, note it uses watch so it will rerun the command on a reqular basis. The default is every 2 seconds. You can change the frequency of that up by using the -n NN switch. Were NN is the number of seconds or even a fraction of a second 0.1.
watch ntpq -cpe -cas
You should see some output similar to this
remote refid st t when poll reach delay offset jitter ============================================================================== -84.52.184.247 193.2.1.117 2 u 27 64 377 61.189 -10.651 2.794 +de-ntp01.10g.ch 212.82.32.15 2 u 30 64 373 24.568 1.526 4.754 -mirror.muntinte 193.190.230.65 2 u 28 64 377 17.568 9.128 5.514 +smtp2.xipalia.c 131.188.3.221 2 u 21 64 267 26.973 -1.563 3.192 *golem.canonical 140.203.204.77 2 u 5 64 373 11.054 -0.279 6.532 ind assid status conf reach auth condition last_event cnt =========================================================== 1 45093 931a yes yes none outlyer sys_peer 1 2 45094 941a yes yes none candidate sys_peer 1 3 45095 9324 yes yes none outlyer reachable 2 4 45096 941a yes yes none candidate sys_peer 1 5 45097 961a yes yes none sys.peer sys_peer 1
In the output, in the bottom section, from as, associations look for one of the servers listed having a condition of sys.peer. These are the ones you are using as your quorum to sync your time to.
Looking at the top, output from pe , short for peers. Look for the the server that have an * (star/asterisk) by it this is the one you are actually syncing to.
The columns in the second output, from associations, show the following data.
- st stratus, The lower the number the closer you are to the real GPS time source. anything 3 and below is good.
- When This is the expected number of seconds before the next poll, or time check.
- reach This is a scrolling number in base 8. It show the status of the connections to the sever. When it reads 377 that is the best.
- delay is the time taken to get a reading from the server.
- Offset is the difference in time your server is from the servers you are syncing to.
- Jitter the amount of wobble on the time server.
For more information see https://www.ntp.org/.
Stopping and starting the ntp server.
To start, stop, restart the ntp server use the usual commands
sudo systemctl start ntp.service sudo systemctl stop ntp.service sudo systemctl restart ntp.service
There is not that much to setting up NTP on Ubuntu 16.04 as you have found out. Job well done.