How to use su command over adb shell?

前端 未结 6 1856
悲&欢浪女
悲&欢浪女 2020-12-14 17:44

I need to make a script that executes a lots of thing on Android device, my device is rooted, when I enter on the shell, I can give the command su, and it works but I need p

相关标签:
6条回答
  • 2020-12-14 18:26

    1. adb shell su

    win cmd

    C:\>adb shell id
    uid=2000(shell) gid=2000(shell)
    
    C:\>adb shell 'su -c id'
    /system/bin/sh: su -c id: inaccessible or not found
    
    C:\>adb shell "su -c id"
    uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
    
    C:\>adb shell su -c id   
    uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
    

    win msys bash

    msys2@bash:~$ adb shell 'su -c id'
    uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
    msys2@bash:~$ adb shell "su -c id"
    uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
    msys2@bash:~$ adb shell su -c id
    uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
    

    2. adb shell -t

    if want run am cmd, -t option maybe required:

    C:\>adb shell su -c am stack list
    cmd: Failure calling service activity: Failed transaction (2147483646)
    
    C:\>adb shell -t su -c am stack list
    Stack id=0 bounds=[0,0][1200,1920] displayId=0 userId=0
    ...
    

    shell options:

     shell [-e ESCAPE] [-n] [-Tt] [-x] [COMMAND...]
         run remote shell command (interactive shell if no command given)
         -e: choose escape character, or "none"; default '~'
         -n: don't read from stdin
         -T: disable pty allocation
         -t: allocate a pty if on a tty (-tt: force pty allocation)
         -x: disable remote exit codes and stdout/stderr separation
    

    Android Debug Bridge version 1.0.41
    Version 30.0.5-6877874

    0 讨论(0)
  • 2020-12-14 18:29

    On my Linux I see an error with

    adb shell "su -c '[your command goes here]'"
    

    su: invalid uid/gid '-c'

    The solution is on Linux

    adb shell su 0 '[your command goes here]'
    
    0 讨论(0)
  • 2020-12-14 18:32

    Well, if your phone is rooted you can run commands with the su -c command.

    Here is an example of a cat command on the build.prop file to get a phone's product information.

    adb shell "su -c 'cat /system/build.prop |grep "product"'"
    

    This invokes root permission and runs the command inside the ' '.

    Notice the 5 end quotes, that is required that you close ALL your end quotes or you will get an error.

    For clarification the format is like this.

    adb shell "su -c '[your command goes here]'"
    

    Make sure you enter the command EXACTLY the way that you normally would when running it in shell.

    0 讨论(0)
  • 2020-12-14 18:32

    By default CM10 only allows root access from Apps not ADB. Go to Settings -> Developer options -> Root access, and change option to "Apps and ADB".

    0 讨论(0)
  • 2020-12-14 18:34

    for my use case, i wanted to grab the SHA1 hash from the magisk config file. the below worked for me.

    adb shell "su -c "cat /sbin/.magisk/config | grep SHA | awk -F= '{ print $2 }'""
    
    0 讨论(0)
  • 2020-12-14 18:37

    The su command does not execute anything, it just raise your privileges.

    Try adb shell su -c YOUR_COMMAND.

    0 讨论(0)
提交回复
热议问题