Can I use my Android device over USB to SSH into a laptop running Ubuntu?

6,554

Solution 1

I don't know how really this question is related with Ubuntu, but it is fun and I will write an answer.


I have a phone with Android 4.4.4 and often use these tools:


How to connect Android device over USB to SSH into a laptop running Ubuntu

1. Open HotSpot app, go to USB tab and enable USB network sharing. We don't need the connected device to use mobile data over USB, but this option will prepare virtual network that we will use.

2. Open Network Discovery app, go to Settings menuDiscoveryDiscover MethodPing/c.... Go back to home screen and tap on Discover. When this process finishes copy the IP of the target network device. There shall be only one device.

enter image description here

enter image description here

3. Open ConnectBot and setup your SSH key if it is needed (tap on the menu in upper right corner).

4. Setup a new connection - use the discovered IP, and tap on + ADD HOST to save.

5. Go back to the ConnectBot home screen, tap on the new connection and use it.

enter image description here

enter image description here


Additionally if your Ubuntu has GUI and properly configured VNC server, you can setup port forwarding for ConnectBot's connection and can use RealVNC Viewer to connect with your desktop. Pocket Bluetooth keyboard and mouse make things really nice.

enter image description here

Solution 2

You can use adb reverse, for example direct your phone's port 8080 to laptop's port 22 by adb reverse tcp:8080 tcp:22, then do ssh [email protected] -p 8080 from your phone.

I think this is a better solution since no virtual network or IP detecting is needed.

Share:
6,554

Related videos on Youtube

Vesa
Author by

Vesa

Updated on September 18, 2022

Comments

  • Vesa
    Vesa almost 2 years

    So I have an Android smartphone (Samsung Galaxy S5) and I would like to figure out if I can connect it over USB to my laptop running Ubuntu and then control the laptop over SSH and USB rather than over SSH and WiFi.

    Is it possible and if so how can I do it?

    • BONEPIEBONEPIE
      BONEPIEBONEPIE about 7 years
      You can download several apps from playstore in order to get an ssh connection without a USB.
    • wjandrea
      wjandrea about 7 years
      Termux is decent.
    • Vesa
      Vesa about 7 years
      But I don't want to use wi-fi because it's not my router. Please read the question. I want to use USB. Of course, if you know some way to do this without either wi-fi or USB I'm also interested but otherwise I need a solution that works over USB. If it could be done with Termux that would be cool because I like Termux. @wjandrea
    • Vesa
      Vesa about 7 years
      @user654662 Please read my comment above
  • Vesa
    Vesa about 7 years
    Looks interesting and I want to test this. But the "embedded hotspot app" is not available on my device. How can I turn on USB network sharing?
  • Vesa
    Vesa about 7 years
    I have followed your instructions and the mobile phone is connected with USB to the laptop and ping is chosen in the Network Discovery settings but the app does not seem to find the IP address. It just says "obtaining ip address from unknown SSID" and the discovery button has a red warning sign and cannot be clicked.
  • pa4080
    pa4080 about 7 years
    @Vesa, the other name of this functionality is "USB Tethering". For your device follow this manual. The USB cable must be plugged in, to be USB Tethering available. Also WiFi HotSpot is another option to create private network
  • Vesa
    Vesa about 7 years
    Yes, I figured that out but I was still not able to connect yesterday. I think I had the right IP and I tried to connect with Juicy SSH but it just said connecting but nothing happened. Network Discovery wasn't working so I got the IP with Termux/Ifconfig. Still something not right though.
  • Vesa
    Vesa about 7 years
    I tried again today. Same problem. JuiceSSH just says connecting without actually doing so. Should port number still be 22 when using USB or something else?
  • pa4080
    pa4080 about 7 years
    Hi, @Vesa, when USB Tethering is enabled, the phone is presented to the system as network interface with an active network connection. You can find its IP address using different tools for Ubuntu, for example via Connection Information. Find the IP and try to establish SSH connection to this address from Ubuntu itself.
  • pa4080
    pa4080 about 7 years
    By default, if there is no any special rules in /etc/ssh/sshd_config or into the firewall settings the system must listen on the ssh port (22) on all interfaces.
  • Vesa
    Vesa about 7 years
    Hello Spas. Yes, that IP address worked and also I had forgotten to turn off the firewall. Now it actually works and I have been trying to do this for a very long time already so I am grateful for your excellent advice and expertise. Now I have to figure out one more thing. The wifi and "Ethernet" are simultaneously connected but there is no Internet connection. I have to figure out how to make Ubuntu use the wifi for Internet while being connected to the phone. Do you know how to do this?
  • Vesa
    Vesa about 7 years
    It looks from the screenshot above like you have an Internet connection while using VNC from the phone so I am guessing you know already how the settings should be configured.
  • pa4080
    pa4080 about 7 years
    Actually I haven't made any special settings. On the screenshot my computer uses internet from the USB connection. When Mobile Data or WiFi is turned-on on your phone, its internet connection will be shared through the USB Tethering connection. "How to use two network connections in Ubuntu" is a good topic for another question, which, maybe, already has an answer. For example check this and this questions.
  • Vesa
    Vesa about 7 years
    Hi Spas. In my scenario the mobile phone is not using 3G so I cannot get Internet connection from there. Your advice is excellent but you are incorrect in saying that when wifi is turned on on the phone the Internet connection will be shared through USB Tethering. Because turning on USB tethering turns off wifi on all Android phones that I have tested. I have created a new question on askubuntu.com and you are welcome to join with your expertise there: askubuntu.com/questions/909009/…
  • pa4080
    pa4080 about 7 years
    Hi, @Vesa, my old device also cannot use WiFi and USB tethering at the same time, but my current phone (which is 3 years old) can. That made me think all modern devices can do that. I hope that later today I will have time to do some tests and write an answer of those new question. Isn't this answer acceptable to the main question here?
  • pa4080
    pa4080 about 7 years
    By the way, what happens when you turn on phone's WiFi connection after USB tethering is enabled?
  • Vesa
    Vesa about 7 years
    Hello Spas. Yes, you have solved the first problem I had. This is the second issue I am working on now at the link I posted recently. If I turn on the phone's wifi connection after USB tethering is enabled it will prompt me like so "Turning on WI-FI will disable USB tethering" or something like that. This is a fairly new phone, Samsung Galaxy S5. Could it be that it is not new enough or could there be a way of doing it on this phone too? If I cannot use the wifi internet from the phone then it should still be possible to connect the computer to wifi and usb tethering simultaneously.
  • pa4080
    pa4080 about 7 years
    Hi, @Vesa. I read few articles about this case. The desired option - Samsung Galaxy as WiFi adapter is intentionally cut off in version S5. The option is available when the device is routed. It is available for previous devices - S4 etc. Also it is close to impossible to override the device's DHCP and set static IP.... Also I didn't succeed configure Two Default Gateways when using my android device.
  • Vesa
    Vesa about 7 years
    I really don't like it when the corporations intentionally cut off services and probably for commercial reasons. So only for rooted devices then? Hmm, I will continue to try to find a soluion for a while. Your advice is always appreciated Spas.
  • Vesa
    Vesa about 7 years
    Hi Spas. I actually got the setup to work, incredibly, at least with SSH, I haven't tested VNC yet. So I use USB tethering like before and in Ubuntu I set the "Ethernet Network" to gateway 0.0.0.0. I think that disables the gateway. Also, I set a fake DNS address. I think this might have been what fixed the problem. I can connect to wifi internet from the laptop now while remote controlling it over SSH/USB.