Preface
There are many situations for server time synchronization, and the handling methods of different situations are also different. For example, if you can connect to the external network, you can directly use ntpdate to regularly synchronize the time of the Internet's time server (such as Alibaba's time server ntp.aliyun.com ).
If you cannot connect to the external network, you need to designate a server as the time server. Then other intranet servers act as clients to synchronize time from the time server.
Synchronize Internet time
1. Install ntpdate
# yum -y install ntpdate
2. Add scheduled tasks
# crontab -e
Add the following
0 */12 * * * /usr/sbin/ntpdate -u ntp.aliyun.com > /dev/null 2>&1; /sbin/hwclock -w |
It means that synchronization is performed every 12 hours, and the time can be modified according to the actual situation.
At this point, the scheduled synchronization configuration is completed. Of course, you can also execute ntpdate -u ntp.aliyun.com separately to perform time synchronization.
Intranet environment time synchronization
Time server setup
- install ntp
# yum -y install ntp
# vi /etc/ntp.conf
Modify content |
illustrate |
restrict 192.168.235.0 mask 255.255.255.0 nomodify notrap |
Indicates that clients on the same network segment are allowed to synchronize time. restrict performs permission control on ntp ignore: ignore all types of NTP connection requests nomodify: restricts the client from using the commands ntpc and ntpq to modify the server-side time noquery: does not provide NTP network time adjustment service notrap: does not accept remote login requests notrust: Do not accept requests from unauthenticated clients 192.168.235.0 represents the subnet IP, 255.255.255.0 represents the subnet mask |
server 127.127.1.0 |
Add this machine as a time server |
fudge 127.127.1.0 startum 10 |
Time server level 0-15 0 represents the top level 10 is usually used to provide time services to LAN hosts |
Save after configuration is completed.
- Join startup
# systemctl enable ntpd
- Start ntpd
# systemctl start ntpd
- Can check status
# systemctl status ntpd
Related commands
Order |
illustrate |
systemctl enable ntpd |
Join startup |
systemctl start ntpd |
Start ntpd |
systemctl restart ntpd |
Restart ntpd |
systemctl stop ntpd |
stop ntpd |
systemctl status ntpd |
View status |
Client synchronization configuration
There are two client configurations, one is to refer to the Internet time synchronization method, and the other is to use ntp synchronization.
ntpdate mode
This method is simple and clear, directly violent
- Install ntpdate
# yum -y install ntpdate
- Add a scheduled task
# crontab -e
Add the following
0 */12 * * * /usr/sbin/ntpdate -u 192.168.235.128> /dev/null 2>&1; /sbin/hwclock -w |
ntp client mode
- install ntp
# yum -y install ntp
- Edit configuration file
Both Ntpd server and client use the same configuration file
# vi /etc/ntp.conf
Modify content |
illustrate |
restrict 192.168.235.129 nomodify notrap nopeer noquery |
Indicates that the local computer is not allowed to modify the time restrict performs permission control on ntp ignore: ignore all types of NTP connection requests nomodify: restricts the client from using the commands ntpc and ntpq to modify the server-side time noquery: does not provide NTP network time adjustment service notrap: does not accept remote login requests notrust: Do not accept requests from unauthenticated clients 192.168.235.0 represents the subnet IP, 255.255.255.0 represents the subnet mask |
server 192.168.235.128 iburst |
Add 192.168.235.128 (the server configured earlier) as the time server |
- Join startup
# systemctl enable ntpd
- Start ntpd
# systemctl start ntpd
- Can check status
# systemctl status ntpd
You can also use ntpstat to view
# ntpstat
Use ntpq -p to view detailed synchronization information
# ntpq -p
remote |
time server |
st: |
That is, stratum level. The smaller the value, the higher the accuracy of ntp serve; |
when |
The unit is seconds. The time synchronization update operation was performed a few seconds ago; |
poll |
How many seconds does it synchronize with the ntp server? |
reach |
The number of times updates have been requested to the upper NTP server; |
delay |
Network transmission process clock delay time |
offset |
The result of time compensation |
jitter |
Difference time between Linux system time and BIOS hardware time |