注意 全部配置操做必须是root用户 ,局域网内node21做为NTP Server,node22,node23做为NTP Client与服务器进行时钟同步,ntpd Server,用于和外部公共ntpd同步标准时间,同时做为内网的Server,ntpd Client,用于与ntpd Server同步时间html
node21 | 192.168.100.21 |
node22 | 192.168.100.22 |
node23 | 192.168.100.23 |
[root@node21 ~]# rpm -qa|grep ntp 若是已安装显示 ntp-4.2.6p5-10.el6.centos.x86_64 fontpackages-filesystem-1.41-1.1.el6.noarch ntpdate-4.2.6p5-10.el6.centos.x86_64
在线安装设置开机启动ntp服务java
yum -y install ntp #启动ntp服务 systemctl start ntpd #设置开机启动服务 systemctl enable ntpd.service
配置前先使用命令:ntpdate -u cn.pool.ntp.org
,同步服务器node
替换成中国时间服务器:http://www.pool.ntp.org/zone/cnlinux
ntpd Server:192.168.100.21 修改/etc/ntp.conf文件,红色字体是修改的内容shell
# For more information about this file, see the man pages # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5). driftfile /var/lib/ntp/drift # Permit time synchronization with our time source, but do not # permit the source to query or modify the service on this system. restrict default nomodify notrap nopeer noquery # Permit all access over the loopback interface. This could # be tightened as well, but to do so would effect some of # the administrative functions. restrict 127.0.0.1 restrict ::1 # Hosts on local network are less restricted. #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap # 容许内网其余机器同步时间,若是不添加该约束默认容许全部IP访问本机同步服务 restrict 192.168.100.0 mask 255.255.255.0 nomodify notrap # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). #server 0.centos.pool.ntp.org iburst #server 1.centos.pool.ntp.org iburst #server 2.centos.pool.ntp.org iburst #server 3.centos.pool.ntp.org iburst # 配置和上游标准时间同步 server 210.72.145.44 # 中国国家授时中心 server 133.100.11.8 #日本[福冈大学] server 0.cn.pool.ntp.org server 1.cn.pool.ntp.org server 2.cn.pool.ntp.org server 3.cn.pool.ntp.org # 配置容许上游时间服务器主动修改本机(内网ntp Server)的时间 restrict 210.72.145.44 nomodify notrap noquery restrict 133.100.11.8 nomodify notrap noquery restrict 0.cn.pool.ntp.org nomodify notrap noquery restrict 1.cn.pool.ntp.org nomodify notrap noquery restrict 2.cn.pool.ntp.org nomodify notrap noquery restrict 3.cn.pool.ntp.org nomodify notrap noquery # 确保localhost有足够权限,使用没有任何限制关键词的语法。 # 外部时间服务器不可用时,以本地时间做为时间服务。 # 注意:这里不能改,必须使用127.127.1.0,不然会致使没法 #在ntp客户端运行ntpdate serverIP,出现no server suitable for synchronization found的错误。 #在ntp客户端用ntpdate –d serverIP查看,发现有“Server dropped: strata too high”的错误,而且显示“stratum 16”。而正常状况下stratum这个值得范围是“0~15”。 #这是由于NTP server尚未和其自身或者它的server同步上。 #如下的定义是让NTP Server和其自身保持同步,若是在ntp.conf中定义的server都不可用时,将使用local时间做为ntp服务提供给ntp客户端。 #下面这个配置,建议NTP Client关闭,建议NTP Server打开。由于Client若是打开,可能致使NTP自动选择合适的最近的NTP Server、也就有可能选择了LOCAL做为Server进行同步,而不与远程Server进行同步。 server 127.127.1.0 # local clock fudge 127.127.1.0 stratum 10 #broadcast 192.168.1.255 autokey # broadcast server #broadcastclient # broadcast client #broadcast 224.0.1.1 autokey # multicast server #multicastclient 224.0.1.1 # multicast client #manycastserver 239.255.254.254 # manycast server #manycastclient 239.255.254.254 autokey # manycast client # Enable public key cryptography. #crypto includefile /etc/ntp/crypto/pw # Key file containing the keys and key identifiers used when operating # with symmetric key cryptography. keys /etc/ntp/keys # Specify the key identifiers which are trusted. #trustedkey 4 8 42 # Specify the key identifier to use with the ntpdc utility. #requestkey 8 # Specify the key identifier to use with the ntpq utility. #controlkey 8 # Enable writing of statistics records. #statistics clockstats cryptostats loopstats peerstats # Disable the monitoring facility to prevent amplification attacks using ntpdc # monlist command when default restrict does not include the noquery flag. See # CVE-2013-5211 for more details. # Note: Monitoring will not be disabled with the limited restriction flag. disable monitor
[root@node21 ~]# systemctl restart ntpd
查看网络中的NTP服务器,同时显示客户端和每一个服务器的关系:apache
[root@node21 ~]# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== 210.72.145.44 .INIT. 16 u - 64 0 0.000 0.000 0.000 clock.tl.fukuok .INIT. 16 u - 64 0 0.000 0.000 0.000 gus.buptnet.edu 10.3.8.150 5 u 53 64 7 113.234 -29.011 5.500 cn.ntp.faelix.n 185.134.196.169 2 u 54 64 7 154.859 -67.243 18.652 static-5-103-13 .GPS. 1 u 53 64 7 406.416 -92.420 21.929 *LOCAL(0) .LOCL. 10 l 60 64 7 0.000 0.000 0.000
[root@node21 ~]# ntpstat synchronised to local net at stratum 11 time correct to within 7948 ms polling server every 64 s
[root@node21 ~]# ntpstat unsynchronised time server re-starting polling server every 64 s
[root@node21 ~]# ntpstat synchronised to NTP server (5.103.139.163) at stratum 2 time correct to within 456 ms polling server every 64 s
node22,node23为客户端。windows
1)检查ntp是否安装,以及是否设置了自启动,参考ntpd Server的ntp安装检查。centos
2)修改/etc/ntp.conf文件,红色字体是修改的内容:缓存
# For more information about this file, see the man pages # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5). driftfile /var/lib/ntp/drift # Permit time synchronization with our time source, but do not # permit the source to query or modify the service on this system. restrict default nomodify notrap nopeer noquery # Permit all access over the loopback interface. This could # be tightened as well, but to do so would effect some of # the administrative functions. restrict 127.0.0.1 restrict ::1 # Hosts on local network are less restricted. #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). #server 0.centos.pool.ntp.org iburst #server 1.centos.pool.ntp.org iburst #server 2.centos.pool.ntp.org iburst #server 3.centos.pool.ntp.org iburst #配置上游时间服务器为本地的ntpd Server服务器 server 192.168.100.21 # 配置容许上游时间服务器主动修改本机的时间 restrict 192.168.100.21 nomodify notrap noquery #下面这个配置,建议NTP Client关闭,建议NTP Server打开。由于Client若是打开,可能致使NTP自动选择合适的最近的NTP Server、也就有可能选择了LOCAL做为Server进行同步,而不与远程Server进行同步。 #server 127.127.1.0 # local clock #fudge 127.127.1.0 stratum 10 #broadcast 192.168.1.255 autokey # broadcast server #broadcastclient # broadcast client #broadcast 224.0.1.1 autokey # multicast server #multicastclient 224.0.1.1 # multicast client #manycastserver 239.255.254.254 # manycast server #manycastclient 239.255.254.254 autokey # manycast client # Enable public key cryptography. #crypto includefile /etc/ntp/crypto/pw # Key file containing the keys and key identifiers used when operating # with symmetric key cryptography. keys /etc/ntp/keys # Specify the key identifiers which are trusted. #trustedkey 4 8 42 # Specify the key identifier to use with the ntpdc utility. #requestkey 8 # Specify the key identifier to use with the ntpq utility. #controlkey 8 # Enable writing of statistics records. #statistics clockstats cryptostats loopstats peerstats # Disable the monitoring facility to prevent amplification attacks using ntpdc # monlist command when default restrict does not include the noquery flag. See # CVE-2013-5211 for more details. # Note: Monitoring will not be disabled with the limited restriction flag. disable monitor
[root@node22 ~]# systemctl restart ntpd
[root@node22 ~]# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== node21 LOCAL(0) 11 u 2 64 0 0.000 0.000 0.000
[root@node21 ~]# date Thu May 31 02:11:12 EDT 2018 [root@node21 ~]# date -R Thu, 31 May 2018 02:15:32 -0400 [root@node21 ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime cp: overwrite ‘/etc/localtime’? y [root@node21 ~]# date -R Thu, 31 May 2018 14:16:42 +0800 [root@node21 ~]# date Thu May 31 14:16:47 CST 2018
linux 系统则是由 cron (crond) 这个系统服务来控制的。Linux 系统上面本来就有很是多的计划性工做,所以这个系统服务是默认启动的。另 外, 因为使用者本身也能够设置计划任务,因此, Linux 系统也提供了使用者控制计划任务的命令 :crontab 命令。bash
crond 是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务相似,当安装完成操做系统后,默认会安装此服务 工具,而且会自动启动crond进程,crond进程每分钟会按期检查是否有要执行的任务,若是有要执行的任务,则自动执行该任务。
Linux下的任务调度分为两类,系统任务调度和用户任务调度。
系统任务调度:系统周期性所要执行的工做,好比写缓存数据到硬盘、日志清理等。在/etc目录下有一个crontab文件,这个就是系统任务调度的配置文件。
/etc/crontab文件包括下面几行:
cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=HOME=/
# run-parts
51 * * * * root run-parts /etc/cron.hourly
24 7 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
前 四行是用来配置crond任务运行的环境变量,第一行SHELL变量指定了系统要使用哪一个shell,这里是bash,第二行PATH变量指定了系统执行 命令的路径,第三行MAILTO变量指定了crond的任务执行信息将经过电子邮件发送给root用户,若是MAILTO变量的值为空,则表示不发送任务 执行信息给用户,第四行的HOME变量指定了在执行命令或者脚本时使用的主目录。第六至九行表示的含义将在下个小节详细讲述。这里不在多说。
用户任务调度:用户按期要执行的工做,好比用户数据备份、定时邮件提醒等。用户可使用 crontab 工具来定制本身的计划任务。全部用户定义的crontab 文件都被保存在 /var/spool/cron目录中。其文件名与用户名一致。
使用者权限文件:
文件:
/etc/cron.deny
说明:
该文件中所列用户不容许使用crontab命令
文件:
/etc/cron.allow
说明:
该文件中所列用户容许使用crontab命令
文件:
/var/spool/cron/
说明:
全部用户crontab文件存放的目录,以用户名命名
crontab文件的含义:
用户所创建的crontab文件中,每一行都表明一项任务,每行的每一个字段表明一项设置,它的格式共分为六个字段,前五段是时间设定段,第六段是要执行的命令段,格式以下:
minute hour day month week command
其中:
minute: 表示分钟,能够是从0到59之间的任何整数。
hour:表示小时,能够是从0到23之间的任何整数。
day:表示日期,能够是从1到31之间的任何整数。
month:表示月份,能够是从1到12之间的任何整数。
week:表示星期几,能够是从0到7之间的任何整数,这里的0或7表明星期日。
command:要执行的命令,能够是系统命令,也能够是本身编写的脚本文件。
在以上各个字段中,还可使用如下特殊字符:
星号(*):表明全部可能的值,例如month字段若是是星号,则表示在知足其它字段的制约条件后每个月都执行该命令操做。
逗号(,):能够用逗号隔开的值指定一个列表范围,例如,“1,2,5,7,8,9”
中杠(-):能够用整数之间的中杠表示一个整数范围,例如“2-6”表示“2,3,4,5,6”
正斜线(/):能够用正斜线指定时间的间隔频率,例如“0-23/2”表示每两小时执行一次。同时正斜线能够和星号一块儿使用,例如*/10,若是用在minute字段,表示每十分钟执行一次。
安装crontab:
yum install crontabs
服务操做说明:
/sbin/service crond start //启动服务
/sbin/service crond stop //关闭服务
/sbin/service crond restart //重启服务
/sbin/service crond reload //从新载入配置
/sbin/service crond status //启动服务
查看crontab服务是否已设置为开机启动,执行命令:
ntsysv
加入开机自动启动:
chkconfig –level 35 crond on
1.命令格式:
crontab [-u user] file
crontab [-u user] [ -e | -l | -r ]
2.命令功能:
经过crontab 命令,咱们能够在固定的间隔时间执行指定的系统指令或 shell script脚本。时间间隔的单位能够是分钟、小时、日、月、周及以上的任意组合。这个命令很是设合周期性的日志分析或数据备份等工做。
3.命令参数:
-u user:用来设定某个用户的crontab服务,例如,“-u ixdba”表示设定ixdba用户的crontab服务,此参数通常有root用户来运行。
file:file是命令文件的名字,表示将file作为crontab的任务列表文件并载入crontab。若是在命令行中没有指定这个文件,crontab命令将接受标准输入(键盘)上键入的命令,并将它们载入crontab。
-e:编辑某个用户的crontab文件内容。若是不指定用户,则表示编辑当前用户的crontab文件。
-l:显示某个用户的crontab文件内容,若是不指定用户,则表示显示当前用户的crontab文件内容。
-r:从/var/spool/cron目录中删除某个用户的crontab文件,若是不指定用户,则默认删除当前用户的crontab文件。
-i:在删除用户的crontab文件时给确认提示。
4.经常使用方法:
1). 建立一个新的crontab文件
在 考虑向cron进程提交一个crontab文件以前,首先要作的一件事情就是设置环境变量EDITOR。cron进程根据它来肯定使用哪一个编辑器编辑 crontab文件。9 9 %的UNIX和LINUX用户都使用vi,若是你也是这样,那么你就编辑$ HOME目录下的. profile文件,在其 中加入这样一行:
EDITOR=vi; export EDITOR
而后保存并退出。不妨建立一个名为<user> cron的文件,其中<user>是用户名,例如, davecron。在该文件中加入以下的内容。
# (put your own initials here)echo the date to the console every
# 15minutes between 6pm and 6am
0,15,30,45 18-06 * * * /bin/echo ‘date’ > /dev/console
保存并退出。确信前面5个域用空格分隔。
在 上面的例子中,系统将每隔1 5分钟向控制台输出一次当前时间。若是系统崩溃或挂起,从最后所显示的时间就能够一眼看出系统是什么时间中止工做的。在有些 系统中,用tty1来表示控制台,能够根据实际状况对上面的例子进行相应的修改。为了提交你刚刚建立的crontab文件,能够把这个新建立的文件做为 cron命令的参数:
$ crontab davecron
如今该文件已经提交给cron进程,它将每隔1 5分钟运行一次。
同时,新建立文件的一个副本已经被放在/var/spool/cron目录中,文件名就是用户名(即dave)。
2). 列出crontab文件
为了列出crontab文件,能够用:
$ crontab -l
0,15,30,45,18-06 * * * /bin/echo `date` > dev/tty1
你将会看到和上面相似的内容。可使用这种方法在$ H O M E目录中对crontab文件作一备份:
$ crontab -l > $HOME/mycron
这样,一旦不当心误删了crontab文件,能够用上一节所讲述的方法迅速恢复。
3). 编辑crontab文件
若是但愿添加、删除或编辑crontab文件中的条目,而E D I TO R环境变量又设置为v i,那么就能够用v i来编辑crontab文件,相应的命令为:
$ crontab -e
能够像使用v i编辑其余任何文件那样修改crontab文件并退出。若是修改了某些条目或添加了新的条目,那么在保存该文件时, c r o n会对其进行必要的完整性检查。若是其中的某个域出现了超出容许范围的值,它会提示你。
咱们在编辑crontab文件时,没准会加入新的条目。例如,加入下面的一条:
# DT:delete core files,at 3.30am on 1,7,14,21,26,26 days of each month
30 3 1,7,14,21,26 * * /bin/find -name “core’ -exec rm {} \;
如今保存并退出。最好在crontab文件的每个条目之上加入一条注释,这样就能够知道它的功能、运行时间,更为重要的是,知道这是哪位用户的做业。
如今让咱们使用前面讲过的crontab -l命令列出它的所有信息:
$ crontab -l
# (crondave installed on Tue May 4 13:07:43 1999)
# DT:ech the date to the console every 30 minites
0,15,30,45 18-06 * * * /bin/echo `date` > /dev/tty1
# DT:delete core files,at 3.30am on 1,7,14,21,26,26 days of each month
30 3 1,7,14,21,26 * * /bin/find -name “core’ -exec rm {} \;
要删除crontab文件,能够用:
$ crontab -r
5). 恢复丢失的crontab文件
若是不当心误删了crontab文件,假设你在本身的$ H O M E目录下还有一个备份,那么能够将其拷贝到/var/spool/cron/<username>,其中<username>是用户名。若是因为权限问题没法完成拷贝,能够用:
$ crontab <filename>
其中,<filename>是你在$ H O M E目录中副本的文件名。
我建议你在本身的$ H O M E目录中保存一个该文件的副本。我就有过相似的经历,有数次误删了crontab文件(由于r键紧挨在e键的右边)。这就是为何有些系统文档建议不要直接编辑crontab文件,而是编辑该文件的一个副本,而后从新提交新的文件。
有些crontab的变体有些怪异,因此在使用crontab命令时要格外当心。若是遗漏了任何选项,crontab可能会打开一个空文件,或者看起来像是个空文件。这时敲delete键退出,不要按<Ctrl-D>,不然你将丢失crontab文件。
实例1:每1分钟执行一次command
命令:
* * * * * command
实例2:每小时的第3和第15分钟执行
命令:
3,15 * * * * command
实例3:在上午8点到11点的第3和第15分钟执行
命令:
3,15 8-11 * * * command
实例4:每隔两天的上午8点到11点的第3和第15分钟执行
命令:
3,15 8-11 */2 * * command
实例5:每一个星期一的上午8点到11点的第3和第15分钟执行
命令:
3,15 8-11 * * 1 command
实例6:每晚的21:30重启smb
命令:
30 21 * * * /etc/init.d/smb restart
实例7:每个月一、十、22日的4 : 45重启smb
命令:
45 4 1,10,22 * * /etc/init.d/smb restart
实例8:每周6、周日的1 : 10重启smb
命令:
10 1 * * 6,0 /etc/init.d/smb restart
实例9:天天18 : 00至23 : 00之间每隔30分钟重启smb
命令:
0,30 18-23 * * * /etc/init.d/smb restart
实例10:每星期六的晚上11 : 00 pm重启smb
命令:
0 23 * * 6 /etc/init.d/smb restart
实例11:每一小时重启smb
命令:
* */1 * * * /etc/init.d/smb restart
实例12:晚上11点到早上7点之间,每隔一小时重启smb
命令:
* 23-7/1 * * * /etc/init.d/smb restart
实例13:每个月的4号与每周一到周三的11点重启smb
命令:
0 11 4 * mon-wed /etc/init.d/smb restart
实例14:一月一号的4点重启smb
命令:
0 4 1 jan * /etc/init.d/smb restart
实例15:每小时执行/etc/cron.hourly目录内的脚本
命令:
01 * * * * root run-parts /etc/cron.hourly
说明:
run-parts这个参数了,若是去掉这个参数的话,后面就能够写要运行的某个脚本名,而不是目录名了
注意环境变量问题
有时咱们建立了一个crontab,可是这个任务却没法自动执行,而手动执行这个任务却没有问题,这种状况通常是因为在crontab文件中没有配置环境变量引发的。
在 crontab文件中定义多个调度任务时,须要特别注意的一个问题就是环境变量的设置,由于咱们手动执行某个任务时,是在当前shell环境下进行的,程 序固然能找到环境变量,而系统自动执行任务调度时,是不会加载任何环境变量的,所以,就须要在crontab文件中指定任务运行所需的全部环境变量,这 样,系统执行任务调度时就没有问题了。
不要假定cron知道所须要的特殊环境,它其实并不知道。因此你要保证在shelll脚本中提供全部必要的路径和环境变量,除了一些自动设置的全局变量。因此注意以下3点:
1)脚本中涉及文件路径时写全局路径;
2)脚本执行要用到java或其余环境变量时,经过source命令引入环境变量,如:
cat start_cbp.sh
#!/bin/sh
source /etc/profile
export RUN_CONF=/home/d139/conf/platform/cbp/cbp_jboss.conf
/usr/local/jboss-4.0.5/bin/run.sh -c mev &
3)当手动执行脚本OK,可是crontab死活不执行时。这时必须大胆怀疑是环境变量惹的祸,并能够尝试在crontab中直接引入环境变量解决问题。如:
0 * * * * . /etc/profile;/bin/sh /var/www/java/audit_no_count/bin/restart_audit.sh
注意清理系统用户的邮件日志
每条任务调度执行完毕,系统都会将任务输出信息经过电子邮件的形式发送给当前系统用户,这样日积月累,日志信息会很是大,可能会影响系统的正常运行,所以,将每条任务进行重定向处理很是重要。
例如,能够在crontab文件中设置以下形式,忽略日志输出:
0 */3 * * * /usr/local/apache2/apachectl restart >/dev/null 2>&1
“/dev/null 2>&1”表示先将标准输出重定向到/dev/null,而后将标准错误重定向到标准输出,因为标准输出已经重定向到了/dev/null,所以标准错误也会重定向到/dev/null,这样日志输出问题就解决了。
系统级任务调度与用户级任务调度
系 统级任务调度主要完成系统的一些维护操做,用户级任务调度主要完成用户自定义的一些任务,能够将用户级任务调度放到系统级任务调度来完成(不建议这么 作),可是反过来却不行,root用户的任务调度操做能够经过“crontab –uroot –e”来设置,也能够将调度任务直接写入/etc /crontab文件,须要注意的是,若是要定义一个定时重启系统的任务,就必须将任务放到/etc/crontab文件,即便在root用户下建立一个 定时重启系统的任务也是无效的。
其余注意事项
新建立的cron job,不会立刻执行,至少要过2分钟才执行。若是重启cron则立刻执行。
当crontab忽然失效时,能够尝试/etc/init.d/crond restart解决问题。或者查看日志看某个job有没有执行/报错tail -f /var/log/cron。
千万别乱运行crontab -r。它从Crontab目录(/var/spool/cron)中删除用户的Crontab文件。删除了该用户的全部crontab都没了。
在crontab中%是有特殊含义的,表示换行的意思。若是要用的话必须进行转义\%,如常常用的date ‘+%Y%m%d’在crontab里是不会执行的,应该换成date ‘+\%Y\%m\%d’。