NTP同步网络时间
废话不多说,直接执行如下命令即可
# yum install -y ntpdate
# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime;ntpdate -u ntp.api.bz && hwclock -w
时区操作
1. 查看时区
date "+%Z"
[root@lb-node1 ~]# ll /etc/localtime
lrwxrwxrwx. 1 root root 38 Oct 17 14:26 /etc/localtime -> ../usr/share/zoneinfo/America/New_York
[root@lb-node1 ~]#
如果采用直接cp的方法修改系统时区,那么就会把它所链接的文件修改掉,例如把美国的时区文件内容修改成了上海的时区内容,有可能会导致有些编程语言或程序在读取系统时区的时候发生错误
- 正确的修改方法是
1. CentOS6+ 、Ubuntu16+ 系统
# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
2. CentOS7、RHEL7、Scientific Linux 7、Oracle Linux 7系统,最好使用timedatectl命令
# timedatectl list-timezones |grep Shanghai #查找中国时区的完整名称
Asia/Shanghai
# timedatectl set-timezone Asia/Shanghai #其他时区以此类推
或者手动创建软链接
# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
同步网络时间
ntp常用服务器:
中国国家授时中心:210.72.145.44
NTP服务器(上海) :ntp.api.bz
美国: time.nist.gov
复旦: ntp.fudan.edu.cn
微软公司授时主机(美国) :time.windows.com
北京邮电大学 : s1a.time.edu.cn
清华大学 : s1b.time.edu.cn
北京大学 : s1c.time.edu.cn
台警大授时中心(台湾):asia.pool.ntp.org
# 同步网络时间
[root@lb-node2 ~]# ntpdate -u ntp.api.bz
12 Nov 19:47:29 ntpdate[6504]: adjust time server 47.98.146.45 offset 0.045074 sec
[root@lb-node2 ~]#
将系统时间写入硬件
[root@lb-node1 ~]# hwclock # 查看硬件时间
Mon 12 Nov 2018 07:55:37 PM CST -0.866369 seconds
[root@lb-node1 ~]# hwclock -w # 同步系统时间到硬件
# hwclock hctosys # 同步硬件时间到系统
# hwclock systoch # 同步系统时间到硬件