Run a shell command as root on android? - android

I thought it would be cool to turn the hardware button lights on when my application needs user attention so the user could get an indicator when the screen is blanked. As it turns out, this requires root access (echo 1 > /system/class/leds/keyboard-backlight/brightness). I found a link on Stack (http://muzikant-android.blogspot.com/2011/02/how-to-get-root-access-and-execute.html) to a class which tests for root access but it fails on my Nexus-S.
Is there a way to run shell commands as root without having to root the device?
Thanks
Edit: this is the error I'm getting from logcat
11-15 12:34:19.889: D/ROOT(2029): Root access rejected [java.io.IOException] : Error running exec(). Command: [su] Working Directory: null Environment: null

The reason it fails it because your phone needs to be rooted.
See the Wikipedia entry for Rooting (Android OS).
It would be a major security flaw if any app on any phone could take root and do anything.

If your phone has root access then you can fire shell scripts using Process and Runtime class..

Related

Android 12: adb root. adbd cannot run as root in production builds

There is a need to install your application as a system one. To do this, I rooted the Samsung A04 (Android 12) using Magisk.
Next, to install my app, I wanted to copy it to /system/priv-app using
adb push /system/priv-app
To do this, it was necessary to activate root via
adb root
but this command gave an error
adbd cannot run as root in production builds
I started looking on the Internet for ways to solve this problem, I tried to install
adbd Insecure - the program closes with an error when activated in
the settings of the desired item
adb_root (plugin in Magisk) - adb root still gave
adbd cannot run as root in production builds error
MagiskHide Props Config - changing the ro.debuggable and ro.secure
values blocked the connection of the phone to the PC (the phone was
no longer recognized as an external device and when entering the
"Developer options" section, the section was closed with an error)
Perhaps someone faced this problem on Android 12 and was able to solve it?
I read that many people have had this problem since Android 11.
Perhaps there is another solution to make the application system?
Thanks in advance

Root access works with adb, but root process on Android Studio doesn't (Android Things)

Currently I'am working on an aplication via Android Studio, that executes commands using the next sintaxis:
Process p = Runtime.getRuntime().exec(new String[] { "su", "-c", "ip link show"});
I'am working with the device IMX7D_PICO (it uses Android Things as SO). It is rooted, as is shown in the following pic:
But, when I run a command as root on Android Studio, I get the next error:
W/System.err: java.io.IOException: Cannot run program "su": error=13, Permission denied
W/System.err: at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:692)
at java.lang.Runtime.exec(Runtime.java:560)
I don't know why su works only on adb. In other hand, I know that there are others ways to obtain the data of ip link show, but the next step, it's open a socket RAW with the command that I said. So, I need to run process as su via Android Studio.
An app's process has less privileges compared to those of the shell's, resulting in
java.io.IOException: Cannot run program "su": error=13, Permission denied
For the permission to be granted you should install SuperSU app or alike and follow the app's prompts while your app is trying to su with Runtime.getRuntime().exec("su").
Once the process has gotten root you can grab the standard input of the root process and write the command(s) to it, reading its standard output. For more details see: execute shell command from android.
I don't know why su doesn't work. I 'suspect' that Android Things is using a single user that is root already, so maybe executing the command without using su would work.
On the other part of your question. Parsing the output of system commands is not the best way of getting info, in this case, you can probably get what you want using the Android class NetworkInterface https://developer.android.com/reference/java/net/NetworkInterface#getNetworkInterfaces().
You can manage raw sockets using the Android framework classes too.

Running su on Android root

Hi so my application runs some commands using the su in android root.
It works fine in JellyBean on the Galaxy Nexus but when I try to run it on the LG Nexus 5 (kitkat), I get an error saying:
su: uid 10069 not allowed to su
I'm not really sure what I'm missing here. I have root access (it runs su fine on the Galaxy Nexus).
Basically the application reads in a command from a TextEdit, strips it, appends the necessary parameters etc. and then calls the script which is put on the phone. I use the ProcessBuilder to build the call for the script.
Any fixes or ideas as to where I should look would be appreciated. Thank you
The issue was SuperSu not being properly installed and granting permission to the application. Once I changed that there were no permissions issues.
If you have problem with rooting to super user.
You can use "run-as" command to access files of your application.
ex: run-as com.your.package
It will drop you to the shell#android:/data/data/com.your.package $
now you can use commands ls
Then it will display the list of file folders
cache
databases
lib
shared_prefs

Where does Android store shutdown logs?

I know that the boot up log can be obtained by pulling out contents of kmsg or dmesg through ADB.
But I'm not aware of how to retrieve the shutdown logs in Android as there's no /var folder in Android (place where most desktop linux distros generally store their shutdown logs).
So how can I obtain the shutdown logs in Android?
Look in some locations such as these:
/proc/last_kmsg
/data/tombstones/
/data/dontpanic/
/data/system/dropbox/
(This list isn't strictly kernel logs, including framework and application logs too, which are also sometimes of interest)
One work around I found for collecting shutdown logs in Android is to run adb pull /proc/kmsg C:\Logs.txt on the host PC and then switch off the device. You will get the logs till the USB communication between the host and the device snaps! I know this is only one case out of the numerous shutdown scenarios but I haven't found satisfactory answers for other cases!
TL;DR:
Run command through adb that copies logcat and proc/kmsg to a file and keep it running even when adb disconnects with nohup, disown or setsid. Probably needs busybox, needs root and adb root, too.
setsid cat proc/kmsg > /sdcard/kmsg.txt &
and
logcat -v long -f /sdcard/logcat.txt (somehow only works without setsid)
Or add normal copy commands to some startup script.
/TL;DR
You can constantly copy proc/kmsg and logcat to a file on your android device or a microSD card to get the logs even after adb disconnects.
You need root access and adb root access for this to work. For the latter, use the setting in the developer options if you have a custom rom or the adbd insecure app.
After using adb shell to get your android shell, type su to get superuser access.
Then you not only need to put an ampersand (&) after the command but also make sure that the command keeps running after adb disconnects. That is done by nohup, disown or setsid (see here for usage).
If it doesn't work because you don't have these commands, you need to install busybox.
See my question here.
See here for how to get logcat and kernel logs and print it to some file or merge it.
See developer.android.com/tools/help/logcat.html for parameters for the logcat command.
In the end you could have a command like setsid cat proc/kmsg > /sdcard/kmsg.txt & for the kernel messages.
For logcat you could have one of the following commands: logcat -v long -f /sdcard/logcat.txt or logcat -v long > /sdcard/logcat.txt
I don't know why, but sometimes it didn't work with setsid and just didn't copy continuously but stopped shortly after executing the command. In these situations, it also showed up when entering jobs, which it didn't otherwise. Then it just worked without setsid, it stayed alive after disconnecting and reconnecting. I guess you must just try when the file does keep getting larger. If someone figured out why it is behaving like it is... let me know and I'll edit the answer.
Probably adding the commands to a startup script could be a solution for some, too.
Hope this helps.
fightcookie
Newer phones do NOT use any of these locations so if you're reading this article then as of now
The kernel crash logs are now in /sys/fs/pstore instead of /proc/last_kmsg
I was looking for the same thing, and finally, I found the answer!
In android 8 all logs are located in \data\log\android_logs\... including apps and kernel logs. Kernel logs are called kmsgcat-log_timestamp_.gz
edit: Although this is a very old thread, I think the answer might be helpful.

How to connect to a dev phone and gain the root permission through "adb shell"

I'm using a dev phone (Nexus One). I connected to it through "adb shell" but I couldn't get the root permission. When I run "su" in the shell, I got
$ su
su: permission denied
Actually I cannot access "/sbin" at all.
When I used the "root" option in "adb", I got errors too:
./adb root -s xxx shell
adbd cannot run as root in production builds
I got this error no matter I used a downloaded SDK or I compiled an SDK from the source. I'm not sure about what "production builds" actually means. Does it mean that I need to compile Android and put it on the phone? I thought a dev phone already has the root permission unlocked.
Thanks for your help.
You should unlock your phone - root it. Nexus One comes with this option, but you have to enable it. You can read this article or google for more.

Categories

Resources