打印

用NTP服务器自动设置BB Black的时间

[复制链接]
121|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
电员师|  楼主 | 2018-8-28 15:34 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
ck, AC, ST, TE, pd
每次启动手动设置时间



我当前的系统版本没有安装用于设置时间的NTP服务。由于板子不带电池,所以只有在板子运行时设置时间。你可以用以下方法来设置时间。



在启动时,系统时间显示为:





root@beaglebone:~# date

Sat Jan  1 12:27:56 GMT 2000





这是猴年马月以前的时间了!这个时间是我在执行git commit后发现的,因为github status里面提示说我最近一次更新是在13年前!



你可以用以下命令来修改时间:





root@beaglebone:~# ntpdate -b -s -u pool.ntp.org

root@beaglebone:~# date

Sat May 18 22:52:56 BST 2013





但是重新启动BB Black以后,时间又回到2000年了。所以,我们需要在每次启动的时候都能够更新时间的方法。





安装NTP软件





接下来需要在BB Black上安装NTP软件。输入命令:(我用的Angstrom系统)





opkg update

opkg list|grep ntp

opkg install ntp



接下来找到离你最近的NTP服务器。根服务器已经严重超过负荷,所以不推荐选择。

在浏览器中输入地址www点pool点ntp点org/zone/cn就能够看到以下服务器:





server 0.cn.pool.ntp.org

server 1.asia.pool.ntp.org

server 3.asia.pool.ntp.org



然后把这些服务器加入到/etc/ntp.conf 文件中,编辑内容如下:



# 这是最基本的NTP配置文件

# driftfile文件必须保存在指定位置

# machine – 记录与machine相关的时钟错误



driftfile /etc/ntp.drift

logfile /var/log/ntpd.log



# www点pool.ntp.org的NTP服务器

server 0.cn.pool.ntp.org

server 1.asia.pool.ntp.org

server 3.asia.pool.ntp.org



# 用一个本地硬件时钟作为备用

# 在使用ntpd -q -g –x来更新时禁用该服务,否则系统只会与自身同步时间

# 本地时钟源 127.127.1.0

# 127.127.1.0时钟源的时钟层数为14

# 定义默认安全设置



restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap



driftfile指定了用于保存NTP服务器历史响应信息的文件。该文件包含了NTP的内部信息,位置在/etc/ntp.drift。

如果你只需要网络内部的设备与服务器同步时钟,并且要防止他们对服务器进行配置,就要按照以上命令进行限制,上面的192.168.1.0是我的网段地址,子网掩码是255.255.255.0。



请注意 – 一定要在本地时钟源和时钟源层数的代码前加注释符号,不然系统只会与自身进行同步!



logfile这个文件非常有用,可以在出现问题时查看/var/log/ntpd.log来了问题所在。



根据你所在的时区来设置localtime文件



下面需要根据你自己的时区来设置/etc/localtime文件。在/usr/share/zoneinfo目录下有很多不同的时区文件,例如:



root@beaglebone:/usr/share/zoneinfo# ls

Africa     CET      EST5EDT  GMT    Greenwich  MST7MDT  PST8PDT  UCT        WET

America    CST6CDT  Etc      GMT+0  HST        NZ       Pacific  UTC        Zulu

Asia       EET      Europe   GMT-0  MET        NZ-CHAT  ROC      Universal  iso3166.tab

Australia  EST      GB       GMT0   MST        PRC      ROK      W-SU       zone.tab



你可以直接复制文件或者用预设值。这里我们选欧洲。



root@beaglebone:/usr/share/zoneinfo# cd Europe/

root@beaglebone:/usr/share/zoneinfo/Europe# ls -al

total 20

drwxr-xr-x 2 root root 4096 Mar 18  2013 .

drwxr-xr-x 9 root root 4096 Jan  1 11:42 ..

-rw-r--r-- 1 root root 3661 Mar 19  2013 London

-rw-r--r-- 1 root root 1464 Mar 19  2013 Moscow

-rw-r--r-- 1 root root 2945 Mar 19  2013 Paris



在显示的列表中选择离你最近的地区,比如London,然后回到/etc目录下,执行:



root@beaglebone:/etc# rm localtime

root@beaglebone:/etc# ln -s /usr/share/zoneinfo/Europe/London /etc/localtime



设置/etc/localtime到时区文件映射链接与简单地复制文件相比,好处在于可以用ls命令来查看时区信息:



root@beaglebone:/etc# ls -al|grep localtime

root@beaglebone:/etc# ls -al localtime



好了,下面我们来启动NTP服务。请按照以下步骤:



启动NTP服务



执行以下命令来启用NTPD服务器



root@beaglebone:/etc# systemctl enable ntpdate.service

root@beaglebone:/etc# systemctl enable ntpd.service



这里启用了两个服务:



root@beaglebone:/lib/systemd/system# more ntpd.service

[Unit]

Description=Network Time Service

After=network.target



[Service]

Type=forking

PIDFile=/run/ntpd.pid

ExecStart=/usr/bin/ntpd -p /run/ntpd.pid



[Install]

WantedBy=multi-user.target

and a second service:



第二个服务是:

root@beaglebone:/lib/systemd/system# more ntpdate.service

[Unit]

Description=Network Time Service (one-shot ntpdate mode)

Before=ntpd.service



[Service]

Type=oneshot

ExecStart=/usr/bin/ntpdate-sync silent

RemainAfterExit=yes



[Install]

WantedBy=multi-user.target



这里重要的来了,你需要将第二个服务修改为:



root@beaglebone:/lib/systemd/system# more ntpdate.service

[Unit]

Description=Network Time Service (one-shot ntpdate mode)

Before=ntpd.service



[Service]

Type=oneshot

ExecStart=/usr/bin/ntpd -q -g -x

RemainAfterExit=yes



[Install]

WantedBy=multi-user.target



即,将“ExecStart=/usr/bin/ntpdate-sync silent”换成“ExecStart=/usr/bin/ntpd -q -g -x”。这样时间才会自动更新。关于原因可以参考网页linux点die点net/man/8/ntpd的内容。



现在大部分操作系统和硬件都包含了一个时间芯片,用来在电源关闭时维持时间。当设备启动时,该芯片可以初始化操作系统的时间。在设备与NTP服务器同步后,操作系统会反过来校正该芯片的时间。在没有安装这种芯片的情况下或者由于某种原因芯片时间与服务器时间相差超过1000秒,那么ntpd服务会认为发生重大错误,这时最可靠的方式是手动设置时间。但是这将导致ntpd服务推出,并在系统日志中保存一个错误信息记录。参数-g能够绕过芯片时间直接设置为服务器时间。然而为了避免硬件问题带来的影响,比如CMOS电池掉电或者时钟计数器工作不正常等,一旦时钟设定后,大于1000秒的误差将导致ntpd服务退出。



最后,重新启动后时间就正常了:



root@beaglebone:~# reboot



注意:如果SSH客户端在重启后仍然运行,请输入~,这样可以终止SSH 客户端进程。



在系统启动后,执行:



root@beaglebone:/lib/systemd/system# date

Fri Apr 18 23:27:49 CTT 2014



差不多就这样了。记住选最近的NTP服务器。



修复硬件时钟



有人发现完成以上工作以后,RTC时间还是不对,所以我又该了一下配置,貌似可行,虽然有点取巧,但是能用就行。



修改ntpdate.service文件如下:



[Unit]

Description=Network Time Service (one-shot ntpdate mode)

Before=ntpd.service



[Service]

Type=oneshot

ExecStart=/usr/bin/ntpd -q -g -x

ExecStart=/sbin/hwclock --systohc

RemainAfterExit=yes



[Install]

WantedBy=multi-user.target

root@beaglebone:/lib/systemd/system#



调用hwclock并让它使用系统时间来设置硬件时钟。当你重启并调用timedateectl时,可以看到以下输出:

root@beaglebone:/lib/systemd/system# timedatectl

Local time: Sun 2013-06-09 01:06:26 BST

Universal time: Sun 2013-06-09 00:06:26 UTC

RTC time: Sun 2013-06-09 01:06:26

Timezone: Europe/London (BST, +0100)

NTP enabled: no

NTP synchronized: yes

RTC in local TZ: no

DST active: yes

Last DST change: DST began at

                  Sun 2013-03-31 00:59:59 GMT

                  Sun 2013-03-31 02:00:00 BST

Next DST change: DST ends (the clock jumps one hour backwards) at

                  Sun 2013-10-27 01:59:59 BST

                  Sun 2013-10-27 01:00:00 GMT

root@beaglebone:/lib/systemd/system#



可以看出RTC时间与本地时间已经相同了,所有信息都正常了。






NTP, 时间同步, BB-Black, 服务器, RTC

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

471

主题

480

帖子

0

粉丝