Linux时钟分为: 系统时钟(System Clock)和 硬件时钟(Real Time Clock,简称RTC)

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

NTP 时间同步

# 查看系统版本
# cat /etc/redhat-release (rhel 系列)
# cat /etc/issue (所有的Linux发行版)
# 列出所有版本信息
# cat /etc/lsb-release
lsb_release -a# 关闭selinux
# 关闭防火墙(过程略)
setenforce 0
sed -i 's#SELINUX=enforcing#SELINUX=disabled#' /etc/selinux/config
servcie iptables stop
# 查看
getenforce
/usr/sbin/sestatus -v
--------------------------------------------------------------------------------------------------------------# ntp服务端(IP:11.223.233.42)
# 安装软件
yum -y install ntp ntpdate  # service ntpd start (rhel 6.X 服务启动)
systemctl start ntpd# 查询网络中的NTP服务器
# ntpq -p
No association ID's returned# 修改配置文件
vim /etc/ntp.conf# 修改如下:
#restrict default nomodify notrap nopeer noquery
restrict default nomodify
restrict 127.0.0.1
restrict ::1# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#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 127.127.1.0# 重启服务
systemctl restart ntpd.service# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*LOCAL(0) .LOCL. 5 l 40 64 1 0.000 0.000 0.000# 查看当前时区
date -R# 修改时区为中国上海
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime# 同步时区
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime# 设置服务开机自启
systemctl enable ntpd.service--------------------------------------------------------------------------------------------------------------# ntp客户端
# 安装软件
yum -y install ntp ntpdate  # 修改配置文件
vim /etc/ntp.confserver 11.223.233.42 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# 查看ntp.conf的属性 (重启ntpd无效,排错时使用)
lsattr /etc/ntp.conf
# 恢复文件安全上下文属性(-R,递归)
restorecon -R -v /etc/ntp.conf # 重启服务
systemctl restart ntpd.service# 查看进程,若看到123则启动成功
netstat -tlunp | grep ntp# ntpdate表示网络同步时间;-u 表示可以越过防火墙与主机同步
ntpdate -u 11.223.233.42# 写入BIOS
# hwclock [-rw] ;
# -r:查看现有BIOS时间,默认为-r参数
# -w:将现在的linux系统时间写入BIOS中
hwclock -w# 查看同步的服务器IP
ntpq -pn # 同步的结果
ntpstat # 设置计划任务,每天晚上1点同步时间
crontab -e
00 01 * * * root  /usr/sbin/ntpdate  11.223.233.42; /sbin/hwclock -w      # 设置服务开机自启
systemctl enable ntpd.service

chrony

# 查看系统版本
# cat /etc/redhat-release (rhel 系列)
# cat /etc/issue (所有的Linux发行版)
# 列出所有版本信息
# cat /etc/lsb-release
lsb_release -a# 关闭selinux
# 关闭防火墙(过程略)
setenforce 0
sed -i 's#SELINUX=enforcing#SELINUX=disabled#' /etc/selinux/config
servcie iptables stop
# 查看
getenforce
/usr/sbin/sestatus -v--------------------------------------------------------------------------------------------------------------
# 服务器端
# 安装软件
yum install chrony
d# 修改默认配置,添加本地时间服务器
vi /etc/chrony.conf #server 0.rhel.pool.ntp.org iburst
#server 1.rhel.pool.ntp.org iburst
#server 2.rhel.pool.ntp.org iburst
#server 3.rhel.pool.ntp.org iburst
server 11.22.3.32 iburst
# Allow NTP client access from local network.(允许哪些服务器到这台服务器来同步时间)
allow 192.168.100.0/24   # 启动服务,设置开机自启
systemctl restart chronyd
systemctl enable chronyd
# 检查端口
ss -nultp|grep chrony# 查看所有可用的时区
timedatectl list-timezones
# 设置时区
timedatectl set-timezone Asia/Shanghai# 手动设置时间与日期
timedatectl set-time “YYYY-MM-DD HH:MM:SS”
timedatectl set-time “YYYY-MM-DD”
timedatectl set-time “HH:MM:SS”# 设置硬件时间
# 硬件时间默认为UTC
# hwclock –systohc –localtime (如下效果等同)
timedatectl set-local-rtc 1# 启用NTP时间同步
# yes或no; 1或0也可以
timedatectl set-ntp yes# 查看时间源信息
chronyc sources--------------------------------------------------------------------------------------------------------------
# 客户端
# 安装软件
yum install chrony vi /etc/chrony.conf
#server 0.rhel.pool.ntp.org iburst
#server 1.rhel.pool.ntp.org iburst
#server 2.rhel.pool.ntp.org iburst
#server 3.rhel.pool.ntp.org iburst
# 添加该行,表示到这台服务器去同步时间
server 192.168.100.1 iburst    # 重启服务,设置自启(需要过几分钟才会自动完成同步)
systemctl restart chronyd
systemctl enable  chronyd# 检查同步源
chronyc sources -v
# 查看时间同步状态
chronyc sourcestats -v# 显示系统的当前时间和日期
# timedatectl status(与下效果等同)
timedatectl# 启用NTP时间同步
# yes或no; 1或0也可以
timedatectl set-ntp yes# 手动修改时间时,必须把NTP enabled 设置为no
timedatectl set-ntp no
timedatectl set-time “2017-10-08 11:00:50”# 设置硬件时间
# 硬件时间默认为UTC
# hwclock –systohc –localtime (如下效果等同)
timedatectl set-local-rtc 1

Linux 时间校准与同步相关推荐

  1. Linux时间校准(ntpdate及NTP客户端代码校准示例)

    背景 机器每次机启后时间就会出现异常,因为机器无法访问外网,只能访问局域网的ntp服务,所以需要保证局域网内部有ntp服务,如何安装ntp服务,参考Ubuntu20.04 Ntp服务安装及验证. 网络 ...

  2. linux 时间校准

    记录一下校准时间操作的执行步骤: 首先使用 date 查看当前时间是否准确 校准时间命令 ntpdate cn.pool.ntp.org

  3. linux 时间修改 及 同步

    date 月日时分年.秒 date -s可以直接设置系统时间 比如将系统时间设定成1996年6月10日的命令如下. #date -s 06/10/96 将系统时间设定成下午1点12分0秒的命令如下. ...

  4. linux 设置与网络同步的时间

    #设置linux时间 与网络时间同步 安装命令 yum install -y ntp 同步网络时间 ntpdate ntp1.aliyun.com

  5. linux怎么和宿主机同步时间,Linux 中设置和同步时间

    准确的系统时间是稳定服务的基础,本文介绍与时间相关的Linux命令,以及如何为自己的Linux服务器维护准确的时间. 时间 一台Linux服务器有两个时间源,一个是硬件时间,即服务器硬件CMOS维护的 ...

  6. Linux时间 时区 同步

    Linux时间 时区 同步卸载ntp服务 pi@raspberrypi:~ $ sudo apt-get remove ntp安装ntpdate客户端 pi@raspberrypi:~ $ sudo ...

  7. linux est 时间,Linux 时间标准简介

    系统如何设置时间 开机时根据硬件时钟来设置系统时钟. 如果开启了 NTP 服务, 会根据网络时间来校正系统时钟,确保时间准确. 关机时会根据系统时钟来设置硬件时钟(localtime 标准除外). 时 ...

  8. Linux 时间一致性环境NTP/Chrony服务器部署配置

    一.背景 生产环境中,一个业务系统往往涉及几十台甚至上百台主机或云主机构成,有些管理系统对整个环境时间一致性由强要求,即使没有,保证环境中时间的一致性,有利于业务交互时时间匹配,保证事务的一致性,甚至 ...

  9. Linux 时间、时区设置

    Linux 时间.时区设置 CentOS 7: Chrony https://chrony.tuxfamily.org/index.html https://www.cnblogs.com/zydev ...

最新文章

  1. 熬10天夜,肝出了这个PDF版“软件安装手册”(附下载)
  2. “静态常量”与“ #define”与“枚举”
  3. C 语言编程 — 使用 assert 断言进行程序设计
  4. 【前端】网页布局基础
  5. 代码整洁之道-对象和数据结构
  6. 如何才能招募更多合伙人?合伙人裂变的核心是什么?
  7. 毕啸南专栏 | 对话王小川:搜狗不是谁的“变量”,是行业主要玩家
  8. 数格子算面积的方法_面试小学数学经典教案:《什么是面积》
  9. 我对hibernate的对象的3种状态的理解
  10. Windows操作系统 | Visual C++库可不能乱删
  11. 网络可视化工具netron
  12. 【NOI2015】bzoj4199 品酒大会【解法一】
  13. Alcohol Sensor(1)
  14. 中国科学技术大学2021计算机考研分数线,【中国科学技术大学】2021考研复试分数线3月13日已公布!速看!...
  15. 怎么mac ftp服务器文件夹,mac 访问 ftp服务器文件夹权限
  16. 目标管理之SMART原则
  17. PubMed数据下载
  18. 中文转换成NCR编码(utf-8 16进制)
  19. 一元线性回归模型(保姆级)
  20. C语言-多层for循环详解

热门文章

  1. HTMLCSS 【三】-- TABLES, DIVS, AND SPANS
  2. js百度地图获取当前定位经纬度及省市区
  3. 医院室内定位导航,便捷、低成本智慧医院室内地图应用解决方案
  4. 仿微博、微信、qq 点击缩略图, 查看高清图 UI 组件
  5. 贴吧猫头鹰隐藏二维码是怎么制作的?
  6. 社群公会GangSDK:程序员入行AI领域需要哪些技能?
  7. 17/10/2019 细胞死亡的十一种方式--转自解螺旋公众号
  8. python三维图能画地图_使用Python绘制地图的三大秘密武器
  9. CSS设置超出几行显示省略号
  10. [总结] 全部笔记博文目录总结(持续更新...)