内网搭建NTP时间同步服务器(centos7)

一、配置centos7本地源

[root@node2 ~]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
[root@node2 ~]# timedatectl set-timezone Asia/Shanghai #设置时区为东8区
①、准备工作
mkdir /iso
cd /iso
[root@node2 iso]# wget https://mirror.tuna.tsinghua.edu.cn/centos/7/isos/x86_64/CentOS-7-x86_64-Everything-2003.iso

下载完成centos7的光盘文件后

②、挂载本地yum源
[root@node2 iso]# mkdir -p /mnt/cdrom/
[root@node2 iso]# mount -t iso9660 CentOS-7-x86_64-Everything-2003.iso  /mnt/cdrom/

cat > /etc/yum.repos.d/myiso.repo << EOF
#本源的名字(不能和其他重复)
[CentOS7.8-local]
name=CentOS7.8-local


#步骤2中挂载镜像创建的目录
baseurl=file:///mnt/cdrom

#启动yum源: 1-启用 0-不启用
enabled=1 

#安全检测:  1-开启 0-不开启
gpgcheck=0
EOF
③、更新yum源
yum clean all
yum makecache
yum repolist
④、利用已经做好的iso-yum本地源安装http 服务方便制作共享软件源
yum install httpd -y
systemctl start httpd ##开启http服务
systemctl enable httpd ##设置开机启动http服务
systemctl status httpd
⑤、解挂载
umount /mnt/cdrom/
⑤、重新挂载
mkdir /var/www/html/myshare 
mount /iso/CentOS-7-x86_64-Everything-2003.iso /var/www/html/myshare
⑥、刷新服务
systemctl restart httpd
⑦、添加repo仓库指向文件,让系统在寻找yum源的时候可以找到设置的http分享的yum源。
cd /etc/yum.repos.d/
rm -rf *
cat > /etc/yum.repos.d/myshare.repo << EOF 
[share]
name=myshare
baseurl=http://10.0.1.35/myshare
enabled=1 
gpgcheck=0
EOF

yum clean all
yum makecache
yum repolist
⑧、 yum源做好开机自启动
chmod +x /etc/rc.d/rc.local
mkdir -p /usr/local/script
cat > /usr/local/script/autostart.sh << EOF
mount /iso/CentOS-7-x86_64-Everything-2003.iso /var/www/html/myshare
EOF

chmod +x /usr/local/script/autostart.sh
cat >> /etc/rc.d/rc.local << EOF
/usr/local/script/autostart.sh
EOF

二、开始安装时间同步服务软件

1、查看服务器是否安装ntp,系统默认安装ntpdate;
[root@node2 ~]#  rpm -qa | grep ntp
2、安装ntp ntpdate,其中ntpdate默认安装,可以只安装ntp;
yum install ntp ntpdate -y
3、查看是否已安装完成,与第1步对比
[root@node2 ~]#  rpm -qa | grep ntp
ntp-perl-4.2.6p5-29.el7.centos.noarch
ntpdate-4.2.6p5-29.el7.centos.x86_64
ntp-doc-4.2.6p5-29.el7.centos.noarch
ntp-4.2.6p5-29.el7.centos.x86_64
4、查看ntp服务器状态
[root@node2 ~]# systemctl status ntpd
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
5、修改配置文件,使该NTP服务器在不联网的情况下,使用本服务器的时间作为同步时间
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
  • 添加两行代码
#允许局域网内所有客户端连接到这台服务器同步时间.但是拒绝让他们修改服务器上的时间
restrict 0.0.0.0 mask 255.255.255.0 nomodify notrap
server 127.127.1.0
fudge 127.127.1.0 stratum 10
6、启动ntp服务
systemctl start ntpd
systemctl status ntpd
systemctl enable ntpd
7、查看是否同步
[root@node2 ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*LOCAL(0)        .LOCL.          10 l   18   64    7    0.000    0.000   0.000

三、客户端配置

一、创建时间同步源
cat > /etc/yum.repos.d/myshare.repo << EOF 
[share]
name=myshare
baseurl=http://10.0.1.35/myshare
enabled=1 
gpgcheck=0
EOF

yum clean all
yum makecache
yum repolist
二、安装时间同步客户端软件
yum install ntp ntpdate -y
三、开始时间同步
ntpdate -u 10.0.1.35
四、写入定时时间同步
crontab -e
* 3 * * * /usr/sbin/ntpdate -u 10.0.1.35 >dev/null 2>&1
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值