27

I'm trying to debug / connect up a device for development using WSL2 (Ubuntu). I've followed steps on this post https://stackoverflow.com/a/58229368/21061 which sets up ADB on both Windows and Linux using the same ADB version.

Once I've done that however, I get an empty list of devices in the Ubuntu terminal. I've tried killing and restarting the ADB server from the Windows command line and that doesn't seem to make any difference. Is this not possible in WSL2 or is there something I'm missing?

Ian
  • 33,605
  • 26
  • 118
  • 198
  • 3
    At the time of writing, USB device access isn't officially supported in WSL2. But there may be a way to use custom compiled Linux kernel in WSL2. See this PR https://github.com/microsoft/WSL2-Linux-Kernel/pull/45. – Biswapriyo Feb 14 '20 at 07:00
  • @Biswapriyo thanks. I spent a couple of days trying this. In the end I've switched my OS to pure Ubuntu. – Ian Feb 14 '20 at 12:53
  • @Biswapriyo do you want to add your comment as an answer? I'll accept it then – Ian Mar 05 '20 at 13:42
  • I did not try it in WSL and not even do in future. I have adb in Windows and dual booted ArchLinux. – Biswapriyo Mar 09 '20 at 15:14
  • I also look forward to have usb adb devices support in wsl2. Anyone know when it will happen? – Paweł Iwaneczko Nov 06 '20 at 00:53
  • I added an answer that worked for me (well kind of a workaround?) to the question you linked – Aur Saraf Nov 09 '20 at 15:55

5 Answers5

40

This answer worked for me using WSL 2:

On Windows:

adb tcpip 5555

Then on WSL 2:

adb connect [ip device]:5555

If it's the first time, it's going to ask you for permission in your phone, make sure to check the box to always grant permission. Then restart adb and connect again:

adb kill-server
adb connect [ip device]:5555

You can get your [ip device] from: Settings > About device > Status. It has a form like: 170.100.100.100

  • 2
    I am getting `unable to connect to 172.19.112.1:5555: Connection timed out` – Marek Urbanowicz Dec 12 '20 at 18:23
  • 1
    @MarekUrbanowicz Which versions are you using? Is working for me using `ADB 1.0.41 version 30.0.5-6877874`, make sure both ADB are the same version. Check if your pc and phone are using the same network. Check if `adb devices` on Windows shows your phone when is plugged to the pc. Also maybe your firewall is blocking the connection – Jorge Guillermo Negrete Dec 14 '20 at 17:03
  • To switch back to USB mode use: adb usb – Slion Aug 25 '21 at 08:39
  • Great stuff, used this to fetch blobs as I'm attempting a Lineage OS build from WSL. – Slion Aug 25 '21 at 08:47
23

Jorge's answer is fine, but beginners like me may need some more details.

[ip device] is [YOUR_PHONE_IP] address. To get the IP address of your phone, go to "Settings -> About phone -> Status -> IP address". It will probably be something like 192.168.x.y.

I did not add adb to my PATH variable - neither in Windows nor in Linux/WSL2. Instead I just downloaded the latest version for both OS's using the links below:

https://dl.google.com/android/repository/platform-tools-latest-linux.zip https://dl.google.com/android/repository/platform-tools-latest-windows.zip

Once I unzipped platform-tools I had to change directory to the unzipped folder (cd platform-tools) in both PowerShell and WSL2.

Then in PowerShell on Windows, I run .\adb tcpip 5555 in the platform-tools folder.

In WSL2 terminal, I run ./adb connect 192.168.2.199:5555 (where 192.168.2.199 was my PHONE_IP address).

The first time you connect using [YOUR_PHONE_IP] address, you will be prompted to confirm the connection. The adb might say it failed to connect while it was waiting for the confirmation. If so, run ./adb kill-server in the WSL2 terminal and then run ./adb connect [YOUR_PHONE_IP]:5555 again.

You can display the list of attached devices via .\adb devices in PowerShell and ./adb devices in WSL2.

That is all. Now you should be able to debug your phone using WSL2.

NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70
Davidou
  • 339
  • 2
  • 4
  • Definitely enough new and useful information here to be worthy of a new answer, IMHO. Thanks for the information, and Welcome to Stack Overflow. – NotTheDr01ds Apr 14 '21 at 20:08
  • @NotTheDr01ds Thanks for the corrections and a warm welcome : ) – Davidou Apr 14 '21 at 20:47
  • Thanks, it works. But for some reason, I'm getting an endless loop of "#Waiting for application to start" – Erick Adam Jan 20 '22 at 19:17
  • I'm using a device (not a phone, can think it as a raspberry pi board), and it doesn't have Ip (no ethernet connection on it yet), then how to access it in `wsl2` via `adb devices`? – Shawn May 19 '22 at 13:32
  • looks like the adb in WSL2 directly connect to your phone(as `./adb connect yourPhoneIP:5555`), then what is the purpose of running `adb tcpip 5555` on windows? – Shawn May 20 '22 at 02:08
21

we can do it more easily on WSL2

if we installed adb in windows and the path is /mnt/c/platform-tools/adb.exe

the WSL2 adb install at /usr/bin/adb

let change adb to adb_bk and set ln -s to link adb.exe

sudo mv /usr/bin/adb /usr/bin/adb_bk

sudo ln -s /mnt/c/platform-tools/adb.exe /usr/bin/adb

now we can use adb and work at bash script

try it

Abhishek Dutt
  • 1,308
  • 7
  • 14
  • 24
chiuyuan he
  • 211
  • 2
  • 2
  • when I then try to push a file, adb.exe doesn't recognize `\\wsl$`... – Michael Sep 24 '22 at 01:22
  • 1
    The trouble with this technique is you are running the Windows version of adb. Which can have some unintended consequences. For example, running an `adb shell getprop` will print each line with Windows line endings (`\r\n`) which can cause issues for other linux tools. – awm129 Dec 11 '22 at 17:01
3
I was trying with this myself to get WSl2 working and i didn´t found any fast way to do this, this is easy and it is safe for everyone that wanna solve the problem:
printf '\n%s\n%s\n' \
'### Alias  for Android Debugging in WSL2' \
'alias adb="/mnt/c/Program\ Files\ \(x86\)/adb/adb.exe"' \ 
 >> ~/.bashrc

source ~/.bashrc
And now we can work as we wants in WSL1/WSL2 shell:
C:\> adb devices
List of devices attached
P1CN21AKXAZ     device

If adb.exe in windows will find your android device then also adb in wsl will. I writing this since I dont recommending anyone to use adb tcpip (that is recommended on few places I seen) without know exactly what risks it entails. I linking my script for hacking any android device within 1-3 seconds by copy and paste from devices that used adb tcpip command without understanding consequences, I want to point out that I am writing this to announce how dangerous it can be to give advice on something without being equal to giving the advice that when you are done, turn it off ASAP!

For people using over tcpip and is done with their tasks, execute:

adb disconnect
Of course you can create an symlink into /usr/bin also for using it system-wide if that is prefered.
 ln -s "/mnt/c/Program\ Files\ \(x86\)/adb/adb.exe" /usr/bin/adb

https://pastebin.com/raw/DtFSMBsF

wuseman
  • 1,259
  • 12
  • 20
  • 1
    adb devices showing device but getting error when run com.android.builder.testing.api.DeviceException: No connected devices!. any solution? – Sreenath Mar 12 '22 at 15:21
  • when I then try to push a file, adb.exe doesn't recognize `\\wsl$`... – Michael Sep 24 '22 at 01:22
3

in WSL2

sudo mv /usr/bin/adb /usr/bin/adb_bk

sudo ln -s /mnt/c/platform-tools/adb.exe /usr/bin/adb

adb devices

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 29 '22 at 06:51
  • 1
    I guess you are proposing to use Windows' adb, right? – Raúl Salinas-Monteagudo Jul 04 '22 at 07:28
  • yes, yes, sharing windows adb with wsl2 – GABRIELRENAN Jul 05 '22 at 12:14
  • when I then try to push a file, adb.exe doesn't recognize `\\wsl$`... – Michael Sep 24 '22 at 01:21
  • Running this in windows creates the reverse tunnel: ssh -R 3128:localhost:3128 mark@localhost I was then able to set my WSL proxy to localhost:3128 and it all worked. – GABRIELRENAN Sep 26 '22 at 17:00