可搜索,可注册,可登录,致敬逗比大佬!尽在救援版逗比根据地 dbgjd.com
投稿文章 | 广告合作 | Telegram 群组 / 公告频道 / 使用教程

Linux 的系统时钟调教

News mlone11978 618℃ 0评论

偶然下午折腾 v2ray ,发现因为系统时间和本地时间不一致搞得我被 reject 了,正好提醒我写一个关于 调教(#滑稽) Linux 系统时钟 的帖子。

Linux的两种时间

Linux 时钟分为 系统时钟(System Clock)和 硬件时钟(Real Time Clock,简称RTC)
Linux 下,默认情况下,系统时间和硬件时间并不会自动同步。系统运行过程中,系统时间和硬件时间异步计时,互不干扰。
系统时钟是当前 Linux Kernel 的时钟,而硬件时钟是主板上由电池供电的时钟,硬件时钟可在 BIOS 中设置。当 Linux 启动时,硬件时钟会去读取系统时钟的设置,并独立于硬件时间进行计时。
Linux 中的所有命令(包括函数)均采用系统时钟。
Linux 中,时钟相关的命令主要有 datehwclock

date

date [指令] [参数]
设定时间

#设定日期20170730 时刻归零00:00:00   date --s "20170730"   #设定时刻17:32:59 日期保持不变   date --s "17:32:59"   #以下六种均可同时设定日期和时刻   date --s "20170730 17:32:59"   date --s "17:32:59 20170730"   date --s "2017/07/30 17:32:59"   date --s "17:32:59 2017/07/30"   date --s "17:32:59 07/30/2017"   date --s "07/30/2017 17:32:59" 

hwclock

#将系统时间写入硬件时间   hwclock --systohc   #将硬件时间写入系统时间   hwclock --hctosys   #将当前时间写入BIOS 避免重启后失效   hwclock -w 

Linux的时区

时区是什么当然不用解释了
这一块讲解怎么设定时区

查看当前时区

date -R

手动设定时区

tzselect
跟着提示一步步完成选择后,复制相应的时区文件,替换系统时区文件;或者创建ln -s链接
例如修改时区为中国上海
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

时间同步的两种方式

手动设定或多或少会有偏差(将来出了偏差…),这时就可从时间服务器更新时间以获得更高精确度

ntpdate更新时间

如果你的linux提示ntpdate:command not found

debian: apt-get install ntpdate   centos: yum install ntpdate 

安装完成后可直接执行命令

ntpdate [ntp时间服务器地址]   [[email protected] ~] ntpdate time.nist.gov   30 Jul 18:01:08 ntpdate[11063]: adjust time server 216.229.0.179 63592.064680 sec 

出现上面的内容即同步成功,然后在crontab里面加上以下:
*/360 * * * * ntpdate time.nist.gov #每隔六小时同步一次
推荐以下时间服务器:

time.nist.gov   time.nuri.net   0.asia.pool.ntp.org   1.asia.pool.ntp.org   2.asia.pool.ntp.org   3.asia.pool.ntp.org 

ntp自建时间服务器

上面我提到使用ntp时间服务器来同步时间,这些时间服务器都是较权威的
而当我们自己搭建时间服务器时,就不用crontab定时去跑了

[[email protected] ~] apt-get install ntp -y   [[email protected] ~] yum install ntp -y   [[email protected] ~] cat /etc/ntp.conf |awk '{if($0 !~ /^$/ && $0 !~ /^#/) {print $0}}' #修改配置文件   restrict default kod nomodify notrap nopeer noquery #拒绝IPV4用户   restrict -6 default kod nomodify notrap nopeer noquery #拒绝IPV6用户   restrict 192.168.100.0 mask 255.255.255.0 nomodify #本地网段授权访问   restrict time.nist.gov #授权访问本地NTP   restrict 0.asia.pool.ntp.org   restrict 1.asia.pool.ntp.org   restrict 2.asia.pool.ntp.org   restrict 127.0.0.1   restrict -6 ::1   server time.nist.gov prefer #设定时间服务器,prefer表示优先   server 0.asia.pool.ntp.org   server 1.asia.pool.ntp.org   server 2.asia.pool.ntp.org   driftfile /var/lib/ntp/drift   keys /etc/ntp/keys   [[email protected] ~] service ntp restart #重启服务   [[email protected] ~] netstat -tlunp | grep ntp #查看进程,若看到123则启动成功   udp 0 0 136.243.26.229:123 0.0.0.0:*   11139/ntpd   udp6 0 0 ::1:123 :::*   11139/ntpd   [[email protected] ~] ntpq -pn #查看同步的服务器IP   remote refid st t when poll reach delay offset jitter   +61.216.153.104 118.163.81.62 3 u 5 64 1 62.575 10.842 1.198   +212.47.249.141 5.103.128.88 3 u 4 64 1 217.645 -12.155 0.224   -51.15.41.135 5.103.128.88 3 u 3 64 1 230.814 -26.141 0.702   *108.59.2.24 130.133.1.10 2 u 2 64 1 235.620 -8.041 0.207   [[email protected] ~] ntpstat #同步的结果   synchronised to local net at stratum 11   time correct to within 12 ms   polling server every 512 s 

其中:

  • remote: NTP 主机 IP。最左边的符号,若 “+” 则表示是正在作用的上游 NTP,若 “*” 则表示也有连上,只是作为次要 NTP
  • refid: 参考的上游NTP地址
  • st: stratum阶层
  • when: 几秒前曾进行同步
  • poll: 多少秒后进行下次同步
  • reach: 已向上游NTP请求同步的次数
  • delay: 网络传输过程中钟延迟的时间
  • offset: 时间补偿结果
  • jitter: 系统时间与硬件时间的差异时间

转载请注明:逗比根据地 » Linux 的系统时钟调教

喜欢 (0)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址