Use NTP in Linux to maintain accurate time
How to keep the correct time, how to use NTP and systemd to keep your computer synchronized without abusing the time server.
What is its time?
It is strange to let Linux tell you the time. You may think it istime
Command to tell you the time, actually not becausetime
It's just a timer that measures how long a process has been running. To get the time, what you need to run isdate
Command, you want to view more dates, you can runcal
Command. The timestamp on the file is also confusing, because it generally has two different display methods depending on the default situation of your release. The following is an example from Ubuntu 16.04 LTS:
$ Ls-l
Drwxrwxr-x 5 carla 4096Mar272017 stuff
Drwxrwxr-x 2 carla 4096Dec811: 32 things
-Rw-r -- 1 carla 626052Nov2112: 07 fat1_timeout
-Rw-r -- 1 carla 2781Apr182017 oddlots.txt
Some display years and some display times make your files more messy. By default, GNU displays the time, not the year, if your file is within six months. I think there may be a reason for doing so. If your Linux is like this, try to usels -l --time-style=long-iso
Command to display timestamps in the same way, sorted alphabetically. See how to change the date and time of Linux: A simple command to learn how to manage time on Linux.
Check current settings
NTP-Network Time Protocol, which is an old-fashioned method to keep computers at the right time.ntpd
The NTP daemon periodically queries the public time server to adjust the time of your computer as needed. It is a simple and lightweight protocol that is easy to set when using its basic functions. Systemdsystemd-timesyncd.service
The NTP service has been "active" on behalf of Alibaba Cloud. It can be usedntpd
.
Before we start dealing with NTP, we should take some time to check whether the current time settings are correct.
There are at least two clocks on your system: the system time, which is managed by the Linux kernel, and the second is the hardware clock on your motherboard, also known as the real-time clock (RTC ). When you enter the BIOS of the system, you can see the time of your hardware clock, you can also change its settings. When you install a new Linux, in some graphical Time managers, you will be asked whether to set your RTC to UTC (the World Standard Time Coordinated Universal Time) Time Zone, because all the time zones and time zones are based on UTC. You can usehwclock
Command to check:
$ Sudohwclock -- debug
Hwclockfrom util-linux 2.27.1
Using the/dev interface to the clock.
Hardware clock is on UTC time
Assuming hardware clock is kept in UTC time.
Waitingfor clock tick...
... Got clock tick
Time read fromHardwareClock: 2018/01/22:14:31
Hw clock time: 2018/01/= 1516659271 seconds since 1969
Time since last adjustment is1516659271 seconds
CalculatedHardwareClock drift is0.000000 seconds
Mon22Jan201802: 14: 30 pm pst. 202760 seconds
Hardware clock is on UTC time
It indicates that the RTC of your computer uses UTC time, although it converts the time to your local time. If it is set to local time, it will displayHardware clock is on local time
.
You should have/etc/adjtime
File. If not, use the following command to synchronize your RTC as the system time,
$ Sudohwclock-w
This command will generate the file, the content looks similar to the following:
$ Cat/etc/adjtime
0.00000015166619530.000000
1516661953
UTC
The new systemd method is to runtimedatectl
Command to run it without the root permission:
$ Timedatectl
Localtime: Mon2018-01-2214: 17: 51 PST
Universaltime: Mon2018-01-2222: 17: 51 UTC
RTC time: Mon2018-01-2222: 17: 51
Time zone: America/Los_Angeles (PST,-0800)
Networktime on: yes
NTP synchronized: yes
RTC inlocal TZ: no
RTC in local TZ: no
It indicates that it uses UTC time. So how can I change it to the local time? There are many ways to do this. The simplest way is to use a graphical Configuration tool, such as YaST in openSUSE. You can also usetimedatectl
:
$ Timedatectlset-local-rtc 0
Or edit/etc/adjtime
, SetUTC
ReplaceLOCAL
.
Systemd-timesyncd Client
Now, I am tired, but we have just arrived at a wonderful part. Who can think that timing is so complicated? We have not even learned about it.man 8 hwclock
Learn how your computer maintains time details.
Systemd providessystemd-timesyncd.service
Client, which can query remote time servers and adjust your local system time. In/etc/systemd/timesyncd.conf
Configure your (time) server. Most Linux distributions provide a default configuration pointing to the time server they maintain. For example, the following is a Fedora Configuration:
[Time]
# NTP =
# FallbackNTP = 0.fedora.pool.ntp.org 1.fedora.pool.ntp.org
You can enter other time servers you want to use, such as your local NTP serverNTP=
Enter a list of servers separated by spaces on the line. (Don't forget to cancel the comment on this line)NTP=
Any content on the row will be overwritten.FallbackNTP
The configuration items on the line.
What if you don't want to use systemd? Then, you will need NTP.
Configure the NTP server and client
Configuring your own lan ntp server is a good practice, so that your computer in the network does not need to constantly query the public NTP server. In most Linux, NTP comes fromntp
Packages, most of which are provided/etc/ntp.conf
File to configure the time server. Check the NTP time server pool to find the appropriate NTP server pool in your region. Then in your/etc/ntp.conf
Enter 4-5 servers, each with a separate line:
Driftfile/var/ntp. drift
Logfile/var/log/ntp. log
Server 0.europe.pool.ntp.org
Server 1.europe.pool.ntp.org
Server 2.europe.pool.ntp.org
Server 3.europe.pool.ntp.org
driftfile
Tellntpd
It needs to save the information used to synchronize your system clock quickly with the time server at startup. Logs will also be saved in their own directories instead of being dumped to syslog. If your Linux release provides these files by default, use them.
Start the daemon now. In most mainstream Linux systems, its command issudo systemctl start ntpd
. After running it for a few minutes, we will check its status again:
$ Ntpq-p
Remote refid st t when poll reach delay offset jitter
========================================================== ======================================
+ Dev. smatwebdesi 192.168.194.893 u 25643792.456-6.39518.530
* Chlorophyll. la 127.67.113.922 u 23643775.1758.8208.230
+ Four0.fairy Y. mat 35.73.197.1442 u 226437116.272-10.03340.151
-195.21.151091195.66.241.22 u 276437107.5591.82227.346
I don't know what this content means, but what's important is that your daemon has started to talk with the time server, which is exactly what we need. You can runsudo systemctl enable ntpd
Command to enable it permanently. If your Linux does not use systemd, the homework left for you is to find out how to run it.ntpd
.
Now, you can set it on another computer in your LANsystemd-timesyncd
So that they can use your local NTP server, or install NTP on them, and then/etc/ntp.conf
Enter your local NTP server.
NTP servers are under attack and demand is increasing. You can help by running your own public NTP server. Next week we will learn how to run your own public server.
Learn more about Linux through the free course "Linux getting started" from the Linux Foundation and edX.
Via: https://www.linux.com/learn/intro-to-linux/2018/1/keep-accurate-time-linux-ntp
Author: carla schroder Translator: qhwdw Proofreader: wxy
This article was originally compiled by LCTT and launched with the honor of Linux in China
This article permanently updates link: https://www.bkjia.com/Linux/2018-03/151481.htm