Is it possible to run a shell command with root privileges only using su and not install SuperSU or Superuser apk?

I have an Android phone that has only a su-binary file and it works, that is, I can use the shell on the phone and run the su command, and I will be root.

When I try to run a command using code, it does not seem to work regardless of how I try to run it. I tried many different variations of the following command.

Runtime.getRuntime().exec("su -c ps"); 

When I run this command on another root phone with Superuser.apk or SuperSU.apk installed, I get a dialog asking if I want to allow it to work with root privileges. When the apk does not exist, it never asks, and the team never works.

I tried installing apks on the first phone, but they don't seem to be doing anything. So, as the initial question asks β†’ Is there a way to run an elevated command from an application without SU applications installed?

+6
source share
4 answers

Perhaps this is due to the fact that you need to pass the su commands as such parameters:
su -c 'ls -l'
Or you may need to specify the full path to su , but I don’t understand why it will not work the way you use it: Runtime.exec("/system/bin/su -c ps")
Or maybe Runtime.exec("/system/bin/su -c \'ps\'")

Try also checking the output of this command: System.getenv("PATH")

Another option might be Runtime.exec("su -c \'ls -s \'")
Make sure you remember to avoid single quotes, as they are part of the actual String .

What I found works the most consistently, and it also works on devices that don't have Superuser or SuperSU installed, because these applications only listen to the broadcast that is sent when the application tries to run the command as root. @Boardy SuperSU and Superuser intercept the broadcast and therefore act as the average person between the privileges of the application and root, but this is not necessary for the root device. This is necessary if you want to have more control over applications in which commands are executed as root, but even then it still limits you only to which applications, and not which commands, get root privileges. Alternatively, you can take a look at RootTools and, more specifically, RootTools.isAccessGiven() , which asks for root privileges for your application.

Source: Run script as root via ADB

+6
source

Not all Android su versions will accept a command to execute from command line options.

In cases where this does not happen, you will need to let 'su' run the privileged shell, get its input file descriptor and pipe command (s). This has been reviewed many times here at Stackoverflow.

+2
source

I believe that you will need to install SU-applications, since this is what gives the user the question of whether the application should run with root privileges or not.

+1
source

You have to do it.

try:

adb root shell ls -l

-2
source

All Articles