Linux Set computer time from GPS with GPS Dongle

I had a little problem with my Raspberry Pi Raspbian Wheezy and with Kali-Linux when I wanted to do some wardriving and wanted to have right time/date on my Pi and I didn’t have anykind of Internet connection.

You could get right time from the GPS but I didn’t get it to work with NTP and my GPS receiver but I found a nice script that get’s me around that problem:

date -s ’01/01/2022 00:01′
sleep 1
pkill ntpd
pkill gpsd
gpsd -b -n -D 2 /dev/ttyUSB0
sleep 2
GPSDATE=`gpspipe -w | head -10 | grep TPV | sed -r ‘s/.*"time":"([^"]*)".*//’ | head -1`
date -s "$GPSDATE"

As far as I know this sets your Pi time to GMT but if that is not your local time then you can add this:

FIDATE=`date –date=’+3 hour’`
echo $FIDATE
date -s "$FIDATE"
sleep 1

This one correct time to be my local (Finnish) time which is +3 hours from that GMT which you get from GPS.

So this script first sets your Pi time to 01/01/2022 00:01 because you need to set time near the right time before getting it from the GPS and after that this script reads NMEA data from GPS and grep the time part from it and parse it so that it can be set to your Pi using normal date command. You have to have working GPS before trying this. I have used this for a while with my wardrive Raspberry Pi setup with Raspbian Wheezy and with Kali-Linux distribution.

    Leave a Reply

    Your email address will not be published. Required fields are marked *