时间服务

时间服务

  • 先理解时间对服务器的重要性

  • 全世界的服务器,时间统一标准,可以以这个为准,cn.ntp.org.cn

  • 学习linux的各种命令,对时间来修改,以及同步

timedaectl

可以修改linux的日期,时间

timedatectl
	date 改时间日期(软件时间,你的系统运行了,程序计算的时间)
	hwclock 改硬件时间(计算的主板上,有一个BISO系统,以及纽扣电池,提供电量)
修改时间、日期、date命令
centos6的修改时区的操作,时区就以亚洲上海为准了
修改时区,cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

# 查看系统中有哪些时区文件
ls /usr/share/zoneinfo/
ll /usr/share/zoneinfo/Asia/Shanghai

修改硬件时间使用 hwclock 命令

查看当前时间

[root@yuanlai-0224 ~]# timedatectl 
      Local time: 一 2022-03-21 12:21:42 CST(中国时区)
  Universal time: 一 2022-03-21 04:21:42 UTC
        RTC time: 一 2022-03-21 04:21:38
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@yuanlai-0224 ~]# 
[root@yuanlai-0224 ~]# 

[root@yuanlai-0224 ~]# timedatectl status
      Local time: 一 2022-03-21 12:21:46 CST
  Universal time: 一 2022-03-21 04:21:46 UTC
        RTC time: 一 2022-03-21 04:21:42
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: no   # timedatectl 自带的ntp服务
NTP synchronized: no   # ntp同步服务是否开启
 RTC in local TZ: no
      DST active: n/a

# 开启timedatectl自带的ntpd
# timedatectl set-ntp on
# 关闭timedatectl自带的ntpd
# timedatectl set-ntp off

修改时间、修改时区

[root@yuanlai-0224 ~]# timedatectl --help
timedatectl [OPTIONS...] COMMAND ...

Query or change system time and date settings.

  -h --help                Show this help message
     --version             Show package version
     --no-pager            Do not pipe output into a pager
     --no-ask-password     Do not prompt for password
  -H --host=[USER@]HOST    Operate on remote host
  -M --machine=CONTAINER   Operate on local container
     --adjust-system-clock Adjust system clock when changing local RTC mode

指令
Commands:
  status                   Show current time settings 查看当前状态
  set-time TIME            Set system time  设置当前的时间 
  set-timezone ZONE        Set system time zone 设置当前的时区
  list-timezones           Show known time zones 查看系统支持哪些时区
  
  
  
  set-local-rtc BOOL       Control whether RTC is in local time
  set-ntp BOOL             Control whether NTP is enabled
[root@yuanlai-0224 ~]# timedatectl set-time '10:00'

[root@yuanlai-0224 ~]# timedatectl set-time '2018-11-8 10:00'

查看系统支持多少个时区
[root@yuanlai-0224 ~]# timedatectl list-timezones | wc -l
425

找出关于上海的时区,具体名字
[root@yuanlai-0224 ~]# timedatectl list-timezones | grep  -i   'shanghai'
Asia/Shanghai
[root@yuanlai-0224 ~]# timedatectl set-timezone Asia/Shanghai

ntp时间同步

  • 强制性更新整个系统的时间,ntpdate,不友好的强制同步时间
  • 搭建ntp服务,自动的,友好的更新,校准系统时间

其强制性ntpdate命令

1.找到时间服务器地址,强制更新即可
[root@yuanlai-0224 ~]# ntpdate -u ntp.aliyun.com


[root@yuanlai-0224 ~]# ntpdate -u ntp.aliyun.com
21 Mar 12:31:11 ntpdate[19892]: step time server 203.107.6.88 offset 106194278.720730 sec


[root@yuanlai-0224 ~]# timedatectl status
      Local time: 一 2022-03-21 12:31:32 CST
  Universal time: 一 2022-03-21 04:31:32 UTC
        RTC time: 四 2018-11-08 02:06:54
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a

搭建ntpd服务

1.安装ntp软件

yum install  ntp -y

2.查看ntp软件信息

ls /usr/lib/systemd/system/ |grep ntp
ntpdate.service
ntpd.service

3.找到ntp软件的配置文件

rpm -ql ntp |grep conf
/etc/ntp.conf
/etc/sysconfig/ntpd
/usr/share/man/man5/ntp.conf.5.gz

4.修改ntp配置文件

vim /etc/ntp.conf

# 新增如下的配置
# 添加ntp的运行日志
logfile /var/log/my_ntp.log
# 记录程序的运行进程号的,可以用于写脚本,读取这个文件,就找到了程序的进程id
pidfile /var/run/ntpd.pid
# 修改同步的ntp服务节点为如下
server ntp.aliyun.com iburst prefer
server cn.pool.ntp.org iburst 

5.修改机器的时间为错误时间

[root@yuanlai-0224 ~]# timedatectl set-time '2018-11-8 10:00'
[root@yuanlai-0224 ~]# timedatectl 
      Local time: 四 2018-11-08 10:00:04 CST
  Universal time: 四 2018-11-08 02:00:04 UTC
        RTC time: 四 2018-11-08 02:00:04
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a

6.启动ntpd服务,等待时间是否同步

[root@yuanlai-0224 ~]# systemctl start  ntpd
[root@yuanlai-0224 ~]# systemctl status ntpd

7.查看ntp是否和上游服务器同步

[root@yuanlai-0224 ~]# ntpstat
unsynchronised
   polling server every 64 s

8.查看时间同步的状态

[root@yuanlai-0224 ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 120.25.115.20   .STEP.          16 u    -   64    0    0.000    0.000   0.000
 203.107.6.88    10.137.38.86     2 u   28   64    1   26.495   -5.532  20.580
 a.chl.la        131.188.3.222    2 u   25   64    1  275.482   52.054  13.988
  1. 这台机器,就是一个时间服务器了,可以作为上游机器使用

date命令和hwclock命令

可以直接用timedatectl替代了

timedatectl直接修改硬件+软件时间了。

让软件时间和硬件时间同步

hwclock 可以将硬件和软件时间做同步
 -s, --hctosys        将硬件时钟同步为当前系统时间
 -w, --systohc        将当前系统时间同步为硬件时钟
posted @ 2023-07-11 16:01  村尚chun叔  阅读(83)  评论(0编辑  收藏  举报