002、NTP系统时间同步与手动设置
本文最后更新于 65 天前,其中的信息可能已经过时,如有错误请发送邮件到wuxianglongblog@163.com

在初始化一台linux服务器后,发现这台服务器的时间不对

[root@dev ~]# date
2016年 10月 11日 星期二 07:04:34 CST

同步系统时间

1)服务端部署

安装所需软件包

[root@test ~]# yum -y install ntp ntpdate

服务端自己先手工同步一次时间。

[root@test ~]# ntpdate ntp.sjtu.edu.cn
21 Jun 16:48:54 ntpdate[10781]: the NTP socket is in use, exiting

或者使用阿里云的时间服务器进行在线同步

[root@test ~]# ntpdate ntp2.aliyun.com
[root@test ~]# ntpdate ntp1.aliyun.com

编辑NTP主配置文件,添加NTP服务器(对于大陆地区的服务器)并同步BIOS时间。
对于位于中国大陆地区IDC机房的服务器,由于CentOS/RHEL默认的ntp服务器无法访问,因此要设置中国大陆地区自己的ntp服务器;
对于位于中国大陆地区以外的IDC机房的服务器,可以使用CentOS/RHEL默认的ntp服务器(但一定要检查确保可以访问);
修改如下:

[root@test ~]# cp /etc/ntp.conf /etc/ntp.conf.bak
[root@test ~]# vim /etc/ntp.conf
restrict default nomodify notrap noquery

restrict 127.0.0.1
restrict 192.168.0.0 mask 255.255.0.0 nomodify     #只允许192.168.0.0网段的客户机进行时间同步。如果允许任何IP的客户机都可以进行时间同步,就修改为"restrict default nomodify"

server ntp1.aliyun.com
server ntp2.aliyun.com
server time1.aliyun.com
server time2.aliyun.com

server time-a.nist.gov
server time-b.nist.gov

server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10

driftfile /var/lib/ntp/drift
broadcastdelay  0.008
keys            /etc/ntp/keys
[root@test ~]# service ntpd start
Starting ntpd:                                             [  OK  ]

[root@test ~]# netstat -tulnp | grep ntp
udp        0      0 182.48.115.233:123          0.0.0.0:*                               10023/ntpd        
udp        0      0 127.0.0.1:123               0.0.0.0:*                               10023/ntpd        
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               10023/ntpd        
udp        0      0 fe80::5054:ff:feab:db19:123 :::*                                    10023/ntpd        
udp        0      0 ::1:123                     :::*                                    10023/ntpd        
udp        0      0 :::123                      :::*                                    10023/ntpd

设置开机自启动

[root@test ~]# /sbin/chkconfig --level=2345 ntpd on

查看现有连接客户端

[root@test ~]# watch ntpq -p

服务端可以设置定期在线同步一次时间

[root@test ~]# crontab -l
#服务器时间同步
0 * * * *  /usr/sbin/ntpdate ntp1.aliyun.com; /sbin/hwclock -w
2)客户端配置

(记住要关闭客户端机器的ntpd服务, 否则使用nepdate的时候, 会报错"ntpdate[14431]: the NTP socket is in use, exiting"; 或者开启ntpd服务前提下, 使用ntpdate -u ip就不会有那个报错了)
命令行手动执行一次验证是否可用:

[root@test2 ~]# ntpdate 182.148.15.33             #182.148.15.33是上面ntp服务器的ip地址
21 Jun 16:45:35 ntpdate[2323]: adjust time server 182.148.15.33 offset 0.319548 sec

配置自动同步

[root@test2 ~]# crontab -l
#服务器时间同步
*/5 * * * *  /usr/sbin/ntpdate 182.148.15.33; /sbin/hwclock -w

设置硬件时间

(1)查看硬件时间
[root@dev ~]# hwclock --show
Sun 18 Dec 2016 09:38:44 PM CST  -0.902813 seconds
[root@dev ~]# clock --show
Sun 18 Dec 2016 09:38:51 PM CST  -0.236699 seconds
(2)修改硬件时间
[root@dev ~]# hwclock –set –date=”07/07/06 10:19″              (月/日/年 时:分:秒)
[root@dev ~]# clock –set –date=”07/07/06 10:19″                  (月/日/年 时:分:秒)
(3)硬件时间和系统时间的同步

按照前面的说法,重新启动系统,硬件时间会读取系统时间,实现同步;
但是在不重新启动的时候,需要用hwclock或clock命令实现同步:

hwclock,clock: 显示硬件时钟
[root@localhost ~]# hwclock
Tue 09 Jan 2024 03:43:31 AM EST  -0.836243 seconds
[root@localhost ~]# clock
Tue 09 Jan 2024 03:43:45 AM EST  -0.852206 seconds
[root@localhost ~]#

-s, --hctosys 以硬件时钟为准,校正系统时钟
-w, --systohc 以系统时钟为准,校正硬件时钟
[root@localhost ~]# date
Wed Jan 11 16:43:44 EST 2023 //系统时间
[root@localhost ~]# clock
Tue 09 Jan 2024 03:44:55 AM EST  -0.141431 seconds //硬件时间
[root@localhost ~]# clock -w  // 以系统时钟为准,校正硬件时钟
[root@localhost ~]# clock
Wed 11 Jan 2023 04:44:02 PM EST  -0.682965 seconds //校准后硬件时间
[root@localhost ~]# date
Wed Jan 11 16:44:11 EST 2023
[root@localhost ~]# 
谨此笔记,记录过往。凭君阅览,如能收益,莫大奢望。
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇