时区 - timezone

检查系统当前时区。

  1. $ date -R
  2. Tue, 21 Apr 2020 19:01:29 -0700

如果显示的不是+0800,删除localtime文件夹后,再关联一个正确时区的链接过去。

  1. $ rm -rf /etc/localtime
  2. $ ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

时间同步 - ntp

  • https://www.cnblogs.com/williamjie/p/10768657.html
  • https://www.cnblogs.com/shanhua-fu/p/9281040.html
    1. $ ntpdate pool.ntp.org
    修改ntp配置文件,修改行“#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap”为自己的网段。
    注释掉
    server 0.centos.pool.ntp.org
    server 1.centos.pool.ntp.org
    server 2.centos.pool.ntp.org
    修改下面几行,去掉下面两行的注释,如果没有,需要手动添加。
    # Undisciplined Local Clock. This is a fake driver intended for backup
    # and when no outside source of synchronized time is available.
    #server 127.127.1.0 # local clock
    #fudge 127.127.1.0 stratum 10 ```bash $ vi /etc/ntp.conf

    For more information about this file, see the man pages

    ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

driftfile /var/lib/ntp/drift

Permit time synchronization with our time source, but do not

permit the source to query or modify the service on this system.

restrict default kod nomodify notrap nopeer noquery restrict -6 default kod nomodify notrap nopeer noquery

Permit all access over the loopback interface. This could

be tightened as well, but to do so would effect some of

the administrative functions.

restrict 127.0.0.1 restrict -6 ::1

Hosts on local network are less restricted.

restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

restrict 192.168.32.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

server 1.centos.pool.ntp.org

server 2.centos.pool.ntp.org

broadcast 192.168.1.255 autokey # broadcast server

broadcastclient # broadcast client

broadcast 224.0.1.1 autokey # multicast server

multicastclient 224.0.1.1 # multicast client

manycastserver 239.255.254.254 # manycast server

manycastclient 239.255.254.254 autokey # manycast client

Undisciplined Local Clock. This is a fake driver intended for backup

and when no outside source of synchronized time is available.

server 127.127.1.0 # local clock fudge 127.127.1.0 stratum 10

Enable public key cryptography.

crypto

includefile /etc/ntp/crypto/pw

Key file containing the keys and key identifiers used when operating

with symmetric key cryptography.

keys /etc/ntp/keys

Specify the key identifiers which are trusted.

trustedkey 4 8 42

Specify the key identifier to use with the ntpdc utility.

requestkey 8

Specify the key identifier to use with the ntpq utility.

controlkey 8

Enable writing of statistics records.

statistics clockstats cryptostats loopstats peerstats

  1. 重启ntp服务
  2. ```bash
  3. $ systemctl start ntpd.service # 启动服务
  4. $ systemctl enable ntpd.service # 开机自动启动
  5. $ # CentOS7 以下版本
  6. $ service ntpd start # 启动服务
  7. $ chkconfig ntpd on # 开机自动启动

集群中的其他服务器节点,需要同步这台机器的时间。
首先关闭,这两台计算机的ntp服务。

  1. $ systemctl stop ntpd.service # 关闭服务
  2. $ systemctl disable ntpd.service # 取消开机自动启动
  3. $ # CentOS7 以下版本
  4. $ service ntpd stop # 关闭服务
  5. $ chkconfig ntpd off # 取消开机自动启动

查看ntp服务状态,服务进程id

  1. $ systemctl status ntpd # 查看ntp服务状态
  2. $ pgrep ntpd # 查看进程id
  3. $ # CentOS7 以下版本
  4. $ service ntpd status # 查看ntp服务状态

同步第一台服务器的时间,并制定计划任务,周期性同步时间,重启定时任务。

  1. $ ntpdate master # 向master同步时间
  2. $ crontab -e # root用户,制定定时任务
  3. */10 * * * * /usr/sbin/ntpdate master
  4. $ systemctl restart crond.service # 重启定时任务
  5. $ # CentOS7 以下版本
  6. $ service crond restart