标题Linux系统时间修改
1、ntp与ntpdate服务配置时间同步
二者区别
ntpd不仅仅是时间同步服务器,它还可以做客户端与标准时间服务器进行同步时间,并非ntpdate立即同步,在生产环境中慎用ntpdate
安装方式
yum -y install ntp.x86_64
yum -y install ntpdate
使用方法
ntpdate + ip #执行后立刻生效强制同步时间可结合cron定时计划任务使用
ntp服务编辑/etc/ntp.conf #systemctl start ntpd启动ntp服务
server 0.centos.pool.ntp.org iburst
2、使用chrony同步时间
软件安装配置
yum -y install chrony #安装包部署
systemctl enable chronyd #设置系统重启生效
systemctl start chronyd #开启服务
Chrony的配置文件是/etc/chrony.conf,进入第一台机子的配置文件,修改如下两处:
server 0.centos.pool.ntp.org iburst
#allow 192.168.0.0/16
常用命令
timedatectl或者timedatectl status #查看当前时间/日期/时区
timedatectl list-timezones #查看所有可用时区
timedatectl set-timezone “ #时区信息” #设置时区
timedatectl set-timezone UTC #设置UTC
timedatectl set-time HH:MM:SS #设置时间
timedatectl set-time YYYY-MM-DD #设置日期
timedatectl set-time “YYYY-MM-DD HH:MM:SS” #设置日期时间
timedatectl set-local-rtc 1 #设置硬件时钟为本地时间
timedatectl set-local-rtc 0 #设置硬件时钟为UTC时间
timedatectl set-ntp true #启动NTP时间同步(启用NTP服务或者Chrony服务)
timedatectl set-ntp false #禁用NTP时间同步
chronyc sources #查看是否同步
chronyc tracking #查看详细信息
3、使用date命令
date -s #临时设置系统时间,重启后失效
hwclock --show #查看系统硬件时间
hwclock --set --date '2024-01-01 10:00:00' #更改系统硬件时间
hwclock --hctosys #设置系统时间和硬件时间同步