服务器托管在别人的生产环境,其中只有一台服务器被允许访问我们自己IDC的某台服务器的8899端口,协议是http。因为这些服务器均无法访问外网NTP,所以没有办法自动同步时间,过一阵时间就有偏差,所以研发就要求定期手动设置每台服务器的时间。
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
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)"
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
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
curl http://127.0.0.1:5123
1745402656
vim nginx.conf
# 配置示例,具体配置详见nginx官方配置说明
# HTTPS server
server {
listen 8899 ssl;
server_name localhost;
# 获取本机的时间戳,并返回
location /ntp/ {
proxy_pass http://127.0.0.1:5123/;
}
}
#curl -k https://192.168.5.113:8899/ntp/
curl http://192.168.5.113:8899/ntp/
1745403004
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
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)"
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