Android - Using ADB to change the screen lock
Since Android 8.0 Oreo, you can change the lock screen using below adb commands
adb shell
athene_f:/ $ locksettings
usage: locksettings set-pattern [--old OLD_CREDENTIAL] NEW_PATTERN
locksettings set-pin [--old OLD_CREDENTIAL] NEW_PIN
locksettings set-password [--old OLD_CREDENTIAL] NEW_PASSWORD
locksettings clear [--old OLD_CREDENTIAL]
locksettings verify [--old OLD_CREDENTIAL]
locksettings set-disabled DISABLED
locksettings get-disabled
flags:
--user USER_ID: specify the user, default value is current user
- locksettings set-pin: sets a PIN
- locksettings set-password: sets a password
- locksettings clear: clears the unlock credential
- locksettings verify: verifies the credential and unlocks the user
- locksettings set-disabled: sets whether the lock screen should be disabled
- locksettings get-disabled: retrieves whether the lock screen is disabled
And if you want to remove the password, just use below code:
$ locksettings clear --old old_password_put_here
Here is the reference link: The Android Soul - How to change or remove lock screen pattern, PIN or password via ADB on Android 8.0 Oreo
I have tested on Android Oreo device. It works just fine.
There's no built-in functionality to set the password via the command-line, neither from a shell on the device nor from adb
. You can't even achieve this by pasting the stored password into the secure settings file from another device. The password isn't stored as plain text, but rather a cryptographic hash of the password is stored. This hash is generated using the password and a salt value: a random number generated once on the device.
Hashing the password prevents someone from stealing your phone and finding the password. This is important not just to protect the password itself (in case you use the same password for other things), but also because the password is used to encrypt the device's key store, and as part of the full-device encryption feature. Salting the password with a device-specific salt ensures an attacker can't use a rainbow table to determine the password. (A rainbow table is a list of every possible hashed password, which one queries to find the original password given the hashed password. The technique allows naïve password implementations such as Windows 95's to be broken in less than a second using a normal desktop computer.)
In addition, the code that updates the stored password when you change it through the GUI has other side-effects. As I've mentioned, the password is used to encrypt the device's key store, which can be used by all apps on the device to store credentials securely. Changing the saved password without updating the key store would result in the stored credentials being lost. In addition, if full-device encryption is used, changing the saved password without updating the device encryption key would result in all storage becoming inaccessible.
To achieve the functionality you want, the easiest way would be to patch a custom ROM to export the functionality that the settings app uses to change the password, and add a command-line program that can change the password in the same way. It might even be possible to hack up an app that can do this using reflection to get access to the un-exported functions, but the app would still need root, and it would be specific to the ROM (and maybe even the version of the ROM) on the device.
The relevant code is here if you're interested.