千家信息网

centos中怎么设置时区

发表于:2024-10-21 作者:千家信息网编辑
千家信息网最后更新 2024年10月21日,centos中怎么设置时区,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。timedatectl statuscentos7 都是这样
千家信息网最后更新 2024年10月21日centos中怎么设置时区

centos中怎么设置时区,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

timedatectl status

centos7 都是这样的设置时区的。

RHEL7.4 192.168.100.1 作为时间服务器,其它主机到这台来同步时间。

时间服务器安装及配置:
#yum install chrony --RHEL7默认已安装chrony,而没有安装ntpd.

#systemctl status chronyd --查看chronyd服务状态
#systemctl enable chronyd --开机启动

#vi /etc/chrony.conf --修改配置文件

#server 0.rhel.pool.ntp.org iburst --注释这4行,由于是内网环境,所以无法跟外部时间服务器进行时间同步。
#server 1.rhel.pool.ntp.org iburst
#server 2.rhel.pool.ntp.org iburst
#server 3.rhel.pool.ntp.org iburst
server 192.168.100.1 iburst --添加这一行,表示与本机同步时间

local stratum 10 --该行取消注释

# Allow NTP client access from local network.
allow 192.168.100.0/24 --允许哪些服务器到这台服务器来同步时间

#systemctl restart chronyd --修改完配置文件后,重启chronyd服务

[root@rhel1 ~]# netstat -antulp | grep chronyd
udp 0 0 0.0.0.0:123 0.0.0.0:* 705/chronyd
udp 0 0 127.0.0.1:323 0.0.0.0:* 705/chronyd
udp6 0 0 ::1:323 :::* 705/chronyd


1.显示系统的当前时间和日期

timedatectl
# timedatectl status
# 两条命令效果等同

[root@rhel1 ~]# timedatectl
Local time: Sat 2017-10-07 10:55:22 CST
Universal time: Sat 2017-10-07 02:55:22 UTC
RTC time: Sat 2017-10-07 02:55:22
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a

注意NTP synchronized值,只有时间服务器自己同步完成时间之后,才能为其它服务器提供时间同步服务。
2.设置日期与时间

timedatectl set-time "YYYY-MM-DD HH:MM:SS"
timedatectl set-time "YYYY-MM-DD"
timedatectl set-time "HH:MM:SS"
3.查看所有可用的时区

timedatectl list-timezones
# 亚洲
timedatectl list-timezones | grep -E "Asia/S.*"
4.设置时区

timedatectl set-timezone Asia/Shanghai
5.设置硬件时间

# 硬件时间默认为UTC
timedatectl set-local-rtc 1
# hwclock --systohc --localtime
# 两条命令效果等同

6.启用NTP时间同步
timedatectl set-ntp yes
# yes或no; 1或0也可以

问题:不明白这里启用ntp时间同步有啥意义。难道是说可以在时间服务器上都安装chronyd和ntpd服务来同时提供时间服务吗????如果设置为yes,手动设置时间则报错。看本文结尾。

7、查看时间源信息

[root@rhel1 ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* rhel1.rusky.com 10 7 377 22m -3603ns[ -19us] +/- 32us
[root@rhel1 ~]# 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
===============================================================================
^* rhel1.rusky.com 10 7 377 22m -3603ns[ -19us] +/- 32us
[root@rhel1 ~]#

客户端时间同步配置:

1.yum install chrony -y

2.修改配置文件

[root@rhel2 ~]# vi /etc/chrony.conf
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#server 0.rhel.pool.ntp.org iburst --注释这4行
#server 1.rhel.pool.ntp.org iburst
#server 2.rhel.pool.ntp.org iburst
#server 3.rhel.pool.ntp.org iburst
server 192.168.100.1 iburst ---添加该行,表示到这台服务器去同步时间。

3.重启chronyd并设置开机启动

[root@rhel2 ~]# systemctl restart chronyd.service
[root@rhel2 ~]# systemctl enable chronyd.service

4. 查看时间同步状态

[root@rhel2 ~]# timedatectl
Local time: Sat 2017-10-07 11:15:43 CST
Universal time: Sat 2017-10-07 03:15:43 UTC
RTC time: Sat 2017-10-07 03:02:26
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: yes --为yes表示已同步
RTC in local TZ: no
DST active: n/a

重启chronyd服务后,需要过几分钟才会自动完成同步。

如果需要手动,可使用下面的命令:

#ntpdate 192.168.100.1 如果同步失败,则可能 是服务端的时间未同步。即服务端NTP synchronized 为no。

============================

手动修改时间时,必须把NTP enabled 设置为no.

[root@rhel1 ~]# date
Sun Oct 8 11:00:51 CST 2017
[root@rhel1 ~]# timedatectl
Local time: Sat 2017-10-07 11:31:45 CST
Universal time: Sat 2017-10-07 03:31:45 UTC
RTC time: Sun 2017-10-08 03:01:57
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a

如果把NTP enabled 修改为yes时,表示开启自动同步时间,此时,是不能手动修改时间的。

如果为No时,表示关闭自动同步时间。

[root@rhel1 ~]# timedatectl set-ntp yes
[root@rhel1 ~]# timedatectl set-time "2017-10-08 11:00:50"
Failed to set time: Automatic time synchronization is enabled
[root@rhel1 ~]# timedatectl set-ntp no
[root@rhel1 ~]# timedatectl set-time "2017-10-08 11:00:50"
[root@rhel1 ~]# date
Sun Oct 8 11:00:51 CST 2017

关于centos中怎么设置时区问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。

时间 同步 服务 服务器 时区 配置 手动 问题 命令 文件 注释 效果 日期 更多 状态 硬件 帮助 解答 易行 简单易行 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 鼎捷易成pos数据库置疑 考公务员网络安全管理岗位怎么样 服务器被入侵后系统锁定 黄陂区标准网络安全维护出厂价 网络安全授权心得 数据库技术咨询服务合同 深圳市天动网络技术有限公司 800mhz低频网络技术 不动产网络安全学习 黄石哪里有软件开发方案 网络安全线上直播主题班会 软考数据库系统工程师pdf 数据库连接池连接数多少合适 浙江电力软件开发 网络安全协会东莞 看视频总是显示无法访问服务器 国内的期货软件开发 合肥网络安全培训机构排名 绘声绘色模板软件开发 什么云服务器好 2019国家网络安全博览会 数据库整形类型是什么 连接上了网络却无法连接到服务器 广东的网络安全的重大事件 高中 网络技术应用分册 岳阳学习网络安全 台州桌面软件开发设计 银行网络技术报告 软件开发如何做技术管理 服务器虚拟化涉及到哪几方面
0