Linux ntp时间服务部署

本文使用云服务器,系统是Ubuntu 20.04 LTS,以下操作都是在Ubuntu上面执行的!!

一、Linux系统时间命令

timedatectl命令

timedatectl 是现代 Linux 系统中最强大的时间管理命令,特别是对于使用 systemd 的系统。它不仅可以设置系统时间,还可以配置时区、启用或禁用 NTP(网络时间同步)等。

用法:

timedatectl # 显示当前的系统时间、时区、NTP 状态等

timedatectl set-time "2024-11-20 10:00:00" # 设置系统时间 timedatectl set-timezone Asia/Shanghai # 设置时区

timedatectl set-ntp true # 启用 NTP 同步

timedatectl show # 显示系统的时间设置

hwclock命令

hwclock 可以将硬件和软件时间做同步

用法:

-s, --hctosys 从硬件时钟设置系统时间

-w, --systohc 从当前系统时间设置硬件时钟

date命令

date 命令用于显示和设置系统时间

用法:

date # 显示当前系统日期和时间

date "+%Y-%m-%d" # 显示自定义格式的日期

date -s "2024-11-20 10:00:00" # 设置系统时间

bash 复制代码
# 查看系统当前时间
root@server:/opt # timedatectl
               Local time: Wed 2024-11-20 09:37:31 HKT
           Universal time: Wed 2024-11-20 01:37:31 UTC
                 RTC time: Wed 2024-11-20 01:37:32
                Time zone: Asia/Hong_Kong (HKT, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

二、搭建ntpd服务

  • 全世界的服务器,时间统一标准,可以以这个为准,cn.ntp.org.cn

基本上Linux的软件,用法都一样

  • 安装

  • 改配置

  • 启动

  • 使用

  • 以后,就是继续改配置,重新加载,重启

  • 继续使用

1、安装ntp软件
bash 复制代码
root@server:/opt # apt-get install ntp
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libnss-systemd libopts25 libpam-systemd libsystemd0 sntp systemd systemd-sysv
Suggested packages:
  ntp-doc systemd-container
The following packages will be REMOVED:
  systemd-timesyncd
The following NEW packages will be installed:
  libopts25 ntp sntp
The following packages will be upgraded:
  libnss-systemd libpam-systemd libsystemd0 systemd systemd-sysv
5 upgraded, 3 newly installed, 1 to remove and 58 not upgraded.
Need to get 5,153 kB of archives.
After this operation, 2,166 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
......
2、查看ntp软件信息
bash 复制代码
root@server:/opt # ls /usr/lib/systemd/system/ | grep ntp
ntp.service
ntp-systemd-netif.path
ntp-systemd-netif.service
root@server:/opt #

3、找到ntp软件的配置文件进行修改

bash 复制代码
# vim /etc/ntp.conf
直接在末行添加以下内容

# 添加ntp的运行日志
logfile /var/log/my_ntp.log

# 记录程序的运行进程号的,可以用于写脚本,读取这个文件,就找到了程序的进程id
pidfile /var/run/ntpd.pid

# 指定了两个 NTP 服务器的地址
server ntp.aliyun.com iburst prefer
server cn.pool.ntp.org iburst 

# ntp.aliyun.com:阿里云的 NTP 服务器。
# cn.pool.ntp.org:是 NTP.org 提供的中国地区的 NTP 服务器池。
4、进行测试
bash 复制代码
# 当前时间是2024 10:59:35
root@server:/opt # date
Wed 20 Nov 2024 10:59:35 AM HKT

# 修改时间为过去时间
root@server:/opt # timedatectl set-time '2024-11-20 10:00'
root@server:/opt # date
Wed 20 Nov 2024 10:00:02 AM HKT
root@server:/opt # timedatectl
               Local time: Wed 2024-11-20 10:00:17 HKT
           Universal time: Wed 2024-11-20 02:00:17 UTC
                 RTC time: Wed 2024-11-20 02:00:17
                Time zone: Asia/Hong_Kong (HKT, +0800)
System clock synchronized: no
              NTP service: n/a
          RTC in local TZ: no


# 启动ntpd服务,等待时间是否同步
root@server:/opt # systemctl restart ntp
root@server:/opt # systemctl status ntp
● ntp.service - Network Time Service
     Loaded: loaded (/lib/systemd/system/ntp.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2024-11-20 10:01:57 HKT; 2s ago
       Docs: man:ntpd(8)
    Process: 801023 ExecStart=/usr/lib/ntp/ntp-systemd-wrapper (code=exited, status=0/SUCCESS)
   Main PID: 801044 (ntpd)
      Tasks: 2 (limit: 4393)
     Memory: 1.6M
     CGroup: /system.slice/ntp.service
             └─801044 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 113:118

Nov 20 10:01:57 server ntpd[801044]: Listen and drop on 1 v4wildcard 0.0.0.0:123
Nov 20 10:01:57 server ntpd[801044]: Listen normally on 2 lo 127.0.0.1:123
......
5、查看时间同步状态
bash 复制代码
root@server:/opt # ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 0.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
 1.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
 2.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
 3.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000

root@server:/opt # timedatectl
               Local time: Wed 2024-11-20 11:02:20 HKT
           Universal time: Wed 2024-11-20 03:02:20 UTC
                 RTC time: Wed 2024-11-20 02:02:36
                Time zone: Asia/Hong_Kong (HKT, +0800)
System clock synchronized: yes
              NTP service: n/a
          RTC in local TZ: no

# 以上可以看到时间是恢复正常了,从过去10点恢复到11点了
6. 这台机器,就是一个时间服务器了,可以作为上游机器使用。
相关推荐
ltl8 小时前
大多数「无锁」代码其实不是无锁的
linux
ltl8 小时前
Linux 内核的内存屏障:一个让我调了三天的 bug
linux
Dontla10 小时前
预置known_hosts如何防止中间人攻击(MITM)?(服务器公钥、服务器私钥)Runner、DNS污染、BGP劫持、同网络ARP欺骗、SSH握手
服务器·网络·ssh
Lonely 净土11 小时前
Rocky Linux 安装教程
linux·运维·服务器
光影少年11 小时前
RN的Fabric 渲染流程
运维·前端·javascript·react native·react.js·fabric
A 糖醋排骨11 小时前
grafana loki alloy轻量级日志采集
运维·grafana·loki
林间码客14 小时前
从DevOps到DevSecOps:构建现代软件交付的基石与护城河
大数据·运维·devsecops·devops
李白你好15 小时前
Windows 离线 Linux 提权辅助查询工具
linux
Lust Dusk15 小时前
记一次Linux应急响应题目解析
linux·运维·服务器·网络·安全·网络安全
ShineWinsu16 小时前
对于Linux:五种IO模型以及非阻塞IO的详细解析
linux·c++·面试·io·阻塞·非阻塞·fcntl