Update ntp.conf
Network Time Protocol is a suite of applications that allows computers to coordinate their system time. Here, we describe the simple steps required to install and configure NTP. The process is applicable to most Linux distributions.
If NTP is not present on your host system, it can be easily downloaded and installed. You will need root permissions to install the application. Installation also varies slightly depending on which Linux distribution you use:.
For more obscure Linux distributions, NTP is available as source code for compilation on a host system. NTP is configured using a configuration file — ntp. The file is read by the NTP daemon at start-up. The daemon must be restarted for any modifications to the configuration file to take effect. The file follows a standard format similar to many other Linux configuration files and can be modified using any standard text editor, such as gedit or nano.
NTP relies on reference clock servers, or peers, in order to synchronize the local system clock. One or more peers can be added to the configuration file, as follows:. All things being equal, a preferred peer will be selected by NTP from a list of similarly performing peers. The prefer option should only be specified once. It is also considered best practice to specify an odd number of peers. A time offset that is out of tolerance in any one peer can then be easily detected. The peer can then be discarded by the selection algorithm.
NTP uses a drift file to store the frequency offset of the local clock oscillator. The frequency offset is stored in parts-per-million PPM. The file is updated by the NTP service every hour. It is considered good practice to specify a drift file, since it allows NTP to synchronize the local clock faster on start up. A drift file is specified as follows:. You can use the statsdir command in the configuration file to specify a directory where statistics files will be stored. Daemons are operating system programs that run in the background without needing any intervention.
While computers are very intelligent, they are still just machines which are subject to small fluctuations in the electronic components. This can cause a time drift. A time drift is a small inconsistency in timekeeping.
The NTP daemon will handle clock time syncing automatically by querying an NTP server at regular time intervals to correct those discrepancies. CentOS comes with an alternative implementation of the Network Time Protocol right out of the box, called chrony. This is needed as both daemons use the same protocol and port, so they cannot work together. Keep in mind that if we are not logged in as the root user, we will need to run these commands as a user with sudo privileges.
Installations on CentOS and other RedHat based distributions are typically best handled best using the yum tool. The -y flag is used to auto-confirm on any prompts. Yum automatically handles dependencies for us, so any missing packages that ntpd needs to run will be installed as well. We should see something similar to this at the end of the command output. We can leave the ntpd configuration as is, or we can change which timeservers our ntpd service will query.
Generally we will want to use a timeserver that is close to either our location or the servers' location. The ntp pool project is a good place to begin learning more about this topic. For ntpd to be able to sync time effectively, it needs to be able to communicate with an external network resource in order to synchronize with authoritative NTP servers. What this means is that we must ensure that port is open for outbound UDP traffic. The port only needs to be open for inbound traffic if our server is going to be configured as a timeserver.
There are other firewalls in use so these steps may differ depending on which firewall is installed on our system. To check if port is open we can run the following command. Or, if firewalld is running, we can use this command. If the port is missing, we will need to add the port in the csf. Also, if using firewalld and the port is not open, we can also run this command to open the port.
The last thing we need to do is select a timezone. The operating system's clock needs to be configured to use the desired timezone. We can check the current time, date and timezone using the timedatectl command. Just copy the command and replace the city name and it should give us the name of the timezone. To set the listed timezone, we need to issue this command. Of course, we should use the timezone name we received from the output in the previous command for our server.
The systemctl start ntpd command will start the ntp daemon for this session. The systemctl enable ntpd command tells the operating system to start it every time the computer boots up. We can confirm the service is running using the systemctl status ntpd command.
It will take some time for it to fully sync due to how NTP functions, but no further action is needed. If you have come to this article just for this section and find something that is unclear, check the previous section above as it may be explained in further detail there. Ubuntu also has an alternative implementation of NTP in the form of the systemd-timesyncd module. This can be turned off with the -g option:. If the sanity limit is set to zero, no sanity checking is performed and any offset is acceptable.
This option overrides the limit and allows the time to be set to any value without restriction; however, this can happen only once. After that, ntpd will exit if the limit is exceeded. This option can be used with the -q option. If you are behind a firewall, ntpd will never work, but ntpdate can work with the -u option. For example: ntpdate -u 0. If you can afford the time to wait whatever time it takes before your system gets in sync, you can use the ntp-wait command:.
For Ubuntu and the like use: dpkg-reconfigure tzdata and select the right locale. It keeps its value between restarts. Ubuntu Community Ask! Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams?
Learn more. How to force a clock update using ntp? Ask Question. Asked 8 years, 11 months ago. Active 8 months ago. Viewed 1. How can I force a clock update at any given time? Here's what I tried: Replace the server to us. Improve this question.
From ntpdate's man page: "Force the time to be stepped using the settimeofday system call, rather than slewed default using the adjtime system call.
This option should be used when called from a startup file at boot time. Consider that the the '-B' flag it mentions that offsets over ms can take hours to sync using the default 'slew' mechanism — Matt S.
Add a comment. Active Oldest Votes. Instead of ntpdate which is deprecated , use ntpd : sudo service ntp stop sudo ntpd -gq sudo service ntp start The -gq tells the ntp daemon to correct the time regardless of the offset g and exit immediately q after setting the time. Improve this answer. Reading ntpd manpage, I am not sure how this forces an update?
The "-q" option tells the NTP daemon to start up, set the time and immediately exit. The "-g" option allows it to correct for time differences larger then sec. For longer term, you should simply configure the NTP daemon to be running always.
This answer should go to the top, because it is correct: ntpdate is deprecated and installing it is a bad idea, because it conflicts with ntp. If the clock is way off, you need to do this manual step because otherwise ntp will not change your clock and will not tell you why.
For me, sudo ntpd -gq does not exit! I'm on Show 12 more comments. Try from command line: sudo service ntp stop sudo ntpdate -s time. Eric Carvalho Eric Carvalho I don't really know. Actually I think you don't have to give the full path to ntpdate , I like to use full paths in scripts just to be sure the right file wiill be found. OK, this was apparently the problem.
Now the clock is being updated as soon as the network connection is established. I did found out that the us. With the -u option, you don't need to stop the ntp service: sudo ntpdate -u time. Instead use systemd-timesyncd.
Show 10 more comments. Use sntp to set the time immediately. It does work with any ntp time server. A convenient list of servers can be found on ntppool.
You need sudo privileges, for example: sudo sntp -Ss -M 0. Guest Guest 5 5 silver badges 2 2 bronze badges. This worked when "sudo ntpd -gq" didn't. For immediate rather than gradual adjustment, when having on hand the accurate time from another clock: sudo date MMDDhhmm — Randall Whitman. Show 3 more comments.
0コメント