Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Set the NTP server of your android device
tags
Android
Android Things

Set the NTP server of your android device

Just use this command to set a server address to automatically sync your device time. ( ͡° ͜ʖ ͡°)✧

$ adb shell settings put global ntp_server <new-ntp-server>

Background

I have a Raspberry Pi 3 with Android Things installed on it. But the time is never synchronized...

After googling it, I found this piece of code in NtpTrustedTime.java

public class NtpTrustedTime implements TrustedTime {
    ...
    public static synchronized NtpTrustedTime getInstance(Context context) {
        if (sSingleton == null) {
            final Resources res = context.getResources();
            final ContentResolver resolver = context.getContentResolver();
            final String defaultServer = res.getString(
                    com.android.internal.R.string.config_ntpServer);
            final long defaultTimeout = res.getInteger(
                    com.android.internal.R.integer.config_ntpTimeout);
            final String secureServer = Settings.Global.getString(
                    resolver, Settings.Global.NTP_SERVER);
            final long timeout = Settings.Global.getLong(
                    resolver, Settings.Global.NTP_TIMEOUT, defaultTimeout);
            final String server = secureServer != null ? secureServer : defaultServer;
            sSingleton = new NtpTrustedTime(server, timeout);
            sContext = context;
        }
        return sSingleton;
    }
}

It shows that android tring to get NTP server from:

  1. Settings.Global.NTP_SERVER first

  2. Then com.android.internal.R.string.config_ntpServer

And the NTP server of my Android Things is time.android.com, that's why it dose not work (I'm in China...).


Fortunately, it is easy to set settings via adb:

$ adb shell settings put global ntp_server asia.pool.ntp.org

# test
$ adb shell settings get global ntp_server 
> asia.pool.ntp.org

🎉 CHEERS

@kylin17
Copy link

kylin17 commented May 18, 2018

How to sync device time from ntp server immediately?

@xujiaao
Copy link
Author

xujiaao commented May 21, 2018

@kylin17

Sorry, I'm not familiar with that. If you are woking with Android Things, you can simply restart it...

@kylin17
Copy link

kylin17 commented May 23, 2018

@xujiaao
Ok, I will try. Thanks!

@chetstone
Copy link

chetstone commented Sep 6, 2018

Works for my android phone too! Thanks!

@mellsaid
Copy link

mellsaid commented Sep 13, 2018

Awesome!!! Few questions plz @xujiaao

  • Do we need to run this command every time we restart or power off the device?
  • Is there a way to permanently change the NTP server?
  • Also is there a way to add a list of servers like :

0.uk.pool.ntp.org
1.uk.pool.ntp.org
2.uk.pool.ntp.org
3.uk.pool.ntp.org

@xujiaao
Copy link
Author

xujiaao commented Sep 26, 2018

Awesome!!! Few questions plz @xujiaao

  • Do we need to run this command every time we restart or power off the device?
  • Is there a way to permanently change the NTP server?
  • Also is there a way to add a list of servers like :

0.uk.pool.ntp.org
1.uk.pool.ntp.org
2.uk.pool.ntp.org
3.uk.pool.ntp.org

You do not need to run the command after restarting (Tested on my Android Phone)...

$ adb shell settings put global ntp_server asia.pool.ntp.org
$ adb shell settings get global ntp_server
> asia.pool.ntp.org

# restart the device
$ adb shell settings get global ntp_server
> asia.pool.ntp.org

But I do not think Android can support multiple NTP servers ... See the code above 😄

@majidorc
Copy link

majidorc commented Jun 25, 2021

Awesome!!! Few questions plz @xujiaao

  • Do we need to run this command every time we restart or power off the device?
  • Is there a way to permanently change the NTP server?
  • Also is there a way to add a list of servers like :

0.uk.pool.ntp.org
1.uk.pool.ntp.org
2.uk.pool.ntp.org
3.uk.pool.ntp.org

You do not need to run the command after restarting (Tested on my Android Phone)...

$ adb shell settings put global ntp_server asia.pool.ntp.org
$ adb shell settings get global ntp_server
> asia.pool.ntp.org

# restart the device
$ adb shell settings get global ntp_server
> asia.pool.ntp.org

But I do not think Android can support multiple NTP servers ... See the code above 😄

thank you.

@becker666
Copy link

becker666 commented Jul 31, 2021

Guys I need a NTP server for a lab project with no access to the net is it possible to set it so it just picks up the android time

@mdavids
Copy link

mdavids commented Aug 4, 2021

If you also want to use IPv6 to contact the NTP pool, use 2.pool.ntp.org.

@eladkarako
Copy link

eladkarako commented Nov 6, 2021

pool.ntp.org isn't working for me for some reason but time.google.com does.

https://time.google.com has some instructions as well.

and it seems you can also use /etc/ntp.conf

@elearningdienst
Copy link

elearningdienst commented Nov 10, 2021

Works well on a Pixel 5 with Android 12. Thanks!

@wsion
Copy link

wsion commented Nov 19, 2021

A stable NTP server for mainland China
ntp.aliyun.com

@bjnews
Copy link

bjnews commented Dec 16, 2021

$ adb shell settings put global ntp_server asia.pool.ntp.org

working...thanx

@H-Kz
Copy link

H-Kz commented Feb 15, 2022

It worked on my Sony Xperia (Android11)! thanks

is there a way to add a list of servers

I found "ntp_server_2" on settings.
Also, I could put the same NTP server address on "ntp_server" & "ntp_server_2".
it might be helpful for someone.

~$ adb shell settings list global | grep ntp
ntp_server=MY.NTP.SERVER.ADDRESS
ntp_server_2=persist.vendor.ntp.svr_2

~$ adb shell settings put global ntp_server_2 MY.NTP.SERVER.ADDRESS

~$ adb shell settings list global|grep ntp
ntp_server=MY.NTP.SERVER.ADDRESS
ntp_server_2=MY.NTP.SERVER.ADDRESS

@fusionneur
Copy link

fusionneur commented Mar 4, 2022

"I found "ntp_server_2" on settings." -- same for oneplus 7t pro (android 11).

In my case, even after changing both settings and rebooting, the system pid 1000 still attempts connecting to time.android.com and i see no other process attempting to connect to the ntp server i set.

Anyone experiencing the same? Are there any other workarounds?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment