NTP 是什么?
NTP 是网络时间协议(Network Time Protocol),它用来同步网络设备【如计算机、手机】的时间的协议。
n
一、timedatectl命令查看当前时区:
[root@localhost Asia]# timedatectl
Local time: Tue 2018-04-24 07:29:13 CST
Universal time: Mon 2018-04-23 23:29:13 UTC
RTC time: Mon 2018-04-23 23:29:13
Time zone: America/New_York (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
[root@localhost Asia]#
二、timedatectl命令详解
[root@localhost Asia]# timedatectl --help
timedatectl [OPTIONS...] COMMAND ...
Query or change system time and date settings.
-h --help Show this help message
--version Show package version
--no-pager Do not pipe output into a pager
--no-ask-password Do not prompt for password
-H --host=[USER@]HOST Operate on remote host
-M --machine=CONTAINER Operate on local container
--adjust-system-clock Adjust system clock when changing local RTC mode
Commands:
status Show current time settings
set-time TIME Set system time
set-timezone ZONE Set system time zone
list-timezones Show known time zones
set-local-rtc BOOL Control whether RTC is in local time
set-ntp BOOL Control whether NTP is enabled
设置NTP开启timedatectl set-ntp yes
设置NTP关闭timedatectl set-ntp no
timedatectl命令也可以设置时间和日期
设置日期格式timedatectl set-time YYYY-MM-DD
设置时间格式timedatectl set-time HH:MM:SS
三、查看所有时区timedatectl list-timezones
[root@localhost Asia]# timedatectl list-timezones|grep Shanghai
Asia/Shanghai
[root@localhost Asia]#
四、改成上海时区timedatectl set-timezone Asia/Shanghai
[root@localhost Asia]# timedatectl status
Local time: Tue 2018-04-24 07:36:56 CST
Universal time: Mon 2018-04-23 23:36:56 UTC
RTC time: Mon 2018-04-23 23:36:56
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
[root@localhost Asia]#
五、ntp服务端配置
安装:#yum -y install ntpdate
# systemctl enable ntpd
#systemctl start ntpd
1. #ntpdate -u asia.pool.ntp.org
2.#ntpdate -u cn.pool.ntp.org
(1)修改服务器端ntp配置文件
# vim /etc/ntp.conf
注意修改中文处注释
# Hosts on local network are less restricted.
# 允许内网中其他机器同步时间
restrict 192.168.128.0 mask 255.255.255.0 nomodify notrap
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# 上层时间服务器,有外网的情况下可以使用 :
server asia.pool.ntp.org perfer
server cn.pool.ntp.org
#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 autokey # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 autokey # manycast client
# allow update time by the upper server
# 不允许上层时间服务器主动修改本机时间
#restrict asia.pool.ntp.org nomodify notrap noquery
#restrict cn.pool.ntp.org nomodify notrap noquery
------------------------------------------------------------
配置文件中有两种关键配置项,一种是server,代表上层时间服务器,本机可以从这些IP上获取时间;一种是restrict,约束了ntp服务的权限。
说下restrict配置。restrict的格式是:restrict [IP] mask [netmask_IP] [parameter]
parameter主要有以下选项
选项 意义
nomodify 客户端不能修改服务器时间,但是可以从服务器获取时间
notrap 客户端不能使用trap(远端事件登录功能remote event logging)
noquery 其他客户端不能从本机获取时间
#systemctl restart ntpd
(2)ntp客户端配置
修改客户机/etc/ntp.conf,按照上面的方法配置server的地址为刚才设置的ntpd服务器ip,并增加restrict配置(参数nomodify noquery notrap)
server 192.168.128.100
restrict 192.168.128.100 nomodify notrap noquery
启动ntp服务。启动服务后等待几分钟,再检查时间同步是否成功。
#systemctl restart ntpd
手动同步。也可以使用下面命令手动同步时间,下面例子中,192.168.0.102是ntp服务器的地址。
# ntpdate -u 192.168.0.102
查看:ntpq -p
命令 "ntpq -q" 输出下面这样的一个表:
[root@localhost ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
221.236.77.83 .INIT. 16 u - 64 0 0.000 0.000 0.000
sr-99-184-140-1 .INIT. 16 u - 64 0 0.000 0.000 0.000
当ntp自动同步有问题时,可以在crontab中加入如下命令(每5分钟同步一次时间):
# crontab -e
*/5 * * * * /usr/sbin/ntpdate -u 192.168.128.100