In order to avoid the time deviation caused by the long-term operation of the host time, it is necessary to perform time synchronization (synchronize). Under Linux system, ntp server is generally used to synchronize the time of different machines. A machine that can be both an ntp server and an ntp client. In the network, it is recommended to use hierarchical time servers like DNS servers to synchronize time.
There are generally two NTP synchronization methods under Linux:
( 1) Use the ntpdate command and cron combination to directly synchronize the time
( 2) Smooth synchronization time using ntpd service
NTP time server configuration "2" to synchronize time directly through ntpdate
( 1) Check whether the NTP service is installed
rpm -qa | grep ntp |
( 2) Install NTP software (ignore if already installed)
yum -y install ntp |
(3) Synchronize time, the command format is as follows: # ntpdate [ntp server domain name or IP]
[root@bogon Desktop]# ntpdate 1.cn.pool.ntp.org 30 Dec 14:46:28 ntpdate[7503]: adjust time server 182.92.12.11 offset 0.062906 sec |
As for the ntp server, you can go to http://www.pool.ntp.org NTP's official website to find
server 1.cn.pool.ntp.org
server 3.asia.pool.ntp.org
server 2.asia.pool.ntp.org
server 0.asia.pool.ntp.org
server 1.asia.pool.ntp.org
If the above content description appears, it proves that the synchronization is successful. But such synchronization is only mandatory to set the system time to the ntp server time. If there is a problem with the cpu tick, it is only a temporary solution. Therefore, it is generally used with the cron command to perform regular synchronization settings. For example, add a scheduled task to crontab:
*/10 * * * * root ntpdate 1.cn.pool.ntp.org;/sbin/hwclock -w |
In this way, the time will be synchronized every 10 minutes and the hardware time will be written
( 4) Add timed tasks
[root@bogon Desktop]#vi /etc/crontab |
illustrate:
The basic format of a scheduled task is
* * * * * [user-name] [command to be executed]
The first * represents minutes, represented by 0 to 59, and * or */1 represents every minute;
The second * represents the hour, represented by 0 to 23, and 0 represents 0 o'clock, that is, 24 o'clock;
The third * represents the date, represented by 0 to 31;
The fourth * represents the month, represented by 1 to 12;
The fifth * represents the day of the week, represented by 0 to 6, and 0 represents Sunday;
In the above five parameters, * can be used to indicate every minute, every hour, every day, every week.
user-name indicates the user who executed the command
command indicates the content to be executed periodically.
( 5) Restart the crond service
service crond restart |
( 6) View the task log
You can run the tail -f /var/log/cron command to query the execution of log scheduled tasks
[root@bogon Desktop]# tail -f /var/log/cron Dec 30 15:02:05 bogon crond[9203]: (CRON) INFO (running with inotify support) Dec 30 15:03:01 bogon CROND[9343]: (root) CMD (ntpdate 1.cn.pool.ntp.org;/sbin/hwclock -w) Dec 30 15:04:01 bogon CROND[9527]: (root) CMD (ntpdate 1.cn.pool.ntp.org;/sbin/hwclock -w) |
Note: To use this method, you need to close the ntp service, because when the server is started, the port will be occupied by the server, and the time cannot be manually synchronized. Otherwise, an error will be reported:
[root@bogon Desktop]# ntpdate 1.cn.pool.ntp.org
30 Dec 15:13:37 ntpdate[11548]: the NTP socket is in use, exiting