ntp服务器搭建

本文详细介绍了如何在Linux系统中手动修改时区为CST,包括使用`timedatectl`和`ln`命令,以及配置NTP服务(chrony)进行时间同步,同时探讨了可能遇到的问题如服务器不可达和防火墙设置。
摘要由CSDN通过智能技术生成

1、手动修改时区

CST可以为如下4个不同的时区的缩写:
美国中部时间:Central Standard Time (USA) UT-6:00
澳大利亚中部时间:Central Standard Time (Australia) UT+9:30
中国标准时间:China Standard Time UT+8:00
古巴标准时间:Cuba Standard Time UT-4:00

小结:
UTC:世界标准时间
CST:中国标准时间。
CST = UTC + 8(小时)

查看系统时间:
[root@localhost ~]# datehi
Mon Oct 12 14:23:53 HKT 2020

修改时区为CST:
[root@localhost ~]# timedatectl set-timezone Asia/Shanghai
[root@localhost ~]# date
Mon Oct 12 14:24:20 CST 2020
或者 将时区信息-Asia/Shanghai,生产软链接到/etc/localtime
[root@localhost ~]# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@localhost ~]# ll /etc/localtime 
lrwxrwxrwx. 1 root root 35 Oct 12 14:24 /etc/localtime -> /usr/share/zoneinfo/Asia/Shanghai

2、手动配置时间

[root@localhost ~]#  date -s "2024-03-28 12:12:12"
Mon Oct 12 15:12:12 CST 2020
[root@localhost ~]# hwclock -w

3、NTP时间服务同步

ntp服务器与客户端搭建

chrony是linux7开始使用的时间同步服务,系统内置。比ntp更加快捷,简单实际配置中总是出现各种问题,排除时,首先关闭selinux,关闭防火墙。
chrony是两个用来维持计算机系统时钟准确性的程序,这两个程序命名为chronyd和chronyc。
chronyd是一个在系统后台运行的守护进程。他根据网络上其他时间服务器时间来测量本机时间的偏移量从而调整系统时钟。对于孤立系统,用户可以手动周期性的输入正确时间(通过chronyc)。
chronyd决定计算机快慢的比例,并加以纠正。chronyd实现了NTP协议并且可以作为服务器或客户端。
chronyc是用来监控chronyd性能和配置其参数的用户界面。他可以控制本机及其他计算机上运行的chronyd进程。

4、配置NTP服务器

服务端:172.16.2.232
安装服务

# yum install ntp -y

修改配置文件

# vi /etc/ntp.conf 
#添加一个网段
restrict 172.16.0.0 mask 255.255.0.0 nomodify notrap

#时间同步服务器设置成自己
server 127.127.1.0 iburst

重启服务,并设置成开机启动, 注:其他同类开启的选项应选择关闭即注释

# systemctl start ntpd
# systemctl enable ntpd
[root@localhost ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

查看端口

# netstat -ntupl | grep 123
[root@localhost ~]# netstat -ntupl | grep 123
udp        0      0 172.16.2.232:123        0.0.0.0:*                           7865/ntpd           
udp        0      0 127.0.0.1:123           0.0.0.0:*                           7865/ntpd           
udp        0      0 0.0.0.0:123             0.0.0.0:*                           7865/ntpd           
udp6       0      0 fe80::3b37:7cee:ac9:123 :::*                                7865/ntpd           
udp6       0      0 ::1:123                 :::*                                7865/ntpd           
udp6       0      0 :::123                  :::*                                7865/ntpd  

4. 配置NTP客户端

客户端:172.16.2.226
安装服务

#yum install chrony -y

修改配置文件

vi /etc/chrony.conf

#设置同步的服务器

server 172.16.2.232 iburst

#开启同步许可

# Allow NTP client access from local network.
#allow 172.16.0.0/16  #可不开启

#开启同步层

# Serve time even if not synchronized to a time source.
#local stratum 10 #可不开启

重启服务

#systemctl restart chronyd

查看状态

#systemctl status chronyd

查看时间同步源

# chronyc sources -v
[root@localhost fail2ban]# chronyc sources -v
210 Number of sources = 1

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \date
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 172.16.2.232                  6   6    17    56  +3728ns[  +18us] +/-   12ms

^* 同步成功
^? 连接不可达。。。。转第四步问题解决

timedatectl

[root@localhost fail2ban]# timedatectl
      Local time: Mon 2020-10-12 15:37:41 CST
  Universal time: Mon 2020-10-12 07:37:41 UTC
        RTC time: Mon 2020-10-12 07:37:41
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

5、问题处理

[root@localhost fail2ban]# chronyc sources -v
210 Number of sources = 1

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^? 172.16.2.232                  0   6     0     -     +0ns[   +0ns] +/-    0ns
这是服务器不可达,时间未同步的反馈,
1. 考虑NTP服务器防火墙是否拦截,可开放123端口
2. 时区配置是否相同,如是否都是CST
  • 13
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值