Linux--Android ADB工具使用
adb shell本质是通过 ADB 协议在主机和设备之间建立一个 “远程 shell 通道”你在主机终端输入的命令会被发送到设备端执行执行结果再返回给主机显示。这和通过 SSH 登录远程 Linux 服务器的体验类似相当于**“远程操控设备的终端”**。MCU开发板能被上位机通过 ADB 连接的核心要求是设备端运行 ADB 服务程序adbd并正确配置通信权限USB 或网络并非必须是 Android 系统。Ubuntu、Debian 等 Linux 系统也可以支持 ADB 连接但需要手动配置Android 默认集成了 ADB 相关组件更易实现。一、ADB 连接的通用前提所有系统均需满足硬件连接USB 连接设备与上位机通过数据电缆非仅充电线连接设备端接口支持 USB 调试如 Type-C。网络连接设备与上位机处于同一局域网有线 / 无线能互相 ping 通。上位机配置安装 ADB 工具Windows 可下载 Android SDK Platform ToolsLinux 可通过apt install adb安装。设备端核心运行 ADB 服务程序adbdAndroid 自带Linux 需手动安装并监听指定端口USB 默认通过 USB 接口通信网络默认监听 5555 端口。二、不同系统的具体设置1. Android 系统最便捷原生支持 ADBAndroid 系统默认集成了adbd服务只需开启调试权限即可步骤 1开启 “开发者选项”进入系统设置 → 关于设备 → 连续点击 “版本号” 7 次激活 “开发者选项”。步骤 2启用 USB 调试进入开发者选项 → 勾选 “USB 调试”部分设备需同时勾选 “允许 ADB 调试”“允许网络 ADB”。步骤 3授权上位机首次通过 USB 连接时设备会弹出 “允许 USB 调试” 对话框勾选 “始终允许来自此计算机” 并确认。验证上位机输入adb devices若显示设备序列号如emulator-5554则连接成功。2. Ubuntu/Debian 系统需手动配置adbdUbuntu/Debian 等 Linux 系统默认没有adbd服务需手动安装并配置步骤如下步骤 1安装adbd服务adbd属于 Android 工具集可通过以下方式安装# Ubuntu/Debian 安装Android工具包含adbdsudoaptupdatesudoaptinstallandroid-tools-adb android-tools-fastboot步骤 2配置 USB 权限USB 连接需此步Linux 系统对 USB 设备有严格权限控制需添加 udev 规则允许普通用户访问 ADB 设备创建规则文件sudonano/etc/udev/rules.d/51-android.rules添加内容允许所有用户访问 USB 调试设备SUBSYSTEMusb, ATTR{idVendor}xxxx, ATTR{idProduct}yyyy,MODE0666,GROUPplugdev备注xxxx和yyyy需替换为设备的 USB 厂商 ID 和产品 ID可通过lsusb命令查看生效规则sudoudevadm control --reload-rulessudoudevadm trigger步骤 3启动adbd服务adbd需以 root 权限运行否则无法访问系统资源# 启动USB模式的adbd默认通过USB接口通信sudoadbd start# 若需网络ADB指定监听端口如5555sudoadbd tcpip5555步骤 4验证连接USB 连接上位机输入adb devices显示设备如12345678。网络连接先获取设备 IP如192.168.1.10上位机输入adb connect 192.168.1.10:5555。3. 其他 Linux 系统如 Buildroot、Yocto 定制系统这类精简系统需在编译时集成adbd组件步骤 1编译时启用adbdBuildroot在make menuconfig中勾选Target packages → Debugging, profiling and benchmark → adbd。Yocto在自定义层中添加IMAGE_INSTALL_append adbd确保编译时包含adbd。步骤 2启动时自动运行adbd在系统启动脚本如/etc/init.d/rcS中添加# 启动adbdUSB模式/usr/bin/adbd start# 或网络模式后台运行/usr/bin/adbd tcpip5555步骤 3配置权限同 Ubuntu如需 USB 连接添加 udev 规则或简化为chmod 0666 /dev/bus/usb/*/*临时开放权限。三、关键差异总结系统类型是否原生支持 ADB核心依赖主要配置点Android是系统自带adbd和调试框架开启开发者选项 USB 调试授权Ubuntu/Debian否需手动安装adbd安装工具包 USB 权限 启动服务定制 Linux否编译时集成adbd编译配置 启动脚本 权限开放四、命令速查连接管理#列出所有连接设备及其序列号adb devices#如果有多个连接设备则需要使用序列号来区分exportANDROID_SERIAL设备序列号adb shellls#多设备下连接指定设备adb-s序列号 shell#网络来连接 ADB#让设备端的 adbd 重启并在 TCP 端口 5555 处监听adb tcpip5555# 远程连接设备设备的 IP 地址是 192.168.1.100adb connect192.168.1.100:5555# 断开连接adb disconnect192.168.1.100:5555获取系统日志adb logcatroot 权限:要获得 root 权限需要先运行adb shell setprop persist.sys.root_access 3 adb root让 ADB 的设备端切换到 root 权限模式这样adb remount等需要 root 权限的命令才会成功。运行shell命令adb shell安装应用adb install卸载应用adb uninstalladb helpAndroid Debug Bridge version 1.0.31 devices [-l] - list all connected devices connect host[:port] - connect to a device via TCP/IP Port 5555 is used by default if no port number disconnect [host[:port]] - disconnect from a TCP/IP device. Port 5555 is used by default if no port number device commands: adb push [-p] local remote - copy file/dir to device adb pull [-p] [-a] remote [local] - copy file/dir from device adb sync [ directory ] - copy host-device only if changed adb shell - run remote shell interactively adb shell command - run remote shell command adb emu command - run emulator console command adb logcat [ filter-spec ] - View device log adb forward --list - list all forward socket connections. adb forward local remote - forward socket connections adb forward --no-rebind local remote adb forward --remove local - remove a specific forward socket connection adb forward --remove-all - remove all forward socket connections adb jdwp - list PIDs of processes hosting a JDWP transport adb install [-l] [-r] [-d] [-s] adb uninstall [-k] package - remove this app package from the device adb bugreport - return all information from the device adb backup [-f file] [-apk|-noapk] [-obb|-noobb] [-shared|-noshared] [-all] adb restore file - restore device contents from the file backup archive adb help - show this help message adb version - show version num scripting: adb wait-for-device - block until device is online adb start-server - ensure that there is a server running adb kill-server - kill the server if it is running adb get-state - prints: offline | bootloader | device adb get-serialno - prints: serial-number adb get-devpath - prints: device-path adb status-window - continuously print device status for a specified device adb remount - remounts the /system partition on the device read-write adb reboot [bootloader|recovery] adb reboot-bootloader - reboots the device into the bootloader adb root - restarts the adbd daemon with root permissions adb usb - restarts the adbd daemon listening on USB adb tcpip port - restarts the adbd daemon listening on TCP port networking: adb ppp tty [parameters] - Run PPP over USB. environmental variables: ADB_TRACE - Print debug information. ANDROID_SERIAL - The serial number to connect to. ANDROID_LOG_TAGS - When used with the logcat option五、注意事项adbd版本兼容性设备端adbd版本需与上位机 ADB 工具版本接近否则可能出现 “版本不匹配” 错误可统一升级为最新版。网络 ADB 安全性默认端口 5555 无加密公网环境需谨慎开启可修改端口或限制 IP 访问。权限问题adbd若不以 root 运行可能无法执行su、修改系统文件等操作需根据需求配置权限。综上ADB 本质是 “客户端 - 服务端” 工具只要设备端能运行adbd并正确配置通信无论 Android 还是其他 Linux 系统都可被上位机 ADB 连接只是 Android 的配置更简单。