ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# 时间服务器 ## 1 概念 ### 1.1 作用 NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议。它的用途是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可达0.1ms,在互联网上绝大多数的地方其精度可以达到1-50ms。 NTP服务器就是利用NTP协议提供时间同步服务的 ### 1.2 常用软件 * ntp软件 CentOS6自带 地址: https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html-single/system_administrators_guide/ * chrony软件 CentOS7自带,比ntp的优势在于不需要做定时任务,只需要每个服务器配置好内网时间服务器地址并启用chrony即可 ### 1.3 主机信息 IP规划为10.0.0.62/172.16.1.62 主机名为m02 主机版本centos7 ## 2 时间服务器部署1[ntp] ### 2.1 软件安装 核实ntp软件包是否安装,备份配置文件 ```sh rpm -qa ntp yum -y install ntp cp /etc/ntp.conf{,.bak} ``` ### 2.2 修改配置 ```sh cat >/etc/ntp.conf <<EOF restrict default kod nomodify notrap nopeer noquery restrict 172.16.1.0/24 10.0.0.0/24 server ntp1.aliyun.com server time.nist.gov EOF ``` ### 2.3 启动ntp服务 1) centos6 ```sh /etc/init.d/ntpd start chkconfig ntpd on chkconfig --list ntpd ``` 时间服务器搭建完成,其他客户端机上只需要做定时任务同步时间即可 2) centos7 ```sh systemctl start ntpd systemctl enable ntpd ``` 3) 定时任务范例 ``` #times sync by noah.luo at 2018-06-02 */5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1 ``` ## 3 时间服务器部署2[chrony] ### 3.1 软件安装 ```sh rpm -qa chrony yum -y install chrony ``` ### 3.2 修改配置 1) sed替换 ```sh sed -i 's#0.rhel.pool.ntp.org#ntp1.aliyun.com#g' /etc/chrony.conf sed -i 's#1.rhel.pool.ntp.org#ntp2.aliyun.com#g' /etc/chrony.conf sed -i 's#2.rhel.pool.ntp.org#ntp3.aliyun.com#g' /etc/chrony.conf sed -i 's#3.rhel.pool.ntp.org#ntp4.aliyun.com#g' /etc/chrony.conf sed -ri 's#^\#allow.*#allow 10.0.0\nallow 172.16.1#g' /etc/chrony.conf ``` 2) 结果验证 ```sh egrep "^allow|^server" /etc/chrony.conf server ntp1.aliyun.com iburst server ntp2.aliyun.com iburst server ntp3.aliyun.com iburst server ntp4.aliyun.com iburst allow 10.0.0 allow 172.16.1 ``` ### 3.3 启动服务 1) centos6方法 ```sh /etc/init.d/chronyd start chkconfig chronyd on chkconfig --list chronyd ``` 2) centos7方法 ```sh systemctl start chronyd.service systemctl enable chronyd.service ``` 3) 客户端时间同步 * chrony方法 安装chrony程序,然后将server配置成刚刚搭建的内网服务器并设置好开机自启动 * ntp方法 安装ntp软件,用定时任务的方式来实现 ``` #times sync by noah.luo at 2018-06-02 */5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1 ``` ## 4 扩展-用windows主机做ntp服务器 ``` 输入regedit,进入注册表编辑器: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\W32Time\Config 修改AnnounceFlags值为5 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\W32Time\TimeProviders\NtpServer 修改Enabled值为1 再在服务器中,将windows time服务设置为延迟启动并立即启动 ```