Linux高级课程----Linux的例行性工作和chrony服务器

Linux的例行性工作

1、单一的例行性工作:仅处理执行一次就结束了,at -> atd

        我们使用 at 命令来生成所要运行的工作,并将这个工作,以文本方式写入/var/spool/at/ 目录内,该 工作便能等待 atd 这个服务的取用与执行了。当然,为了安全问题,不是所有人都可以进行 at 工作调度。我们可以利用/etc/at.allow 和/etc/at.deny 这两个文件来进行 at 的使用限制。

at命令的实际工作过程

1、寻找到/etc/at.allow 文件存在,写在这个文件的用户才能使用at命令
2、/etc/at.allow 不存在,则寻找/etc/at.deny 文件,写在该文件的用户不能使用at命令
3、若两个文件都不存在,那么只有root可以使用at命令

注意:在 rhel 里面,由于假设系统上的所有用户都是可信任的,所以允许所有人使用 at 命
。/etc/at.deny 文件为空,如果你想设置拒绝某人使用 at 命令,你可将该用户写入/etc/at.deny 文件。其次,atd在寻找文件的时候,会先寻找allow文件中的用户,找到之后即使你在deny中将该用户写入文件也不会让该用户无法使用at命令

[root@server ~]# systemctl status atd
● atd.service - Job spooling tools
   Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2022-11-04 06:55:34 EDT; 2h 2min ago
 Main PID: 1323 (atd)
    Tasks: 1 (limit: 4652)
   Memory: 1.0M
   CGroup: /system.slice/atd.service
           └─1323 /usr/sbin/atd -f

Nov 04 06:55:34 server systemd[1]: Started Job spooling tools.
Nov 04 08:46:00 server atd[4058]: Starting job 1 (a0000101a8157e) for user 'root' (0)
Nov 04 08:46:00 server atd[4058]: pam_unix(atd:session): session opened for user root by >
Nov 04 08:46:00 server atd[4058]: pam_unix(atd:session): session closed for user root
lines 1-13/13 (END)

注意,在写完你所需要at执行的命令后需要用ctrl+d来提交。

at命令详解 
命令格式:at [参数] [时间]

参数说明
-m当任务完成之后,即使没有标准输出,将给用户发送邮件
-latq的别名,可以列出目前系统上面的所有该用户的at调度
-d

atrm别名,可以取消一个在at调度中的工作

-v

使用较为明显的时间格式,列出at调度中的任务列表

-c可以列出后面接的该项工作的实际命令内容
-f从文件中读取作业

[root@server ~]# at now +10  minutes
warning: commands will be executed using /bin/sh
at> hello
at> <EOT>
job 2 at Fri Nov  4 09:05:00 2022
[root@server ~]# at -l
2       Fri Nov  4 09:05:00 2022 a root
[root@server ~]# at -v
Garbled time
[root@server ~]# at -c
Usage: at [-V] [-q x] [-f file] [-mMlbv] timespec ...
       at [-V] [-q x] [-f file] [-mMlbv] -t time
       at -c job ...
       atq [-V] [-q x]
       at [ -rd ] job ...
       atrm [-V] job ...
       batch
[root@server ~]# at -f
at: option requires an argument -- 'f'
Usage: at [-V] [-q x] [-f file] [-mMlbv] timespec ...
       at [-V] [-q x] [-f file] [-mMlbv] -t time
       at -c job ...
       atq [-V] [-q x]
       at [ -rd ] job ...
       atrm [-V] job ...
       batch
[root@server ~]# at -d 2
[root@server ~]# at -l
[root@server ~]#

时间格式

参数说明
HH:MM在今天的HH小时MM分钟执行,如果今天的这个时间点已经过了,则明天执行
HH:MM YYYY-MM-DD强制规定在某年某月的某一天的特殊时刻进行该工作
now+2minutes熊现在开始几分钟minutes,hours,days,or weeks
[root@server ~]# at now +1  minutes
warning: commands will be executed using /bin/sh
at> wall hello
at> <EOT>
job 1 at Fri Nov  4 08:46:00 2022

Broadcast message from root@server (somewhere) (Fri Nov  4 08:46:00 2022):

hello

 以上是root用户使用at的方式,接下来演示其他用户使用at的方式

[rehl@server ~]$ touch file `date +%T`.txt
[rehl@server ~]$ ll
total 0
-rw-rw-r--. 1 rehl rehl 0 Nov  4 09:31  09:31:02.txt
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Desktop
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Documents
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Downloads
-rw-rw-r--. 1 rehl rehl 0 Nov  4 09:31  file
-rw-rw-r--. 1 rehl rehl 0 Oct 30 03:41  file1
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Music
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Pictures
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Public
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Templates
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Videos
-rw-rw-r--. 1 rehl rehl 0 Nov  4 09:30 'wn'$'\b''enjian'
[rehl@server ~]$ at now +2 minutes
warning: commands will be executed using /bin/sh
at>  touch file `date +%T`.txt
at>
at> <EOT>
job 5 at Fri Nov  4 09:34:00 2022
[rehl@server ~]$ ll
total 0
-rw-rw-r--. 1 rehl rehl 0 Nov  4 09:31  09:31:02.txt
-rw-rw-r--. 1 rehl rehl 0 Nov  4 09:34  09:34:00.txt
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Desktop
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Documents
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Downloads
-rw-rw-r--. 1 rehl rehl 0 Nov  4 09:34  file
-rw-rw-r--. 1 rehl rehl 0 Oct 30 03:41  file1
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Music
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Pictures
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Public
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Templates
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Videos
-rw-rw-r--. 1 rehl rehl 0 Nov  4 09:30 'wn'$'\b''enjian'
[rehl@server ~]$

但是其他用户无法达到at的文件所在地,只有root用户可以

[rehl@server ~]$ cd /var/spool/at
-bash: cd: /var/spool/at: Permission denied
[rehl@server ~]$
[root@server ~]# cd /var/spool/at
[root@server at]#

可以在使用root用户在at中查看当前的at进程

[root@server at]# cd ~
[root@server ~]# at now +10 minutes
warning: commands will be executed using /bin/sh
at>  touch file `date +%T`.txt
at> <EOT>
job 6 at Fri Nov  4 09:53:00 2022
[root@server ~]# cd /var/spool/at
[root@server at]# ll
total 4
-rwx------. 1 root root 2792 Nov  4 09:44 a0000601a815c1
drwx------. 2 root root    6 Nov  4 09:34 spool
[root@server at]#

-rwx------. 1 root root 2792 Nov  4 09:44 a0000601a815c1

这个就是当前的at进程,我们可以看到只有root对他有权限,所以其他人无法cd到at。

循环执行的例行性工作 

循环执行的例行性工作:每隔一定的周期就需要执行一次。
        循环执行的例行性工作调度是由crond这个系统服务来控制的。同样,我们也可以限制使用crontab的用户账号。

crontab命令的实际工作过程 
1、以账号来判别是否可使用crontab命令
        /etc/cron.allow 将可以使用 crontab 的账号写入其中,若不在该文件内的用户 则不可使用 
crontab
        /etc/cron.deny 将不可以使用 crontab 的账号写入其中,若不在该文件内的用 户则可使用 
crontab。
2、当用户使用 crontab 新建工作调度之后,该项工作就会被记录到/var/spool/cron/ 里面
3、cron 执行的每一项工作都会被 记录到/var/log/cron 这个日志文件中

crontab命令详解 
命令格式: crontab [-u user] [-l | -r | -e] 

参数说明
-u只有root才能执行这个任务,帮助某个用户新建/删除crontab
-e编辑crontab的工作内容
-l查阅crontab的工作内容
-r删除所有的crontab的工作内容
代表意义分钟小时日期月份命令
数字范围0-590-231-311-120-7,0和7都代表周日执行的命令

文件内的特殊字符

特殊字符含义
*代表任何时刻
代表分隔时段
-代表一段时间范围
/数字指定时间的间隔频率,例如每3分钟进行一次,*/3

 查看crond服务

[root@server ~]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2022-11-04 06:55:34 EDT; 3h 32min ago
 Main PID: 1313 (crond)
    Tasks: 1 (limit: 4652)
   Memory: 2.3M
   CGroup: /system.slice/crond.service
           └─1313 /usr/sbin/crond -n

Nov 04 07:01:01 server CROND[2191]: (root) CMD (run-parts /etc/cron.hourly)
Nov 04 07:01:02 server anacron[2200]: Anacron started on 2022-11-04
Nov 04 07:01:02 server anacron[2200]: Will run job `cron.daily' in 9 min.
Nov 04 07:01:02 server anacron[2200]: Jobs will be executed sequentially
Nov 04 07:10:02 server anacron[2200]: Job `cron.daily' started
Nov 04 07:10:02 server anacron[2200]: Job `cron.daily' terminated
Nov 04 07:10:02 server anacron[2200]: Normal exit (1 job run)
Nov 04 08:01:01 server CROND[2927]: (root) CMD (run-parts /etc/cron.hourly)
Nov 04 09:01:01 server CROND[4329]: (root) CMD (run-parts /etc/cron.hourly)
Nov 04 10:01:01 server CROND[7447]: (root) CMD (run-parts /etc/cron.hourly)
[root@server ~]#

crond默认只有黑名单,允许通过的名单需要自己设置

[root@server ~]# ll /etc/cron.allow
ls: cannot access '/etc/cron.allow': No such file or directory
[root@server ~]# ll /etc/cron.deny
-rw-r--r--. 1 root root 0 Jun 12  2019 /etc/cron.deny
[root@server ~]#

查看crond的日志文件

[root@server log]# ll cron
-rw-------. 1 root root 14027 Nov  4 10:01 cron
[root@server log]#

用户的crontab命令设置

[root@server log]# crontab -e
[root@server log]#

使用以上命令来进入编辑器。而在进行编辑之前我们需要前往/etc/crontab查看crontab的编辑格式

[root@server etc]# vim /etc/crontab
[root@server etc]#

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

~
~
~
~
~

按照以上格式来编辑crontab文件

* * * * * wall hello
~
~
~
~

 这条命令代表每分钟使用wall发出hello的命令。

[root@server etc]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab

Broadcast message from root@server (somewhere) (Fri Nov  4 10:43:01 2022):

hello

其他用户配置crontab文件的方法

首先我们前往/etc/sudoers查看sudo的相关信息

[root@server etc]# vim /etc/sudoers

注意这里,wheel组是默认可以使用sudo命令的,我们可以将其他用户加入这个组,或者将想要使用sudo的用户写入文件中。

## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL




## Allows members of the 'sys' group to run networking, software,
## service management apps and more.
# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS


## Allows people in group wheel to run all commands
%wheel  ALL=(ALL)       ALL

[rehl@server ~]$ usermod -G wheel xiaolan

 测试sudo是否能够执行。

[xiaolan@server ~]$ sudo ls /var/spool/cron
root
[xiaolan@server ~]$

开始编辑

[xiaolan@server ~]$ sudo crontab -u xiaolan -e
* * * * * touch ~/file`date+\%T`.txt

随后保存退出

[xiaolan@server ~]$ ll
total 0
-rw-r--r--. 1 xiaolan xiaolan 0 Nov  4 11:26 file.txt
[xiaolan@server ~]$

系统的例行性任务

系统的例行性任务我们需要前往/etc/crontab文件编辑

[root@server ~]# vim /etc/crontab
[root@server ~]#
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
* * * * * root wall hello
[root@server ~]# date
Fri Nov  4 11:31:55 EDT 2022

Broadcast message from root@server (somewhere) (Fri Nov  4 11:32:01 2022):

hello

当需要执行多个任务时,先创建一个文件,将任务放到文件中,随后在调用该文件实现多个任务同时执行。

[root@server ~]# vim 1.sh

touch ~/file`date +%T`.txt
mkdir  ~/dir`date +\%T`
~
~
~
~
~
~
[root@server ~]# ll
total 20
-rw-r--r--. 1 root root  197 Oct 30 03:25 '!'
-rw-r--r--. 1 root root   51 Nov  4 11:41  1.sh
-rw-------. 1 root root 1382 Oct 16 12:54  anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Desktop
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Documents
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Downloads
-rw-r--r--. 1 root root    0 Oct 30 03:40  file1
-rw-r--r--. 1 root root    0 Nov  1 23:41  file2
-rw-r--r--. 1 root root 1654 Oct 16 13:04  initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Music
-rw-r--r--. 1 root root 3283 Oct 28 11:28  myfile.zip
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Pictures
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Public
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Templates
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Videos

在这里我们可以看见1.sh的文件权限为-rw-r--r--,为了crontab能够顺利执行,我们需要给1.sh改变权限。

[root@server ~]# chmod a+x 1.sh
[root@server ~]# ll
total 20
-rw-r--r--. 1 root root  197 Oct 30 03:25 '!'
-rwxr-xr-x. 1 root root   51 Nov  4 11:41  1.sh
-rw-------. 1 root root 1382 Oct 16 12:54  anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Desktop
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Documents
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Downloads
-rw-r--r--. 1 root root    0 Oct 30 03:40  file1
-rw-r--r--. 1 root root    0 Nov  1 23:41  file2
-rw-r--r--. 1 root root 1654 Oct 16 13:04  initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Music
-rw-r--r--. 1 root root 3283 Oct 28 11:28  myfile.zip
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Pictures
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Public
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Templates
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Videos
[root@server ~]#
[root@server ~]# ll
total 20
-rw-r--r--. 1 root root  197 Oct 30 03:25 '!'
-rwxr-xr-x. 1 root root   51 Nov  4 11:41  1.sh
-rw-------. 1 root root 1382 Oct 16 12:54  anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Desktop
drwxr-xr-x. 2 root root    6 Nov  4 11:47  dir11:47:01
drwxr-xr-x. 2 root root    6 Nov  4 11:48  dir11:48:01
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Documents
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Downloads
-rw-r--r--. 1 root root    0 Oct 30 03:40  file1
-rw-r--r--. 1 root root    0 Nov  4 11:47  file11:47:01.txt
-rw-r--r--. 1 root root    0 Nov  4 11:48  file11:48:01.txt
-rw-r--r--. 1 root root    0 Nov  1 23:41  file2
-rw-r--r--. 1 root root 1654 Oct 16 13:04  initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Music
-rw-r--r--. 1 root root 3283 Oct 28 11:28  myfile.zip
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Pictures
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Public
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Templates
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Videos
[root@server ~]#

chrony服务器 

        Chrony是一个开源自由的网络时间协议 NTP 的客户端和服务器软软件。它能让计算机保持系统时钟与时钟服务器(NTP)同步,因此让你的计算机保持精确的时间,Chrony也可以作为服务端软件为其他计算机提供时间同步服务。
        Chrony由两个程序组成,分别是chronyd和chronyc
        chronyd是一个后台运行的守护进程,用于调整内核中运行的系统时钟和时钟服务器同步。它确定计算机增减时间的比率,并对此进行补偿。
        chronyc提供了一个用户界面,用于监控性能并进行多样化的配置。它可以在chronyd实例控制的计算机上工作,也可以在一台不同的远程计算机上工作。
        NTP 是网络时间协议(Network Time Protocol)的简称,通过 udp 123 端口进行网络时钟同步。
        RHEL7中默认使用chrony作为时间服务器,也支持NTP,需要额外安装。
        NTP与chrony不能同时存在,只能用其中一个。

安装与配置 

首先,我们需要查看本机是否有chrony这个文件包,如果没有需要自行下载安装。

[root@server ~]# rpm -qa | grep chrony
chrony-4.1-1.el8.x86_64
[root@server ~]#

查看chrony状态

[root@server ~]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enab>
   Active: active (running) since Fri 2022-11-04 06:55:26 EDT; 5h 9min ago
     Docs: man:chronyd(8)

如果处于关闭状态需要启动它,可以输入systemctl start chronyd来重启chrony。

同步时间,带*表示启用的时间服务器。


[root@server ~]# chronyc sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^+ ntp0.icolo.io                 2  10   275   135  -9570us[-9791us] +/-  216ms
^* mail.rettensteiner.com        2  10   275   106    +28ms[  +28ms] +/-  176ms
^+ eudyptula.init7.net           2  10   337  1105    +21ms[  +21ms] +/-  182ms
^+ mail.light-speed.de           2  10   337   625    -12ms[  -12ms] +/-  160ms
[root@server ~]# date
Fri Nov  4 12:07:37 EDT 2022
[root@server ~]#

如果需要同步其他的时间服务器,我们需要进入/etc/chrony.conf文件进行配置

[root@server ~]# vim /etc/chrony.conf

找到这里

# Please consider joining the pool (http://www.pool.ntp.org/join.html).
pool 2.rhel.pool.ntp.org iburst

更改为已知的时间服务器,这里以阿里云为例

# Please consider joining the pool (http://www.pool.ntp.org/join.html).
pool ntp.aliyun.com iburst

为保证程序能够达到预期效果,我们需要检查网络是否畅通

[root@server ~]# ping ntp.aliyun.com
PING ntp.aliyun.com (203.107.6.88) 56(84) bytes of data.
64 bytes from 203.107.6.88 (203.107.6.88): icmp_seq=1 ttl=128 time=84.1 ms
64 bytes from 203.107.6.88 (203.107.6.88): icmp_seq=2 ttl=128 time=75.5 ms
64 bytes from 203.107.6.88 (203.107.6.88): icmp_seq=3 ttl=128 time=76.1 ms
64 bytes from 203.107.6.88 (203.107.6.88): icmp_seq=4 ttl=128 time=71.2 ms
64 bytes from 203.107.6.88 (203.107.6.88): icmp_seq=5 ttl=128 time=68.4 ms

--- ntp.aliyun.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4008ms
rtt min/avg/max/mdev = 68.357/75.055/84.141/5.369 ms
[root@server ~]#

为使程序正常运行,我们需要重启chronyd

[root@server ~]# systemctl restart chronyd
[root@server ~]#

我们可以通过systemctl ststus chronyd这个命令来查看chronyd的运行情况

[root@server ~]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enab>
   Active: active (running) since Fri 2022-11-04 12:15:44 EDT; 1min 43s ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 10154 ExecStopPost=/usr/libexec/chrony-helper remove-daemon-state (code=exite>
  Process: 10163 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, st>
  Process: 10159 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 10161 (chronyd)
    Tasks: 1 (limit: 4652)
   Memory: 1.0M
   CGroup: /system.slice/chronyd.service
           └─10161 /usr/sbin/chronyd

Nov 04 12:15:44 server systemd[1]: Starting NTP client/server...
Nov 04 12:15:44 server chronyd[10161]: chronyd version 4.1 starting (+CMDMON +NTP +REFCL>
Nov 04 12:15:44 server chronyd[10161]: Frequency -1.446 +/- 0.408 ppm read from /var/lib>
Nov 04 12:15:44 server chronyd[10161]: Using right/UTC timezone to obtain leap second da>
Nov 04 12:15:44 server systemd[1]: Started NTP client/server.
Nov 04 12:15:49 server chronyd[10161]: Selected source 203.107.6.88 (ntp.aliyun.com)
Nov 04 12:15:49 server chronyd[10161]: System clock TAI offset set to 37 seconds
lines 1-21/21 (END)

我们可以看见它选择了ntp.aliyun.com作为自己的源。


[root@server ~]# chronyc sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* 203.107.6.88                  2   6   377     7  +3960us[+7407us] +/-   47ms
[root@server ~]#

为了保证服务器的稳定性,我们一般会加载多个时间服务器

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
pool ntp.aliyun.com iburst
pool ntp.tencent.com iburst
pool 210.72.145.44 iburst

在写完之后我们都需要重启下这个进程。

时间服务器的相关配置

[root@server ~]# chronyc sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^+ 203.107.6.88                  2   7   375    32  +1911us[+1911us] +/-   50ms
^* 139.199.215.251               2   9   327   741  +1084us[ +235us] +/-   46ms
^? 210.72.145.44                 0  10     0     -     +0ns[   +0ns] +/-    0ns
[root@server ~]#

从上方的代码我们可以看见一个名为Stratum的参数,它代表的是列表中的时间服务器属于第几层,层数一共是0-15层。在之前的操作中我们已经将客户端的时间服务器配置完毕,接下来我们配置服务端的时间服务器。

在设置时间服务器之前,我们需要先确保两台虚拟机之间的网络是畅通的

[root@server ~]# ping 192.168.240.130
PING 192.168.240.130 (192.168.240.130) 56(84) bytes of data.
64 bytes from 192.168.240.130: icmp_seq=1 ttl=64 time=0.497 ms
64 bytes from 192.168.240.130: icmp_seq=2 ttl=64 time=0.361 ms
64 bytes from 192.168.240.130: icmp_seq=3 ttl=64 time=0.305 ms
64 bytes from 192.168.240.130: icmp_seq=4 ttl=64 time=0.371 ms

--- 192.168.240.130 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3057ms
rtt min/avg/max/mdev = 0.305/0.383/0.497/0.072 ms
[root@server ~]#
[root@localhost ~]# ping 192.168.240.128
PING 192.168.240.128 (192.168.240.128) 56(84) bytes of data.
64 bytes from 192.168.240.128: icmp_seq=1 ttl=64 time=0.397 ms
64 bytes from 192.168.240.128: icmp_seq=2 ttl=64 time=1.03 ms
64 bytes from 192.168.240.128: icmp_seq=3 ttl=64 time=0.398 ms
64 bytes from 192.168.240.128: icmp_seq=4 ttl=64 time=0.365 ms

--- 192.168.240.128 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3109ms
rtt min/avg/max/mdev = 0.365/0.548/1.032/0.279 ms
[root@localhost ~]#

接下来我们以128这个网段的虚拟机作为服务端,130网段的虚拟机所谓客户端。服务端需要关闭防火墙

[root@server ~]# systemctl stop firewalld
[root@server ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: en>
   Active: inactive (dead) since Sat 2022-11-05 08:36:43 EDT; 16s ago
     Docs: man:firewalld(1)
  Process: 979 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exit>
 Main PID: 979 (code=exited, status=0/SUCCESS)

Nov 05 08:15:35 server systemd[1]: Starting firewalld - dynamic firewall daemon...
Nov 05 08:15:38 server systemd[1]: Started firewalld - dynamic firewall daemon.
Nov 05 08:15:39 server firewalld[979]: WARNING: AllowZoneDrifting is enabled. This is co>
Nov 05 08:36:42 server systemd[1]: Stopping firewalld - dynamic firewall daemon...
Nov 05 08:36:43 server systemd[1]: firewalld.service: Succeeded.
Nov 05 08:36:43 server systemd[1]: Stopped firewalld - dynamic firewall daemon.
lines 1-13/13 (END)

随后在服务端书写时间服务器的配置文件

[root@server ~]# vim /etc/chrony.conf

找到这里,这里是决定ntp是否能够通过的代码段。

# Allow NTP client access from local network.
allow 192.168.168.0/24

重启服务

[root@server ~]# systemctl restart chronyd
[root@server ~]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enab>
   Active: active (running) since Sat 2022-11-05 08:43:20 EDT; 33s ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 3345 ExecStopPost=/usr/libexec/chrony-helper remove-daemon-state (code=exited>
  Process: 3353 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, sta>
  Process: 3350 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 3352 (chronyd)
    Tasks: 1 (limit: 4652)
   Memory: 1.5M
   CGroup: /system.slice/chronyd.service
           └─3352 /usr/sbin/chronyd

Nov 05 08:43:16 server systemd[1]: Starting NTP client/server...
Nov 05 08:43:16 server chronyd[3352]: chronyd version 4.1 starting (+CMDMON +NTP +REFCLO>
Nov 05 08:43:18 server chronyd[3352]: Frequency -2.725 +/- 6.674 ppm read from /var/lib/>
Nov 05 08:43:20 server chronyd[3352]: Using right/UTC timezone to obtain leap second data
Nov 05 08:43:20 server systemd[1]: Started NTP client/server.
Nov 05 08:43:27 server chronyd[3352]: Selected source 203.107.6.88 (ntp.aliyun.com)
Nov 05 08:43:27 server chronyd[3352]: System clock TAI offset set to 37 seconds
Nov 05 08:43:30 server chronyd[3352]: Selected source 139.199.215.251 (ntp.tencent.com)
lines 1-22/22 (END)

接下来配置客户端的设置,首先查看chrony包是否已经装载

[root@localhost ~]# rpm -qa |grep chrony
chrony-4.1-1.el8.x86_64
[root@localhost ~]#
[root@localhost ~]# vim /etc/chrony.conf

配置服务器地址

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#pool 2.rhel.pool.ntp.org iburst
pool 192.168.240.128 iburst
[root@localhost ~]# systemctl restart chronyd

验证

[root@localhost ~]# date -s '5:00'
Sat Nov  5 05:00:00 EDT 2022

[root@localhost ~]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2022-11-05 08:52:56 EDT; 3h 51min left
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 1040 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SU>
  Process: 1006 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 1033 (chronyd)
    Tasks: 1 (limit: 4636)
   Memory: 740.0K
   CGroup: /system.slice/chronyd.service
           └─1033 /usr/sbin/chronyd

Nov 05 08:52:56 localhost.localdomain systemd[1]: Starting NTP client/server...
Nov 05 08:52:56 localhost.localdomain chronyd[1033]: chronyd version 4.1 starting (+CMDMON +NTP >
Nov 05 08:52:56 localhost.localdomain chronyd[1033]: Frequency 2.068 +/- 32.437 ppm read from /v>
Nov 05 08:52:56 localhost.localdomain chronyd[1033]: Using right/UTC timezone to obtain leap sec>
Nov 05 08:52:56 localhost.localdomain systemd[1]: Started NTP client/server.
Nov 05 08:53:05 localhost.localdomain chronyd[1033]: Source 192.168.240.128 offline
Nov 05 08:57:03 localhost.localdomain chronyd[1033]: Source 192.168.240.128 online
Nov 05 05:00:07 localhost.localdomain chronyd[1033]: Backward time jump detected!

从这里我们可以看见客户端已经选择了128这个网段为源

[root@localhost ~]# date
Sat Nov  5 05:17:06 EDT 2022
[root@localhost ~]# chronyc -a makestep
200 OK
[root@localhost ~]# date
Sat Nov  5 09:18:01 EDT 2022
[root@localhost ~]# systemctl enable chronyd --now设置开机启动并立刻启动
[root@localhost ~]#

练习:

在每天早上9点,让你的服务器可以通过ntp.aliyun.com和ntp.tencent.com同步自己的时间。

首先要配置好时间服务器

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
pool ntp.aliyun.com iburst
pool ntp.tencent.com iburst
pool 210.72.145.44 iburst

 测试时间是否同步

[root@server ~]#  chronyc sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^+ 203.107.6.88                  2   9   377    42  -4520us[-4814us] +/-   47ms
^* 139.199.215.251               2   8   177    35   +672us[ +378us] +/-   33ms
^? 210.72.145.44                 0   9     0     -     +0ns[   +0ns] +/-    0ns
[root@server ~]#

设置开机自启

[root@server ~]# systemctl enable chronyd --now
[root@server ~]#

书写计划命令

[root@server ~]# vim /etc/crontab
[root@server ~]#
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
* 9 * * * root  chronyc sources
~
~

这样,在每天的九点钟就可以自行进行时间校准了。

以上就是本次的全部内容,谢谢观看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值