利用操作系统时间做Web时间源服务器

1. 背景

服务器托管在别人的生产环境,其中只有一台服务器被允许访问我们自己IDC的某台服务器的8899端口,协议是http。因为这些服务器均无法访问外网NTP,所以没有办法自动同步时间,过一阵时间就有偏差,所以研发就要求定期手动设置每台服务器的时间。

2. 方案

  1. 利用我们自己IDC的服务器,定期同步时间(例如:每30分钟同步一次)
  2. 利用我们自己IDC服务器本机系统时间做为时间源,通过web服务对外提供本机系统的时间戳
  3. 托管在别人生产环境内的服务器利用一台服务器做“nginx正向代理”跳板,请求我们自己IDC的服务器,获取时间戳后,同步本机系统时间。

3. 实现

3.1 配置服务器时间同步

  • 安装ntpdate
yum install -y ntpdate
  • 设置定时同步时间任务
crontab -e
# 阿里云ntp,可以使用ntp1至ntp7 
*/30 * * * * /usr/sbin/ntpdate ntp1.aliyun.com > /dev/null 2>&1
# 中国国家授时中心
0 */4 * * * /usr/sbin/ntpdate ntp.ntsc.ac.cn > /dev/null 2>&1
0 */12 * * * /usr/sbin/ntpdate cn.ntp.org.cn > /dev/null 2>&1
# 系统时间写入本机BIOS
0 0 * * * /usr/sbin/hwclock -w > /dev/null 2>&1

3.2 编写获取时间戳脚本

vim /home/script/web_ntp/get_timestamp.sh
#!/bin/bash

echo -e "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n$(date +%s)"

3.3 编写返回HTTP时间戳脚本

  • 安装socat
yum install -y socat
  • 编写脚本
vim /home/script/web_ntp/respond.sh
#!/bin/bash 
#set -x

socat TCP-LISTEN:5123,reuseaddr,fork EXEC:/home/script/web_ntp/get_timestamp.sh

# 测试T监听5123服务的命令
# curl http://IP:5123
  • TCP-LISTEN:5123 监听TCP 5123端口
  • reuseaddr 允许快速重用本地地址和端口
  • fork 为每个请求启动一个子进程
  • EXEC:/home/script/web_ntp/get_timestamp.sh 执行获取本机时间戳的脚本

3.4 编写systemd管理文件

vim  /home/script/web_ntp/web_ntp.service
[Unit]
Description=Time Web Service

[Service]
#Type=forking
ExecStart=/home/script/web_ntp/respond.sh
Restart=always

[Install]
WantedBy=multi-user.target
ln -s /home/script/web_ntp/web_ntp.service /etc/systemd/system/
systemctl daemon-reload
systemctl start web_ntp
netstat -nltp | grep 5123
tcp        0      0 0.0.0.0:5123            0.0.0.0:*               LISTEN      16148/socat    

3.5 测试服务

curl http://127.0.0.1:5123
1745402656

3.6 配置nginx代理

vim  nginx.conf
# 配置示例,具体配置详见nginx官方配置说明
# HTTPS server
server {
    listen 8899 ssl;
    server_name  localhost;
    
    # 获取本机的时间戳,并返回
    location /ntp/ {
         proxy_pass http://127.0.0.1:5123/;
    }
}

3.7 测试服务端Web_ntp

  • 测试内网访问
#curl -k https://192.168.5.113:8899/ntp/
curl http://192.168.5.113:8899/ntp/
1745403004
  • 测试公网访问(需要启用防火墙IP和端口映射)
curl -v -k https://公网IP:8899/ntp/
*   Trying 公网IP:8899...
* Connected to 公网IP port 8899 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server accepted to use http/1.1
* Server certificate:
*  subject: CN=*.xunku.org
*  start date: Sep 26 08:47:52 2023 GMT
*  expire date: Sep 25 08:47:51 2024 GMT
*  issuer: C=CN; O=Beijing Xinchacha Credit Management Co., Ltd.; CN=Xcc Trust DV SSL CA
*  SSL certificate verify result: certificate has expired (10), continuing anyway.
> GET /ntp/ HTTP/1.1
> Host: 公网IP:8899
> User-Agent: curl/7.71.1
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: nginx/1.18.0
< Date: Thu, 24 Apr 2025 03:01:58 GMT
< Content-Type: text/plain
< Transfer-Encoding: chunked
< Connection: keep-alive
< 
1745463718
* Connection #0 to host 公网IP left intact

3.8 编写客户端同步时间戳脚本

vim /home/script/web_ntp/set_datetime.sh 
#!/bin/bash

set -x

# IP换成服务端监听公网IP和端口
TIME_STAMP=`curl  -k  https://IP:8899/ntp/`

echo -e "${TIME_STAMP}"

/bin/date --set "@${TIME_STAMP}" && echo -e "$(date +%s)"

3.9 客户端定时执行

  • 设置客户端定时执行任务
crontab -e
# 
* */2 * * * /home/script/web_ntp/set_datetime.sh  > /dev/null 2>&1
  • 手动执行脚本验证
/home/script/web_ntp/set_datetime.sh 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    11    0    11    0     0    423      0 --:--:-- --:--:-- --:--:--   423
1745463296
2025年 04月 24日 星期四 10:54:56 CST
1745463296

你可能感兴趣的:(服务器)