NTP全称Network Time Protocol,即网络时间协议。对于正在运行应用集群、数据库等等对时间精确度有严格要求的生产环境中,强烈建议部署对时服务器,确保每台服务器的时间一致。
实验环境
操作系统:RHEL 6.10
NTPD:4.2.8p15
服务端1IP:192.168.168.11
服务端2IP:192.168.168.12
服务端篇
第一步 安装依赖包
- [root@wanghualang ~]# yum -y install gcc wget vim make libcap-devel
第二步 下载、解压软件包
- [root@wanghualang ~]# cd /usr/local/src/
- [root@wanghualang src]# wget --no-check-certificate https://www.eecis.udel.edu/~ntp/ntp_spool/ntp4/ntp-4.2/ntp-4.2.8p15.tar.gz
- [root@wanghualang src]# tar xzf ntp-4.2.8p15.tar.gz
第三步 新建用户组、用户
- [root@wanghualang ~]# groupadd ntp
- [root@wanghualang ~]# useradd ntp -g ntp -s /sbin/nologin -M
第四步 编译安装NTP
- [root@wanghualang ~]# cd /usr/local/src/ntp-4.2.8p15
- [root@wanghualang ntp-4.2.8p15]# ./configure \
- --prefix=/usr/local/ntp-4.2.8p15 \
- --sysconfdir=/etc \
- --enable-linuxcaps \
- --enable-all-clocks \
- --enable-parse-clocks \
- --enable-clockctl \
- --with-lineeditlibs=readline
- [root@wanghualang ntp-4.2.8p15]# make
- [root@wanghualang ntp-4.2.8p15]# make install
第五步 建立程序软链接
- [root@wanghualang ~]# ln -sf /usr/local/ntp-4.2.8p15/sbin/* /usr/sbin/
第六步 配置服务
- [root@wanghualang ~]# vim /etc/ntp.conf
- #IPv4和IPv6用户仅能同步时间
- restrict default kod nomodify notrap nopeer noquery
- restrict -6 default kod nomodify notrap nopeer noquery
- #放行本地来源
- restrict 127.0.0.1
- restrict -6 ::1
- #放行网络对时来源
- restrict ntp1.aliyun.com nomodify
- restrict time1.cloud.tencent.com nomodify
- #上层时钟服务器
- server ntp1.aliyun.com iburst
- server time1.cloud.tencent.com iburst
- #上层服务器不可用时,使用本地时间提供对时
- server 127.127.1.0
- fudge 127.127.1.0 stratum 8
- #日志文件
- logfile /var/log/ntpd.log
- #误差文件
- driftfile /var/lib/ntp/drift
- [root@wanghualang ~]# echo "SYNC_HWCLOCK=yes" > /etc/sysconfig/ntpd
第七步 配置启动服务脚本、开机启动
- [root@wanghualang ~]# vim /etc/init.d/ntpd
- #!/bin/bash
- #chkconfig: 2345 55 25
- NTP_PID="/var/run/ntpd.pid"
- function start_ntp() {
- . /etc/sysconfig/ntpd
- ntpd -x -g -u ntp:ntp -p $NTP_PID
- if [ $? -eq 0 ]; then
- clear
- ntpd --version
- echo ""
- echo -e "NTP服务启动成功" "\033[32m Success\033[0m"
- echo ""
- else
- echo ""
- echo -e "NTP服务启动失败" "\033[31m Failure\033[0m"
- echo ""
- fi
- }
- function stop_ntp() {
- rm -f $NTP_PID && ps aux | grep ntpd | grep -v grep | awk '{print $2}' | xargs kill -9 >/dev/null 2>&1
- }
- function status_ntp() {
- if [ -e $NTP_PID ]; then
- echo ""
- echo -e "NTP服务正在运行" "\033[32m ON\033[0m"
- echo ""
- else
- echo ""
- echo -e "NTP服务未运行" "\033[31m OFF\033[0m"
- echo ""
- fi
- }
- case "$1" in
- start)
- start_ntp
- ;;
- stop)
- stop_ntp
- ;;
- status)
- status_ntp
- ;;
- *)
- echo "Usage:start|stop|status"
- ;;
- esac
- [root@wanghualang ~]# chmod +x /etc/init.d/ntpd
- [root@wanghualang ~]# chkconfig --add ntpd
- [root@wanghualang ~]# chkconfig ntpd on
第八步 启动服务
- [root@wanghualang ~]# service ntpd start
第九步 验证同步
- [root@wanghualang ~]# ntpq -p
客户端篇
第一步 快速安装NTP
- [root@wanghualang ~]# yum -y install ntp
第二步 配置NTP
- [root@wanghualang ~]# mv /etc/ntp.conf /etc/ntp.conf.bak
- [root@wanghualang ~]# vim /etc/ntp.conf
- #拒绝IPv4和IPv6上所有类型的NTP连接
- restrict default ignore
- restrict -6 default ignore
- #放行本地来源
- restrict 127.0.0.1
- restrict -6 ::1
- #放行局域网来源
- restrict 192.168.168.0 mask 255.255.255.0
- #上层时钟服务器
- server 192.168.168.11 iburst
- server 192.168.168.12 iburst
- #日志文件
- logfile /var/log/ntpd.log
- #误差文件
- driftfile /var/lib/ntp/drift
第三步 启动NTP
- [root@wanghualang ~]# service ntpd start
第四步 验证同步
- [root@wanghualang ~]# ntpq -p
您可以选择一种方式赞助本站
支付宝扫一扫
微信扫一扫
赏