adb shell 本质是通过 ADB 协议在主机和设备之间建立一个 “远程 shell 通道”,你在主机终端输入的命令会被发送到设备端执行,执行结果再返回给主机显示。这和通过 SSH 登录远程 Linux 服务器的体验类似,相当于**“远程操控设备的终端”**。

MCU(开发板)能被上位机通过 ADB 连接的核心要求是设备端运行 ADB 服务程序(adbd),并正确配置通信权限(USB 或网络),并非必须是 Android 系统。Ubuntu、Debian 等 Linux 系统也可以支持 ADB 连接,但需要手动配置(Android 默认集成了 ADB 相关组件,更易实现)。

一、ADB 连接的通用前提(所有系统均需满足)

  1. 硬件连接:
    • USB 连接:设备与上位机通过数据电缆(非仅充电线)连接,设备端接口支持 USB 调试(如 Type-C)。
    • 网络连接:设备与上位机处于同一局域网(有线 / 无线),能互相 ping 通。
  2. 上位机配置:
    安装 ADB 工具(Windows 可下载 Android SDK Platform Tools,Linux 可通过apt install adb安装)。
  3. 设备端核心:
    运行 ADB 服务程序(adbd,Android 自带,Linux 需手动安装),并监听指定端口(USB 默认通过 USB 接口通信,网络默认监听 5555 端口)。

二、不同系统的具体设置

1. Android 系统(最便捷,原生支持 ADB)

Android 系统默认集成了adbd服务,只需开启调试权限即可:

  • 步骤 1:开启 “开发者选项”
    进入系统设置 → 关于设备 → 连续点击 “版本号” 7 次,激活 “开发者选项”。
  • 步骤 2:启用 USB 调试
    进入开发者选项 → 勾选 “USB 调试”(部分设备需同时勾选 “允许 ADB 调试”“允许网络 ADB”)。
  • 步骤 3:授权上位机
    首次通过 USB 连接时,设备会弹出 “允许 USB 调试?” 对话框,勾选 “始终允许来自此计算机” 并确认。
  • 验证:
    上位机输入adb devices,若显示设备序列号(如emulator-5554),则连接成功。

2. Ubuntu/Debian 系统(需手动配置adbd)

Ubuntu/Debian 等 Linux 系统默认没有adbd服务,需手动安装并配置,步骤如下:

  • 步骤 1:安装adbd服务
    adbd属于 Android 工具集,可通过以下方式安装:

    # Ubuntu/Debian 安装Android工具包(含adbd)
    sudo apt update && sudo apt install android-tools-adb android-tools-fastboot
    
  • 步骤 2:配置 USB 权限(USB 连接需此步)
    Linux 系统对 USB 设备有严格权限控制,需添加 udev 规则允许普通用户访问 ADB 设备:

    1. 创建规则文件:

      sudo nano /etc/udev/rules.d/51-android.rules
      
    2. 添加内容(允许所有用户访问 USB 调试设备):

      SUBSYSTEM=="usb", ATTR{idVendor}=="xxxx", ATTR{idProduct}=="yyyy", MODE="0666", GROUP="plugdev"
      

      备注:xxxx和yyyy需替换为设备的 USB 厂商 ID 和产品 ID,可通过lsusb命令查看)

    3. 生效规则:

      sudo udevadm control --reload-rules && sudo udevadm trigger
      
  • 步骤 3:启动adbd服务
    adbd需以 root 权限运行(否则无法访问系统资源):

    # 启动USB模式的adbd(默认通过USB接口通信)
    sudo adbd start  
    
    # 若需网络ADB,指定监听端口(如5555)
    sudo adbd tcpip 5555  
    
  • 步骤 4:验证连接

    • USB 连接:上位机输入adb devices,显示设备(如12345678)。
    • 网络连接:先获取设备 IP(如192.168.1.10),上位机输入adb connect 192.168.1.10:5555。

3. 其他 Linux 系统(如 Buildroot、Yocto 定制系统)

这类精简系统需在编译时集成adbd组件:

  • 步骤 1:编译时启用adbd

    • Buildroot:在make menuconfig中勾选Target packages → Debugging, profiling and benchmark → adbd。
    • Yocto:在自定义层中添加IMAGE_INSTALL_append = " adbd",确保编译时包含adbd。
  • 步骤 2:启动时自动运行adbd

    在系统启动脚本(如/etc/init.d/rcS)中添加:

    # 启动adbd(USB模式)
    /usr/bin/adbd start  
    
    # 或网络模式(后台运行)
    /usr/bin/adbd tcpip 5555 &
    
  • 步骤 3:配置权限
    同 Ubuntu,如需 USB 连接,添加 udev 规则(或简化为chmod 0666 /dev/bus/usb/*/*临时开放权限)。

三、关键差异总结

系统类型是否原生支持 ADB核心依赖主要配置点
Android是系统自带adbd和调试框架开启开发者选项 + USB 调试授权
Ubuntu/Debian否需手动安装adbd安装工具包 + USB 权限 + 启动服务
定制 Linux否编译时集成adbd编译配置 + 启动脚本 + 权限开放

四、命令速查

  • 连接管理

    #列出所有连接设备及其序列号
    adb devices
    #如果有多个连接设备,则需要使用序列号来区分
    export ANDROID_SERIAL=<设备序列号>
    adb shell ls
    #多设备下连接指定设备
    adb -s 序列号 shell
    #网络来连接 ADB
    #让设备端的 adbd 重启,并在 TCP 端口 5555 处监听
    adb tcpip 5555
    # 远程连接设备,设备的 IP 地址是 192.168.1.100
    adb connect 192.168.1.100:5555
    # 断开连接
    adb disconnect 192.168.1.100:5555
    
  • 获取系统日志adb logcat

  • root 权限:要获得 root 权限,需要先运行:

    adb shell setprop persist.sys.root_access 3
    adb root
    

    让 ADB 的设备端切换到 root 权限模式,这样 adb remount 等需要 root 权限的命令才会成功。

  • 运行shell命令adb shell

  • 安装应用adb install

  • 卸载应用adb uninstall

adb help:

Android 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

五、注意事项

  1. adbd版本兼容性:设备端adbd版本需与上位机 ADB 工具版本接近,否则可能出现 “版本不匹配” 错误(可统一升级为最新版)。
  2. 网络 ADB 安全性:默认端口 5555 无加密,公网环境需谨慎开启(可修改端口或限制 IP 访问)。
  3. 权限问题:adbd若不以 root 运行,可能无法执行su、修改系统文件等操作,需根据需求配置权限。

综上,ADB 本质是 “客户端 - 服务端” 工具,只要设备端能运行adbd并正确配置通信,无论 Android 还是其他 Linux 系统,都可被上位机 ADB 连接,只是 Android 的配置更简单。

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐