Centos7设置内网时间服务器(NTP)–双网卡(内外网)

前言

  公司内网之前一直用的windows的时间同步服务器,但是最近发现VMware主机同步这个windows的时间服务器因为兼容性问题导致VM主机时间不准,故而想要搭建一台linux的时间同步服务器。

背景:

1、操作系统: 时间服务器(Centos7),客户端(linux、windows)
2、时间服务器双网卡:eth0        ------内网(10.10.10.100)
                  eth1         -----外网(192.168.1.100)

一、配置双网卡

1、vi /etc/sysconfig/network-scripts/ifcfg-eth0 内网网卡修改为以下数据

BOOTPROTO=static        -----静态分配
ONBOOT=yes              -----开机/重启激活网卡
IPADDR=10.10.10.100    -----IP地址
PREFIX=24               -----子网掩码(写成NETMASK=255.255.255.0也行)

2、vi /etc/sysconfig/network-scripts/ifcfg-eth1 外网网卡修改为以下数据

BOOTPROTO=static        -----静态分配
ONBOOT=yes              -----开机/重启激活网卡
IPADDR=192.168.1.100    -----IP地址
PREFIX=24               -----子网掩码(写成NETMASK=255.255.255.0也行)
GATEWAY=192.168.1.2     -----网关

3、重启网卡

systemctl restart network

4、添加临时路由测试内外网网络

#首先设置默认网关,让所有IP包默认情况下均通过 192.168.1.2进行转发。
route add default gw 192.168.1.2

#添加访问内网的路由,让10段IP包通过内外网关10.10.10.2进行转发
route add -net 10.10.10.0 netmask 255.255.255.0 gw 10.10.10.2

5、添加永久路由
CentOS7永久静态路由需要写到 /etc/sysconfig/network-scripts/route-eth0文件中,故在/etc/sysconfig/network-scripts/下,新建文件名为route-eth0的文件,(eth0代表网卡名)

#添加两条静态路由:(网卡的设备名叫eth0)----因为上面设置的默认路由是外网的,所以只需要添加内网的静态路由
[root@centos7 ~]# vim /etc/sysconfig/network-scripts/route-eth0
10.10.10.0/24 via 10.10.10.2 dev eth0
10.25.250.0/24 via 10.10.10.2 dev eth0

#查看路由表
[root@centos7 ~]# route  -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.25.250.0     10.10.10.2      255.255.255.0   UG    0      0        0 eth0
10.10.10.0      10.10.10.2      255.255.255.0   U     0      0        0 eth0
10.15.150.0     10.10.10.2      255.255.255.0   UG    0      0        0 eth0

centos双网卡重启断网_centos双网卡重启断网

二、安装NTP服务

1.使用yum安装

代码如下(示例):

yum install ntp -y    -----------安装完成最后会显示   complete!

systemctl start ntpd     ----启动服务

systemctl enable ntpd    ----服务自启

2.配置NTP服务端

2.1 查找当前地区,最适合的时间服务器

步骤一:打开网站:http://www.pool.ntp.org/zone/asia

步骤二:复制自动推荐的最合适的同步服务器

centos双网卡重启断网_内网_02


2.2 编辑ntp配置文件 /etc/ntp.conf

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

然后粘贴之前复制的时间同步地址

centos双网卡重启断网_centos双网卡重启断网_03

2.启动ntp服务

systemctl start ntpd #启动
systemctl enable ntpd.service #设置开机启动服务

3、开启防火墙端口

ntp服务使用udp-123端口

firewall-cmd --permanent --add-port=123/udp
 
firewall-cmd --reload

三、客户端配置

1、安装ntp服务,与服务端安装方式相同

yum install ntp -y    -----------安装完成最后会显示   complete!

systemctl start ntpd     ----启动服务

systemctl enable ntpd    ----服务自启

2、配置客户端
修改配置文件:

vim /etc/ntp.conf

#注释默认的server,配置ntp服务端地址:(以下语句加入配置文件/etc/ntp.conf)
server 10.10.10.100 iburst

启动服务:systemctl start ntpd

服务自启:systemctl enable ntpd

3、ntpstat查看ntp服务器是否正确(启动服务后大概10多分钟才能查看到,生效比较慢)

centos双网卡重启断网_centos双网卡重启断网_04


4、使用ntpdate命令同步时间—此时只同步了一次时间

ntpdate 10.10.10.100       #服务端server地址

5、写入crontab定期同步时间

crotab -e

#输入
00 00 01 * * ntpdate10.10.10.100