LinuxCommandLibrary

adb-forward

Forward ports between host and Android device

TLDR

Forward a TCP port

$ adb forward tcp:[local_port] tcp:[remote_port]
copy

List all forwardings
$ adb forward --list
copy

Remove a forwarding rule
$ adb forward --remove tcp:[local_port]
copy

Remove all forwarding rules
$ adb forward --remove-all
copy

SYNOPSIS

adb forward <local> <remote>

PARAMETERS

<local>
    Specifies the local socket to forward from. Can be:
- tcp:<port>: TCP connection on localhost:<port>
- localabstract:<unix domain socket name>: Unix domain socket in the abstract namespace
- localreserved:<unix domain socket name>: Unix domain socket in the reserved namespace
- localfilesystem:<unix domain socket name>: Unix domain socket on the filesystem

<remote>
    Specifies the remote socket to forward to. Can be:
- tcp:<port>: TCP connection on device localhost:<port>
- localabstract:<unix domain socket name>: Unix domain socket in the abstract namespace
- localreserved:<unix domain socket name>: Unix domain socket in the reserved namespace
- localfilesystem:<unix domain socket name>: Unix domain socket on the filesystem
- jdwp:<pid>: Connect to the JDWP process on the device with the specified PID

DESCRIPTION

The adb forward command establishes a connection that forwards TCP/IP communication from a specific port on your development machine to a specified port on an Android device or emulator instance. This is extremely useful for debugging applications running on the device, accessing network services running on the device from your host machine, or enabling communication between different parts of an application across different devices or emulators. The command provides flexibility in specifying the local and remote endpoints, allowing you to forward traffic to specific applications or services running on the Android device. It supports both TCP and Unix domain sockets, enabling different types of network interactions. Multiple forwarding rules can be set up simultaneously. The ADB server manages the forwardings; therefore, the ADB daemon must be running on both your host machine and the Android device for this to function correctly.
Note that the user needs to have the required permissions on both sides to create the tunnels.

CAVEATS

Forwarding requires ADB to be properly configured and accessible. Ensure ADB server is running and device is authorized.

EXAMPLE USAGE

Forwarding TCP port 1337 on your computer to TCP port 8080 on the device:
adb forward tcp:1337 tcp:8080

Forwarding to a specific JDWP process:
1. List PIDs of interest: adb jdwp
2. Forward traffic: adb forward tcp:8000 jdwp:1234

REMOVING FORWARDING

To remove a forwarding rule, use:
adb forward --remove <local>
or remove all forwardings:
adb forward --remove-all

HISTORY

The adb forward command has been a core feature of the Android Debug Bridge (ADB) since its inception. ADB was developed by Google as part of the Android SDK to provide developers with tools to debug and interact with Android devices and emulators. The 'forward' command was introduced early on as a mechanism for developers to access services running on the device from their development machines, primarily for debugging purposes. Over time, its usage has expanded to various scenarios including accessing databases, web servers, and other custom services running on Android.

SEE ALSO

adb(1), adb-devices(1)

Copied to clipboard