• 06-时间管理


    三个时间属性:

     1 [root@localhost ~]# stat anaconda-ks.cfg 
     2   File: ‘anaconda-ks.cfg’
     3   Size: 1854          Blocks: 8          IO Block: 4096   regular file
     4 Device: fd00h/64768d    Inode: 33574978    Links: 1
     5 Access: (0670/-rw-rwx---)  Uid: (    0/    root)   Gid: (    0/    root)
     6 Context: system_u:object_r:admin_home_t:s0
     7 Access: 2019-08-10 20:11:22.311000230 +0800
     8 Modify: 2019-08-10 20:10:06.757030040 +0800
     9 Change: 2019-08-10 22:35:19.830068534 +0800
    10  Birth: -
      Access:(Access Time),简写为atime,表示文件的访问时间(查看,访问时就会发生更改) 

    cat  

      Modify:(Modify Time),简写为mtime,表示文件内容的修改时间(文件内容发生改变时就会发生更给,即block的内容发生变化)
      

    vi

      Change: (Change Time),简写为ctime,表示文件状态的修改时间(文件的权限、拥有者、所属的组、链接数发生改变时就会发生更改;当内容改变时也会随之改变(即inode内容发生改变和Block内容发生改变时会变化)

    chown 

    
    

    时间管理

    时间管理命令:date
      显示时间:date
      按指定格式显示时间:date +"格式"
        
     1 [root@localhost ~]# date +"%Y-%m-%d"
     2 2019-08-11
     3 [root@localhost ~]# date +"%Y-%m-%d *m:%h"
     4 2019-08-11 *m:Aug
     5 [root@localhost ~]# date +"%Y-%m-%d %m:%h"
     6 2019-08-11 08:Aug
     7 [root@localhost ~]# date +"%Y-%m-%d %M:%h"
     8 2019-08-11 07:Aug
     9 [root@localhost ~]# date +"%Y-%m-%d %M:%H"
    10 2019-08-11 07:18

      设置时间:date -s "时间"

    1 [root@localhost ~]# date -s "2018-9-10"
    2 Mon Sep 10 00:00:00 CST 2018
    3 [root@localhost ~]# date -s "2018-9-10 10:00:00"
    4 Mon Sep 10 10:00:00 CST 2018
    5 [root@localhost ~]# date -s "10:00:50"
    6 Mon Sep 10 10:00:50 CST 2018
    7 [root@localhost ~]# date -s "10:00:50 2019-8-11"
    8 Sun Aug 11 10:00:50 CST 2019

    linux时间不止有date,也就是系统时间(sys时间),还有个硬件时间(BIOS时间)

      显示硬件时间:hwclock

      硬件时间和系统时间如何同步?

        hwclock --help   

          -s, --hctosys set the system time from the hardware clock 将硬件时钟同步到系统时钟
          -w, --systohc set the hardware clock from the current system time 将系统时钟同步到硬件时钟

    系统时区(TZ)

    第一种方法:

    使用tzselect命令设置系统时区;tzselect命令只告诉你选择的时区的写法,并不会自动生效。

    在全局配置中添加【用户变量/.profile、/~bash_profile或者全局变量/etc/profile中】

    [root@ntp-server ~]# vi /etc/profile    # 添加TZ变量环境变量 ,把获取的TZ值加入到全局变量中
    TZ='Asia/Shanghai'; export TZ
    "/etc/profile" 79L, 1871C written
    
    [root@ntp-server ~]# source /etc/profile    # 使能TZ变量生效
    
    [root@ntp-server ~]# date -R    # 验证时区
    Tue, 16 Oct 2018 17:08:05 +0800
    
    [root@ntp-server ~]# more /etc/sysconfig/clock 
    ZONE="Asia/Shanghai"

    第二种方法:

    [root@ntp-client ~]# ls /usr/share/zoneinfo/
    Africa      EST5EDT      Jamaica      PRC
    America     Etc          Japan        PST8PDT
    Antarctica  Europe       Kwajalein    right
    Arctic      GB           leapseconds  ROC
    Asia        GB-Eire      Libya        ROK
    Atlantic    GMT          MET          Singapore
    Australia   GMT0         Mexico       Turkey
    Brazil      GMT-0        MST          tzdata.zi
    Canada      GMT+0        MST7MDT      UCT
    CET         Greenwich    Navajo       Universal
    Chile       Hongkong     NZ           US
    CST6CDT     HST          NZ-CHAT      UTC
    Cuba        Iceland      Pacific      WET
    EET         Indian       Poland       W-SU
    Egypt       Iran         Portugal     zone1970.tab
    Eire        iso3166.tab  posix        zone.tab
    EST         Israel       posixrules   Zulu

    [root@ntp-client ~]# ls -l /etc/localtime
    lrwxrwxrwx. 1 root root 35 Aug 10 20:09 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai

     

    将 /etc/localtime里的连接指向重新指定即可

    或者采用命令行的方式修改:

    
    

    [root@ntp-client ~]#  timedatectl list-timezones  #列表

    [root@ntp-client ~]# timedatectl list-timezones | grep shanghai
    [root@ntp-client ~]# timedatectl list-timezones | grep Shanghai #获取完整写法

    [root@ntp-client ~]# timedatectl list-timezones | grep Shan
    Asia/Shanghai

    [root@ntp-client ~]# timedatectl set-timezone Asia/Shanghai

    一定要要注意大小写

    第一种方法的优先级会高于第二种方法

     

    时间同步

    如果局域网内有多台PC或想多台电脑时间保持高度一致,可使用网络时间协议:

    NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议。

    它的用途是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可达0.1ms,在互联网上绝大多数的地方其精度可以达到1-50ms。

    NTP服务器就是利用NTP协议提供时间同步服务的;

    支持ntp协议的软件ntp(6.x版本自带),chrony(7.x版本自带)

    实验环境:

    server端:

    [root@ntp-server ~]# cat /etc/redhat-release 
    CentOS Linux release 7.5.1804 (Core) 
    [root@ntp-server ~]# uname -r
    3.10.0-862.el7.x86_64
    [root@ntp-server ~]# uname -i
    x86_64
    [root@ntp-server ~]# uname -n
    ntp-server
    [root@ntp-server ~]# ifconfig ens33|grep netmask
            inet 10.0.0.100  netmask 255.255.255.0  broadcast 10.0.0.255
    [root@ntp-server ~]# ifconfig ens33|grep netmask|awk '{print $2}'
    10.0.0.100

    client端:

    [root@ntp-client ~]# cat /etc/redhat-release 
    CentOS Linux release 7.5.1804 (Core) 
    [root@ntp-client ~]# uname -r
    3.10.0-862.el7.x86_64
    [root@ntp-client ~]# uname -i
    x86_64
    [root@ntp-client ~]# uname -n
    ntp-client
    [root@ntp-client ~]# ifconfig ens33|grep netmask
            inet 10.0.0.101  netmask 255.255.255.0  broadcast 10.0.0.255
    [root@ntp-client ~]# ifconfig ens33|grep netmask|awk '{print $2}'
    10.0.0.101

    NTP:

    [root@ntp-server ~]# yum -y install ntp
    [root@ntp-server ~]# rpm -qa ntp
    ntp-4.2.6p5-28.el7.centos.x86_64
    [root@ntp-server ~]# cp /etc/ntp.conf{,.ori}
    [root@ntp
    -server ~]# vim /etc/ntp.conf
    只是将server更改本机的IP

    #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 ntp1.aliyun.com



    [root@ntp-server ~]# ntpq -p
    remote refid st t when poll reach delay offset jitter
    ==============================================================================
    120.25.115.20 10.137.53.7 2 u 10 64 1 30.222 -287986 0.000

    watch ntpq -p #实时监控

     
    [root@ntp-server ~]# ntpstat
    unsynchronised
    time server re-starting
    polling server every 8 s

    
    
    报错:
    [root@ntp-client ~]# ntpdate 10.0.0.100 11 Aug 18:43:52 ntpdate[2756]: no server suitable for synchronization found

    关闭防火墙:systemctl stop firewalld
    关闭selinux:setenforce 0

    基本上就是等些时间就会成功,这里是吃了顿饭回来就可以同步了

    11 Aug 18:56:44 ntpdate[2943]: no server suitable for synchronization found
    [root@ntp-client ~]# ntpdate 10.0.0.100
    11 Aug 18:56:54 ntpdate[2952]: no server suitable for synchronization found
    [root@ntp-client ~]# ntpdate 10.0.0.100
    11 Aug 18:58:46 ntpdate[2963]: no server suitable for synchronization found
    [root@ntp-client ~]# ntpdate 10.0.0.100
    11 Aug 19:00:40 ntpdate[2995]: no server suitable for synchronization found
    [root@ntp-client ~]# ntpdate 10.0.0.100
    11 Aug 11:24:27 ntpdate[3251]: step time server 10.0.0.100 offset -28798.796057 sec

    chrony

    使用前先关闭ntp服务:[root@ntp-server ~]# systemctl stop ntpd

    检查服务是否已经安装:

    [root@ntp-client ~]# rpm -qa chrony
    chrony-3.2-2.el7.x86_64

    编辑配置文件:[root@ntp-client ~]# vim /etc/chrony.conf

    chrony.conf的配置文件和ntp服务的配置文件其实差不多

    也只需要将server更改即可

    #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 ntp1.aliyun.com iburst

    测试:[root@ntp-server ~]# ntpdate 10.0.0.101

    配置文件解释

    restrict [ 客户端IP ] mask [ IP掩码 ] [参数]

    “客户端IP” 和 “IP掩码” 指定了对网络中哪些范围的计算机进行控制,如果使用default关键字,则表示对所有的计算机进行控制

    参数指定了具体的限制内容,常见的参数如下:

    参数 意义
     ignore 拒绝连接到NTP服务器
    nomodiy 客户端不能更改服务端的时间参数
    noquery 不提供客户端的时间查询
    notrap 不提供trap远程登录功能,trap服务是一种远程时间日志服务
    notrust 客户端除非通过认证,否则该客户端来源将被视为不信任子网
    nopeer 提供时间服务,但不作为对等体
    kod 向不安全的访问者发送Kiss-Of-Death报文


    KOD是NTPv4提出的一种全新的访问控制技术,主要用于服务器向客户端上提供状态报告和接入控制等信息。在服务器上使能KOD功能后,服务器会根据系统的运行状态向客户端发送DENY Kiss和RATE Kiss码


    当客户端接收到DENY Kiss码,客户端将断开与服务器的所有连接,并停止向服务器发送报文。


    当客户端接收到RATE Kiss码,客户端将立即缩短与该服务器的轮询时间间隔,且以后每次接收到RATE Kiss码,轮询时间间隔都会进一步缩短

    server host  [ key n ] [ version n ] [ prefer ] [ mode n ] [ minpoll n ] [ maxpoll n ] [ iburst ]

    其中host是上层NTP服务器的IP地址或域名,随后所跟的参数解释如下所示:

    key 表示所有发往服务器的报文包含有秘钥加密的认证信息,n是32位的整数,表示秘钥号
    version 表示发往上层服务器的报文使用的版本号,n默认是3,可以是1或者2
    prefer 如果有多个server选项,具有该参数的服务器优先使用
    mode 指定数据报文mode字段的值
    minpoll 指定与查询该服务器的最小时间间隔为2的n次方秒,n默认为6,范围为4-14
    maxpoll 指定与查询该服务器的最大时间间隔为2的n次方秒,n默认为10,范围为4-14
    iburst 当初始同步请求时,采用突发方式接连发送8个报文,时间间隔为2秒

    stratum(层次)

    0层的服务器采用的是原子钟、GPS钟等物理设备,stratum 1与stratum 0 是直接相连的

    往后的stratum与上一层stratum通过网络相连,同一层的server也可以交互

    stratum根据上层server的层次而设定(+1)

    其他

    [root@ntp-client ~]# timedatectl --help

    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  设置时间同步

  • 相关阅读:
    ER模型
    一道人人的笔试题
    关系代数运算
    推荐两个不错的CAD二次开发(.Net)手册
    CAD 致命错误
    CAD二次开发(.NET)之PaletteSet和Palette
    养生
    我看面向对象
    .NET中参数化查询数据库
    自定义按照index和key访问的List
  • 原文地址:https://www.cnblogs.com/zzzhbr/p/11334082.html
Copyright © 2020-2023  润新知