Article directory
step
1. Install NTP
First, update your package list in the terminal, then install NTP. Enter the following command:
sudo apt update
sudo apt install ntp
2. Configure NTP
Edit the NTP configuration file. Here we use the nano text editor, but you can also use any other editor you like.
sudo nano /etc/ntp.conf
In this file, find the lines starting with "pool", they define the servers that NTP will synchronize with. You can keep the default values, or add/change them as needed. For example:
pool 0.ubuntu.pool.ntp.org iburst
pool 1.ubuntu.pool.ntp.org iburst
pool 2.ubuntu.pool.ntp.org iburst
pool 3.ubuntu.pool.ntp.org iburst
iburst
option indicates that the client will send a stream of packets if the server does not respond.
3. Restart the NTP service
Save and close the configuration file. Then restart the NTP service for the changes to take effect.
sudo systemctl restart ntp
4. Check NTP service status
You can use the following command to check the status of the NTP service.
sudo systemctl status ntp
If everything is OK, you will see output showing that the service is running.
5. Verify NTP synchronization
ntpq -p
Check whether the local ntp service is normal
You can check whether the server is synchronizing with other NTP servers by running the following command.
ntpq -p
The output of the above ntpq -p
command shows that the NTP server is synchronizing with multiple remote servers. In the 'remote' column, the characters at the beginning of each row have special meaning:
- '*' indicates the synchronization source currently in use.
- '+' indicates a candidate synchronization source.
- '-' indicates a discarded sync source.
- 'Space' indicates an alternative sync source.
As you can see from this list, the NTP server is working properly and a synchronization source (*dns2.synet.edu.) has been selected. Additionally, there are several candidate sources (+36.110.235.196 and +36.110.233.85).
The server cannot connect to the external network, how to configure it?
If you want the server to serve only within the intranet, you need to check /etc/ntp.conf
the configuration file to ensure that only the internal time source is listed as server or pool, and comment out or delete the configuration of other external servers.
/etc/ntp.conf
For example, the following lines in the file need to be commented out or deleted:
0.ubuntu.pool.ntp.org
1.ubuntu.pool.ntp.org
2.ubuntu.pool.ntp.org
3.ubuntu.pool.ntp.org
And add an internal time source, or use the local hardware clock if not available:
server 127.127.1.0
fudge 127.127.1.0 stratum 10
Then restart the NTP service:
sudo systemctl restart ntp
Run it again ntpq -p
and you should only see the internal server or local clock.
ntpdate -q xxx
Query ntp server time
192.168.1.140
I executed it on another of my hosts ntpdate -q 192.168.1.134
(134 is the host where my NTP server is located):
Explanation: The parameter of
the above command represents query, that is, the query does not actually set the system time. The time of the NTP server with IP address 192.168.1.134 is being queried here.ntpdate -q 192.168.1.134
-q
The meaning of each part in the output result is as follows:
-
server 192.168.1.134, stratum 2, offset 12.812453, delay 0.02638
: This line displays NTP server information. stratum refers to the server's position in the NTP hierarchy. The smaller the number, the closer it is to the reference source. Its value is 2. offset refers to the time difference between your system time and the NTP server, in seconds, here it is 12.812453 seconds. delay is the round trip delay time from your system to the NTP server, here it is 0.02638 seconds. -
25 Sep 16:06:12 ntpdate[21820]: step time server 192.168.1.134 offset 12.812453 sec
: This line is the log record of the actual update operation performed. However, since-q
the parameters are only queried and not updated, this operation does not really change the system time. If there is no-q
parameter, the system will try to adjust the system time based on this offset.
This command and the results show that the system time of your machine is about 12.812453 seconds different from that of the specified NTP server.
(Anyway, you can get the time by executing this command, which means there is no problem with our deployment)