问题导读:
1.ntp的作用是什么?
2.ntp的英文全拼是什么?
3.如何重启ntp?
4.客户端如何与服务端达到时间同步?
ubuntu server ntp时间同步服务器安装及使用
一、服务端
1查询和安装
sudo apt-get install ntp
2 安装后默认启动服务,如果没有启动,启动之。sudo /etc/init.d/ntp start
3 sudo vi /etc/ntp.conf 修改为如下
restrict default nomodify notrap noquery
restrict 127.0.0.1
restrict 10.91.0.0 mask 255.255.255.0 nomodify
server 0.pool.ntp.org
server 1.pool.ntp.org
server 2.pool.ntp.org
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
driftfile /var/lib/ntp/drift
broadcastdelay 0.008
keys /etc/ntp/keys
4 重启ntp服务
sudo /etc/init.d/ntp restart
二、客户端
1 使用ntpdate命令,如果不存在这个命令,则先安装apt-get install ntp
sudo /usr/sbin/ntpdate 10.91.0.10 //即使用ip为10.91.0.10的ntp服务器同步时间
2 设置定时同步。
sudo vi /etc/crontab
30 01 * * * /usr/sbin/ntpdate 10.91.0.10
系统便会在每天早上1点30分自动将系统时间同步到ntp服务器的时间。
当然这里crontab的时间是指客户端的时间,同步后等同于ntp服务器的时间。
Centos用户的ntp配置如下
检查时间服务是否安装
rpm -qa ntp
安装
yum insatll ntp
更新配置文件
vim /etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
server 0.centos.pool.ntp.org
server 1.centos.pool.ntp.org
server 2.centos.pool.ntp.org
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
完事启动
service ntpd start
检查是否同步完成
watch ntpq -p
设置开机自启
chkconfig --leviel 35 ntpd on
注意防火墙问题
硬件时间从根本上讲是CMOS时钟
linux查看硬件时间:hwclock --show
linux查看系统时间:date -R
centos VPS的时间及时区同步。
系统时间部分:
1. 编辑时区配置文件:vi /etc/sysconfig/clock
ZONE="Asia/Shanghai"
UTC=false #把UTC设置为false关闭状态
ARC=false
2.运行如下命令
#删除默认配置
rm -rf /etc/localtime
#重新指定配置
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#同步系统时间
ntpdate -u pool.ntp.org
3.硬件时间部分:
#将硬件时间调整成与目前的系统时钟一致。
hwclock --systohc
或clock --systohc
#强制系统时间写入CMOS中防止重启失效
hwclock -w
或clock -w
以上步骤虽然成功了但是我这hyper-v架构的centos系统时间走的不准,1天能快几分钟的样子。
用crontab来同步一下时间即可解决。
#编辑计划任务表
vi /etc/crontab
#表中最后添加下面这行内容
30 5 * * * root /usr/sbin/ntpdate -u pool.ntp.org && /sbin/clock --systohc
#每天5点半同步时间,clock --systohc是硬盘时间同步系统时间的意思。