Skip to content

Instantly share code, notes, and snippets.

@xujiaao
Last active October 10, 2026 22:01
Show Gist options
  • Select an option

  • Save xujiaao/63cb3bbea9fe22e79206e5eb7ba82d0e to your computer and use it in GitHub Desktop.

Select an option

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

@don-dolarson

don-dolarson commented May 26, 2023 •

Copy link
Copy Markdown

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

wodeguaiguai commented Jul 21, 2023 •

Copy link
Copy Markdown

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
Copy Markdown

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

@albertopasqualetto

Copy link
Copy Markdown

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

@costaht

costaht commented Jan 10, 2024 •

Copy link
Copy Markdown

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.

@splash112

Copy link
Copy Markdown

I ended up enabling the ntp server on my OpenWRT router and let time.android.com resolve to the router. Worked immediately.

@SourLemonJuice

Copy link
Copy Markdown

How to sync device time from ntp server immediately?

I find this command in Android offical docs: cmd network_time_update_service force_refresh in ADB Shell
https://source.android.com/docs/core/connect/time/network-time-detection#interacting-network-time-update-service

@BriellaBugs

Copy link
Copy Markdown

How to sync device time from ntp server immediately?

I find this command in Android offical docs: cmd network_time_update_service force_refresh in ADB Shell https://source.android.com/docs/core/connect/time/network-time-detection#interacting-network-time-update-service

Thank you 1 year later! Hello from google

@killablue

killablue commented Oct 10, 2026 •

Copy link
Copy Markdown

Android 14 & higher natively support multiple NTP servers for redundancy.

I figured out how to put multiple NTP servers onto ntp_server after looking at NtpTrustedTime for like a day.
$ adb shell settings put global ntp_server "ntp://time.google.com'|'ntp://time.cloudflare.com'|'ntp://pool.ntp.org"

It is explained inside the documentation on how each server is separated however command line also uses the pipe character for commands so you have put it inside quote marks like this '|'

You can verify if your device support multiple NTP server using
$ adb shell cmd network_time_update_service force_refresh
&
$ adb shell cmd network_time_update_service dump

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