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控管權限
# nomodify - 用戶端不能更改ntp服務器的時間參數
# noquery - 用戶端不能使用ntpq,ntpc等命令來查詢ntp服務器
# notrap - 不提供trap遠端登陸
restrict 192.168.0.0 mask 255.255.0.0 nomodify
notrap
# 以下是兩個ntp服務器
restrict cn.pool.ntp.org
restrict 164.67.62.194
# 用server設定上層ntp服務器,“prefer”表示優先
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 # 系統時間與bios事件的偏差記錄
driftfile /var/lib/ntp/drift
keys /etc/ntp/keys # 日志
logfile /var/log/ntp.log
broadcastdelay 0.008
一個說明:
我在vmware下搭建的ntp服務器,別人可以同步它,它自己的時間卻不對。照理是它同步上層,下層同步它呀。后來找了蠻久的資料,發現這個文件:/etc/ntp/step-tickers,說是ntp服務器無法主動同步上層服務器,所以將地址寫入該文件中即可。
我在物理機里暫時沒發現這個問題,也許是物理機本來自己的時間就正確,就難以發現它有沒有主動同步上層服務器。
運行ntp
service ntpd start/stop/restart
chkconfig ntpd on
查看ntp的端口,應該看到123端口
netstat -unlnp 查看ntp服務器有無和上層連通
[root@S5 ~]# ntpstat
synchronised to NTP server () at stratum 2
time correct to within 74 ms
polling server every 128 s
注意:此命令出現上述synchronised結果比較慢,我的用了大概5分鐘。
查看ntp服務器與上層間的聯系 [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服務器與上層ntp服務器的狀態
[root@S5 ~]# ntpq -p
其中,
remote - 本機和上層ntp的ip或主機名,“+”表示優先,“*”表示次優先
refid - 參考上一層ntp主機地址
st - stratum階層
when - 多少秒前曾經同步過時間
poll - 下次更新在多少秒后
reach - 已經向上層ntp服務器要求更新的次數
delay - 網絡延遲
offset - 時間補償
jitter - 系統時間與bios時間差
客戶端同步時間的方法
方法一:使用 ntpdate
ntpdate [IP_Address]
看下面的例子,其中192.168.1.5是我在內網搭建的ntp服務器
[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 的特點:
1,ntpdate只運行一次就結束,即只同步一次。所以要使用ntpdate同步時間的話要將該命令寫入cron中。
vi /etc/crontab
* * * * * root ntpdate 192.168.1.5
&& hwclock -w
之所以使用hwclock -w命令稍后再說。
2,ntpdate同步時間是跳躍的,如上面的顯示,時間立即就同步了,這將可能導致文件時間標記、監控數據的紊亂。
方法二:使用ntpd服務
step1:/etc/ntpd.conf 寫入 server [IP_Address]
step2:chkconfig ntpd on
step3:service ntpd start
大概過一刻鐘開始起作用。
FAQ:系統報錯 - Sep 25 11:59:39 localhost kernel: set_rtc_mmss: can't
update from 7 to 59
==================================網上查得源碼==========================================
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時間與系統時間差異超過30分鐘,就會報上述錯誤,只要修改bios時間或系統時間即可。
虛擬機下個人體驗:
我的vm客戶端用ntpd同步服務器后,慢慢變成標準時間,可接著又比標準時間越來越慢,然后出現上述報錯。所以我覺得ntpd的同步頻率比我的vm時鐘頻率跑得慢。
ntpd 的特點:
1,ntpd服務始終運行,定期同步時間。
2,ntpd服務是慢慢改變時間直至標準時間。
3,如果系統時間與bios時間差異超過30分鐘,ntpd就會報錯。
總結:
1,推薦使用 ntpd 服務。
2,ntpd開啟后,無法使用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的時間、時區 1,時間
系統時間:date 命令
bios時間:hwclock命令 (-r 讀取bios時間,-w 寫入bios時間)
每次開機時系統重新讀bios時間。
所以前面ntpdate命令時的cron腳本需要用到hwclock,以避免機器重啟后時間混亂。
2,時區
linux的所有時區文件都放在 /usr/share/zoneinfo 下。
linux系統的時區文件是
/etc/localtime,這是個時間格式的檔案,不是ACSII碼。它與/usr/share/zoneinfo/下的自己的時區文件是一樣的。
我的時區是Asia-Shanghai,用diff比較兩個文件,如下:
[root@zy180 ~]# diff /usr/share/zoneinfo/Asia/Shanghai
/etc/localtime
[root@zy180 ~]#
在 /etc/sysconfig/clock 文件中也寫有本時區的信息:
[root@zy180 ~]# cat /etc/sysconfig/clock
ZONE="Asia/Shanghai"
UTC=false
ARC=false
NTP時間服務器:
使用環境:當一個計算機群體需要需要進行時間的嚴格同步,即可使用NTP服務器。
NTP通信協議原理:
1.首先主機啟動NTP。
2.客戶端會向NTP服務器發送調整時間的message。
3.然后NTP server會送出當前的標準時間給client
4.client接受來自server的時間后,會根據這個信息來調整自己的時間。這樣就實現了網絡對時。
NTP這個deamon采用了123端口。(UDP)
“當我們要利用Tim server來進行實踐的同步更新時,就需要使用NTP軟件提供的ntpdate來連接端口123”
安裝:
首先檢查是否安裝了組建:
[root@testmechine ~]# rpm -qa ntp
ntp-4.2.2p1-9.el5.centos.1
假如沒有安裝則yum install ntp來進行安裝。
所需要的配置文件:
1./etc/ntp.conf
linux各版本雖然目錄不同,但文件名相同??梢杂脀hich ntp.conf 或者locate
ntp.conf來查找。這是NTP唯一的一個設置文件。
2./usr/share/zoneinfo/
這個里面規定了這個主要時區的時間設置文件。
3./etc/sysconfig/clock
這個文件是linux的主要時區設置文件,每次開機后linux會自動讀取這個文件來設置系統所默認的顯示時間,可以看看它里面到底設置了什么:
[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
本地端時間配置文件。
5./bin/date
這個是時間的修改命令,除了輸出時間,還可以修改時間。
6./sbin/hwclock
因為linux系統上面BIOS時間與linux系統時間是分開的,所以使用date這個指令調整了時間之后,還需要使用hwclock才能將修改過的時間寫入BIOS中。
7./usr/sbin/ntpd
這是NTP的daemon文件,需要啟動它才能提供NTP服務,這個命令會讀取/etc/ntp.conf里面的設置。
8./usr/sbin/ntpdate
這是client用來連接NTP Server的主要執行文件,如果您不想啟用NTP,只想啟用NTP
Client功能的話,可以只應用此命令。
9,/usr/sbin/ntptrace
可以用來追蹤某臺時間服務器的時間對應關系。
NTP時間服務器采用類似于分層的架構:
關于ntp.conf的設置:
[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.
#注銷原來的命令
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
#進行如下替換
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 #以這臺主機最優先
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的最終設置。
然后我們修改/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的時間也會跟著修改
# Additional options for ntpdate
NTPDATE_OPTIONS=""
啟動NTP:
[root@testmechine ~]# /etc/init.d/ntpd start
Starting ntpd: [ OK ]、
看看端口啟動情況:
[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?:::*
總結
以上是生活随笔為你收集整理的linux防火墙添加udp 123,centos下的ntp的安装和配置 (注意 防火牆的udp 123要打開,不然會不能用)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux加载虚拟sriov网卡,如何配
- 下一篇: linux jsp 环境,Linux系统