时间同步:Linux同步国家授时中心的时间
2023-09-14 08:59:05 时间
Linux服务器运行久时,系统时间就会存在一定的误差,一般情况下可以使用date命令进行时间设置,但在做数据库集群分片等操作时对多台机器的时间差是有要求的,此时就需要使用ntpdate进行时间同步。
date命令:
date -R :查看当前时间,结果如下:Thu, 04 Jun 2020 09:36:39 +0800 # +0800 表示东八区
date -s 09:38:40 :设置当前时间,结果如下:Tue Mar 4 09:38:40 CST 2020
安装时间同步工具:
rpm -q ntp :检查是否已经安装了ntp工具
yum -y install ntp 如果没有安装ntp,则使用yum命令安装
yum -y install ntp 如果没有安装ntp,则使用yum命令安装
ntpdate命令:
ping ntp.api.bz :检查时间服务器的可用性
ntpdate -u ntp.api.bz :网络时间同步命令
注意:若不加上-u参数, 会出现以下提示:no server suitable for synchronization found
-u:从man ntpdate中可以看出-u参数可以越过防火墙与主机同步;
ntp.api.bz:NTP(上海)的官方时间服务器。
ntp常用服务器:
中国国家授时中心:210.72.145.44
NTP服务器(上海) :ntp.api.bz
美国:time.nist.gov
复旦:ntp.fudan.edu.cn
微软公司授时主机(美国) :time.windows.com
台警大授时中心(台湾):asia.pool.ntp.org
复旦:ntp.fudan.edu.cn
微软公司授时主机(美国) :time.windows.com
台警大授时中心(台湾):asia.pool.ntp.org
经测试中国国家授时中心与NTP上海服务器可以正常同步时间,注意需要加上-u参数!
改写为自动定时更新:
1、编写被定时执行的脚本:
cd /usr/local/bin
vim ntpdate_timesync.sh # 写入如下内容。
ntpdate -u ntp.api.bz >> ntpdate.log
# 与ntp.api.bz同步一次时间,输出记录到ntpdate.log。操作前,先ping ntp.api.bz 通过,否则更换时间服务器。
chmod +x ntpdate_timesync.sh
bash /usr/local/bin/ntpdate_timesync.sh # 测试脚本是否可以正常工作
cat ntpdate.log # 查看是否有日志正确输出,如:4 Jun 10:42:54 ntpdate[3767]: adjust time server 114.118.7.161 offset -0.003343 sec
2、对脚本实现定时执行管理:
yum install crontabs -y 安装定时工具crontabs
service crond status
service crond start
crontab -l # 查看定时任务清单
systemctl enable crond.service # 将定时工具设置为开机自启
crontab -l # 编辑定时工具的配置文件,写入如下内容:每天21:30自动执行一次脚本
# For details see man 4 crontabs # Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed 30 21 * * * bash /usr/local/bin/ntpdate_timesync.sh
相关文章
- [Linux]linux下取消用户名和密码直接登录
- Linux搭建时间同步服务器
- Linux下同步工具inotify+rsync使用详解
- linux文件同步之rsync学习(一)
- 为什么 Linux 不需要碎片整理
- linux命令中的字符串中间增加指令,例如路径中增加日期变量,实时获取当前的日期
- Linux chrony时间同步服务
- linux ps命令详解:aux与ef参数的区别;按cpu使用率降序显示;按内存使用率降序显示
- [手游新项目历程]-34- linux-127.0.0.1
- Linux下安装 boost 库
- 【Linux 内核 内存管理】物理分配页 ③ ( 物理页分配标志位分析 | ALLOC_WMARK_MIN | ALLOC_WMARK_MASK | ALLOC_HARDER )
- 【Linux 内核】调度器 ⑧ ( 进程优先级源码 includelinuxschedprio.h | 进程分类 | 实时进程 | 普通进程 | 进程优先级数值 | 0 ~ 99 实时进程 )
- L80.linux命令每日一练 -- 第11章 Linux系统管理命令 -- ntsysv和setup
- L61.linux命令每日一练 -- 第九章 Linux进程管理命令 -- renice和nohup
- L44.linux命令每日一练 -- 第七章 Linux用户管理及用户信息查询命令 -- su和visudo
- L3.linux命令每日一练 -- 第一章 Linux命令行简介
- Linux系统之静态路由配置
- 嵌入式linux开发,Linux下访问PHY芯片寄存器
- 嵌入式linux时间同步,ntpdate的交叉编译
- conda安装r Linux系统中 linux中安装r
- Linux之最强录屏工具vokoscreen(一百四十八)
- 【Linux】linux经常使用基本命令
- Linux/Windows双系统时间同步
- linux===给新手的 10 个有用 Linux 命令行技巧(转)
- linux 科普扫盲----同步国内镜像源比如阿里云镜像站制作企业用离线yum源
- Linux lvm管理讲解及命令
- Linux NTP时间同步全解析(CentOS7环境)
- 02 从头开始atac项目 ubuntu20 install r4.2 Linux系统环境配置 服务器版本的rstudio r install in linux /ubuntu/centos