CentOS7/Ubuntu18系统时间同步ntp(转载)


转自  https://blog.csdn.net/u010226454/article/details/80896959



---centos7.2上搭建ntp服务器,并实现时间同步


对于容器编排系统,前段时间主要研究kubernetes,现在实验室要用dcos,所以在实验室集群上搭建了该系统。搭建dcos集群需要各机器时间同步,而实验室集群并没有联网,这里讲述下怎么搭建ntp服务器并进行时间同步。
使用的系统为centos7.2,机器使用情况如下表所示,这里以10.107.18.35为ntp server,其他的为client对时间进行同步,本文系统使用的是timedatectl命令。


机器ip    作用
10.107.18.35    单独一台boot节点
10.107.18.37    单独一台master节点
10.107.13.150    4台slave节点
10.107.18.34    4台slave节点
10.107.18.38    4台slave节点
10.107.18.39    4台slave节点
2. 具体流程


1.在集群中所有节点上安装ntp
yum install ntp
2.所有节点设置时区,这里设置为中国所用时间timedatectl set-timezone Asia/Shanghai
3.在server节点上启动ntp服务


systemctl start ntpd
systemctl enable ntpd
4.在server节点上设置现在的准确时间
timedatectl set-time HH:MM:SS
5.在server节点上设置其ntp服务器为其自身,同时设置可以接受连接服务的客户端,是通过更改/etc/ntp.conf文件来实现的,其中server设置127.127.1.0为其自身,新增加一个restrict段为可以接受服务的网段
这里写图片描述
6.重启ntpd服务
systemctl restart ntpd
7.在client节点上设置ntp服务器为server节点
这里写图片描述
8.在client节点上同步server的时间
ntpdate 10.107.18.35
9.client节点启动ntpd服务


systemctl start ntpd
systemctl enable ntpd
10.所有节点启动时间同步
timedatectl set-ntp yes



--解决上面的时间设置报错问题


Centos7中增加了timedate命令来时间对日期、时间和时区的控制。
[root@node1 ~]# 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@node1 ~]# timedatectl
       Local time: 日 2016-04-17 00:20:06 CST
   Universal time: 六 2016-04-16 16:20:06 UTC
         RTC time: 六 2016-04-16 16:20:06
        Time zone: Asia/Shanghai (CST, +0800)
      NTP enabled: yes
NTP synchronized: yes
  RTC in local TZ: no
       DST active: n/a


二、修改时间
执行如下命令修改时间:
timedatectl set-time HH:MM:SS
备注:同时修改hardware clock和system clock.
例子:
[root@node1 ~]# timedatectl set-time 09:57:30
[root@node1 ~]# date
2016年 04月 17日 星期日 09:57:32 CST


三、修改日期
执行如下命令修改日期:
timedatectl set-time YYYY-MM-DD
  备注:修改日志不同时修改时间,将重置现在的时间为00:00:00.
例子:
[root@node1 ~]# timedatectl set-time 2016-04-18
[root@node1 ~]# date
2016年 04月 18日 星期一 00:00:01 CST


四、同时修改日期和时间
执行如下命令同时修改日期和时间:
timedatectl set-time 'YYYY-MM-DD HH:MM:SS'


例子:
[root@node1 ~]# timedatectl set-time '2016-04-18 10:01:20'
[root@node1 ~]# date
2016年 04月 18日 星期一 10:01:21 CST


五、修改时区
执行如下命令显示可用的时区:
timedatectl list-timezones
[root@node1 ~]# timedatectl list-timezones
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Bamako
Africa/Bangui
Africa/Banjul
...
执行如下命令设置使用的时区:
timedatectl set-timezone time_zone
[root@node1 ~]# timedatectl set-timezone Asia/Shanghai
[root@node1 ~]# date
2016年 04月 18日 星期一 10:05:28 CST
[root@node1 ~]# ls -lrt /etc/localtime
lrwxrwxrwx 1 root root 35 4月  17 00:08 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai


六、同步时间到一个远程服务器
timedatectl命令可以用来控制是否开启NTP,开启NTP将启动chronyd或者ntpd服务,依赖于被安装的那个。
timedatectl set-ntp boolean
开启:
timedatectl set-ntp yes
关闭:
timedatectl set-ntp no
备注:
执行set-ntp时会同时开启或关闭ntpd或者chronyd服务。但是ntpd服务和chronyd可以通过systemctl命令来单独控制,不是必须使用timedatectl来进行控制。
如果使用set-ntp是yes的状态(即:timedatectl命令中NTP enabled状态显示为yes,那么将不能同时使用set-time来修改时间。
[root@node3 ~]# timedatectl
       Local time: 一 2016-04-18 10:25:48 CST
   Universal time: 一 2016-04-18 02:25:48 UTC
         RTC time: 六 2016-04-16 17:09:34
        Time zone: Asia/Shanghai (CST, +0800)
      NTP enabled: yes
NTP synchronized: no
  RTC in local TZ: no
       DST active: n/a
[root@node3 ~]# timedatectl set-time '2016-04-18 10:25:58'
Failed to set time: Automatic time synchronization is enabled
[root@node3 ~]# timedatectl set-ntp no
[root@node3 ~]# timedatectl
       Local time: 一 2016-04-18 10:26:16 CST
   Universal time: 一 2016-04-18 02:26:16 UTC
         RTC time: 六 2016-04-16 17:10:02
        Time zone: Asia/Shanghai (CST, +0800)
      NTP enabled: no
NTP synchronized: no
  RTC in local TZ: no
       DST active: n/a
[root@node3 ~]# timedatectl set-time '2016-04-18 10:26:28'


-------------------------------------------------------------------------------------

CentOS6

1.时区修改

[root@gisserver3 ~]# tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent or ocean.
  1) Africa
  2) Americas
  3) Antarctica
  4) Arctic Ocean
  5) Asia
  6) Atlantic Ocean
  7) Australia
  8) Europe
  9) Indian Ocean
10) Pacific Ocean
11) none - I want to specify the time zone using the Posix TZ format.
#? 5

……


2.时间同步

ntpdate 0.arch.pool.ntp.org

posted @ 2019-07-01 14:33  ParamousGIS  阅读(1645)  评论(0编辑  收藏  举报