设置NTP时间同步

一、配置某台Linux主机为ntp时间服务器

1、安装ntp

     yum -y install ntp

2、修改ntp.conf配置文件

    ①vim/etc/ntp.conf

    ②  配置ntp服务器

     

    如果在/ntp.conf中定义的server都不可用时,将使用local时间作为ntp服务提供给ntp客户端

  以下的定义是让NTP Server和其自身保持同步

    ntp.conf最后添加俩句话:

    server 127.127.1.0 #local clock
    fudge  127.127.1.0 stratum 10

    配置详解:

    第一行restrict、default定义默认访问规则,

    nomodify禁止远程主机修改本地服务器配置,

    notrap拒绝特殊的ntpdq捕获消息,

    noquery拒绝btodq/ntpdc查询(这里的查询是服务器本身状态查询)。

    restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap这句是手动增加的,意思是从192.168.1.1-192.168.1.254的服务     器都可以使用我们的NTP服务器来同步时间。

    server 192.168.146.225这句也是手动增加的,指明局域网中作为NTP服务器的IP; 

    配置文件的最后两行作用是当服务器与公用的时间服务器失去联系时以本地时间为客户端提供时间服务。

3、手动同步服务器系统时间

    ntpdate 0.asia.pool.ntp.org

    注意:在执行ntpdate之前要确保ntpd服务为停止状态

4、启动、停止服务

     启动服务:servicentpd start  

     停止服务:servicentpd stop

     *****说明centos 7 开启停止服务*****

      /bin/systemctl start  ntpd.service #开启

      /bin/systemctl stop  ntpd.service #停止

5、设置ntpd服务开机启动

     centos6: chkconfig ntpd on

     centos7: systemctl enable  ntpd.service

二、Windows安装NTP服务器

1、进入到服务器,依次点击:开始---运行---regedit
然后点击确定。
 
依次展开:HKEY_LOCAL_MACHINE、SYSTEM、CurrentControlSet、Services、W32Time、TimeProviders、NtpServer
在NtpServer项的右侧键值ENablied,将默认的0改为1,1为启用NTP服务器。
 
2、依次展开:HKEY_LOCAL_MACHINE、SYSTEM、CurrentControlSet、Services、W32Time、Config找到Config项右侧的AnnounceFlags。把默认的10改为5
   5的意思就是自身为可靠的时间源
 
修改完以上之后关闭注册表编辑器

3、再:开始---运行---cmd---确定


4、在命令提示符中输入:net stop w32Time回车等待NTP服务停止
然后再输入:net start w32Time回车
启动NTP服务
 
测试,同步成功!

三、配置其他主机为ntp客户端

     

1、安装ntp

yum -y install ntp
  • 1

2、修改配置文件/etc/ntp.conf

#注释默认配置,添加刚刚创建的ntpserver
#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
server 192.168.48.11 #ntp服务器IP

3、安装使用定时器crontab

3.1、安装
yum -y install vixie-cron
yum -y install crontabs
3.2、启停服务
service crond start //启动服务
service crond stop //关闭服务
service crond restart //重启服务
service crond reload //重新载入配置 s
ervice crond status //查看crontab服务状态
3.3、查看所有定时任务
     crontab -l
 
3.4、添加定时任务
   crontab -e
    

3.5、crontab 时间表达式

3.5.1基本格式 :
*  *  *  *  *  command

分  时  日  月   周    命令


3.5.2常用实例

// 每分钟执行一次
   *  *  *  *  * 
// 每隔一小时执行一次
    00  *  *  *  *   
    * */1 * * *  (/表示频率)
// 每小时的15和45分各执行一次 
    15,45 * * * * (,表示并列)
// 在每天上午 8- 11时中间每小时 15 ,45分各执行一次
    15,45 8-11 * * * command (-表示范围)
// 每个星期一的上午8点到11点的第3和第15分钟执行
    3,15 8-11 * * 1 command
// 每隔两天的上午8点到11点的第3和第15分钟执行
    3,15 8-11 */2 * * command

4、编辑定时器任务

sudo crontab -e
添加如下内容:

0 0 * * * /usr/sbin/ntpdate 192.168.48.11 >> /home/hadoop/logs/ntpd.log

             

注意:只有NTP服务器启动服务,其他NTP客户端不能启动NTP服务,否则时间不同步!!!

各节点时间同步完成;




猜你喜欢

转载自blog.csdn.net/u012637358/article/details/80360104