系統采樣,本采樣和命令都是在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本身是分時操作系統,這就跟時鍾周期等概念息息相關