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]# timedatectlLocal time: Mon 2020-10-12 15:37:41 CSTUniversal time: Mon 2020-10-12 07:37:41 UTCRTC time: Mon 2020-10-12 07:37:41Time zone: Asia/Shanghai (CST, +0800)NTP enabled: yes
NTP synchronized: yesRTC in local TZ: noDST 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