Skip to content

Instantly share code, notes, and snippets.

@xujiaao
Last active April 17, 2024 03:40
Show Gist options
  • Star 96 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save xujiaao/63cb3bbea9fe22e79206e5eb7ba82d0e to your computer and use it in GitHub Desktop.
Save xujiaao/63cb3bbea9fe22e79206e5eb7ba82d0e to your computer and use it in GitHub Desktop.
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

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

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

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.

@elearningdienst
Copy link

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

@Uj947nXmRqV2nRaWshKtHzTvckUUpD

"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?

@smed79
Copy link

smed79 commented Sep 24, 2022

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

Return the error message: Bad arguments

Fixed by:

$ adb shell settings put --user 0 global ntp_server europe.pool.ntp.org

Thank you.

@cosmingarbau
Copy link

I have 3 NTP addresses.

$ adb shell settings list global | grep ntp
ntp_server=<NTP_Server_Address_1>
ntp_server_2=<NTP_Server_Address_2>
ntp_server_dhcpopt=<NTP_Server_Address_3>

If the first NTP server is unreachable, the device tries to connect to the first one, after which it connects to the next NTP server.
Now, the command adb shell settings get global ntp_server returns the first NTP server that is unreachable, it does not return the server to which it is connected.
Is there any adb command to know which NTP server it is connected to?

Thanks

@Uj947nXmRqV2nRaWshKtHzTvckUUpD
Copy link

adb shell settings get global --- instructs on getting that specific config only and it is the expected behavior
to see how ntp actually connects using the current configurations, you would need to see the sync in action

There are actually more ntp servers used on android. System uses time.android.com (defined normally by the global settings vars .. not 100% sure) while gps uses whatever is defined on gps.conf. For example i created magisk module to force gps to use pool.ntp.org or time.xtracloud.net.

Some things you can do:

  • for eg. you can use 3c network monitor, but since ntp sync does not happen so often you will not be able to see it in real time easily
  • you might need a script to watch for connections on port 123 and log them
  • or you can use some burp packet listener to monitor traffic from phone on pc
  • best bet is to use a vpn firewall like netguard which does the logging as long as traffic is routed through its local vpn. You will watch for system (pid 1000) or gps (pid 1021). in my attempts i could not force sync time, even rebooting or setting time to manual, as it remains cached somehow. but the sync normally happens at least once a day

running android 11. i think the behavior is different from phone to phone.

@B0j4ngl35
Copy link

Are you at liberty of sharing the magisk module that you created that forces GPS to utilize 'pool.ntp.org' or 'time.xtracloud.net'?

@Uj947nXmRqV2nRaWshKtHzTvckUUpD
Copy link

I created a repo with it https://github.com/fusionneur/fusionGPS . The module itself is a port for compatibility with latest magisk (see references in readme). I used it in the last 4 months and observed an improved location locking than with the default gps.conf on my OnePlus 7T PRO. Let me know on how it works for you. Also I invite anyone to propose updated and improved optimizations in gps.conf

@B0j4ngl35
Copy link

You sir, are a gentleman and a scholar.

@ipcjs
Copy link

ipcjs commented Dec 1, 2022

time.windows.com is a good choice in China.

@don-dolarson
Copy link

don-dolarson commented May 26, 2023

Thank you for this thread.

There is no existing ntp_server setting in LineageOS 20 but can easily be created just by pushing the "put global ntp_server domain" command through adb.

Setting is preserved between reboots and my router logs can confirm that NTP address changed from time.android.com to the chosen NTP server. GPS automatically connects to time.xtracloud.net (Cloudflare) in this ROM.

@wodeguaiguai
Copy link

wodeguaiguai commented Jul 21, 2023

Failed to execute this command, my devices is android 12 colorOS, it seems no write permisson

➜ /Users/guaio >adb shell settings put global ntp_server ntp.aliyun.com

Exception occurred while executing 'put':
java.lang.SecurityException: Permission denial: writing to settings requires:android.permission.WRITE_SECURE_SETTINGS
	at com.android.providers.settings.SettingsProvider.enforceWritePermission(SettingsProvider.java:2640)
	at com.android.providers.settings.SettingsProvider.mutateGlobalSetting(SettingsProvider.java:1632)
	at com.android.providers.settings.SettingsProvider.insertGlobalSetting(SettingsProvider.java:1586)
	at com.android.providers.settings.SettingsProvider.call(SettingsProvider.java:588)
	at android.content.ContentProvider.call(ContentProvider.java:2473)
	at android.content.ContentProvider$Transport.call(ContentProvider.java:521)
	at com.android.providers.settings.SettingsService$MyShellCommand.putForUser(SettingsService.java:382)
	at com.android.providers.settings.SettingsService$MyShellCommand.onCommand(SettingsService.java:278)
	at com.android.modules.utils.BasicShellCommandHandler.exec(BasicShellCommandHandler.java:97)
	at android.os.ShellCommand.exec(ShellCommand.java:38)
	at com.android.providers.settings.SettingsService.onShellCommand(SettingsService.java:50)
	at android.os.Binder.shellCommand(Binder.java:970)
	at android.os.Binder.onTransact(Binder.java:854)
	at android.os.Binder.execTransactInternal(Binder.java:1226)
	at android.os.Binder.execTransact(Binder.java:1163)

@qianbinbin
Copy link

Thanks for the information. I've tried several servers, cn.ntp.org.cn is the fastest one in China.

@albertopasqualetto
Copy link

In my device if I first of all do adb shell settings get global ntp_server, it returns null.

@costaht
Copy link

costaht commented Jan 10, 2024

For those trying to connect to your TV, I used adb connect 192.168.1.65:5555 to connect to my tv via LAN. Once you run that command you're gonna have to click Accept on the pop-up that is shown on your tv screen.
adb devices should show your TV as active, then you can run the commands above.

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