我手边没有扎根的 Nougat 设备,但类似以下内容可能足够接近adb shell
(假设您使用的是 SuperSU):
env -i USER=shell "$(PATH=/system/xbin:/system/bin:/su/bin:/sbin:/magisk/.core/bin which su)" shell --context u:r:shell:s0 --shell /system/bin/sh --command COMMAND
我(非常简短地)从 Termux 在有根的 Marshmallow 设备上对其进行了测试。
详细说明:
- 该
-i
标志用于从空环境开始
USER=shell
不是特别需要,但由于某种原因su
拒绝在完全空的环境中运行
$(PATH=/system/xbin:/system/bin:/su/bin:/sbin:/magisk/.core/bin which su)
指向设备上 su 二进制文件的完整路径,如果您愿意,可以硬编码
shell
指示su
二进制文件以 shell 用户身份登录(与 相同adb shell
)
--context u:r:shell:s0
设置适当的 SELinux 上下文
--shell /system/bin/sh
指示 SuperSU 使用系统 shell 而不是它自己的sush
shell
另一种选择是从设备实际运行 adb,通过 TCP 连接到自身。如果您需要一些只能通过 adb 获得的功能(例如在我的情况下它是adb forward
),那么这可能是您唯一的选择。不幸的是,这不是特别方便。
我无法使用任何公开可用的 adb 二进制文件获得成功,因此我自己构建了它并进行了一些小的更改。您可以分别在https://github.com/shakalaca/fastboot-adb-android和https://github.com/brbsix/fastboot-adb-android看到我使用的来源和所做的更改。
安装 adb 后,这是我用来连接设备的命令的简短列表:
# Add iptables rules to block external connections to port 9999'
su root iptables -N adbd
su root iptables -A adbd -i lo -p tcp -m tcp --dport 9999 -j ACCEPT
su root iptables -A adbd -p tcp -m tcp --dport 9999 -j DROP
su root iptables -A INPUT -j adbd
# Necessary in order to display authorization prompt
su shell setprop ro.debuggable 1
su shell setprop service.adb.tcp.port 9999
su root start adbd
adb connect 127.0.0.1:9999
adb wait-for-local-device
去关机:
adb kill-server
su root stop adbd
su shell setprop ro.debuggable 0
su shell setprop service.adb.tcp.port 0
su root iptables -D INPUT -j adbd
su root iptables -F adbd
su root iptables -X adbd