ubuntu下ntp源码编译和安装

1. 下载和解压

wget https://www.eecis.udel.edu/~ntp/ntp_spool/ntp4/ntp-4.2/ntp-4.2.8p11.tar.gz
tar -xzvf ntp-4.2.8p11.tar.gz

2. 编译源码 

mkdir /usr/local/ntp
cd ntp-4.2.8p11
./configure --prefix=/usr/local/ntp --enable-all-clocks --enable-parse-clocks

make
sudo make install

3. 配置文件

   /etc/ntp.conf,没有就创建一个

# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift

# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable

# Specify one or more NTP servers.

# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information.
pool 0.ubuntu.pool.ntp.org iburst
pool 1.ubuntu.pool.ntp.org iburst
pool 2.ubuntu.pool.ntp.org iburst
pool 3.ubuntu.pool.ntp.org iburst

# Use Ubuntu's ntp server as a fallback.
pool ntp.ubuntu.com

# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
# details.  The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
# might also be helpful.
#
# Note that "restrict" applies to both servers and clients, so a configuration
# that might be intended to block requests from certain clients could also end
# up blocking replies from your own upstream servers.

# By default, exchange time with everybody, but don't allow configuration.
restrict -4 default kod notrap nomodify nopeer noquery limited
restrict -6 default kod notrap nomodify nopeer noquery limited
#restrict default nomodify

# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1

# Needed for adding pool entries
restrict source notrap nomodify noquery

# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
#restrict 192.168.123.0 mask 255.255.255.0 notrust


# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
#broadcast 192.168.123.255

# If you want to listen to time broadcasts on your local subnet, de-comment the
# next lines.  Please do this only if you trust everybody on the network!
#disable auth
#broadcastclient

#Changes recquired to use pps synchonisation as explained in documentation:
#http://www.ntp.org/ntpfaq/NTP-s-config-adv.htm#AEN3918

#server 127.127.8.1 mode 135 prefer    # Meinberg GPS167 with PPS
#fudge 127.127.8.1 time1 0.0042        # relative to PPS for my hardware

#server 127.127.22.1                   # ATOM(PPS)
#fudge 127.127.22.1 flag3 1            # enable PPS API

server ntp2.aliyun.com
fudge  127.127.1.0 stratum 10

1)允许任何IP的客户机都可以进行时间同步

将“restrictdefault kod nomodify notrap nopeer noquery”这行修改成:

restrict -4 default kod notrap nomodify nopeer noquery limited
restrict -6 default kod notrap nomodify nopeer noquery limited

或者

restrict default nomodify

2)只允许192.168.1.*网段的客户机进行时间同步

在“restrictdefault nomodify notrap noquery(表示默认拒绝所有IP的时间同步)”之后增加一行:

restrict 192.168.1.0 mask 255.255.255.0nomodify

4. 启动ntp服务

/usr/local/ntp/bin/ntpd-c /etc/ntp.conf -p /tmp/ntpd.pid

也可以
cp /usr/local/ntp/bin/ntpd /etc/init.d/ntpd
service ntpd start

5. 查看ntp服务器工作情况

root@chendh-vm:/ntp/ntp-4.2.8p11# netstat -anptu | grep ntp
udp        0      0 192.168.1.11:123        0.0.0.0:*                           28836/ntpd      
udp        0      0 127.0.0.1:123           0.0.0.0:*                           28836/ntpd      
udp        0      0 0.0.0.0:123             0.0.0.0:*                           28836/ntpd      
udp6       0      0 fe80::7616:9f3b:688:123 :::*                                28836/ntpd      
udp6       0      0 ::1:123                 :::*                                28836/ntpd      
udp6       0      0 :::123                  :::*                                28836/ntpd 

6. 客户端ntp同步时间

ntpdate 192.168.1.11 #192.168.1.11是NTP服务器的IP

其他

  使用timedatectl来观察是否启用了set-ntp;

  使用timedatectl set-ntp no 命令关掉set-ntp,这样可以用date命令修改时间,然后测试ntp。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值