Linux系统之时间同步方法

简介: Linux系统之时间同步方法

一、使用NTP服务时间同步

1.安装ntp

[root@node ~]# yum -y install ntp

2.启动ntp服务

[root@node ~]# systemctl start  ntpd
[root@node ~]# systemctl enable  ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

3.查看ntp服务

[root@node ~]# systemctl status ntpd
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2022-08-04 08:00:47 CST; 7h left
 Main PID: 20746 (ntpd)
    Tasks: 1
   Memory: 768.0K
   CGroup: /system.slice/ntpd.service
           └─20746 /usr/sbin/ntpd -u ntp:ntp -g

Aug 04 08:00:47 node ntpd[20746]: Listen normally on 6 lo ::1 UDP 123
Aug 04 08:00:47 node ntpd[20746]: Listen normally on 7 ens33 fe80::8a83:7297:7cb8:9934 UDP 123
Aug 04 08:00:47 node ntpd[20746]: Listening on routing socket on fd #24 for interface updates
Aug 04 08:00:47 node systemd[1]: Started Network Time Service.
Aug 04 08:00:47 node ntpd[20746]: 0.0.0.0 c016 06 restart
Aug 04 08:00:47 node ntpd[20746]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM
Aug 04 08:00:47 node ntpd[20746]: 0.0.0.0 c011 01 freq_not_set
Aug 04 08:00:56 node ntpd[20746]: 0.0.0.0 c61c 0c clock_step -28798.353463 s
Aug 04 00:00:57 node ntpd[20746]: 0.0.0.0 c614 04 freq_mode
Aug 04 00:00:58 node ntpd[20746]: 0.0.0.0 c618 08 no_sys_peer

4.修改ntp.conf文件

[root@node ~]# vim /etc/ntp.conf
[root@node ~]# grep server /etc/ntp.conf 
# Use public servers from the pool.ntp.org project.
#server 0.centos.pool.ntp.org iburst
server ntp.aliyun.com iburst
#broadcast 192.168.1.255 autokey    # broadcast server
#broadcast 224.0.1.1 autokey        # multicast server
#manycastserver 239.255.254.254        # manycast server

5.重启服务

[root@node ~]# systemctl restart ntpd

6.检查同步状态

[root@node ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 203.107.6.88    100.107.25.114   2 u    2   64    1   30.975  149.499   0.393

二、使用ntpdate同步

1.使用ntpd命令同步

[root@node ~]# ntpdate ntp.aliyun.com
 4 Aug 00:07:17 ntpdate[20924]: adjust time server 203.107.6.88 offset -0.001543 sec

2.查看时间

[root@node ~]# date
Thu Aug  4 00:07:46 CST 2022

三、处理ntpdate同步失败报错处理方法

1.报错信息

cna02:~ # ntpdate ntp1.aliyun.com
 4 Aug 07:23:41 ntpdate[18694]: the NTP socket is in use, exiting

2.处理方法

cna02:~ # service ntpd stop
Redirecting to /bin/systemctl stop ntpd.service

3.重新同步

cna02:~ # ntpdate ntp1.aliyun.com
 3 Aug 23:22:36 ntpdate[28980]: step time server 120.25.115.20 offset -28911.609110 sec

4.查看时间是否正确

cna02:~ # date
Wed Aug  3 23:22:41 CST 2022

四、使用chrony服务时间同步

1.安装chrony

[root@node ~]# yum -y install chrony
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.bfsu.edu.cn
 * extras: mirrors.bfsu.edu.cn
 * updates: mirrors.bfsu.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package chrony.x86_64 0:3.2-2.el7 will be updated
---> Package chrony.x86_64 0:3.4-1.el7 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================================================================================================================
 Package                                  Arch                                     Version                                        Repository                              Size
===============================================================================================================================================================================
Updating:
 chrony                                   x86_64                                   3.4-1.el7                                      base                                   251 k

Transaction Summary
===============================================================================================================================================================================
Upgrade  1 Package

Total download size: 251 k
Downloading packages:
No Presto metadata available for base
chrony-3.4-1.el7.x86_64.rpm                                                                                                                             | 251 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : chrony-3.4-1.el7.x86_64                                                                                                                                     1/2 
  Cleanup    : chrony-3.2-2.el7.x86_64                                                                                                                                     2/2 
  Verifying  : chrony-3.4-1.el7.x86_64                                                                                                                                     1/2 
  Verifying  : chrony-3.2-2.el7.x86_64                                                                                                                                     2/2 

Updated:
  chrony.x86_64 0:3.4-1.el7                                                                                                                                                    

Complete!

2.启动chrony服务

[root@node ~]# systemctl enable --now chronyd
Created symlink from /etc/systemd/system/multi-user.target.wants/chronyd.service to /usr/lib/systemd/system/chronyd.service.

3.查看chrony状态

[root@node ~]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2022-08-04 00:12:27 CST; 29s ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 21136 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 21132 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 21134 (chronyd)
    Tasks: 1
   Memory: 364.0K
   CGroup: /system.slice/chronyd.service
           └─21134 /usr/sbin/chronyd

Aug 04 00:12:27 node systemd[1]: Starting NTP client/server...
Aug 04 00:12:27 node chronyd[21134]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG)
Aug 04 00:12:27 node systemd[1]: Started NTP client/server.
Aug 04 00:12:35 node chronyd[21134]: Selected source 193.182.111.141
Aug 04 00:12:35 node chronyd[21134]: Source 78.46.102.180 replaced with 94.237.64.20

4.修改配置文件

[root@node ~]# grep server /etc/chrony.conf 
# Use public servers from the pool.ntp.org project.
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server ntp1.aliyun.com iburst

5.重启服务

[root@node ~]# systemctl restart chronyd

6.检查时间同步状态

[root@node ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 120.25.115.20                 2   6    71    26   +751us[+1549us] +/-   19ms

五、手动修改时间

1.修改时间方法1

[root@node ~]# date -s "2012-05-23 01:01:01"
Wed May 23 01:01:01 CST 2012
[root@node ~]# date
Wed May 23 01:01:05 CST 2012

2.修改时间方法2

修改时间顺序为月日时分年.秒
```cpp
[root@tianyi ~]# date 090621282021.28
Mon Sep 6 21:28:28 CST 2021

## 3.硬件时间向系统时间同步
```cpp
[root@node ~]# hwclock -w

4.按格式输出时间

[root@node ~]# date "+%Y-%m-%d %H:%M:%S"
2012-05-23 01:04:10

5.设置时区

[root@tianyi backups]# timedatectl list-timezones |grep Shanghai
Asia/Shanghai
[root@tianyi backups]# timedatectl set-timezone Asia/Shanghai
相关文章
|
5月前
|
Ubuntu Linux Anolis
Linux系统禁用swap
本文介绍了在新版本Linux系统(如Ubuntu 20.04+、CentOS Stream、openEuler等)中禁用swap的两种方法。传统通过注释/etc/fstab中swap行的方式已失效,现需使用systemd管理swap.target服务或在/etc/fstab中添加noauto参数实现禁用。方法1通过屏蔽swap.target适用于新版系统,方法2通过修改fstab挂载选项更通用,兼容所有系统。
431 3
Linux系统禁用swap
|
5月前
|
Linux
Linux系统修改网卡名为eth0、eth1
在Linux系统中,可通过修改GRUB配置和创建Udev规则或使用systemd链接文件,将网卡名改为`eth0`、`eth1`等传统命名方式,适用于多种发行版并支持多网卡配置。
944 3
|
6月前
|
Ubuntu Linux
计算机基础知识:linux系统怎么安装?
在虚拟机软件中创建一个新的虚拟机,并选择相应操作系统类型和硬盘空间大小等参数。将下载的 ISO 镜像文件加载到虚拟机中。启动虚拟机,进入安装界面,并按照步骤进行安装。安装完成后,可以在虚拟机中使用 Linux 系统。
|
6月前
|
存储 Ubuntu Linux
「正点原子Linux连载」第二章Ubuntu系统入门
在图2.8.2.4中,我们使用命令umount卸载了U盘,卸载以后当我们再去访问文件夹/mnt/tmp的时候发现里面没有任何文件了,说明我们卸载成功了。
|
Ubuntu Linux 网络安全
Linux系统初始化脚本
一款支持Rocky、CentOS、Ubuntu、Debian、openEuler等主流Linux发行版的系统初始化Shell脚本,涵盖网络配置、主机名设置、镜像源更换、安全加固等多项功能,适配单/双网卡环境,支持UEFI引导,提供多版本下载与持续更新。
537 0
Linux系统初始化脚本
|
6月前
|
运维 Linux 开发者
Linux系统中使用Python的ping3库进行网络连通性测试
以上步骤展示了如何利用 Python 的 `ping3` 库来检测网络连通性,并且提供了基本错误处理方法以确保程序能够优雅地处理各种意外情形。通过简洁明快、易读易懂、实操性强等特点使得该方法非常适合开发者或系统管理员快速集成至自动化工具链之内进行日常运维任务之需求满足。
389 18
|
5月前
|
安全 Linux Shell
Linux系统提权方式全面总结:从基础到高级攻防技术
本文全面总结Linux系统提权技术,涵盖权限体系、配置错误、漏洞利用、密码攻击等方法,帮助安全研究人员掌握攻防技术,提升系统防护能力。
478 1
|
5月前
|
监控 安全 Linux
Linux系统提权之计划任务(Cron Jobs)提权
在Linux系统中,计划任务(Cron Jobs)常用于定时执行脚本或命令。若配置不当,攻击者可利用其提权至root权限。常见漏洞包括可写的Cron脚本、目录、通配符注入及PATH变量劫持。攻击者通过修改脚本、创建恶意任务或注入命令实现提权。系统管理员应遵循最小权限原则、使用绝对路径、避免通配符、设置安全PATH并定期审计,以防范此类攻击。
1142 1
|
6月前
|
缓存 监控 Linux
Linux系统清理缓存(buff/cache)的有效方法。
总结而言,在大多数情形下你不必担心Linux中buffer与cache占用过多内存在影响到其他程序运行;因为当程序请求更多内存在没有足够可用资源时,Linux会自行调整其占有量。只有当你明确知道当前环境与需求并希望立即回收这部分资源给即将运行重负载任务之前才考虑上述方法去主动干预。
1883 10
|
6月前
|
安全 Linux 数据安全/隐私保护
为Linux系统的普通账户授予sudo访问权限的过程
完成上述步骤后,你提升的用户就能够使用 `sudo`命令来执行管理员级别的操作,而无需切换到root用户。这是一种更加安全和便捷的权限管理方式,因为它能够留下完整的权限使用记录,并以最小权限的方式工作。需要注意的是,随意授予sudo权限可能会使系统暴露在风险之中,尤其是在用户不了解其所执行命令可能带来的后果的情况下。所以在配置sudo权限时,必须谨慎行事。
964 0