# Setting up ntpd

## hbmartin

Is there any simple way to set up ntpd to poll a time server and set itself accordingly.

Reading the extensive NTP docs have left me knowing more about time keeping theory than I ever though I could know (   :Shocked:  ) but I still don't understand how to do the simple setup described above  :Smile: 

Thanks,

Harold

----------

## kwiqsilver

Add the server to your /etc/ntp.conf file, if you haven't already. Run "rc-update add ntpd default", if you haven't, and it will update automatically for you. Sometimes I've noticed it's too slow to react to a bad discrepancy, so I put a script in /etc/cron.daily/ that just contains "/etc/init.d/ntpd restart", so at midnight the clock gets forced to the ntp value, instead of stepping back slowly (ntp's default method).

----------

## pjp

Moved from Other Things Gentoo.

----------

## ronmon

This was covered pretty thoroughly in another thread a while back but I couldn't find it with a search. Here it is in a nutshell.

Make your /etc/ntp.conf look something like this:

```

restrict default noquery notrust nomodify

restrict 127.0.0.1

restrict 192.168.1.0 mask 255.255.255.0

fudge 127.127.1.0 stratum 3

server 127.127.1.0

server some.timeserver.net minpoll 7 maxpoll 13 prefer

server another.timeserver.net minpoll 7 maxpoll 13

driftfile /etc/ntp.drift

logfile /var/log/ntp.log

```

Replace the bogus server names above with real stratum2 servers. There is a list here. Choose a couple that have a suitable Access Policy and that are close to you. Check for good latency by pinging them. The 'restrict 192.168.1.0' line is the upstream interface (the one going to the internet).

Your /etc/conf.d/ntpd file only needs one line:

```

NTPD_OPTS="-U ntp"

```

If you use dhcp, you want to add the -N option so that it doesn't overwrite your ntp.conf when it starts. Edit your /etc/conf.d/net:

```

dhcpcd_eth0="-N"

```

Start ntpd and make it automatically start at boot:

```

/etc/init.d/ntpd start

rc-update add ntpd default

```

That's all from my dedicated router box. The machines in my LAN are set up the same, except for a slightly different ntp.conf that synchronizes first, and more frequently, with my router:

```

restrict default noquery notrust nomodify

restrict 127.0.0.1

restrict 192.168.0.0 mask 255.255.255.0

fudge 127.127.1.0 stratum 3

server 127.127.1.0

server myrouter minpoll 4 maxpoll 10 prefer

server some.timeserver.net minpoll 9 maxpoll 15

server another.timeserver.net minpoll 9 maxpoll 15

driftfile /etc/ntp.drift

logfile /var/log/ntp.log

```

----------

## hbmartin

How can I force it to immediately synchronize with the time server instead of gradually changing the time?

Thanks,

Harold

----------

## nikai

 *hbmartin wrote:*   

> How can I force it to immediately synchronize with the time server instead of gradually changing the time?

 

 ntpdate <server>

----------

## nahpets

the ntpd daemon is for gradually correcting the time in small increments and will complain if the time difference is too large (> 1000).  My time is always off by 6-10 hours when I reboot, and I found a simple solution.

You'll notice that /etc/conf.d/ has two ntp related configuration files: ntp-client.conf and ntpd.conf.  To get your time to be set properly when you boot, edit "ntp-client.conf" to set it up properly, then do:

```

# rc-update add ntp-client default

```

This will make "ntpdate" correctly set the system time, and "ntpd" will be started afterwards.

This is a better solution than adding a ntpdate cronjob.

nahpets

----------

## allan_q

 *nahpets wrote:*   

> the ntpd daemon is for gradually correcting the time in small increments and will complain if the time difference is too large (> 1000).  My time is always off by 6-10 hours when I reboot, and I found a simple solution.

 

One solution to the time difference is to use the -g flag for ntpd. My /etc/conf.d/ntpd contains the following:

```
NTPD_OPTS="-U ntp -g"
```

This flag disregards the >1000s sanity limit and syncs your time with the NTP server once--similar to ntpdate. After that, it keeps the time synced and will exit if the time diff is >1000s again (something's wrong with your clock  :Wink: ). I would only need to add ntpd into the default runlevel with:

```
rc-update add ntpd default
```

The manpage for ntpd and ntpdate mentions that ntpdate is to be retired. To get the same functionallity, it recommends you use ntpd with the "-g -q" flags to set the clock and exit.

----------

## nahpets

I read that in the manpages too, and I did try the "-g" option but it didn't work... no idea why.  The only way I was able to get the time set correctly was using ntpdate.  Maybe the "-g" option isn't fully implemented yet???

nahpets

----------

## allan_q

Hmm...you may need to set the iburst keyword to get ntpd to set the clock faster.

 *man ntpd wrote:*   

> The  -q   option is intended for this purpose. Setting this option will cause ntpd  to exit just after setting the clock for  the  first  time. The procedure for initially setting the clock is the same as in continuous mode; most applications will probably want to specify the iburst keyword  with  the  server configuration command. With this keyword a volley of messages are exchanged to groom the data and the clock is set in  about  a minute. If nothing is heard after a couple of minutes, the daemon times out and exits.
> 
> 

 

Underscore mine. The ntp.conf would then have the server line listed as:

```
server x.x.x.x iburst
```

As the manpage says, this is best if you use the -q flag. Leave it out if you plan to use ntpd continuously so it can slowly step the clock.

----------

## GlennM

Can someone tell me why my ntp server is setting itself as stratum 16? This by itself is not a problem, except that other clients refuse to sync with it. My ntp.conf file is pretty straightforward:

```
restrict 127.0.0.1

restrict 192.168.1.0 mask 255.255.255.0 notrust nomodify notrap

server  127.127.1.0

fudge   127.127.1.0 stratum 3

server  ns.nts.umn.edu

server  ntp-2.cso.uiuc.edu

driftfile /etc/ntp/drift

broadcastdelay  0.008
```

After running for a while, ntptrace tells me that it is stratum 16. This is kind of worthless  :Smile:  Is there some way to force the stratum level?

----------

## allan_q

The fudge line is setting your local ntpd as stratum 3 (if no other servers are reachable). Your config looks okay. What's the output of:

```
ntpq -p
```

Also, comment out the restrict lines and restart ntpd. Stratum 16 can also mean that the server is unreachable.

----------

## GlennM

Ooh, hey, something's different  :Smile:  Once I let it run a while with that fudge line, it's all set up nice now. Plus, by commenting out the restrict lines, my other computer can sync to it, too! Thanks much!

So, clearly I screwed something up with the restrict, my intention was to allow other computers on this subnet (192.168.1.) to sync with it. What exactly should I use?

----------

## allan_q

Sorry. I don't know how to get clients to sync to your ntpd. I can suggest checking out the access control options. On my PC, it's located at:

```
file:///usr/share/doc/ntp-4.1.2/html/accopt.htm
```

The path may be different on yours. At least you've narrowed it down to those restrict lines.

----------

## cyberpatrol

 *nikai wrote:*   

>  *hbmartin wrote:*   How can I force it to immediately synchronize with the time server instead of gradually changing the time? 
> 
>  ntpdate <server>

 

This is what the ntp documentation says about ntpdate: "After a suitable period of mourning, the ntpdate program is to be retired from this distribution."

ntpd -q should be used instead. It does the same with only one difference. ntpd -q doesn't expect the server name given as a parameter, instead it uses /etc/ntp.conf.

----------

## daBrado

concerning ntpdate...

I was trying to replace the use of 'ntpdate' in the ntp-client init script, and instead use 'ntpd -gq'.  However, ntpdate updates instantly, while 'ntpd -gq' takes around a minute to update.  This is rather irritating when starting up.  Is there some way to get ntpd to actually act like ntpdate, including speed of update?  And yes, my server line in my ntp.conf file includes the 'iburst' option. 

Any help would be appriciated...

----------

## tomk

Have you uncommented the NTPDATE_CMD="ntpdate" line in /etc/conf.d/ntpd ? This will run ntpdate when ntpd is startede up.

HTH

----------

## allan_q

 *daBrado wrote:*   

> However, ntpdate updates instantly, while 'ntpd -gq' takes around a minute to update.  This is rather irritating when starting up.  Is there some way to get ntpd to actually act like ntpdate, including speed of update?  And yes, my server line in my ntp.conf file includes the 'iburst' option. 
> 
> 

 

Digging around Google Groups, it looks like using 'ntpq -gq' is slower than ntpdate. Two relevant discussions are from Aug. 2002 and Apr. 2003. One poster recommends using '-N high' to increase the priority although I don't think it'll help. I guess until they retire ntpdate, it's the best solution for you. But from this post, they seemed to be very eager to get rid of it.

EDIT: *tomk wrote:*   

> Have you uncommented the NTPDATE_CMD="ntpdate" line in /etc/conf.d/ntpd ? This will run ntpdate when ntpd is startede up. 

 

Using ntpdate would work but it is now recommended to use ntpd since ntpdate may be retired. Unfortunately, as daBrado pointed out, it takes more time to sync up the clock.

----------

## Mayhem

Thank you all, i now show up in time for my appointments.  :Wink: 

----------

## daBrado

 *allan_q wrote:*   

> Digging around Google Groups, it looks like using 'ntpq -gq' is slower than ntpdate. Two relevant discussions are from Aug. 2002 and Apr. 2003. One poster recommends using '-N high' to increase the priority although I don't think it'll help. I guess until they retire ntpdate, it's the best solution for you. But from this post, they seemed to be very eager to get rid of it.
> 
> 

 

Hmm... thanks for the links, and for digging around for me  :Embarassed: .  I see from that last discussion, the development version is supposed to be faster.  That is the development version as of 2003-04-14, which if I can discern anything from the archive, is the current release version and the version Gentoo is using (4.1.2)...

However, I am not getting the 6 seconds that the guy reports, and I'm pretty sure it wasn't timing out (as it seemed to be successful).

So I guess my next question is: does anybody else notice a long (greater than 6 second) delay when using "ntp -q -g" ?

----------

## Mayhem

takes ages for me... then again im using it on a broken imac rev. a, so every time i boot my machine my clock is off by many hours.

----------

## jonnymalm

Great HowTo here

https://forums.gentoo.org/viewtopic.php?t=41099&highlight=ntp

----------

