NTP 是什么?
NTP 是网络时间协议(Network Time Protocol),它用来同步网络设备【如计算机、手机】的时间的协议。
阿里云的 NTP 服务器:- ntp1.aliyun.com
- ntp2.aliyun.com
- ntp3.aliyun.com
- ntp4.aliyun.com
一、timedatectl命令查看当前时区:
[root@localhost Asia]# timedatectl
Local time: Tue 2018-04-24 07:29:13 CST
Universal time: Mon 2018-04-23 23:29:13 UTC
RTC time: Mon 2018-04-23 23:29:13
Time zone: America/New_York (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
[root@localhost Asia]#
二、timedatectl命令详解
[root@localhost Asia]# 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
三、查看所有时区timedatectl list-timezones
[root@localhost Asia]# timedatectl list-timezones|grep Shanghai
Asia/Shanghai
[root@localhost Asia]#
四、改成上海时区timedatectl set-timezone Asia/Shanghai
[root@localhost Asia]# timedatectl status
Local time: Tue 2018-04-24 07:36:56 CST
Universal time: Mon 2018-04-23 23:36:56 UTC
RTC time: Mon 2018-04-23 23:36:56
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
[root@localhost Asia]#
五、同步时间服务器
安装:#yum -y install ntpdate
# systemctl enable ntpd
#systemctl start ntpd
#ntpdate ntp1.aliyun.com
设置NTP开启timedatectl set-ntp yes
设置NTP关闭timedatectl set-ntp no
timedatectl命令也可以设置时间和日期
设置日期格式timedatectl set-time YYYY-MM-DD
设置时间格式timedatectl set-time HH:MM:SS