如何获取设备端ADB TCP连接日志?需记录连接方IP地址
Great question! Monitoring who's connecting to your device via ADB over TCP—similar to how Linux logs SSH attempts in /var/log/auth.log—is totally feasible. Here are practical methods I’ve tested and recommend:
1. Use ADB's Built-in Logcat Output
The ADB daemon (adbd) logs connection events to the system log by default; you just need to filter for the right tags.
To view real-time TCP connection logs, run this command on your computer (or directly on the device if you have a terminal):
adb logcat -v time *:S adbd:DBreakdown:
-v time: Adds timestamps to each log entry*:S: Mutes all log tags except those explicitly set to a higher leveladbd:D: Sets the ADB daemon log level to Debug, which includes TCP connection details
You’ll see entries like this:
08-15 14:32:45.678 D/adbd( 1234): accepted tcp connection from 192.168.1.105:62001
08-15 14:33:10.123 D/adbd( 1234): closing tcp connection to 192.168.1.105:62001To persist these logs to a file (for later review), redirect the output:
# Save to your device's storage adb logcat -v time *:S adbd:D > /sdcard/adb_tcp_connections.log # Or save directly to your computer adb logcat -v time *:S adbd:D > ~/adb_tcp_logs.txt
2. Modify ADB Daemon Configuration (Advanced, Root Required)
If you want adbd to always log detailed connection events without manually running logcat, you can adjust its startup parameters (requires root access):
- Edit the adbd init script (location varies by ROM, common paths are
/init.rcor/vendor/etc/init/hw/init.rc) - Find the line starting with
service adbdand add a log level parameter:service adbd /sbin/adbd --log-level=debug - Reboot your device—now
adbdwill automatically output detailed connection logs to the system log, which you can access anytime withadb logcat adbd:D
3. Custom Monitoring Scripts (For Automation)
If you want a dedicated log file or real-time alerts, a simple shell script (root required) can filter and track connections:
- Create a script like this on your device:
#!/system/bin/sh LOG_FILE="/sdcard/adb_tcp_monitor.log" echo "ADB TCP Connection Log started at $(date)" >> $LOG_FILE tail -f /dev/log/main | grep -E 'adbd.*(accepted|closing) tcp connection' >> $LOG_FILE - Make it executable:
chmod +x /sdcard/adb_monitor.sh - Run it in the background:
nohup /sdcard/adb_monitor.sh &
This will append all connection events to adb_tcp_monitor.log with timestamps, making it easy to review later.
Bonus: TCP Dump (For Full Traffic Analysis)
If you need more than just connection logs (like what commands were sent), use tcpdump to capture all traffic on ADB’s default port (5555):
# Capture traffic to a pcap file (root required) tcpdump -i any port 5555 -w /sdcard/adb_tcp_traffic.pcap # View the captured data later tcpdump -r /sdcard/adb_tcp_traffic.pcap
Note: This captures raw network traffic, so you’ll need to parse it to extract connection details.
Hope these methods help you track ADB TCP connections effectively! If you run into issues with specific custom ROMs or devices, feel free to follow up with more details.
内容的提问来源于stack exchange,提问作者Jacob Collins

