# Owner: glow # KeyID: E65A8520F36AEE13CFE4F56BEB8FE331063A4794 # Key: https://glow.li/pgp # Verify: curl "https://glow.li/posts/access-termux-via-usb/index.html.asc" | gpg -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA512 +++ { "date": "2016-09-20", "layout": "post", "oldurls": [ "technology/2016/09/20/access-termux-via-usb.html", "technology/2016/9/20/access-termux-via-usb/" ], "tags": [ "Technology", "Termux", "CLI", "Android", "Tutorial", "Snippet", "SSH" ], "title": "Access Termux via USB" } +++ I've been using [Termux over SSH]() for quite a while now. I've always done so over WiFi. This works reasonably well at home, where I control the IP Addresses. In other networks it was more annoying, because I always had to run `ifconfig` first, to get my IP. But the big annoyances start when you want to use it in a place that has no WiFi network available. Previously, I used an Android tablet and a keyboard. With this I set up a WiFi direct connection between my phone. It's wonky and sometimes requires to restart both devices before working again, but it works. Then I got a laptop. Installed a proper Linux on it and tried to connect to Termux. I tried setting up WiFi Direct. But after an hour or so of messing around `wpa_supplicant` I concluded that it's just too much of a hassle. But somewhere in the GitHub issues I came across the mention of connecting to Termux via ADB. The ADB shell is bad. I didn't actually manage to launch Termux programs via the ADB shell, but I heard that it's possible. But, and this is the point of this article, you can create port forwarding via ADB. This means that you can map a local port on your computer to a port on your Android device. Then you can open the 8022 port to access Termux via SSH over USB. This is awesome, when you're on the train, in a coffee shop or otherwise in a place with wonky or nonexistent WiFi. ## Here is how you do it ### 1. You need ADB Most operating systems have an ADB package. On my one I was simply able to install it with: ``` sudo apt install adb ``` For other platforms you can check this site [here](https://www.xda-developers.com/install-adb-windows-macos-linux/). ### 2. Create your port forward This part is really simple as well, and that's key. I don't want to do 500 things every time I want to connect to my phone. ``` adb forward tcp:8022 tcp:8022 ``` This is really all it takes. The first instance of `tcp:8022` is the local port you want to bind the remote port to. The second one is the port from your Android device. Because Termux' SSHD runs on port 8022 by default, this is what you want. ### 3. Connect to it Now that your local port is bound to your Android device you can simply connect to localhost on your computer: ``` ssh localhost -p 8022 #This is were you put the local port #(ie. the first one) ``` This will connect you to your SSH Server. If you haven't set this up yet read [this](). You will need to run the ADB command after every restart or after you've unplugged your device. I set up an alias for myself for the following command sequence: ``` adb forward tcp:8022 tcp:8022 && adb forward tcp:8080 tcp:8080&& ssh localhost -p 8022 # This will also setup the port 8080, which is used by the httpd webserver on termux ``` -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEE5lqFIPNq7hPP5PVr64/jMQY6R5QFAmRuqEkACgkQ64/jMQY6 R5SX2w/+OQM9ZKnqVL79Fmpv/znSEg8nE/n20twmRNdD2myUisxvpxi6l3Z0eCof +j1vd3elTCYUD29suJaFi77RogwrMbkbymaZkZF1p4S2HBcazCPD0UarLf0byKLY YblcaseHW8Muij5bDZ7Usrrs4Q27nSvR3ptaPMnrxbKyWwlpNL/7K7RkS2+S2CAe iMcNLGaEpZZcU49IHlzjoTKzUDfWB2QND27zdHRMuoZ7tBRp9Xh55f59v6sdMLnQ c1Vk9p/5WzBIgvgCGMBxtSV2tyV50wZN8tNtAhebmtP8K3cqvnfr87wXW4kE3lod /ZxlC5TAk+klpFaJSiDAEOflvqzXr4yssi7lgXRfXLpLauARFHH8TsClRZgcA5D4 2gwwnEUbePCj8qvu+I4sTJ+Wsj9BEMm6EcrgdMpcPedPwt0Y1M7oZiBSGOuFnSDQ v7tY6JvOzuOa7yzj/j5nondsERqnpSKbHMo4nCDZYUlIe2ktQrBUrsNvw+5pLayK nZxjA7BJGLi/hq5mZHjpeLhp4NDi+OMj3ycPow05ekw7YxKvUlrvwnWNQ/cutOrn X3G+sjVSebBnlTmuOhvsRJZVUuKCaMwtlIba8/y9SuiVwuvFeGtdTZHnAYura/WJ uZjFQclubh5OT5dtJqomrvhG8oBfKj0EQeSiP0yAzgvFkKz01cI= =2ivM -----END PGP SIGNATURE-----