NTP 时间同步服务配置

简介: 中国国家授时中心:210.72.145.44   -- 无法使用NTP服务器(上海) :ntp.api.bz中国ntp服务器:    cn.pool.ntp.org      pool.ntp.
中国国家授时中心:210.72.145.44   -- 无法使用
NTP服务器(上海) :ntp.api.bz
中国ntp服务器:    cn.pool.ntp.org      pool.ntp.org

时间同步工具
rdate:rdate -s
ntpdate:ntpdate -u(使用-u参数会返回误差,也可以使用-s)
以上两个工具都可以用来同步网络时间服务器,系统默认都有安装,除非安装的时候没有选装NTP,没有安装可以使用如下方式安装
yum -y install ntp

同步时间
1.修改时区
cp -y /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
vim  /etc/sysconfig/clock
ZONE="Asia/Shanghai"
UTC=false
ARC=false

2.同步时间
/usr/sbin/ntpdate -u cn.pool.ntp.org

3.写入硬件时间
服务器每次重启都会参考硬件的时间,所以需要将当前系统的时间写入到硬件。
查看当前硬件时间:
hwclock -r
[root@localhost ~]# hwclock -r
修改系统时间:
date -s "YYYY-MM-DD hh:mm:ss" &&hwclock --systoh
写入硬件时间:
hwclock -w

------------------------------------------------------------------------------------------
示例
[root@zzh ~]# date
Thu Feb 14 09:13:35 CST 2019
[root@zzh ~]# date -s 09:16:00
Thu Feb 14 09:16:00 CST 2019
[root@zzh ~]# clock -w
[root@zzh ~]# hwclock -w
[root@zzh ~]# date
Thu Feb 14 09:16:15 CST 2019
------------------------------------------------------------------------------------------

-- 修改系统时间和硬件时间命令释义
1.date    {查看目前本地的时间}
2.hwclock --show    {查看硬件的时间}
3.如果硬件的时间是对不上,那就对硬件的时间进行修改、
4.hwclock --set --date '2019-02-14 15:30:54'   {设置硬件时间}
5.hwclock  --hctosys  {设置系统时间和硬件时间同步}
6.clock -w    {保存时钟}
7.最后在通过重启 init 6 查看系统时间是否生效

自动时间同步
1.配置开机启动校验
vim /etc/rc.d/rc.local
/usr/sbin/ntpdate -u cn.pool.ntp.org> /dev/null 2>&1; /sbin/hwclock -w
2.配置定时任务同步
vim /etc/crontab
00 01 * * * root /usr/sbin/ntpdate -u cn.pool.ntp.org > /dev/null 2>&1; /sbin/hwclock -w 
或者
crontab -e
00 01 * * * /usr/sbin/ntpdate -u cn.pool.ntp.org > /dev/null 2>&1; /sbin/hwclock -w

10 5 * * * root /usr/sbin/ntpdate ntp_ip_addr && /sbin/hwclock -w

-- 注意:
ntp服务默认只会同步系统时间,如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件,在/etc/sysconfig/ntpd文件中,
添加 SYNC_HWCLOCK=yes 这样,就可以让硬件时间与系统时间一起同步。

目录
相关文章
|
网络协议 Linux 数据安全/隐私保护
|
10天前
|
Linux
配置时间同步服务
在Redhat 9.2上,Chrony被用来同步系统时间与NTP服务器。默认情况下,它已在RHEL7/CentOS7中预装。要安装,首先通过`yum -y remove chrony`卸载,然后用`yum -y install chrony`进行安装。安装后,使用`systemctl restart chronyd`重启服务,并用`systemctl enable chronyd`设置开机启动。编辑`/etc/chrony.conf`添加阿里云NTP服务器如`ntp1.aliyun.com`和`ntp2.aliyun.com`以同步时间。使用`chronyc sources -n`确认时间源。
28 4

热门文章

最新文章