时间篇之centos6下修复的ntp操作(ntpd和ntpdate两个服务区别)


系统采样,本采样和命令都是在centos6.4的系统中进行

主要比较centos7和centos6之间的差异,因为大部分都开始采用centos7
但是有些老系统还采用centos6,这样我们就需要熟悉centos6下的时间处理的方法。

这里讲解ntpd和ntpdate两个服务的区别和使用方法,注意他们两个之间的区别

1、ntpd是自动执行的远程更新本地系统时钟的服务。
2、ntpdate是手工执行的服务,也就是一般用它执行一次本地时间更新,
    如果做成半自动,可以写入到crontab自动任务,从而变成自动任务。

[root@fp-mysql-4 ~]# cat  /etc/redhat-release 
CentOS release 6.4 (Final)
[root@fp-mysql-4 ~]# uname -r
2.6.32-358.el6.x86_64

[root@fp-mysql-4 ~]# rpm -qa | grep ntp  //ntp包,包括两个服务.
ntpdate-4.2.4p8-3.el6.centos.x86_64   //ntpdate服务
ntp-4.2.4p8-3.el6.centos.x86_64       //ntpd服务

//在centos6中是没有我们通常的命令systemctl的. systemctl作为systemd管理入口和工具在centos7中很重要,Systemd是Linux的系统和服务管理器

  [root@fp-mysql-4 src]# systemctl
  -bash: systemctl: command not found

//linux 2.5内核之后hz由100改成了1000

 [root@fp-mysql-4 ~]# cat /boot/config-`uname -r` | grep 'CONFIG_HZ='
  CONFIG_HZ=1000

//硬件计时器时间,cmos时间,rtc实时时钟芯片提供的时间.

[root@fp-mysql-4 ~]# hwclock -r
Wed 09 Mar 2022 11:21:36 AM CST -1.109619 seconds

//当前系统时间,系统定时器时间
[root@fp-mysql-4 ~]# date
Wed Mar 9 11:21:38 CST 2022

相关ntp的服务命令(ntpd和ntpdate)

[root@fp-mysql-4 ~]# ps -ef | grep ntpd
root      60927      1  0 11:01 ?        00:00:00 ntpd -u ntp:ntp -p /var/run/ntpd.pid -g
root      60930  60860  0 11:01 pts/0    00:00:00 grep ntp
[root@fp-mysql-4 ~]# ps -ef|grep xntpd
root 60983 60860 0 11:24 pts/0 00:00:00 grep xntpd
//重启ntpd服务
[root@fp-mysql-4 ~]#service restart ntpd   
//查看ntpd和ntpdate
[root@fp-mysql-4 ~]# service ntpd status ntpd (pid 60927) is running...
[root@fp-mysql-4 src]# service ntpdate status  //没有输出任何东西
[root@fp-mysql-4 ~]# crontab -l
no crontab for root

查看当前系统运行时间情况

[root@fp-mysql-4 ~]# uptime
11:04:39 up 139 days, 17:36, 1 user, load average: 0.00, 0.00, 0.00
//运行到目前的时间
[root@fp-mysql-4 ~]# date -d "$(awk -F. '{print $1}' /proc/uptime) second ago" +"%Y-%m-%d %H:%M:%S"
2021-10-20 17:28:18

重启服务

[root@fp-mysql-4 ~]# /etc/init.d/ntpd restart
Shutting down ntpd:                                        [  OK  ]
Starting ntpd:                                             [  OK  ]
#/etc/init.d/ntpd start
#/etc/init.d/ntpd stop

 

 查看ntpd开机启动列表

设置前
[root@fp-mysql-4 ~]# chkconfig --list ntpd
ntpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@fp-mysql-4 ~]#  chkconfig --list
auditd             0:off    1:off    2:on    3:on    4:on    5:on    6:off
blk-availability    0:off    1:on    2:on    3:on    4:on    5:on    6:off
crond              0:off    1:off    2:on    3:on    4:on    5:on    6:off
ip6tables          0:off    1:off    2:on    3:on    4:on    5:on    6:off
iptables           0:off    1:off    2:off    3:off    4:off    5:off    6:off
lvm2-monitor       0:off    1:on    2:on    3:on    4:on    5:on    6:off
mysql              0:off    1:off    2:on    3:on    4:on    5:on    6:off
netconsole         0:off    1:off    2:off    3:off    4:off    5:off    6:off
netfs              0:off    1:off    2:off    3:on    4:on    5:on    6:off
network            0:off    1:off    2:on    3:on    4:on    5:on    6:off
nfs                0:off    1:off    2:off    3:off    4:off    5:off    6:off
nfslock            0:off    1:off    2:off    3:on    4:on    5:on    6:off
nmb                0:off    1:off    2:on    3:on    4:on    5:on    6:off
ntpd               0:off    1:off    2:off    3:off    4:off    5:off    6:off
ntpdate            0:off    1:off    2:off    3:off    4:off    5:off    6:off
postfix            0:off    1:off    2:on    3:on    4:on    5:on    6:off
rdisc              0:off    1:off    2:off    3:off    4:off    5:off    6:off
restorecond        0:off    1:off    2:off    3:off    4:off    5:off    6:off
rpcbind            0:off    1:off    2:on    3:on    4:on    5:on    6:off
rpcgssd            0:off    1:off    2:off    3:on    4:on    5:on    6:off
rpcidmapd          0:off    1:off    2:off    3:on    4:on    5:on    6:off
rpcsvcgssd         0:off    1:off    2:off    3:off    4:off    5:off    6:off
rsyslog            0:off    1:off    2:on    3:on    4:on    5:on    6:off
saslauthd          0:off    1:off    2:off    3:off    4:off    5:off    6:off
smb                0:off    1:off    2:on    3:on    4:on    5:on    6:off
sshd               0:off    1:off    2:on    3:on    4:on    5:on    6:off
udev-post          0:off    1:on    2:on    3:on    4:on    5:on    6:off
winbind            0:off    1:off    2:off    3:off    4:off    5:off    6:off
zabbix-agent       0:off    1:off    2:on    3:on    4:on    5:on    6:off
0,1,2,3,4,5,6表示的是等级
0表示:挂起
1表示:单用户模式
2表示:无网络连接的多用户命令行模式,多用户不带nfs
3表示:有网络连接的多用户命令行模式,标准的多用户模式
4表示:不可用,未定义
5表示:带图形界面的多用户模式,x11模式
6表示:重新启动

设置后:
[root@fp-mysql-4 ~]# chkconfig --level 2345 ntpd on

  [root@fp-mysql-4 ~]# chkconfig --list ntpd
   ntpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off

 注意:centos7中改命令改成了
 1、systemctl list-unit-files  
 2、[root@ht23 ~]# systemctl list-unit-files | grep ntp
  ntpd.service enabled
  ntpdate.service disabled
 3、[root@ht23 ~]#systemctl is-enabled ntpdate

查看 /etc/ntp.conf(ntpd配置文件)

[root@fp-mysql-4 ~]# grep "^server" /etc/ntp.conf
server 0.centos.pool.ntp.org
server 1.centos.pool.ntp.org
server 2.centos.pool.ntp.org

[root@fp-mysql-4 ~]# cat /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 kod nomodify notrap nopeer noquery
restrict -6 default kod 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 -6 ::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
server 1.centos.pool.ntp.org
server 2.centos.pool.ntp.org

#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

# Undisciplined Local Clock. This is a fake driver intended for backup
# and when no outside source of synchronized time is available.
#server 127.127.1.0 # local clock
#fudge 127.127.1.0 stratum 10

# 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


查看同步情况,可以列出目前我们的NTP与相关的上层NTP的状态

[root@fp-mysql-4 ~]#  ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*120.25.115.20   10.137.53.7      2 u   17   64  173   39.543   -0.019   0.146
+139.199.214.202 100.122.36.196   2 u   42   64  377   38.537    0.483   0.116
+203.158.177.10  45.127.113.2     3 u   18   64  277  141.023   -3.701   0.130

remote:即NTP主机的IP或主机名称。注意最左边的符号,如果由“+”则代表目前正在作用钟的上层NTP,如果是“*”则表示也有连上线,不过是作为次要联机的NTP主机。

refid:参考的上一层NTP主机的地址

st:即stratum阶层

when:几秒前曾做过时间同步更新的操作

poll:下次更新在几秒之后

reach:已经向上层NTP服务器要求更新的次数

delay:网络传输过程钟延迟的时间

offset:时间补偿的结果

jitter:Linux系统时间与BIOS硬件时间的差异时间

//查看当前的状态

[root@fp-mysql-4 ~]# ntpstat
synchronised to NTP server (120.25.115.20) at stratum 3
time correct to within 7 ms
polling server every 64 s 

 时间重置测试

 恢复之前可以
1、先停止ntpd,再进行ntpdate操作,然后让同步程序自己持续同步服务器时间
2、写入crontab,持续手工同步利用ntpdate  (crontab -e编辑)
*/30 * * * * ntpdate cn.pool.ntp.org # 每半个小时同步一下时间 

//时间重置到 2009年,我们看看再如何恢复
[root@fp-mysql-4 ~]# date 042817252009.00 

[root@fp-mysql-4 ~]# date 
Tue Apr 28 17:25:00 CST 2009

[root@fp-mysql-4 ~]# date +'%Y/%m/%d %H:%M:%S'
2009/04/28 17:25:05

[root@fp-mysql-4 ~]# date
Tue Apr 28 17:25:32 CST 2009

[root@fp-mysql-4 ~]# hwclock -r
Wed 09 Mar 2022 12:57:49 PM CST -1.234650 seconds

//写入bios时间

[root@fp-mysql-4 ~]# hwclock -w
[root@fp-mysql-4 ~]# date
Tue Apr 28 17:26:07 CST 2009

[root@fp-mysql-4 ~]# hwclock -r
Tue 28 Apr 2009 05:26:13 PM CST -1.328373 seconds

[root@fp-mysql-4 ~]# /etc/init.d/ntpd restart
Shutting down ntpd: [ OK ]
Starting ntpd: [ OK ]

重启之后,并不能改变时间

[root@fp-mysql-4 ~]# date +'%Y/%m/%d %H:%M:%S'
2009/04/28 17:28:33

 

//得到unix时间戳 

[root@fp-mysql-4 ~]# date +%s
1240910968

//根据unix时间戳再得到当前的日期和时间

[root@fp-mysql-4 ~]# date -d @1240910968
Tue Apr 28 17:29:28 CST 2009

//再检查下同步情况

[root@fp-mysql-4 ~]# ntpq -pn
remote refid st t when poll reach delay offset jitter
==============================================================================
162.159.200.1 10.28.10.82 3 u 54 64 7 160.803 4058911 4.130
178.215.228.24 131.188.3.221 2 u 53 64 3 237.201 4058911 0.209
61.239.100.101 .GPS. 1 u 53 64 3 57.424 4058911 1.114

[root@fp-mysql-4 ~]# ntpstat
unsynchronised
time server re-starting
polling server every 64 s

//因为我重启了ntpd,需要一段时间同步,unsynchronised才能变成synchronised

 

//过了十几分钟一切都自己恢复正常,因为已经开始同步了

[root@fp-mysql-4 ~]# ntpstat
synchronised to NTP server (178.215.228.24) at stratum 3
time correct to within 472 ms
polling server every 64 s

[root@fp-mysql-4 ~]# date
Wed Mar 9 13:09:31 CST 2022

//读取下硬件时间,发现还是硬件设置的时候,需要写入才可以

[root@fp-mysql-4 ~]# date

Wed Mar 9 13:09:31 CST 2022
[root@fp-mysql-4 ~]# hwclock -r
Tue 28 Apr 2009 05:40:29 PM CST -1.109746 seconds
[root@fp-mysql-4 ~]# hwclock -w  //将当前时间和日期写入BIOS
[root@fp-mysql-4 ~]# hwclock -r
Wed 09 Mar 2022 01:11:06 PM CST -1.391015 seconds  //计时方法不一样,是pm方式

设置时区命令

[root@fp-mysql-4 ~]# tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent or ocean.
 1) Africa
 2) Americas
 3) Antarctica
 4) Arctic Ocean
 5) Asia
 6) Atlantic Ocean
 7) Australia
 8) Europe
 9) Indian Ocean
10) Pacific Ocean
11) none - I want to specify the time zone using the Posix TZ format.
#

查看北京时间

[root@fp-mysql-4 ~]# date -R  //输出的 UTC + (+0800) = 北京时间
Wed, 09 Mar 2022 13:38:08 +0800 

  [root@fp-mysql-4 ~]# date
  Wed Mar 9 13:38:32 CST 2022   //输出的是cst即北京时间

  复制相关的时区文件
[root@fp-mysql-4 ~]#  cp /usr/share/zoneinfo/$主时区/$次时区 /etc/localtime

  例如修改时区

  [root@fp-mysql-4 ~]#cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

查看文件的一些时间属性

在Linux下查看文件三种时间 
1、atime(access time):最近访问文件内容时间(Last Access Time)。
2、mtime(modify time):最近修改文件内容时间(Last Modification Time)。
3、ctime(change time):最近更改文件属性(Inode内容更改)的时间,包括文件名、大小、内容、权限、属主、属组等(Last Change Time)。

//stat命令

[root@fp-mysql-4 src]# stat changeconf.sh
File: `changeconf.sh'
Size: 217 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 1445783 Links: 1   //通过inode也可以删除文件
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-03-22 17:13:38.123979187 +0800
Modify: 2017-03-22 17:12:48.449979192 +0800
Change: 2017-03-22 17:12:48.449979192 +0800

1.输入“touch test.txt”创建新文件,输入“stat test.txt”即可查看文件filetime.txt的时间属性。
备注:新创建文件的三种时间抓取当前时间,本例中为2019-01-05 19:42:36

[root@fp-mysql-4 src]# stat test.txt
File: `test.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 1447438 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-03-09 13:45:06.797572818 +0800
Modify: 2022-03-09 13:45:06.797572818 +0800
Change: 2022-03-09 13:45:06.797572818 +0800

//修改文件之后

[root@fp-mysql-4 src]# vi test.txt
[root@fp-mysql-4 src]# stat test.txt
File: `test.txt'
Size: 4 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 1447440 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-03-09 13:45:30.343700307 +0800
Modify: 2022-03-09 13:45:30.343700307 +0800
Change: 2022-03-09 13:45:30.352700267 +0800


//输入“ls -lc test.txt”可查看文件test.txt的ctime。
[root@fp-mysql-4 src]# ls -lc test.txt 
-rw-r--r-- 1 root root 4 Mar 9 13:45 test.txt
//输入“ls -lu test.txt”可查看文件new.txt的atime。

[root@fp-mysql-4 src]# ls -lu test.txt
-rw-r--r-- 1 root root 4 Mar 9 13:45 test.txt

//输入“ls -l test.txt”可查看文件new.txt的mtime。

-rw-r--r-- 1 root root 4 Mar 9 13:45 test.txt

 二、使用ntpdate

//重启ntpdate的话会出现
[root@fp-mysql-4 src]# /etc/init.d/ntpdate restart
ntpdate: Synchronizing with time server:                   [FAILED]
[root@fp-mysql-4 src]# service ntpdate start
ntpdate: Synchronizing with time server: [FAILED]

//这个是由于什么原因呢? 注意这里很重要,首先你要停止 ntpd服务,这里演示一下

[root@fp-mysql-4 src]# service ntpdate start
ntpdate: Synchronizing with time server: [FAILED]
[root@fp-mysql-4 src]# service ntpd stop
Shutting down ntpd: [ OK ]
[root@fp-mysql-4 src]# service ntpdate start
ntpdate: Synchronizing with time server: 
[ OK ]
//这样你就可以在crontab -e 编辑你的任务比如:  */5 * * * *  /usr/sbin/ntpdate ntp.fudan.edu ,就相当于每5秒钟,手工自动执行一次

  //同时要注意是,一旦你启动了ntpdate,ntpd这个服务就可以自有的关闭和启动了。 所以有个前置的条件是,启动ntpdate之前必须关闭ntpd.

设置ntpdate开机启动

[root@fp-mysql-4 ~]# chkconfig --level 2345 ntpdate on
[root@fp-mysql-4 ~]# chkconfig --list ntpdate
ntpdate 0:off    1:off    2:on    3:on    4:on    5:on    6:off

小结:
   时间体系是linux很重要的部分,还是要深入的掌握。linux内核中对于时间部分的处理是很多的,同时也涉及到很多的概念,例如:时区、cst,utc,+0008,
  rtc,pit,jiffies,tick,xtime等等概念,因为很多内核和软件都需要靠时钟的频率来进行相关的处理。linux本身是分时操作系统,这就跟时钟周期等概念息息相关

posted @ 2022-03-09 11:07  jinzi  阅读(1886)  评论(0编辑  收藏  举报