Centos7 NTP 服务器和客户端配置(含离线状态)附赠手动配置系统时间
前情提要:临时被通知要改服务器时间,本想偷懒手动更改系统时间,后面又说统一成NTP服务器时间。一定需要询问清楚公司NTP服务器IP地址 (文章最后有彩蛋,手动配置系统时间)
一、搭建NTP服务器
1、查看服务器、客户端操作系统版本
[root@localhost ~]#cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
2、查看服务器是否安装ntp,系统默认安装ntpdate;
[root@web ~]#rpm -qa | grep ntp
fontpackages-filesystem-1.44-8.el7.noarch
ntpdate-4.2.6p5-28.el7.centos.x86_64
python-ntplib-0.3.2-1.el7.noarch
3、安装ntp ntpdate,其中ntpdate默认安装,可以只安装ntp;
[root@web ~]#yum install ntp ntpdate -y ###在线安装
[root@web ~]#rpm -ivh *.rpm --force --nodeps ###离线安装 也可单独安装包
#离线安装包清单,上传至服务器
ntp-4.2.6p5-28.el7.centos.x86_64.rpm ###重点必须安装
ntpdate-4.2.6p5-28.el7.centos.x86_64.rpm
openssl-1.0.2k-16.el7_6.1.x86_64.rpm
openssl-libs-1.0.2k-16.el7_6.1.x86_64.rpm
#注意 如果安装时报错,请检查对应安装包的版本和库文件
autogen-libopts-5.18-5.el7.x86_64.rpm ##可能用上的包
4、查看是否已安装完成,与第2步对比
[root@web ~]#rpm -qa | grep ntp
fontpackages-filesystem-1.44-8.el7.noarch
ntpdate-4.2.6p5-28.el7.centos.x86_64
ntp-4.2.6p5-28.el7.centos.x86_64
python-ntplib-0.3.2-1.el7.noarch
5、查看ntp服务器状态,两条命令效果一样
[root@web ~]#systemctl status ntpd
● ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
或
[root@web ~]#service ntpd status
Redirecting to /bin/systemctl status ntpd.service
● ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
6、修改配置文件,使该NTP服务器在不联网的情况下,使用本服务器的时间作为同步时间
[root@web ~]#vim /etc/ntp.conf
####把如下四行代码注释掉
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
##在下面再添加一行
server 127.127.1.0 iburst # 网络同步服务器
7、启动ntp服务
[root@web ~]#ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #修改时区(改为中国标准时区)
[root@web ~]#systemctl start ntpd
或
[root@web ~]#service ntpd start
8、再次查看服务器状态
[root@web ~]#systemctl status ntpd
● ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled)
Active: active (running)
或
[root@web ~]#service ntpd status
Redirecting to /bin/systemctl status ntpd.service
● ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled)
Active: active (running) since
9、设置开机启动
[root@web ~]#systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpdservice.
10、设置防火墙,打开udp123端口 123端口为ntp服务使用端口,必须打开
[root@web ~]#firewall-cmd --permanent --add-port=123/udp
或
[root@web ~]#firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=0.0.0.0/0 port protocol=tcp port=123
[root@web ~]#firewall-cmd --reload
Success
11、查看防火墙已打开端口
[root@web ~]#iptables -L -n #n输入具体端口号
12、查看是否同步
[root@db3 ~]# ntpstat
或
[root@web ~]#ntpq -p
remote refid st t when poll reach delay offset jitter
=======================================================
*LOCAL(0) .LOCL. 5 l 20 64 7 0.000 0.000 0.000
二、客户端配置
前5步与服务器一致
6、修改配置文件,将刚刚搭建好的NTP服务器作为客户端上游时间服务器
[root@web ~]#vim /etc/ntp.conf
#注释掉其他上游时间服务器
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
#配置上游时间服务器为本地的ntpd Server服务器
server 68.219.63.2 #该IP是ntp服务器的IP
#配置允许上游时间服务器主动修改本机的时间
restrict 68.219.63.2 nomodify notrap noquery
###Fudge 68.219.63.2 stratum 10 #网友说是这个命令可以试试
7、与本地ntpd Server同步一下
[root@web ~]#ntpdate -u 192.168.0.163
8、启动ntp服务
[root@web ~]#systemctl start ntpd
或
[root@web ~]#service ntpd start
9、设置开机启动
[root@db3 ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
10、查看状态
[root@localhost ~]# ntpstat
[root@localhost ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
=======================================================
*192.168.0.163 LOCAL(0) 11 u 21 64 377 0.068 -0.824 0.367
彩蛋:附上手动更改系统时间步骤
[root@localhost ~]# date #查看当前系统时间
[root@localhost ~]#date -s "2021-1-14 12:00:00" #修改当前系统时间
[root@localhost ~]#hwclock --show #查看硬件时间
[root@localhost ~]#hwclock --set --date "2021-1-14 12:00:00" #修改硬件时间
[root@localhost ~]#hwclock --hctosys #同步系统时间和硬件时间
[root@localhost ~]#clock -w #保存时钟
[root@localhost ~]#init 6或reboot #重启后便发现系统时间被修改
总结:本文主要参考大佬文章,其中少部分为自己个人看法和配置,操作时只需要配置客户端未配置服务端;感谢CSDN各位大佬的贡献,妥妥的让我成为大自然搬运工。
参考大佬文献: