Linux系统定时任务&时间服务器配置

Linux系统定时任务&时间服务器配置

2026年02月26日·5 分钟阅读·28 次阅读·0 点赞·0 条评论

任务一:定义延迟任务,每天 9 点定时给自己发送邮件提醒自己,I'm the best.

1、安装 mailx

[root@localhost ~]# rpm -ivh mailx-12.5-43.fc38.x86_64.rpm 
warning: mailx-12.5-43.fc38.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 18b8e74c: NOKEY
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:mailx-12.5-43.fc38               ################################# [100%]

2、配置 mail.rc 文件,定义发件地址、邮箱授权码等参数

tail /etc/mail.rc
 
# For Linux and BSD, this should be set.
set bsdcompat
 
######################################################################
set from=m137****8953@163.com   #设置发件人邮箱地址
set smtp=smtp.163.com   #指定 SMTP 服务器地址,网易163邮箱的SMTP服务器是 smtp.163.com
set smtp-auth-user=m137****8953@163.com   #设置 SMTP 认证的用户名,通常与发件人邮箱一致
set smtp-auth-password=KPNJWJL******FVF   #设置 SMTP 认证密码(授权码)
set smtp-auth=login   #指定 SMTP 认证方式,login 表示使用用户名+密码认证(网易邮箱支持此方式)

补充:

  • 如何获取 163 邮箱授权码
    1. 登录网易邮箱网页版 → 设置 → POP3/SMTP/IMAP。
    2. 开启 SMTP 服务,按提示获取授权码。

3、设置 cron 定时任务

[root@localhost ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
 
0 9 * * * echo "I'm the best." | mailx -s "开启元气满满的一天!" m137****8953@163.com
 

4、确认当前已有的定时任务

[root@localhost ~]# crontab -l
0 9 * * * echo "I'm the best." | mailx -s "开启元气满满的一天!" m137****8953@163.com


任务二:配置时间服务器,通过两台 linux 主机模拟实现 linux 主机时间同步操作。

服务端配置:

1. 安装 chrony

yum install chrony -y

2.编辑 chrony 配置文件

共计修改三行内容,已注释标明

[root@localhost ~]# vim /etc/chrony.conf
 
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (https://www.pool.ntp.org/join.html).
#pool 2.rhel.pool.ntp.org iburst   #将行注释,作为时间服务器,不需要再向其他服务器同步时间
 
# Use NTP servers from DHCP.
sourcedir /run/chrony-dhcp
 
# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift
 
# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
makestep 1.0 3
 
# Enable kernel synchronization of the real-time clock (RTC).
rtcsync
 
# Enable hardware timestamping on all interfaces that support it.
#hwtimestamp *
 
# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2
 
# Allow NTP client access from local network.
allow 192.168.233.0/24  #此行注释放通,定义允许向该服务器发起时间同步的ip网段
 
# Serve time even if not synchronized to a time source.
local stratum 10  #将此行注释放通,定义时间服务器层级,默认第10层
 

3.重启 chrony 服务

[root@localhost ~]# systemctl restart chronyd

4.关闭 Linux 防火墙及 SElinux(此为教学模拟环境,图方便才如此操作,真实生产环境需慎重)

[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# setenforce 0

客户端配置:

1.编辑 chrony 配置文件,并重启服务

[root@localhost ~]# vim /etc/chrony.conf
 
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (https://www.pool.ntp.org/join.html).
pool 192.168.233.130 iburst  #将此行的地址更改为服务端ip地址
 
 
[root@localhost ~]# systemctl restart chronyd

2.手动修改时间

date 123115302023.30
 
[root@localhost ~]# date
Sun Dec 31 03:31:03 PM CST 2023

3.重启 chronyd 服务,向时间服务器发起时间同步

[root@localhost ~]# systemctl restart chronyd
[root@localhost ~]# date
Thu Apr 24 04:06:49 PM CST 2025

时间同步完成

标签:
©

版权声明:本文采用 CC BY-NC-SA 4.0 协议授权,转载请注明出处并保留原始链接。

原文链接:https://www.jerrygao.cn//blog/linuxE7B3BBE7BB9FE5AE9AE697B6E4BBBBE58AA1E697B6E997B4E69C8DE58AA1E599A8E9858DE7BDAE

评论 0

💬

还没有评论,成为第一个留言的人吧!