1、如何在 CentOS 中設置 NTP 伺服器
1.LinuxNTP客戶端主機需要ntpupdate軟體包來和伺服器同步時間。可以輕松地使用yum或apt-get安裝這個軟體包。安裝完軟體包之後,用伺服器的IP地址運行下面的命令。#ntpdate基於RHEL和Debian的系統命令都相同。2.Windows如果你正在使用Windows,在日期和時間設置(DateandTimesettings)下查找網路時間(InternetTime)。3.Cisco設備如果你想要同步Cisco設備的時間,你可以在全局配置模式下使用下面的命令。#ntpserver來自其它廠家的支持NTP的設備有自己的用於網路時間的參數。如果你想將設備和NTP伺服器同步時間,請查看設備的說明文檔。
2、如何在 CentOS 中設置 NTP 伺服器
配置的環境及要求:
1.假設在192.168.0.0網段內,要以IP為192.168.0.240的Linux機器時間伺服器。
2.192.168.0.240伺服器能上外網,能與比較權威的公網時間伺服器同步
3.同網段內的其他機器每小時自動向192.168.0.240同步時間
一、安裝
yum -y install ntp ntpdate
二、配置
2.1.NTP server的主配置文件為/etc/ntp.conf 現對/etc/ntp.conf的各項進行說明
cp /etc/ntp.conf /etc/ntp.conf.bak
vim /etc/ntp.conf
2.vi /etc/ntp.conf(以下是ntp.conf文件的內容)
#設置此伺服器同上層伺服器做時間同步的IP地址,prefer意味著首選IP地址
#經試驗,下面的幾個時間伺服器速度還不錯(默認配置即可)
server 210.72.145.44 prefer
server 218.21.130.42
server 0.asia.pool.ntp.org
server 1.asia.pool.ntp.org
server 2.asia.pool.ntp.org
#記錄上次的NTP server與上層NTP server聯接所花費的時間
driftfile /etc/ntp/drift
#設置默認策略為允許任何主機進行時間同步
restrict default ignore
#設置允許訪問此時間伺服器的時間服務的IP地址
#根據自己實際情況配置
restrict 127.0.0.1
restrict 192.168.10.2# 指定某台機器時間同步
restrict 192.168.0.0 mask 255.255.255.0 #允許192.168.0.0/254子網內主機時間同步
restrict 0.0.0.0 mask 0.0.0.0 nomodify notrap #允許任何主機跟進行時間同步
#指定階層編號為10,降低其優先度。
fudge 127.127.1.1 stratum 10
#設置ntp日誌的path
statsdir /var/log/ntp/
#設置ntp日誌文件
logfile /var/log/ntp/ntp.log
三、維護
----添加為服務
chkconfig --level 345 ntpd on
----啟動
service ntpd start
或
/etc/rc.d/init.d/ntpd start
----停止
service ntpd stop
或
/etc/rc.d/init.d/ntpd stop
----管理命令
ntpq –p #查看本機和上層伺服器的時間同步結果
ntptrace #可以用來追蹤某台時間伺服器的時間對應關系
ntpdate IP #客戶端要和NTP server進行時鍾同步。
/var/log/ntp/ntp.log #查看ntp日誌
----------------------------------
開啟防火牆埠
iptables -A INPUT -p udp -dport 123 -j ACCEPT
/etc/init.d/iptables save
service iptables restart
四、配置客戶端
測試同步:
ntpdate ip地址
設置自動同步:
echo 「00 */1 * * * root /usr/sbin/ntpdate 192.168.0.240;/sbin/hwclock -w」>>/etc/crontab
每小時同NTP server進行一次時鍾同步,並寫入本機BIOS
3、如何在centos 7中設置ntp 服務
確認已經ntp程序包:# yum install ntp
配置時間源# vi /etc/ntp.conf server time.lib.tsinghua.e.cn server 0.pool.ntp.org server 1.pool.ntp.org server 2.pool.ntp.org
配置是否為其他PC提供時間服務# vi /etc/ntp.conf restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
配置開機時自動運行時間服務# chkconfig ntpd on
啟動或停止時間服務# service ntpd start # service ntpd stop # service ntpd restart
驗證ntp服務已經運行# pgrep ntpd
初始同步# ntpdate -u time.lib.tsinghua.e.cn
確認同步成功# ntpq -p
如果要提供時間服務,還必須相應的設置iptable防火牆的配置。CentOS的時間服務使用udp 123埠。
4、centos6.3 ntp時間同步怎麼配置 ?
一、伺服器配置
1 安裝ntp
yum install ntp -y
2 修改ntp配置文件
vi /etc/ntp.conf
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
#server 2.centos.pool.ntp.org
server 0.asia.pool.ntp.org
server 1.asia.pool.ntp.org
server 2.asia.pool.ntp.org
server 3.asia.pool.ntp.org
註:服務端的配置文件內網的配置不略有不同,如果內網有很多機器,只需要配置一台外網機器服務端,然後配置內網的,安裝方式都一樣,只是內網的配置文件/etc/ntp.conf略有不同
nomodify - 用戶端不能更改ntp伺服器的時間參數
noquery - 用戶端不能使用ntpq,ntpc等命令來查詢ntp伺服器
notrap - 不提供trap遠端登陸
3 啟動
/etc/rc.d/init.d/ntpd start
4 設置開機啟動
chkconfig ntpd on
5 查詢網路中的NTP伺服器,同時顯示客戶端和每個伺服器的關系
ntpq -p 其中,
remote - 本機和上層ntp的ip或主機名,「+」表示優先,「*」表示次優先
refid - 參考上一層ntp主機地址
st - stratum階層
when - 多少秒前曾經同步過時間
poll - 下次更新在多少秒後
reach - 已經向上層ntp伺服器要求更新的次數
delay - 網路延遲
offset - 時間補償
jitter - 系統時間與bios時間差
6 內網客戶端配置
安裝及啟動方式都一樣,只是server的配置不一樣,把server的配置,修改成連接外網的那台內網IP
server 192.168.1.8
這是我之前安裝的記錄,詳細你可以參考:
http://mnt.conf.blog.163.com/blog/static/115668258201432492034844/
5、如何在 CentOS 中設置 NTP 伺服器
一、搭建時間伺服器
1、在一台linux伺服器安裝ntp server
tar zxvf ntp-4.2.6.tar.gz
cd ntp-4.2.6
./configure --prefix=/usr/local/ntp --enable-all-clocks --enable-parse-clocks
make && make install
2、修改ntp.conf配置文件
vi /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 default nomodify
(允許任何IP的客戶機都可以進行時間同步,如果是只允許某個網段的客戶機進行時間同步可以這樣寫
restrict 10.58.26.0 mask 255.255.255.0 nomodify)
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
#server 0.rhel.pool.ntp.org(默認時間伺服器)
#server 1.rhel.pool.ntp.org(默認時間伺服器)
#server 2.rhel.pool.ntp.org(默認時間伺服器)
server 10.128.14.25 (手工設置的時間伺服器)
(如果是可以直連外網,可以使用LINUX默認提供的三組標准時間伺服器,否則可以自己指定一個同步時間源)
#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 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
3、以守護進程啟動ntpd
#/etc/rc.d/init.d/ntpd -c /etc/ntp.conf -p /tmp/ntpd.pid
#/etc/rc.d/init.d/ntpd start
#ps -ef|grep ntpd
4、
在ntp server上啟動ntp服務後,ntp
server自身或者與其server的同步的需要一個時間段,這個過程可能是5分鍾,在這個時間之內在客戶端運行ntpdate命令進行同步時會產生
no server suitable for synchronization found的錯誤。
下面命令可以知道何時ntp server完成了和自身同步的過程
在ntp server上使用命令:
# watch ntpq -p
注意LOCAL的這個就是與自身同步的ntp server。
注意reach這個值,在啟動ntp server服務後,這個值就從0開始不斷增加,當增加到17的時候,從0到17是5次的變更,每一次是poll的值的秒數,是64秒*5=320秒的時間。
二、配置時間同步客戶機
vi /var/spool/cron/root(或crontab -e)
增加一行,在每天的1點10分、9點10分、17點10分與時間同步伺服器進行同步並寫入BIOS
10 1 ,9,17* * * root /usr/sbin/ntpdate 10.128.14.25; /sbin/hwclock -w
如果同步不正常,可以加輸出日誌或看系統日誌
輸出日誌的方法:
10 1 ,9,17* * * root /usr/sbin/ntpdate 10.128.14.25>>/tmp/1.txt; /sbin/hwclock -w
在1.txt中可查看時間同步時的輸出結果。
或者看/var/mail/root系統日誌
Subject: Cron <root@tyzssq8> /usr/sbin/ntpdate 10.128.14.25;/sbin/hwclock -w
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Message-Id: <[email protected]>
Date: Tue, 27 Nov 2012 18:30:01 +0800 (CST)
27 Nov 18:29:59 ntpdate[6917]: step time server 10.128.14.25 offset -1.361968 sec
可以看到同步成功了,如果未成功會報出錯誤。
三、無法同步的問題
檢查ntp server主機的防火牆。可能是ntp server的防火牆屏蔽了upd 123埠。
可以用命令
#service iptables stop
6、伺服器centos6.5時間不對怎麼改時間的
date命令改:
date -s 20120523 //設置成20120523,這copy樣會把具體時間設置成空00:00:00
date -s 01:01:01 //設置具體時間,不會對日期做更改
date -s "01:01:01 2012-05-23" //這樣可以設置全部時間
date -s "01:01:01 20120523" //這樣可以設置全部時間
date -s "2012-05-23 01:01:01" //這樣可以設置全部時間
date -s "20120523 01:01:01" //這樣可以設置全部時間
7、如何在 CentOS 中設置 NTP 伺服器
備 CentOS 伺服器
現在讓我們來開始在 CentOS 上設置 NTP 伺服器。
首先,我們需要保證正確設置了伺服器的時區。在 CentOS 7 中,我們可以使用 timedatectl 命令查看和更改伺服器的時區(比如,"Australia/Adelaide",LCTT 譯註:中國可設置為 Asia/Shanghai )
# timedatectl list-timezones | grep Australia
# timedatectl set-timezone Australia/Adelaide
# timedatectl
繼續並使用 yum 安裝需要的軟體
# yum install ntp
然後我們會添加全球 NTP 伺服器用於同步時間。
# vim /etc/ntp.conf
server 0.oceania.pool.ntp.org
server 1.oceania.pool.ntp.org
server 2.oceania.pool.ntp.org
server 3.oceania.pool.ntp.org
默認情況下,NTP 伺服器的日誌保存在 /var/log/messages。如果你希望使用自定義的日誌文件,那也可以指定。
logfile /var/log/ntpd.log
如果你選擇自定義日誌文件,確保更改了它的屬主和 SELinux 環境。
# chown ntp:ntp /var/log/ntpd.log
# chcon -t ntpd_log_t /var/log/ntpd.log
現在初始化 NTP 服務並確保把它添加到了開機啟動。
# systemctl restart ntp
# systemctl enable ntp
驗證 NTP Server 時鍾
我們可以使用 ntpq 命令來檢查本地伺服器的時鍾如何通過 NTP 同步。
控制到 NTP 伺服器的訪問
默認情況下,NTP 伺服器允許來自所有主機的查詢。如果你想過濾進來的 NTP 同步連接,你可以在你的防火牆中添加規則過濾流量。
# iptables -A INPUT -s 192.168.1.0/24 -p udp --dport 123 -j ACCEPT
# iptables -A INPUT -p udp --dport 123 -j DROP
該規則允許從 192.168.1.0/24 來的 NTP 流量(埠 UDP/123),任何其它網路的流量會被丟棄。你可以根據需要更改規則。
配置 NTP 客戶端
1. Linux
NTP 客戶端主機需要 ntpupdate 軟體包來和伺服器同步時間。可以輕松地使用 yum 或 apt-get 安裝這個軟體包。安裝完軟體包之後,用伺服器的 IP 地址運行下面的命令。
# ntpdate <server-IP-address>
基於 RHEL 和 Debian 的系統命令都相同。
2. Windows
如果你正在使用 Windows,在日期和時間設置(Date and Time settings)下查找網路時間(Internet Time)。
3. Cisco 設備
如果你想要同步 Cisco 設備的時間,你可以在全局配置模式下使用下面的命令。
# ntp server <server-IP-address>
來自其它廠家的支持 NTP 的設備有自己的用於網路時間的參數。如果你想將設備和 NTP伺服器同步時間,請查看設備的說明文檔。
結論
總而言之,NTP 是在你的所有主機上同步時鍾的一個協議。我們已經介紹了如何設置 NTP 伺服器並使支持 NTP 的設備和伺服器同步時間。
8、centos怎麼設置時間與另一台伺服器時間同步
一,用ntpdate從時間伺服器更新時間
如果你的linux系統根本沒有ntpdate這個命令
yum install ntp
安裝完了之後,你不要做什麼配置,也不需要,直接測試一下
[root@localhost ~]# ntpdate time.nist.gov
22 Oct 21:11:43 ntpdate[5014]: adjust time server 207.200.81.113 offset -0.018788 sec
如果出去上面的內容說明,同步成功了。然後在crontab裡面加上以下內容。
*/10 * * * * ntpdate time.nist.gov #域名或IP
每隔十分鍾同步一次。推薦幾個時間伺服器。
time.nist.gov
time.nuri.net
asia.pool.ntp.org
asia.pool.ntp.org
asia.pool.ntp.org
asia.pool.ntp.org
二,用ntp搭建自己的時間伺服器
上面我們是利用別人的時間伺服器來同步時間,這些時間伺服器都是比較權威的。當我們自己搭建時間伺服器就不用crontab來定時去跑。
1,安裝時間伺服器ntp
yum install ntp
2,配置ntp
查看復制列印?
[root@localhost ~]# cat /etc/ntp.conf |awk '{if($0 !~ /^$/ && $0 !~ /^#/) {print $0}}'
restrict default ignore //默認不允許修改或者查詢ntp,並且不接收特殊封包
restrict 127.0.0.1 //給於本機所有許可權
restrict 192.168.1.0 mask 255.255.255.0 notrap nomodify //給於區域網機的機器有同步時間的許可權
server time.nist.gov prefer //設置時間伺服器,加prefer表示優先
server 0.asia.pool.ntp.org
server 1.asia.pool.ntp.org
server 2.asia.pool.ntp.org
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
driftfile /var/lib/ntp/drift
keys /etc/ntp/keys
3,啟動 ntp
[root@localhost ~]# /etc/init.d/ntpd start
4,查看並測試
[root@localhost ~]# netstat -upnl |grep ntpd //查看時程
[root@localhost ~]# ntpq -pn //查看同步的伺服器IP
remote refid st t when poll reach delay offset jitter
==============================================================================
50.77.217.185 .INIT. 16 u - 64 0 0.000 0.000 0.000
202.90.158.4 .INIT. 16 u - 64 0 0.000 0.000 0.000
202.71.100.89 .INIT. 16 u - 64 0 0.000 0.000 0.000
202.134.1.10 .INIT. 16 u - 64 0 0.000 0.000 0.000
*127.127.1.0 .LOCL. 10 l 18 64 377 0.000 0.000 0.001
[root@localhost ~]# ntpstat //同步的結果
synchronised to local net at stratum 11
time correct to within 12 ms
polling server every 512 s
remote:即NTP主機的IP或主機名稱。注意最左邊的符號,如果由「+」則代表目前正在作用鍾的上層NTP,如果是「*」則表示也有連上線,不過是作為次要聯機的NTP主機。
refid:參考的上一層NTP主機的地址
st:即stratum階層
when:幾秒前曾做過時間同步更新的操作
poll:下次更新在幾秒之後
reach:已經向上層NTP伺服器要求更新的次數
delay:網路傳輸過程鍾延遲的時間
offset:時間補償的結果
jitter:Linux系統時間與BIOS硬體時間的差異時間
9、CentOS7怎麼修改伺服器系統時間
在CentOS 7裡面有一個命令timedatectl可以幫助我們修改伺服器的時區。
1. 查看伺服器里的時間設置 timedatectl ,它等同於 timedatectl status :
?
1
2
3
4
5
6
7
8
9
$ timedatectl
Local time: Mon 2016-08-29 16:20:35 CST
Universal time: Mon 2016-08-29 08:20:35 UTC
RTC time: Mon 2016-08-29 08:18:07
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
2. 了解 timedatectl 命令的各個參數
?
$ timedatectl -h
timedatectl [OPTIONS...] COMMAND ...
Query or change system time and date settings.
-h --help Show this help message
--version Show package version
--no-pager Do not pipe output into a pager
--no-ask-password Do not prompt for password
-H --host=[USER@]HOST Operate on remote host
-M --machine=CONTAINER Operate on local container
--adjust-system-clock Adjust system clock when changing local RTC mode
Commands:
status Show current time settings
set-time TIME Set system time
set-timezone ZONE Set system time zone
list-timezones Show known time zones
set-local-rtc BOOL Control whether RTC is in local time
set-ntp BOOL Control whether NTP is enabled
3. 設置時間?
$ sudo timedatectl set-time 15:26:58$ timedatectl
Local time: Mon 2016-08-29 15:27:00 CST
Universal time: Mon 2016-08-29 07:27:00 UTC
RTC time: Mon 2016-08-29 07:27:00
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
PS:下面看下CentOS修改伺服器系統時間
linux安裝完畢後,一般都是國外的世界,一點都不方便設置任務,或者導致網站獲取本地的時間錯亂,所以就需要把伺服器的時間改為和本地時間一致,也就是換成中國的時間。
第一條指令:date –s '2016-10-31 10:10:10'
第二條指令:clock –w //將日期寫入CMOS
時間按照上面的格式,操作完畢可以再用
date
查看伺服器時間,如果和設置的一樣,就表示成功了
10、如何在 CentOS 中設置 NTP 伺服器
centos 5.6安裝NTP伺服器 為有些服務和實驗需要多台機器的時間同步,所以就有了搭建NTP的前提條件了,具體步驟如下: 1,安裝ntp服務 yum install ntp.i* -y 2,修改/etc/ntp.conf restrict default kod nomodify notrap nopeer noquery # restrict -6 default kod nomodify notrap nopeer noquery #針對ipv6設置 #這是ntp的默認設置 # 允許本地所有操作 restrict 127.0.0.1 restrict -6 ::1 # 允許的區域網絡段 restrict 10.0.0.0 mask 255.0.0.0 nomodify motrap restrict 192.168.0.0 mask 255.255.0.0 nomodify motrap # 使用上層的internet ntp伺服器 restrict 207.46.232.182 mask 255.255.255.255 nomodify notrap noquery server 207.46.232.182 # 如果無法與上層ntp server通信以本地時間為標准時間 server 127.127.1.0 # local clock fudge 127.127.1.0 stratum 10 # 計算本ntp server 與上層ntpserver的頻率誤差 driftfile /var/lib/ntp/drift # Key file containing the keys and key identifiers used when operating # with symmetric key cryptography. keys /etc/ntp/keys #日誌文件 logfile /var/log/ntp.log #說明: restrict <IP 地址> <子網掩碼> <網段> <子網掩碼> [] ignore :關閉所有ntp服務 noquery:不提供ntp服務 notrap:不提供trap遠程事件登錄的功能 nomodiy:表示客戶端不能更改ntp伺服器的時間參數,但可以通過ntp伺服器進行時間矯正 notrust:拒絕沒有通過認證的客戶端 nokod:kod技術可以組織一種dos攻擊 2,在修改/etc/ntp/step-tickers文件 207.46.232.182 127.127.1.0 #當ntp伺服器啟動時自動與這個文件中的記錄ip進行時間校對 3,修改/etc/sysconfig/ntpd SYNC_HWCLOCK=YES #允許BIOS與系統時間同步 4,客戶端設置略。