Skip to content

Instantly share code, notes, and snippets.

@Steakeye
Last active October 1, 2024 08:33
Show Gist options
  • Save Steakeye/f44bff0e142d6b228edcd3d123dcdb87 to your computer and use it in GitHub Desktop.
Save Steakeye/f44bff0e142d6b228edcd3d123dcdb87 to your computer and use it in GitHub Desktop.
Setup ADB on WSL and the Windows host so you can connect to the Android device from WSL
netsh advfirewall firewall add rule name="ADB WSL2: Open Port 5037" dir=in action=allow protocol=TCP localport=5037 remoteip=172.16.0.0/12 profile=domain,private
# Kill any currently running ADB server
adb kill-server
# Fall back to this method if the simpler version doesn't work
#export WSL_HOST_IP="$(tail -1 /etc/resolv.conf | cut -d' ' -f2)"
export WSL_HOST_IP="$(ip route list default | awk '{print $3}')"
# Use socat to relay host TCP packets for port 5037 to WSL on the same port
socat TCP-LISTEN:5037,reuseaddr,fork TCP:$WSL_HOST_IP:5037

Using ADB in WSL via host ADB

This gist covers how you allow the WSL install of ADB to receive data from the host Windows instance of ADB.

This method was derived from the following Stack Overflow questions:

Host setup

The following section describes how to set up the host machine so that the guest and host versions of ADB are able to communicate, thus allowing you to develop in WSL and pass through data to an external device connected to your host machine.

To summarise, the required ADB port is open between the guest and host WSL instance; then every time you start up your host machine, you start the ADB server in a particular mode that allows the forwarding of data.

Adding a Firewall rule for WSL TCP data on ADB port

This only needs to be run once.

Buy running enable-adb-port-for-wsl2.ps1 you will set up a Firewall rule on the host machine that will allow data coming from the WSL subnet on port 5037 to be passed through to any process listening on 5037 on your host machine. This firewall rule will be given the name "ADB WSL2: Open Port 5037" should you need to find it and make changes.

Note: You'll want to 'Run As Administrator'

Setting up your host ADB server

This will need to be run every time you boot up your machine.

By running start-adb-for-wsl.bat on your host machine, you will restart ADB in nodaemon mode, which allows it to port forward any data it receives, thereby acting like a proxy between your test devices and your guest OS (WSL).

Guest WSL setup

The following section describes how to set up the guest WSL OS so that the guest versions of ADB is able to communicate with the host.

Adding ADB config to your environment variables

This only needs to be run once.

By running setup-adb-env-var.sh you will find the IP of your host machine and create a variable called ADB_SERVER_SOCKET that is acts as a directive for ADB to look for tcp data from the host IP over the default ADB port, 5037.

Alternative: socat

If you choose this method, this will need to be run every time you boot up your machine.

If for some reason to don't want to set up env vars that configure your WSL ADB server to look for external connections, you can use socat to open up a TCP connection on an ad-hoc basis.

Run listen-for-host-adb-data.sh when you start up WSL to start listening for tcp data on port 5037 from the host machine.

You need to install socat on WSL if it's not already installed, here's an article about socat https://www.redhat.com/sysadmin/getting-started-socat.

{
echo ''
echo '# Setting up ADB on WSL2'
echo 'export WSL_HOST_IP="$(ip route list default | awk '"'"'{print $3}'"'"')"'
echo 'export ADB_SERVER_SOCKET=tcp:$WSL_HOST_IP:5037'
} >> ~/.bashrc
:: Start ADB server so it listens to all network interfaces, not just localhost, this was we can listen for events coming from WSL
adb kill-server
adb -a nodaemon server start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment