linux防火墙添加udp 123,centos下的ntp的安装和配置 (注意 防火牆的udp 123要打開,不然會不能用)...
centos下的ntp的安裝和配置安裝 yum install
ntp
配置文件 /etc/ntp.conf
restrict default kod nomodify
notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1 # 用restrict控管權(quán)限
# nomodify - 用戶端不能更改ntp服務(wù)器的時(shí)間參數(shù)
# noquery - 用戶端不能使用ntpq,ntpc等命令來(lái)查詢ntp服務(wù)器
# notrap - 不提供trap遠(yuǎn)端登陸
restrict 192.168.0.0 mask 255.255.0.0 nomodify
notrap
# 以下是兩個(gè)ntp服務(wù)器
restrict cn.pool.ntp.org
restrict 164.67.62.194
# 用server設(shè)定上層ntp服務(wù)器,“prefer”表示優(yōu)先
server cn.pool.ntp.org prefer
server 164.67.62.194
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10 # 系統(tǒng)時(shí)間與bios事件的偏差記錄
driftfile /var/lib/ntp/drift
keys /etc/ntp/keys # 日志
logfile /var/log/ntp.log
broadcastdelay 0.008
一個(gè)說(shuō)明:
我在vmware下搭建的ntp服務(wù)器,別人可以同步它,它自己的時(shí)間卻不對(duì)。照理是它同步上層,下層同步它呀。后來(lái)找了蠻久的資料,發(fā)現(xiàn)這個(gè)文件:/etc/ntp/step-tickers,說(shuō)是ntp服務(wù)器無(wú)法主動(dòng)同步上層服務(wù)器,所以將地址寫(xiě)入該文件中即可。
我在物理機(jī)里暫時(shí)沒(méi)發(fā)現(xiàn)這個(gè)問(wèn)題,也許是物理機(jī)本來(lái)自己的時(shí)間就正確,就難以發(fā)現(xiàn)它有沒(méi)有主動(dòng)同步上層服務(wù)器。
運(yùn)行ntp
service ntpd start/stop/restart
chkconfig ntpd on
查看ntp的端口,應(yīng)該看到123端口
netstat -unlnp 查看ntp服務(wù)器有無(wú)和上層連通
[root@S5 ~]# ntpstat
synchronised to NTP server () at stratum 2
time correct to within 74 ms
polling server every 128 s
注意:此命令出現(xiàn)上述synchronised結(jié)果比較慢,我的用了大概5分鐘。
查看ntp服務(wù)器與上層間的聯(lián)系 [root@S5 ~]# ntptrace -n
127.0.0.1 127.0.0.1: stratum 2, offset 0.064524, synch
distance 0.121167
210.72.145.44: timed out, nothing received***Request timed
out
查看ntp服務(wù)器與上層ntp服務(wù)器的狀態(tài)
[root@S5 ~]# ntpq -p
其中,
remote - 本機(jī)和上層ntp的ip或主機(jī)名,“+”表示優(yōu)先,“*”表示次優(yōu)先
refid - 參考上一層ntp主機(jī)地址
st - stratum階層
when - 多少秒前曾經(jīng)同步過(guò)時(shí)間
poll - 下次更新在多少秒后
reach - 已經(jīng)向上層ntp服務(wù)器要求更新的次數(shù)
delay - 網(wǎng)絡(luò)延遲
offset - 時(shí)間補(bǔ)償
jitter - 系統(tǒng)時(shí)間與bios時(shí)間差
客戶端同步時(shí)間的方法
方法一:使用 ntpdate
ntpdate [IP_Address]
看下面的例子,其中192.168.1.5是我在內(nèi)網(wǎng)搭建的ntp服務(wù)器
[root@zy180 ~]# date
Thu Sep 25 13:04:17 CST 2008
[root@zy180 ~]# ntpdate 192.168.1.5
25 Sep 14:33:13 ntpdate[10302]: step time server 192.168.1.5 offset
5327.700798 sec
[root@zy180 ~]# date
Thu Sep 25 14:33:18 CST 2008
ntpdate 的特點(diǎn):
1,ntpdate只運(yùn)行一次就結(jié)束,即只同步一次。所以要使用ntpdate同步時(shí)間的話要將該命令寫(xiě)入cron中。
vi /etc/crontab
* * * * * root ntpdate 192.168.1.5
&& hwclock -w
之所以使用hwclock -w命令稍后再說(shuō)。
2,ntpdate同步時(shí)間是跳躍的,如上面的顯示,時(shí)間立即就同步了,這將可能導(dǎo)致文件時(shí)間標(biāo)記、監(jiān)控?cái)?shù)據(jù)的紊亂。
方法二:使用ntpd服務(wù)
step1:/etc/ntpd.conf 寫(xiě)入 server [IP_Address]
step2:chkconfig ntpd on
step3:service ntpd start
大概過(guò)一刻鐘開(kāi)始起作用。
FAQ:系統(tǒng)報(bào)錯(cuò) - Sep 25 11:59:39 localhost kernel: set_rtc_mmss: can't
update from 7 to 59
==================================網(wǎng)上查得源碼==========================================
if (abs(real_minutes - cmos_minutes) < 30) {
update_cmos() } else { printk("set_rtc_mmss: can't update from %d
to %d\n", cmos_minutes, real_minutes); }
========================================================================================
bios時(shí)間與系統(tǒng)時(shí)間差異超過(guò)30分鐘,就會(huì)報(bào)上述錯(cuò)誤,只要修改bios時(shí)間或系統(tǒng)時(shí)間即可。
虛擬機(jī)下個(gè)人體驗(yàn):
我的vm客戶端用ntpd同步服務(wù)器后,慢慢變成標(biāo)準(zhǔn)時(shí)間,可接著又比標(biāo)準(zhǔn)時(shí)間越來(lái)越慢,然后出現(xiàn)上述報(bào)錯(cuò)。所以我覺(jué)得ntpd的同步頻率比我的vm時(shí)鐘頻率跑得慢。
ntpd 的特點(diǎn):
1,ntpd服務(wù)始終運(yùn)行,定期同步時(shí)間。
2,ntpd服務(wù)是慢慢改變時(shí)間直至標(biāo)準(zhǔn)時(shí)間。
3,如果系統(tǒng)時(shí)間與bios時(shí)間差異超過(guò)30分鐘,ntpd就會(huì)報(bào)錯(cuò)。
總結(jié):
1,推薦使用 ntpd 服務(wù)。
2,ntpd開(kāi)啟后,無(wú)法使用ntpdate,看如下例子:
[root@zy180 ~]# service ntpd start
Starting ntpd: [ OK ]
[root@zy180 ~]# ntpdate 192.168.1.5
25 Sep 15:25:22 ntpdate[12334]: the NTP socket is in use,
exiting
附:linux的時(shí)間、時(shí)區(qū) 1,時(shí)間
系統(tǒng)時(shí)間:date 命令
bios時(shí)間:hwclock命令 (-r 讀取bios時(shí)間,-w 寫(xiě)入bios時(shí)間)
每次開(kāi)機(jī)時(shí)系統(tǒng)重新讀bios時(shí)間。
所以前面ntpdate命令時(shí)的cron腳本需要用到hwclock,以避免機(jī)器重啟后時(shí)間混亂。
2,時(shí)區(qū)
linux的所有時(shí)區(qū)文件都放在 /usr/share/zoneinfo 下。
linux系統(tǒng)的時(shí)區(qū)文件是
/etc/localtime,這是個(gè)時(shí)間格式的檔案,不是ACSII碼。它與/usr/share/zoneinfo/下的自己的時(shí)區(qū)文件是一樣的。
我的時(shí)區(qū)是Asia-Shanghai,用diff比較兩個(gè)文件,如下:
[root@zy180 ~]# diff /usr/share/zoneinfo/Asia/Shanghai
/etc/localtime
[root@zy180 ~]#
在 /etc/sysconfig/clock 文件中也寫(xiě)有本時(shí)區(qū)的信息:
[root@zy180 ~]# cat /etc/sysconfig/clock
ZONE="Asia/Shanghai"
UTC=false
ARC=false
NTP時(shí)間服務(wù)器:
使用環(huán)境:當(dāng)一個(gè)計(jì)算機(jī)群體需要需要進(jìn)行時(shí)間的嚴(yán)格同步,即可使用NTP服務(wù)器。
NTP通信協(xié)議原理:
1.首先主機(jī)啟動(dòng)NTP。
2.客戶端會(huì)向NTP服務(wù)器發(fā)送調(diào)整時(shí)間的message。
3.然后NTP server會(huì)送出當(dāng)前的標(biāo)準(zhǔn)時(shí)間給client
4.client接受來(lái)自server的時(shí)間后,會(huì)根據(jù)這個(gè)信息來(lái)調(diào)整自己的時(shí)間。這樣就實(shí)現(xiàn)了網(wǎng)絡(luò)對(duì)時(shí)。
NTP這個(gè)deamon采用了123端口。(UDP)
“當(dāng)我們要利用Tim server來(lái)進(jìn)行實(shí)踐的同步更新時(shí),就需要使用NTP軟件提供的ntpdate來(lái)連接端口123”
安裝:
首先檢查是否安裝了組建:
[root@testmechine ~]# rpm -qa ntp
ntp-4.2.2p1-9.el5.centos.1
假如沒(méi)有安裝則yum install ntp來(lái)進(jìn)行安裝。
所需要的配置文件:
1./etc/ntp.conf
linux各版本雖然目錄不同,但文件名相同。可以用which ntp.conf 或者locate
ntp.conf來(lái)查找。這是NTP唯一的一個(gè)設(shè)置文件。
2./usr/share/zoneinfo/
這個(gè)里面規(guī)定了這個(gè)主要時(shí)區(qū)的時(shí)間設(shè)置文件。
3./etc/sysconfig/clock
這個(gè)文件是linux的主要時(shí)區(qū)設(shè)置文件,每次開(kāi)機(jī)后linux會(huì)自動(dòng)讀取這個(gè)文件來(lái)設(shè)置系統(tǒng)所默認(rèn)的顯示時(shí)間,可以看看它里面到底設(shè)置了什么:
[root@testmechine sysconfig]# cat /etc/sysconfig/clock
# The ZONE parameter is only evaluated by system-config-date.
# The timezone of the system is defined by the contents of
/etc/localtime.
ZONE="Asia/Shanghai"
UTC=true
ARC=false
4./etc/localtime
本地端時(shí)間配置文件。
5./bin/date
這個(gè)是時(shí)間的修改命令,除了輸出時(shí)間,還可以修改時(shí)間。
6./sbin/hwclock
因?yàn)閘inux系統(tǒng)上面BIOS時(shí)間與linux系統(tǒng)時(shí)間是分開(kāi)的,所以使用date這個(gè)指令調(diào)整了時(shí)間之后,還需要使用hwclock才能將修改過(guò)的時(shí)間寫(xiě)入BIOS中。
7./usr/sbin/ntpd
這是NTP的daemon文件,需要啟動(dòng)它才能提供NTP服務(wù),這個(gè)命令會(huì)讀取/etc/ntp.conf里面的設(shè)置。
8./usr/sbin/ntpdate
這是client用來(lái)連接NTP Server的主要執(zhí)行文件,如果您不想啟用NTP,只想啟用NTP
Client功能的話,可以只應(yīng)用此命令。
9,/usr/sbin/ntptrace
可以用來(lái)追蹤某臺(tái)時(shí)間服務(wù)器的時(shí)間對(duì)應(yīng)關(guān)系。
NTP時(shí)間服務(wù)器采用類似于分層的架構(gòu):
關(guān)于ntp.conf的設(shè)置:
[root@testmechine ~]# cat /etc/ntp.conf
# Permit time synchronization with our time source, but do
not
# permit the source to query or modify the service on this
system.
#注銷原來(lái)的命令
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
#添加如下命令:
restrict default nomodify noquery notrap
restrict 202.112.10.60
restrict 202.106.199.36
restrict 202.112.7.150
restrict 127.0.0.1 mask 255.0.0.0
restrict 192.168.0.0 mask 255.255.255.0 nomodify
# 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
#進(jìn)行如下替換
server time.buptnet.edu.cn
server s1b.time.edu.cn
server s1c.time.edu.cn
server s1d.time.edu.cn
#broadcast 192.168.1.255 key
42?# broadcast server
#broadcastclient?# broadcast client
#broadcast 224.0.1.1 key
42?# multicast server
#multicastclient
224.0.1.1?# multicast client
#manycastserver
239.255.254.254?# manycast server
#manycastclient 239.255.254.254 key 42 # manycast client
# Undisciplined Local Clock. This is a fake driver intended for
backup
# and when no outside source of synchronized time is available.
#加入如下代碼:
server 202.112.10.60 prefer #以這臺(tái)主機(jī)最優(yōu)先
server 202.106.199.36
server 202.112.7.150
server
127.127.1.0?# local clock
fudge?127.127.1.0 stratum
10
# Drift file. Put this in a directory which the daemon can write
to.
# No symbolic links allowed, either, since the daemon updates the
file
# by creating a temporary in the same directory and then
rename()'ing
# it to the file.
driftfile /var/lib/ntp/drift
# 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
[root@testmechine ~]#
以上是ntp.conf的最終設(shè)置。
然后我們修改/etc/sysconfig/ntpd:
# Drop root to id 'ntp:ntp' by default.
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid"
# Set to 'yes' to sync hw clock after successful ntpdate
SYNC_HWCLOCK=yes #make no into yes; BIOS的時(shí)間也會(huì)跟著修改
# Additional options for ntpdate
NTPDATE_OPTIONS=""
啟動(dòng)NTP:
[root@testmechine ~]# /etc/init.d/ntpd start
Starting ntpd: [ OK ]、
看看端口啟動(dòng)情況:
[root@testmechine ~]# netstat -nltpu|grep ntpd
udp?0?0
192.168.0.20:123?0.0.0.0:*?7237/ntpd?udp?0?0
127.0.0.1:123?0.0.0.0:*?7237/ntpd?udp?0?0
0.0.0.0:123?0.0.0.0:*?7237/ntpd?udp?0?0
::1:123?:::*
總結(jié)
以上是生活随笔為你收集整理的linux防火墙添加udp 123,centos下的ntp的安装和配置 (注意 防火牆的udp 123要打開,不然會不能用)...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linux加载虚拟sriov网卡,如何配
- 下一篇: linux jsp 环境,Linux系统