adb-forward
Forward ports between host and Android device
TLDR
Forward a TCP port
List all forwardings
Remove a forwarding rule
Remove all forwarding rules
SYNOPSIS
adb [-s <serial> | -d | -e | -t <transport_id>] forward [--no-rebind] <local> <remote>
adb [-s <serial> | -d | -e | -t <transport_id>] forward --list
adb [-s <serial> | -d | -e | -t <transport_id>] forward --remove <local>
adb [-s <serial> | -d | -e | -t <transport_id>] forward --remove-all
PARAMETERS
<local> <remote>
Specifies the local and remote endpoints for the forward rule. These can be various types of sockets:
tcp:<port>: TCP port (e.g., tcp:8080).
localabstract:<name>: Unix domain socket in the abstract namespace.
localfilesystem:<path>: Unix domain socket at a specific filesystem path.
dev:<path>: Character device (e.g., dev:/dev/ttyS0).
jdwp:<pid>: Java Debug Wire Protocol (JDWP) connection to a specific process ID.
--list
Lists all currently active port forwarding rules set for the specified device.
--remove <local>
Removes a specific port forwarding rule identified by its local endpoint specification.
--remove-all
Removes all active port forwarding rules for the specified device.
--no-rebind
Prevents adb from rebinding an existing local socket if it's already in use by another forward rule. If the local port is in use, the command will fail rather than overwriting an existing rule.
-s <serial>
Targets a specific Android device by its unique serial number. Useful when multiple devices are connected.
-d
Targets the only currently connected USB device. Fails if multiple USB devices are connected or no USB device is found.
-e
Targets the only currently running emulator. Fails if multiple emulators are running or no emulator is found.
-t <transport_id>
Targets a specific device by its transport ID. Useful for distinguishing devices when other selection criteria are ambiguous.
DESCRIPTION
adb forward establishes a network port forwarding rule between the host machine (where adb is run) and an Android device or emulator. This allows applications on the host to connect to a specific port on the device, or vice-versa (with adb reverse). It's commonly used for debugging, accessing services running on the device from the host, or testing network applications.
For example, you can forward a local host port (e.g., 8080) to a device port (e.g., 5000) so that connecting to localhost:8080 on your computer actually connects to port 5000 on the Android device. This command is part of the Android Debug Bridge (ADB) utility, which provides a versatile command-line tool for communication with an Android device.
CAVEATS
Requires the adbd daemon to be running on the Android device and USB debugging to be enabled and authorized. The specified local port must be available on the host machine. Forwarding ports can expose device services to the local network, potentially creating security vulnerabilities if not managed carefully. Some operations (e.g., accessing certain device filesystems or ports) might require root privileges on the Android device.
REVERSE PORT FORWARDING
While adb forward creates a host-to-device connection, adb reverse (introduced later, starting from Android 5.0 Lollipop) allows forwarding a port from the device to the host. This is useful for device-side applications to connect to services running on the development machine.
RULE PERSISTENCE
adb forward rules are typically temporary. They are automatically cleared when the adbd server on the device restarts, or when the adb server on the host machine restarts. For persistent forwarding, a script or startup service would be required.
HISTORY
adb has been an integral part of the Android SDK since its early days. The forward command has been a core feature from the beginning, providing essential connectivity for developers to interact with and debug applications on Android devices and emulators. Its functionality has remained largely consistent, with minor enhancements and support for new socket types (like jdwp) being added over time.