In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to use NTP in Linux to maintain accurate time", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "how to use NTP in Linux to keep accurate time" this article.
How long is it?
It's strange to ask Linux to tell you the time. You might think that you are using the time command to tell you the time, but it is not, because time is just a timer that measures how long a process has been running. To get time, you need to run the date command. If you want to see more dates, you can run the cal command. The timestamp on the file is also a confusing place, because it generally has two different display methods, depending on the default of your distribution. The following is an example from Ubuntu 16.04 LTS:
Ls-ldrwxrwxr-x 5 carla carla 4096 Mar 27 2017 stuffdrwxrwxr-x 2 carla carla 4096 Dec 8 11:32 things-rw-rw-r-- 1 carla carla 626052 Nov 21 12:07 fatpdf.pdf-rw-rw-r-- 1 carla carla 2781 Apr 18 2017 oddlots.txt
Some show the year, some show the time, which makes your file more confusing. By default, GNU shows the time instead of the year if your file is less than six months old. I think there may be a reason for doing so. If your Linux is like this, try using the ls-l-- time-style=long-iso command to have the timestamps displayed in the same way, in alphabetical order. See how to change the date and time of Linux: simple commands to learn various ways to manage time on Linux.
Check current settin
NTP, the network time protocol, is an old-fashioned way to keep computers at the right time. Ntpd is a NTP daemon that adjusts your computer time on demand by periodically querying the public time server. It is a simple, lightweight protocol that is easy to set up when using its basic functions. Systemd has "done NTP's job" by using systemd-timesyncd.service, which can be used as a client to ntpd.
Before we start "dealing" with NTP, take a moment to check that the current time setting is correct.
There are (at least) two clocks on your system: 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, and you can also change its settings. When you install a new Linux, in some graphical time managers, you will be asked if you want to set your RTC to the UTC (Universal Standard time Coordinated Universal Time) time zone, because all time zones and daylight saving time are based on UTC.
You can use the hwclock command to check:
Sudo hwclock-- debughwclock from util-linux 2.27.1Using the / dev interface to the clock.Hardware clock is on UTC timeAssuming hardware clock is kept in UTC time.Waiting for clock tick.got clock tickTime read from Hardware Clock: 2018-01-22 22:14:31Hw clock time: 2018-01-22 22:14:31 = 1516659271 seconds since 1969Time since last adjustment is 1516659271 secondsCalculated Hardware Clock drift is 0.000000 secondsMon 22 Jan 02:14:30 PM PST. 202760 seconds
Hardware clock is on UTC time indicates that your computer's RTC uses UTC time, although it converts that time to your local time. If it is set to local time, it will display Hardware clock is on local time.
You should have a / etc/adjtime file. If not, use the following command to synchronize your RTC for system time
$sudo hwclock-w
This command will generate the file, which looks like this:
$cat / etc/adjtime0.000000 1516661953 0.0000001516661953UTC
The newly invented systemd method is to run the timedatectl command, which does not require root permission:
$timedatectl Local time: Mon 2018-01-22 14:17:51 PST Universal time: Mon 2018-01-22 22:17:51 UTC RTC time: Mon 2018-01-22 22:17:51 Time zone: America/Los_Angeles (PST,-0800) Network time on: yesNTP synchronized: yes RTC in local TZ: no
RTC in local TZ: no indicates that it uses UTC time. So how do you change it to local time? There are many ways to do this. The easiest way is to use a graphical configuration tool, such as YaST in openSUSE. You can also use timedatectl:
$timedatectl set-local-rtc 0
Or edit / etc/adjtime to replace UTC with LOCAL.
Systemd-timesyncd client
Now, I'm tired, but we just got to the wonderful part. Who would have thought timing was so complicated? We haven't even scratched the surface yet; read man 8 hwclock to learn more about how your computer keeps time.
Systemd provides the systemd-timesyncd.service client, which can query the remote time server and adjust your local system time. Configure your server in / etc/systemd/timesyncd.conf. Most Linux distributions provide a default configuration that points to the time server they maintain, for example, the following is Fedora:
[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 own local NTP server, and enter a list of servers separated by spaces on the NTP= line. (don't forget to uncomment this line) anything on the NTP= line will overwrite the configuration item on the FallbackNTP line.
What if you don't want to use systemd? Then you will need NTP.
Configure NTP server and client
It is a good practice to configure your own LAN NTP server so that your on-net computer does not have to keep querying public NTP servers. The NTP on most Linux comes from the ntp package, and most of them provide / etc/ntp.conf files to configure the time server. Check the NTP time server pool to find the appropriate NTP server pool for your area. Then enter 4-5 servers in your / etc/ntp.conf, each on a separate line:
Driftfile / var/ntp.driftlogfile / var/log/ntp.logserver 0.europe.pool.ntp.orgserver 1.europe.pool.ntp.orgserver 2.europe.pool.ntp.orgserver 3.europe.pool.ntp.org
Driftfile tells ntpd that it needs to save information to quickly synchronize your system clock using a time server at startup. The logs will also be saved in their own specified directory instead of being dumped to syslog. If your Linux distribution provides these files by default, please use them.
Now start the daemon; in most mainstream Linux its command is sudo systemctl start ntpd. After a few minutes of running it, we check its status again:
$ntpq-p remote refid st t when poll reach delay offset jitter===+dev.smatwebdesi 192.168.194.89 3 u 25 64 37 92.456-6.395 18.530*chl.la 127.67.113.92 2 u 23 64 37 75.175 8.820 8.230+four0.fairy.mat 35.73.197.144 2 u 22 64 37 116.272-10.033 40.151-195.21.152.161 195.66.241.2 2 u 27 64 37 107.559 1.822 27.346
I don't know what this means, but the important thing is that your daemon has started talking to the time server, which is exactly what we need. You can run the sudo systemctl enable ntpd command to enable it permanently. If your Linux does not use systemd, then the homework left for you is to figure out how to run ntpd.
Now, you can set up systemd-timesyncd on other computers on your LAN so that they can use your local NTP server, or install NTP on them and enter your local NTP server on their / etc/ntp.conf.
NTP servers are under attack, and demand is growing. You can help by running your own public NTP server. Next week we will learn how to run your own public server.
The above is all the contents of the article "how to use NTP to keep accurate time in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.