7
November
2025
15:50

Setting computer time using GPS on smartphone via SSH

7 November 2025 15:50

This method provides accuracy up to 2-3 seconds, because request-response via ssh takes time.

Introduction. General idea

Setting the date and time is an urgent task if there is no 4G data transmission.

This method uses GPS satellites:

  • First, the smartphone is synchronized via GPS/GLONASS satellites.
  • then the computer connects to the smartphone via ssh and sets the time in the operating system according to the smartphone clock (but the PC clock will eventually lag by 2-3 seconds due to the request-response).

Why not NTP or SNTP server?

  • ВAn Android user is allowed to use ports higher than 1024 inclusive, so in Android it is impossible to run an NTP server so that its port becomes 123. All Linux utilities connect to port 123 for time synchronization.

ПоэI used another option - SSH with an arbitrary port above 1023. In this case, the port suggested by SSHdaemon is used - 8022, but it can be anything in the range 1024-49151.

Checking that the port is listening (open):

nmap 192.168.1.57 -p 1025

It should be like this:
Starting Nmap 7.80 ( https://nmap.org ) at 2025-11-07 16:16 MSK
Nmap scan report for ... (192.168.1.57)
Host is up (0.45s latency).

PORT STATE SERVICE
1025/tcp open NFS-or-IIS

Nmap done: 1 IP address (1 host up) scanned in 0.77 seconds

I used the application as an SSH server in Android https://play.google.com/store/apps/details?id=com.daemon.ssh.

Access to SSH is by password.

Mobile Wi-Fi hotspot or Wi-Fi router, where the PC and smartphone are on the same local network.

Solution

This code was debugged by me in Linux Mint and Android:

sudo date +"%Y-%m-%d %T" --set "$(echo date +'%Y-%m-%d_%T' | sshpass -fpwd ssh -q 192.168.1.57 -p 8022 -l user | sed -e 's/_/ /')" 

where
setpass - a utility for using a password instead of keys,
pwd - file name. which stores the ssh password. pwd can be replaced with other text according to the file name, for example "1" or "a".
ssh - command line utility for connecting to an ssh server,
8022 - ssh server port in Android OS,
-l user - ssh user login name,
q - quiet ssh mode without issuing unnecessary messages,
192.168.1.57 - IP address of the SSH server (for Android),
sed - to replace underscore with space,

date --set - command to set the time on the local computer.

Before running the utility, disable NTP time synchronization over the network:

timedatectl set-ntp false
timedatectl

Explanation of operating principle

On the Internet on forums I came across a simplified way to set the PC time via the SSH console - Secure Socket Shell, through which the command is executed date on a remote PC.

The options are as follows:

date --set="$(ssh user@server date)" 

ssh user@server sudo date -s @`( date -u +"%s" )` 

date +%Y%m%d%T -s "`ssh user@server 'date "+%Y%m%d %T "'`" 

date --set="$(ssh user@server 'date -u')"

I started debugging the first version of the "date --set=" command.

Setting the time manually from the command line in Linux:

sudo date +"%Y-%m-%d %H:%M:%S" --set "2025-11-07 14:40:00" 

Displaying the current date and time in the desired format in Android or Linux

date +"%Y-%m-%d %H:%M:%S" 

(Result: 2025-11-07 16:09:45).

Connecting to an SSH server from Linux

Manual connection with password request:

ssh user@192.168.1.57 -p 8022

or

ssh 192.168.1.57 -p 8022 -l user

where user is the login name of the ssh user on the server to which access is granted,
192.168.1.57 - IP address of the SSH server,
8022 is the TCP port that the SSH server uses.

Automated connection to ssh server with password:

sshpass -fpwd ssh -q 192.168.1.57 -p 8022 -l user

where pwd is a text file in the current directory containing the ssh password.

(Instead of -fpwd, you can use the cleartext password -ppassword, which I do not recommend. There is no space between -f and the file name, or the -p switch and the password. See help man sshpass).

Disadvantage

With this method, the PC clock lags behind world time by 2-3 seconds.

Enabling NTP (if Internet is available)

If the Internet appears, enable native Linux time synchronization (on SystemD-based systems):

timedatectl set-ntp true

View the result of time synchronization:

timedatectl

Checking the deviation of PC time from world time

Website to check PC clock:https://time.is.



Related publications