---
# System prepended metadata

title: "ADB accessing in\_WSL2"
tags: [android, adb, wsl]

---

# ADB accessing in WSL2

### Steps

1. WSL2 enable `ssh-server` and allowing login via ssh-key.

```bash=
apt-get install openssh-server
```

```bash=
vim /etc/ssh/sshd_config
# Port 22
# PasswordAuthentication yes
```

```bash=
/etc/init.d/ssh restart
```

2. Write ssh public key of host to WSL2's `authorized_keys` file.

> **{REMOTE-USERNAME}** is your username in WSL2
> **{REMOTE-IP-ADDRESS}** is your WSL2's IP address

```bash=
# Windows (pwsh)
type $env:USERPROFILE\.ssh\id_rsa.pub | ssh {REMOTE-USERNAME}@{REMOTE-IP-ADDRESS} "cat >> .ssh/authorized_keys"
```

3. Start new ADB server session in Windows (Host) side.

```bash=
# Windows (pwsh)
adb kill-server
adb start-server
```

4. SSH tunnelling from Host to WSL2.

**Hint: DO NOT use `localhost`, just use `127.0.0.1` directly**

```bash=
# Windows (pwsh)
ssh -XC -R 5037:127.0.0.1:5037 {REMOTE-USERNAME}@{REMOTE-IP-ADDRESS}
```

|Option|Description|
|-|-|
|-X|Enables X11 forwarding. This can also be specified on a per-host basis in a configuration file.|
|-C|Requests compression of all data (including stdin, stdout, stderr, and data for forwarded X11, TCP and UNIX-domain connections).|
|-R|`-R remote_socket:host:hostport`<br>Specifies that connections to the given TCP port or Unix socket on the remote (server) host are to be forwarded to the local side.|

5. In WSL2 side, now we can perform adb commands directly.

```bash=
# WSL2
adb devices
# output:
#    List of devices attached
#    0123456789ABCDEF  device
```

### References

* [How to Enable SSH on Ubuntu 20.04](https://linuxize.com/post/how-to-enable-ssh-on-ubuntu-20-04/)
* [ADB in WSL2](https://github.com/microsoft/WSL/discussions/4692#discussioncomment-87987)
* [[WSL2][Ubuntu][踩坑]adb的两种实现](https://blog.csdn.net/u014175785/article/details/113438143)

###### tags: `android` `adb` `wsl`