⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 network-ntp.html

📁 这是很好的学习嵌入式LINUX的文章
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta name="generator" content="HTML Tidy, see www.w3.org" /><title>Clock Synchronization with NTP</title><meta name="GENERATOR" content="Modular DocBook HTML Stylesheet Version 1.7" /><link rel="HOME" title="FreeBSD 使用手册" href="index.html" /><link rel="UP" title="Network Servers" href="network-servers.html" /><link rel="PREVIOUS"title="File and Print Services for Microsoft燱indows clients (Samba)"href="network-samba.html" /><link rel="NEXT" title="高级网络" href="advanced-networking.html" /><link rel="STYLESHEET" type="text/css" href="docbook.css" /><meta http-equiv="Content-Type" content="text/html; charset=GB2312" /></head><body class="SECT1" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#840084"alink="#0000FF"><div class="NAVHEADER"><table summary="Header navigation table" width="100%" border="0" cellpadding="0"cellspacing="0"><tr><th colspan="3" align="center">FreeBSD 使用手册</th></tr><tr><td width="10%" align="left" valign="bottom"><a href="network-samba.html"accesskey="P">后退</a></td><td width="80%" align="center" valign="bottom">章 23. Network Servers</td><td width="10%" align="right" valign="bottom"><a href="advanced-networking.html"accesskey="N">前进</a></td></tr></table><hr align="LEFT" width="100%" /></div><div class="SECT1"><h1 class="SECT1"><a id="NETWORK-NTP" name="NETWORK-NTP">23.10. Clock Synchronizationwith NTP</a></h1><i class="AUTHORGROUP"><span class="CONTRIB">Contributed by</span> Tom Hukins.</i> <div class="SECT2"><h2 class="SECT2"><a id="AEN35138" name="AEN35138">23.10.1. Overview</a></h2><p>Over time, a computer's clock is prone to drift. The Network Time Protocol (NTP) isone way to ensure your clock stays accurate.</p><p>Many Internet services rely on, or greatly benefit from, computers' clocks beingaccurate. For example, a web server may receive requests to send a file if it has beenmodified since a certain time. In a local area network environment, it is essential thatcomputers sharing files from the same file server have synchronized clocks so that filetimestamps stay consistent. Services such as <span class="CITEREFENTRY"><spanclass="REFENTRYTITLE">cron</span>(8)</span> also rely on an accurate system clock to runcommands at the specified times.</p><p>FreeBSD ships with the <span class="CITEREFENTRY"><spanclass="REFENTRYTITLE">ntpd</span>(8)</span> <acronym title="Network Time Protocol"class="ACRONYM">NTP</acronym> server which can be used to query other <acronymtitle="Network Time Protocol" class="ACRONYM">NTP</acronym> servers to set the clock onyour machine or provide time services to others.</p></div><div class="SECT2"><h2 class="SECT2"><a id="AEN35154" name="AEN35154">23.10.2. Choosing Appropriate NTPServers</a></h2><p>In order to synchronize your clock, you will need to find one or more <acronymtitle="Network Time Protocol" class="ACRONYM">NTP</acronym> servers to use. Your networkadministrator or ISP may have set up an NTP server for this purpose--check theirdocumentation to see if this is the case. There is an <ahref="http://www.eecis.udel.edu/~mills/ntp/servers.html" target="_top">online list ofpublicly accessible NTP servers</a> which you can use to find an NTP server near to you.Make sure you are aware of the policy for any servers you choose, and ask for permissionif required.</p><p>Choosing several unconnected NTP servers is a good idea in case one of the servers youare using becomes unreachable or its clock is unreliable. <spanclass="CITEREFENTRY"><span class="REFENTRYTITLE">ntpd</span>(8)</span> uses the responsesit receives from other servers intelligently--it will favor unreliable servers less thanreliable ones.</p></div><div class="SECT2"><h2 class="SECT2"><a id="AEN35166" name="AEN35166">23.10.3. Configuring YourMachine</a></h2><div class="SECT3"><h3 class="SECT3"><a id="AEN35171" name="AEN35171">23.10.3.1. BasicConfiguration</a></h3><p>If you only wish to synchronize your clock when the machine boots up, you can use<span class="CITEREFENTRY"><span class="REFENTRYTITLE">ntpdate</span>(8)</span>. This maybe appropriate for some desktop machines which are frequently rebooted and only requireinfrequent synchronization, but most machines should run <span class="CITEREFENTRY"><spanclass="REFENTRYTITLE">ntpd</span>(8)</span>.</p><p>Using <span class="CITEREFENTRY"><span class="REFENTRYTITLE">ntpdate</span>(8)</span>at boot time is also a good idea for machines that run <span class="CITEREFENTRY"><spanclass="REFENTRYTITLE">ntpd</span>(8)</span>. The <span class="CITEREFENTRY"><spanclass="REFENTRYTITLE">ntpd</span>(8)</span> program changes the clock gradually, whereas<span class="CITEREFENTRY"><span class="REFENTRYTITLE">ntpdate</span>(8)</span> sets theclock, no matter how great the difference between a machine's current clock setting andthe correct time.</p><p>To enable <span class="CITEREFENTRY"><spanclass="REFENTRYTITLE">ntpdate</span>(8)</span> at boot time, add <varclass="LITERAL">ntpdate_enable="YES"</var> to <tt class="FILENAME">/etc/rc.conf</tt>. Youwill also need to specify all servers you wish to synchronize with and any flags to bepassed to <span class="CITEREFENTRY"><span class="REFENTRYTITLE">ntpdate</span>(8)</span>in <var class="VARNAME">ntpdate_flags</var>.</p></div><div class="SECT3"><h3 class="SECT3"><a id="AEN35205" name="AEN35205">23.10.3.2. GeneralConfiguration</a></h3><p>NTP is configured by the <tt class="FILENAME">/etc/ntp.conf</tt> file in the formatdescribed in <span class="CITEREFENTRY"><spanclass="REFENTRYTITLE">ntp.conf</span>(5)</span>. Here is a simple example:</p><pre class="PROGRAMLISTING">server ntplocal.example.com preferserver timeserver.example.orgserver ntp2a.example.netdriftfile /var/db/ntp.drift</pre><p>The <var class="LITERAL">server</var> option specifies which servers are to be used,with one server listed on each line. If a server is specified with the <varclass="LITERAL">prefer</var> argument, as with <ttclass="HOSTID">ntplocal.example.com</tt>, that server is preferred over other servers. Aresponse from a preferred server will be discarded if it differs significantly from otherservers' responses, otherwise it will be used without any consideration to otherresponses. The <var class="LITERAL">prefer</var> argument is normally used for NTPservers that are known to be highly accurate, such as those with special time monitoringhardware.</p><p>The <var class="LITERAL">driftfile</var> option specifies which file is used to storethe system clock's frequency offset. The <span class="CITEREFENTRY"><spanclass="REFENTRYTITLE">ntpd</span>(8)</span> program uses this to automatically compensatefor the clock's natural drift, allowing it to maintain a reasonably correct setting evenif it is cut off from all external time sources for a period of time.</p><p>The <var class="LITERAL">driftfile</var> option specifies which file is used to storeinformation about previous responses from the NTP servers you are using. This filecontains internal information for NTP. It should not be modified by any otherprocess.</p></div><div class="SECT3"><h3 class="SECT3"><a id="AEN35228" name="AEN35228">23.10.3.3. Controlling Access to YourServer</a></h3><p>By default, your NTP server will be accessible to all hosts on the Internet. The <varclass="LITERAL">restrict</var> option in <tt class="FILENAME">/etc/ntp.conf</tt> allowsyou to control which machines can access your server.</p><p>If you want to deny all machines from accessing your NTP server, add the followingline to <tt class="FILENAME">/etc/ntp.conf</tt>:</p><pre class="PROGRAMLISTING">restrict default ignore</pre><p>If you only want to allow machines within your own network to synchronize their clockswith your server, but ensure they are not allowed to configure the server or used aspeers to synchronize against, add</p><pre class="PROGRAMLISTING">restrict 192.168.1.0 mask 255.255.255.0 notrust nomodify notrap</pre><p>instead, where <tt class="HOSTID">192.168.1.0</tt> is an IP address on your networkand <tt class="HOSTID">255.255.255.0</tt> is your network's netmask.</p><p><tt class="FILENAME">/etc/ntp.conf</tt> can contain multiple <varclass="LITERAL">restrict</var> options. For more details, see the <varclass="LITERAL">Access Control Support</var> subsection of <spanclass="CITEREFENTRY"><span class="REFENTRYTITLE">ntp.conf</span>(5)</span>.</p></div></div><div class="SECT2"><h2 class="SECT2"><a id="AEN35248" name="AEN35248">23.10.4. Running the NTPServer</a></h2><p>To ensure the NTP server is started at boot time, add the line <varclass="LITERAL">xntpd_enable="YES"</var> to <tt class="FILENAME">/etc/rc.conf</tt>. Ifyou wish to pass additional flags to <span class="CITEREFENTRY"><spanclass="REFENTRYTITLE">ntpd</span>(8)</span>, edit the <varclass="VARNAME">xntpd_flags</var> parameter in <ttclass="FILENAME">/etc/rc.conf</tt>.</p><p>To start the server without rebooting your machine, run <tt class="COMMAND">ntpd</tt>being sure to specify any additional parameters from <varclass="VARNAME">xntpd_flags</var> in <tt class="FILENAME">/etc/rc.conf</tt>. Forexample:</p><pre class="SCREEN"><samp class="PROMPT">#</samp> <kbd class="USERINPUT">ntpd -p /var/run/ntpd.pid</kbd></pre><div class="NOTE"><blockquote class="NOTE"><p><b>注:</b> Under FreeBSD&nbsp;5.X, various options in <ttclass="FILENAME">/etc/rc.conf</tt> have been renamed. Thus, you have to replace everyinstance of <var class="LITERAL">xntpd</var> with <var class="LITERAL">ntpd</var> in theoptions above.</p></blockquote></div></div><div class="SECT2"><h2 class="SECT2"><a id="AEN35270" name="AEN35270">23.10.5. Using ntpd with a TemporaryInternet Connection</a></h2><p>The <span class="CITEREFENTRY"><span class="REFENTRYTITLE">ntpd</span>(8)</span>program does not need a permanent connection to the Internet to function properly.However, if you have a temporary connection that is configured to dial out on demand, itis a good idea to prevent NTP traffic from triggering a dial out or keeping theconnection alive. If you are using user PPP, you can use <varclass="LITERAL">filter</var> directives in <tt class="FILENAME">/etc/ppp/ppp.conf</tt>.For example:</p><pre class="PROGRAMLISTING"> set filter dial 0 deny udp src eq 123 # Prevent NTP traffic from initiating dial out set filter dial 1 permit 0 0 set filter alive 0 deny udp src eq 123 # Prevent incoming NTP traffic from keeping the connection open set filter alive 1 deny udp dst eq 123 # Prevent outgoing NTP traffic from keeping the connection open set filter alive 2 permit 0/0 0/0</pre><p>For more details see the <var class="LITERAL">PACKET FILTERING</var> section in <spanclass="CITEREFENTRY"><span class="REFENTRYTITLE">ppp</span>(8)</span> and the examples in<tt class="FILENAME">/usr/share/examples/ppp/</tt>.</p><div class="NOTE"><blockquote class="NOTE"><p><b>注:</b> Some Internet access providers block low-numbered ports, preventing NTPfrom functioning since replies never reach your machine.</p></blockquote></div></div><div class="SECT2"><h2 class="SECT2"><a id="AEN35287" name="AEN35287">23.10.6. Further Information</a></h2><p>Documentation for the NTP server can be found in <ttclass="FILENAME">/usr/share/doc/ntp/</tt> in HTML format.</p></div></div><div class="NAVFOOTER"><hr align="LEFT" width="100%" /><table summary="Footer navigation table" width="100%" border="0" cellpadding="0"cellspacing="0"><tr><td width="33%" align="left" valign="top"><a href="network-samba.html"accesskey="P">后退</a></td><td width="34%" align="center" valign="top"><a href="index.html"accesskey="H">起点</a></td><td width="33%" align="right" valign="top"><a href="advanced-networking.html"accesskey="N">前进</a></td></tr><tr><td width="33%" align="left" valign="top">File and Print Services for <spanclass="TRADEMARK">Microsoft</span>&reg;&nbsp;<span class="TRADEMARK">Windows</span>&reg;clients (Samba)</td><td width="34%" align="center" valign="top"><a href="network-servers.html"accesskey="U">上一级</a></td><td width="33%" align="right" valign="top">高级网络</td></tr></table></div></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -