由于在很多的时候我们服务器上的时间总是与准确的时间差一些吧!因此,我们总希望能够时间同步。具体的操作步骤如下(以Centos7为例):
1、查看系统当前时间,从而判断时间是否准确?
[root@localhost ~]# date
2、查看Linux系统时间同步命令ntpdate是否安装?如果提示“-bash: ntpdate: command not found”则未安装。(ntpdate是一个linux时间同步服务软件,一般默认情况下是未安装。)
ntpdate
3、安装Linux系统时间同步服务软件:ntpdate,如果已安装,则跳过此步骤。
[root@localhost ~]# yum -y install ntpdate
4、Linux系统时间同步:让当前服务器同步到网络时间,从而来更新当前服务器的时间。以下命令表示的是让当前服务器时间同步到ntp1.aliyun.com。例如:时间服务器地址还有time.nist.gov、time.nuri.net等等。
[root@localhost ~]# ntpdate ntp1.aliyun.com
5、Linux硬件时间同步:修改服务器硬件时间映射到我们的系统时间,也可以说系统时间同步到硬件时间,防止系统重启后时间还原。
[root@localhost ~]# hwclock --systohc
或
[root@localhost ~]# hwclock -w
6、查看Linux系统时间和硬件时间是否同步?不出意外的话,现在date和hwclock现实的时间均为internet时间,显示的时间应该一样的。
[root@localhost ~]# date
[root@localhost ~]# hwclock
7、Linux定时执行时间同步任务,所以我们利用crontab -e 来添加定时任务,以下命令表示:每隔一个小时同步一下internet时间。此步骤完全可以忽略,看个人需要的情况来吧!
* */1 * * * root ntpdate ntp1.aliyun.com;hwclock -w