Summer's Blog
😈酷炫主页
✨运维
🎉安装
👀踩坑
  • k8s
  • shell
  • python
  • redis
  • elasticsearch
  • mysql
  • ceph
  • spark
  • 关于
  • 思维
  • 命令
  • 友链
  • 分类
  • 标签
  • 归档
👨‍👩‍👦‍👦腾讯云社区
🗣GitHub

Summer———夏苏文

💨运维界的前行者
😈酷炫主页
✨运维
🎉安装
👀踩坑
  • k8s
  • shell
  • python
  • redis
  • elasticsearch
  • mysql
  • ceph
  • spark
  • 关于
  • 思维
  • 命令
  • 友链
  • 分类
  • 标签
  • 归档
👨‍👩‍👦‍👦腾讯云社区
🗣GitHub
  • vim使用技巧

  • zabbix使用技巧

  • linux使用技巧

    • 查看隐藏文件大小
    • 修改 hostname
    • 查看硬盘大小
    • 各种cd之间的区别汇总
    • 查看CPU个数
    • 查看当前目录文件数
    • rwxrwxr含义
    • 查看占用内存CPU最多进程
    • yum下载 RPM 包
    • rz命令和sz命令使用方法
    • 服务器ntp时间同步
      • 有外网情况下时间同步
        • 1.安装ntpdate
        • 2.date命令
        • 3.ntpdate命令
        • 4.ntp常用服务器
        • 5.加入crontab
      • 内网机器同步
        • 1.各服务器安装ntp服务
        • 2.主服务器设置
        • 3.其他机器配置(必须root用户)
    • ssh免密配置
    • samba的配置和使用
    • root盘满了怎么办
    • 如何屏蔽linux服务
    • 如何替换jar中的jar配置
    • Linux启动流程
    • 统计脚本执行时间
    • 批量替换示例
    • 大文件切割技巧
    • 一条命令让CPU嗨起来
    • 记录服务器raid操作
    • linux测试网速
    • VM虚拟机扩容操作
    • linux如何快速删除大文件
    • 正则处理文本记录
    • linux测试硬盘读写操作
    • 根据进程查看文件位置
    • iostat查看硬盘读写速度
    • linux内核调优参数记录
  • mysql使用技巧

  • docker使用技巧

  • k8s使用技巧

  • redis使用技巧

  • elasticsearch使用技巧

  • window使用技巧

  • nginx使用技巧

  • ansible使用技巧

  • 运维
  • linux使用技巧
summer
2020-05-02

服务器ntp时间同步

Linux服务器运行久时,系统时间就会存在一定的误差,一般情况下可以使用date命令进行时间设置,但在做数据库集群分片等操作时对多台机器的时间差是有要求的,此时就需要使用ntpdate进行时间同步

# 有外网情况下时间同步

# 1.安装ntpdate

yum install ntpdate -y
1

ntpdate简单用法:

ntpdate ip
ntpdate 210.72.145.44
1
2

# 2.date命令

  • date :查看当前时间 结果如下:Tue Mar 4 01:36:45 CST 2014
  • date -s 09:38:40 :设置当前时间
  • date -s 05/10/2009 结果如下:Tue Mar 4 09:38:40 CST 2014

# 3.ntpdate命令

[root@summer test]# ntpdate -u ntp.api.bz
18 Nov 17:16:26 ntpdate[4234]: adjust time server 114.118.7.163 offset -0.000128 sec
[root@summer test]# 
1
2
3

注意:若不加上-u参数, 会出现以下提示:no server suitable for synchronization found

  • -u:从man ntpdate中可以看出-u参数可以越过防火墙与主机同步;
  • 210.72.145.44:中国国家授时中心的官方服务器。

# 4.ntp常用服务器

中国国家授时中心:210.72.145.44 NTP服务器(上海) :ntp.api.bz 美国:time.nist.gov 复旦:ntp.fudan.edu.cn 微软公司授时主机(美国) :time.windows.com 台警大授时中心(台湾):asia.pool.ntp.org

中国国家授时中心与NTP上海服务器可以正常同步时间,注意需要加上-u参数!

ntpdate -u ntp.api.bz

ntpdate -u 210.72.145.44

# 5.加入crontab

echo "*/20 * * * * /usr/sbin/ntpdate -u ntp.api.bz >/dev/null &" >> /var/spool/cron/root
1

# 内网机器同步

一般生产环境会提供一个时间校准服务IP地址 然后其他各服务器去NTP同步,这里介绍以其中一个服务器为基准,然后另外其他服务器都去同步该台服务器的时间从而实现所有服务器时间都一致

# 1.各服务器安装ntp服务

rpm -qa | grep ntp \\查看服务器是否安装ntp,一般系统默认安装ntpdate
1

# 2.主服务器设置

vim /etc/ntp.conf
1
  • 修改1(授权192.168.1.0-192.168.1.255网段上的所有机器可以从这台机器上查询和同步时间)
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
 
为restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
1
2
3
  • 修改2(集群在局域网中,不使用其他互联网上的时间)
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 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  • 添加3(当该节点丢失网络连接,依然可以采用本地时间作为时间服务器为集群中的其他节点提供时间同步)
server 127.127.1.0
 
fudge 127.127.1.0 stratum 10
1
2
3
  • 4修改/etc/sysconfig/ntpd 文件
vim /etc/sysconfig/ntpd
增加内容如下(让硬件时间与系统时间一起同步)

SYNC_HWCLOCK=yes
1
2
3
4
  • 5重新启动ntpd服务
systemctl restart ntpd
systemctl enable ntpd \\设置ntpd服务开机启动
systemctl status ntpd \\查看状态
1
2
3

# 3.其他机器配置(必须root用户)

  • 在其他机器配置10分钟与时间服务器同步一次
crontab -e
编写定时任务如下:

*/10 * * * * /usr/sbin/ntpdate 主IP
1
2
3
4
  • 十分钟后查看机器是否与时间服务器同步
date
1
#linux
上次更新: 1/29/2021, 6:39:19 PM
rz命令和sz命令使用方法
ssh免密配置

← rz命令和sz命令使用方法 ssh免密配置→

最近更新
01
ceph块设备使用iscsi
03-29
02
Citrix设置vm开机自启动
02-17
03
ntp与chrony时间同步
12-17
更多文章>
Theme by Vdoing | Copyright © 2019-2023 夏苏文 | MIT License

网站已在灾难中运行:

蜀ICP备2022029853号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式