Install Network Time Protocol(NTP) on BeagleBone with Angstrom linux and set local time zone


Referenced from : http://cwraig.id.au/?p=513

1. Update your Package List

opkg update

2. Get the ntp Package

opkg install ntp ntpdate

Update 2014/07/03
for the latest beaglebone image(2013-06-20) with kernel 3.8.12, after this installation, 
system will add to service list, so only need to change time zone

3. Now we need to stop the ntpd service so we can request a manual update

/etc/init.d/ntpd stop

manual update

ntpdate pool.ntp.org

type in date, you should see the date is changing to current date and time, but the time zone is not corrected.

The time zone info is contained in /etc/localtime , use this to remove it:
rm /etc/localtime

before link up our time zone, look into /usr/share/zoneinfo folder for your timezone.

I did not find taiwan in asia, not plan to use hong_kong, but there is still:
/usr/share/zoneinfo/Etc/GMT-8

now we make the link with ln command
ln -s /usr/share/zoneinfo/Etc/GMT-8 /etc/localtime

now type date again to check, should have correct local time:
root@beaglebone:/etc# date
Fri Sep 20 21:46:50 GMT-8 2013

now , we need to restart the ntdp service:

/etc/init.d/ntpd start

But this does not start the service when we reboot the board, but this one dose:
http://derekmolloy.ie/automatically-setting-the-beaglebone-black-time-using-ntp/

When you boot the Beaglebone, the time comes up as:

root@beaglebone:~# date
Sat Jan 1 12:27:56 GMT 2000
Which is quite some time ago! I noticed this because I did a git commit and in the github status it said that I did the update 13 years ago!

To set the date once off you could use something like:
root@beaglebone:~# ntpdate -b -s -u pool.ntp.org
root@beaglebone:~# date
Sat May 18 22:52:56 BST 2013

However, if you reboot your Beaglebone the time will be back to the good old year 2000. So, we need a way to update this each time the Beaglebone boots.

we can update our time with pool.ntp.org , which is a root server, pertty busy. a better way is to use our local timer server, just google ntp server taipei, I got:
server 0.tw.pool.ntp.org
server 1.tw.pool.ntp.org
server 2.tw.pool.ntp.org
server 3.tw.pool.ntp.org

now, edit /etc/ntp.conf file to contain these server
my original one is like this:
# This is the most basic ntp configuration file
# The driftfile must remain in a place specific to this
# machine - it records the machine specific clock error
driftfile /etc/ntp.drift
# This obtains a random server which will be close
# (in IP terms) to the machine. Add other servers
# as required, or change this.
server pool.ntp.org
# Using local hardware clock as fallback
# Disable this when using ntpd -q -g -x as ntpdate or it will sync to itself
server 127.127.1.0
fudge 127.127.1.0 stratum 14
# Defining a default security setting
restrict default


modified as following:
# This is the most basic ntp configuration file
# The driftfile must remain in a place specific to this
# machine - it records the machine specific clock error
driftfile /etc/ntp.drift
# This obtains a random server which will be close
# (in IP terms) to the machine. Add other servers
# as required, or change this.
# server pool.ntp.org
server 0.tw.pool.ntp.org
server 1.tw.pool.ntp.org
server 2.tw.pool.ntp.org
server 3.tw.pool.ntp.org
# Using local hardware clock as fallback
# Disable this when using ntpd -q -g -x as ntpdate or it will sync to itself
#server 127.127.1.0
#fudge 127.127.1.0 stratum 14
# Defining a default security setting
restrict default

Time zone setting is using the ln as we saw before, here is the way to see the current setting:

root@beaglebone:/etc# ls -al localtime

now we need to enable 2 services:
ntpdate.service
ntpd.service

the first thing is to add .service files in /lib/systemd/system
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 this one:
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

Then start the service with following command:
root@beaglebone:/etc# systemctl enable ntpdate.service
root@beaglebone:/etc# systemctl enable ntpd.service

now type reboot to start these 2 services.

after boot up, you can type date again to verify if date and time are updated.
or use this command to see the status of service
systemctl status ntpd.service


update 2014 5 15
For ubuntu, the time service is already in it, using date to see date and time.

Method A: using tzselect
then it comes up with a selection menu, and then tell you:
"
You can make this change permanent for yourself by appending the line
        TZ='Asia/Taipei'; export TZ
to the file '.profile' in your home directory; then log out and log in again.

"
Then it comes a command, use this in our command line
ubuntu@arm:~$  TZ='Asia/Taipei'; export TZ

the date again, it's correctly CST(taiwan time) now

But if you reset, date will back to UTC, so we should add to the .profile....
But there is no such a file on my beaglebone ubuntu 1210
so, refer to the method B, I did:
sudo ln -sf /usr/share/zoneinfo/Asia/Taipei /etc/localtime 

Then after reboot the board, the date is correct my Taiwan time....

Method B:
if the time zone is not correct, the changing method is about the same as above(log in as root):

1. Change to the directory /usr/share/zoneinfo. A list of time zone regions will be displayed.
2. back up old setting of time zone: 
mv /etc/localtime /etc/localtime-old
3.Link your machine’s clock to a city in your timezone. Use the following command, replacing the region and city with your appropriate entries: ln -sf /usr/share/zoneinfo/Asia/Taipei /etc/localtime 







留言

這個網誌中的熱門文章

Heltec ESP32+OLED+Lora, hardware testing

micro SD card for ESP32, on lolin32 with OLED and heltec 32 lora oled