在移动开发和嵌入式系统调试中,ADB(Android Debug Bridge)是最常用的工具之一。但很多人不知道的是,ADB不仅可以连接Android设备,还能与运行Debian系统的设备建立连接。这种连接方式在IoT设备开发、服务器调试等场景下特别有用。
我最近在调试一台运行Debian的工控设备时,就遇到了需要通过ADB传输文件、执行命令的需求。经过多次尝试和踩坑,总结出了一套稳定的连接方法。下面就把这个过程中积累的经验分享给大家,包括环境准备、连接建立、问题排查等完整流程。
ADB连接Debian系统主要解决以下几个问题:
ADB连接Debian的核心是在Debian系统上运行adbd(ADB守护进程)。这与Android设备上的实现原理相同,只是需要针对Debian环境进行适配:
在开发机(连接Debian的电脑)上需要:
bash复制# Ubuntu/Debian
sudo apt install android-tools-adb
# macOS
brew install android-platform-tools
bash复制adb version
在目标Debian设备上需要:
bash复制sudo apt update
sudo apt install android-tools-adbd
bash复制sudo systemctl enable adbd
sudo systemctl start adbd
bash复制systemctl status adbd
正常应该显示"active (running)"注意:如果系统没有android-tools-adbd包,需要从源码编译安装adbd,这个过程比较复杂,建议优先选择包含该包的Debian版本。
如果Debian设备支持USB OTG:
bash复制sudo adb usb
bash复制adb devices
应该能看到设备序列号更常用的方式是通过网络连接:
bash复制ip a
bash复制sudo adb tcpip 5555
bash复制adb connect <Debian设备的IP>:5555
bash复制adb devices
为了避免每次重启都要重新连接:
bash复制sudo systemctl enable adbd
bash复制echo "adb connect <IP>:5555" > connect_debian.sh
chmod +x connect_debian.sh
如果adb devices不显示设备:
bash复制systemctl status adbd
bash复制sudo ufw status
需要放行5555端口bash复制sudo ufw allow 5555/tcp
bash复制sudo systemctl restart adbd
如果连接经常断开:
bash复制ping <Debian设备IP>
bash复制adb kill-server
export ADB_TIMEOUT=5000
adb start-server
如果出现权限错误:
bash复制sudo usermod -aG plugdev $USER
bash复制ls /etc/udev/rules.d/51-android.rules
如果没有,需要创建:bash复制echo 'SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", MODE="0666"' | sudo tee /etc/udev/rules.d/51-android.rules
sudo udevadm control --reload-rules
常用文件操作命令:
bash复制# 从开发机推送到Debian
adb push local_file /path/on/debian
# 从Debian拉取到开发机
adb pull /path/on/debian local_file
# 查看Debian文件系统
adb shell ls /path
在Debian上执行命令:
bash复制adb shell "your_command"
例如查看系统信息:
bash复制adb shell "uname -a"
将Debian的端口转发到开发机:
bash复制adb forward tcp:local_port tcp:remote_port
例如将Debian的80端口转发到开发机的8080:
bash复制adb forward tcp:8080 tcp:80
当连接多个设备时:
bash复制adb -s <设备序列号> <命令>
获取设备序列号:
bash复制adb devices
bash复制sudo ufw deny 5555/tcp
bash复制sudo systemctl stop adbd
bash复制adb push --sync local_file /path/on/debian
bash复制sudo ifconfig <网卡> mtu 1500
bash复制adb shell setprop log.tag.ADB VERBOSE
我在实际项目中发现,通过ADB连接Debian系统最稳定的方式是使用USB连接,网络连接适合临时调试。对于生产环境,建议配置好自动重连机制,并在脚本中加入连接状态检查。