目录
前言
环境:centos 7.9
在生产服务器中,我们需要保证服务器的时间是最准确的,所以需要进行时间同步,时间同步有2中方式,一种是:手动同步,另外一种是后台守护进程自动同步。
时间同步服务主要有2个软件可以实现,ntp和chrony,ntp是centos6默认的时间同步服务,在centos7之后,不在使用默认的ntp服务,改用chrony实现时间同步。
ntp的缺点:ntp启动之后,并不是立即同步时间为时间服务器的时间,比如,当前客户端是18:00,而时间服务器是18:15,当客户端启动ntp服务之后时间并不是立即同步为18:15,而是慢慢的慢慢的同步一点点时间,直到与时间服务器时间相同,之所以ntp这样设计,是基于这样的考虑,如果立即同步客户端的时候,那么之间的15分钟的日志记录将会是空白的。
ntpdate 命令手动同步时间
ntpdate 命令用于时间同步,可以将当前服务器的时间同步为时间服务器的时间(首先得有一个时间服务器),用法如下:
#安装ntpdate命令
yum install ntpdate
#手动同步时间,192.168.0.2为时间服务器IP
#所谓时间服务器,就是安装有某个可以同步时间的应用服务服务器,不能是任意服务器
ntpdate 192.168.0.2
内网环境下,通常,ntpdate 命令会配合crontab 来定时同步时间,如下:
0 12 * * * * /usr/sbin/ntpdate 192.168.0.2
ntpd服务自动同步时间(联网自动同步时间)
假如你的服务器是可以链接互联网的,那么也可以开启ntpd服务让系统自动进行互联网时间同步,如下:
systemctl enable --now ntpd
systemctl status ntpd
#ntpd服务默认配置文件/etc/ntp.conf里面配置了其去同步外网时间服务器
搭建ntp内网时间服务器(内网ntp时间服务器-搭建失败)
以上都假设服务器可以链接外网或者有一台时间服务器,但是在企业内网环境下,服务器不允许连接外网,所以需要自己搭建一台内网时间服务器,但是本身时间服务器自身的时间需要同步外网服务器的时候,所以时间服务器要可以连接外网,这时候时间服务器即作为客户端又做为服务端,客户端的意思是时间服务器要去同步外网时间,服务端的意思是自己要提供时间给内网其他机器同步。
#关闭防火墙selinux,因为内网其他客户端需要来连接同步本机时间
systemctl disable --now firewalld
sed -ri 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
#安装ntp服务
yum install -y ntp
修改ntp.conf配置文件
安装完ntp服务之后,需要编辑ntp服务的配置文件,如下:
[root@node2 ~]# cat /etc/ntp.conf
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift #保持默认
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
# restrict default nomodify notrap nopeer noquery #注释掉
# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1 #保持默认
restrict ::1 #保持默认
# Hosts on local network are less restricted.
restrict 192.166.1.0 mask 255.255.0.0 nomodify notrap #这句是新加的,表示允许内网其它网段机器同步本机时间
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#server 参数用于定义上层时间服务器,即要与上层哪个时间服务器同步时间
server 0.centos.pool.ntp.org iburst #0.centos.pool.ntp.org是centos官方时钟服务器的域名
server 1.centos.pool.ntp.org iburst #1.centos.pool.ntp.org是centos官方时钟服务器的域名
server 2.centos.pool.ntp.org iburst #2.centos.pool.ntp.org是centos官方时钟服务器的域名
server 3.centos.pool.ntp.org iburst #3.centos.pool.ntp.org是centos官方时钟服务器的域名
server cn.pool.ntp.org iburst #这是中国地区的NTP服务器池
server ntp.aliyun.com iburst #这是阿里云提供的NTP服务器
server ntp1.aliyun.com iburst #这是阿里云提供的NTP服务器
server ntp2.aliyun.com iburst #这是阿里云提供的NTP服务器
server 127.0.0.1 iburst
346




被折叠的 条评论
为什么被折叠?



