NTP (Network Time Protocol) 作用:将网络中的各个主机的时间进行同步,局域网中的误差<0.1ms 架构:C/S 实验环境:两台有网络的Centos,其中134为服务端,135为客户端。134同步网络时间,其他服务器同步134时间。 1、查看134的系统信息、时间和时区 cat /etc/redhat-release date timedatectl | grep "Time zone" 此时可以看到134时区为上海、时间和网络时间不同步 2、查看135的系统信息、时间和时区 cat /etc/redhat-release date timedatectl | grep "Time zone" 此时可以看到135时区为上海、时间和网络时间不同步 3、在服务端134编写计划任务设置服务器自动同步阿里云时间服务器,并把本地时间同步到硬件时间。

          • /usr/sbin/ntpdate -u ntp1.aliyun.com &>/dev/null
          • /usr/sbin/hwclock -w &>/dev/null

4、此时查看服务器134的时间,已经和网络时间同步 5、在服务端134安装ntp服务,需要有yum源。(配置yum源 https://blog.51cto.com/14015577/2414577) yum install ntp -y 6、配置服务器端134 NTP配置文件介绍 /etc/ntp.conf 内容: (1)、restrict:指定授权哪些主机可以使用ntp进行时间同步 格式:restrict IP地址 mask 子网掩码 参数 参数:ignore:关闭ntp服务器 nomodify:指定客户端可以进行时间同步,但是不能修改服务器端时间 例子: restrict 192.168.6.123 restrict 192.168.6.0 mask 255.255.255.0 restrict 0.0.0.0 mask 0 0 0 0 nomodify ( 2)、server:指定从哪个外部时间服务器来获取时间 例子: server 0.centos.pool.ntp.org iburst server 1.centos.pool.ntp.org iburst server 2.centos.pool.ntp.org iburst server 3.centos.pool.ntp.org iburst

此时134配置如下: server 127.127.1.0 restrict 127.0.0.1 restrict 192.168.0.0 mask 255.255.255.0 启动ntp服务并查看状态 systemctl restart ntpd systemctl status ntpd ntpstat

7、客户端135安装ntpdate服务 yum install ntpdate -y 8、客户端134编写计划任务自动同步135服务器上的时间,并把时间同步到硬件时间 crontab -l

          • /usr/sbin/ntpdate -u 192.168.70.134 &>/dev/null
          • /usr/sbin/hwclock -w &>/dev/null

此时,可以看到135时间和网络时间同步。内网中的服务器只需同步134即可。减少资源占用。