Sometimes you really want to play with a Raspberry Pi, but don’t have a display, keyboard, or mouse handy, and the wifi isn’t configured correctly to just be able to SSH in. Invariably you spend an hour digging around for a keyboard or refreshing a wireless clients list, but this doesn’t have to be the case. After a quick one-time setup, everything you need to use a Raspberry Pi will already be in your pocket.
The idea
Once configured, if you have an Android phone with USB tethering and a cable, you should be well-equipped to use your Pi. Bonus points if you have a bluetooth mouse and keyboard. We’ll be setting up a USB network interface on the Pi and installing a VNC server to pass a session over that interface, thus making your phone a Raspberry Pi display. By the end you should be able to just power up your Pi, plug in your phone, turn on USB tethering, and open up a full desktop.
Setting up the network
Log into your Pi via SSH or open up a terminal in its GUI and pull up your network interfaces.
sudo nano /etc/network/interfaces
Paste the following onto the bottom of the file, then save and exit (ctrl-X, Y):
iface usb0 inet static address 192.168.42.42 netmask 255.255.255.0 network 192.168.42.0 broadcast 192.168.42.255
On your next restart, you should have a new interface when you type ifconfig
. We’ve set this interface to have a static IP address, always 192.168.42.42, which you will later use to start your VNC session or connect via SSH on your phone.
Configuring VNC
VNC, or Virtual Network Computing, is a way of sharing a graphical desktop environment over a network, which in this case happens to be your phone’s USB cable. First, we need to install a VNC server onto the Raspberry Pi. We’ll be using TightVNC since raspberrypi.org has a tutorial for it and it’s easy to find help on forums.
sudo apt-get install tightvncserver
Next, use the command tightvncserver
to configure VNC for your Pi. It should ask you for a password–be aware that TightVNC will truncate your password to eight characters. It does tell you this in the terminal, but it can be easy to miss and lead to many failed login attempts.
Lastly, we need the VNC server to start up every time the Pi starts so that you really do only need your phone. First, change into your /etc/init.d directory.
cd /etc/init.d
Create a new file called vncboot. You’ll need root privileges to change anything in this directory.
sudo nano vncboot
Paste the following into the file (change export USER=’pi’ to your username if not pi, and edit the screen resolution in the start) block if necessary):
#! /bin/sh ### BEGIN INIT INFO # Provides: vncboot # Required-Start: $local_fs # Required-Stop: $local_fs # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Run tightvnc on boot ### END INIT INFO PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin export USER='pi' eval cd ~$USER . /lib/init/vars.sh . /lib/lsb/init-functions case "$1" in start) log_begin_msg "Starting VNC server" su $USER -c '/usr/bin/vncserver :1 -geometry 1680x1050 -depth 24' log_end_msg $? exit 0 ;; stop) pkill Xtightvnc log_begin_msg "Stopping VNC server" log_end_msg $? exit 0 ;; *) echo "Usage: /etc/init.d/vncboot {start|stop}" exit 1 ;; esac
Save this file, then update its permissions:
sudo chmod 755 vncboot
Finally, run the following command to add it to your startup:
sudo update-rc.d vncboot defaults
Reboot your Pi and it should be ready to rock.
Getting connected
Now that one side of your setup is complete, you’ll need a VNC client on your phone. VNC Viewer seems plenty quick for this purpose and you can’t argue with the price. Optionally, you may also download an SSH client like JuiceSSH for those times when a GUI just isn’t necessary.
With your app downloaded, power up your Pi and connect your phone via a data USB cable. As your Pi boots up, you should get a notification that the phone is now connected as a media device. Go into your phone’s settings and turn on USB tethering.
Assuming you’ve given the Pi enough time to boot, you should now be ready to pull up your desktop. Open your VNC viewer app, connect to 192.168.42.42:1
(the :1 is important here), and provide your password. If you just need to SSH, open up your SSH app and connect to 192.168.42.42
.
Congratulations! You now have a Raspberry Pi display, keyboard, and mouse even when you don’t physically have those items available.
nice article.
What if I want to use bluetooth tethering and bluetooth dongle?
is it also possible?
On the Raspberry Pi side of things this should be possible. IP networking (like what’s being done with the USB port) can work over Bluetooth on the Pi (good writeup at http://notes.pitfall.org/ip-over-bluetooth-to-a-raspberry-pi.html). If an Android phone could be configured to use IP networking over a Bluetooth connection I don’t see why it couldn’t work, but I’m not certain what such a setup would look like on the phone side.
I’m also having an issue with not seeing usb0 in my interfaces. I reboot and run ifconfig but I’m only seeing lo, eth0, and wlan0. I went back to make sure the edits were saved, and they are there.
I’m wondering if my USB wifi dongle is causing problems.
You may be right about /etc/network/interfaces not updating anything if network manager is handling the connections because of the USB wifi. In theory if it’s manually configured in that file, network manager shouldn’t try to manage it, but who knows. I’m not finding an easy answer to this with a quick search, so my instinct would be to change usb0 to a different number like usb1 in your /etc/network/interfaces and see if that works. I’ll keep looking.
thx for the great tuto.
I’m also having the problem with the static ip of usb0 on raspbian.
I’ve made the changes in the interfaces file, and “cat /etc/network/interfaces” show them to be there fine. But on the next reboot, no usb0 in ifconfig. I’ve tried it a number of times. I’ve also tried to use usb1 instead of usb0, but no luck.
Any ideas ? JP.
It could be that the android device you’re using doesn’t have USB tethering, or that the cable in use is a 2-wire charge cable instead of a 4-wire data cable. I’d say the first thing would be to check your device to ensure tethering is an option, and then plug it into your PC to see if it’s recognized (if it’s a cable issue, the PC won’t see it at all).
How to fix the usb0 not showing up in ifconfig.
Make sure to do each step.
– Connect data cable
– Physically go and turn on usb tethering again. Mine shut off every time I plugged in the usb
– Run ifconfig with tether turned on. You will see usb0 but it will probably have a different address.
– Change the address in the VNC app to what is actually shown on the ifconfig print out
* mine changed from 192.168.42.42 to 192.168.42.214. I left it at port 1, so 192.168.42.214 but they key here is that it’s not actually on 42.42 but something else.
That should fix it.
FYI I am on a raspberry pi 3 late 2017 and android 6.0 not that it matters.
Here is a picture of it working.
https://pasteboard.co/GNj836W.jpg
Great tutorial,
I tried it with Rasbian on a rpi b2, but the static ip over usb0 is not showing up. To make sure, do I need to plug my android phone, in order to show the new connection with ifconfig?
Erik
I don’t have a Pi around to confirm this right now, but I don’t think the IP will show up until the phone is attached; however, the interface itself should definitely show up. So once those settings have been saved into /etc/network/interfaces, when you restart your Pi and run ‘ifconfig’ you should definitely see that there’s an interface called usb0, even though it’s not connected to anything. To verify that your settings are in there, run ‘cat /etc/network/interfaces’ and check that those settings show up. Good luck!
How do able to use an actual keyboard plugged in my raspi? Because I dont have a bluetooth keyboard, and onscreen keyboard is too small
You’ll probably need to use x11vnc to accomplish this. tightvnc starts its own X session that’s separate from the host’s X session, so the keyboard and mouse would need to be on the phone in the setup I described. I’ve tracked down a thread about setting up x11vnc here: https://www.raspberrypi.org/forums/viewtopic.php?p=108862#p108862
This should allow for a shared X session so that the display on your phone and the inputs on the Pi are in the same world, so to speak. Good luck!
Thanks for the tutorial! Have you seen the VNC Viewer have issues with the port? I have the port number after the IP address but VNC Viewer throws up the following error:
“The port on which the computer is listening for a connection could not be contacted.”
Any suggestions? I have tried many different ports including: 0, 1, 2, 3, 4, 5, 6, 5800, 5900, 5901.
Thanks again!
Nevermind. The RPi is failing when it tries to run the script on startup. I can SSH in, start the tightvncserver manually, and then connect with VNC Viewer with no problem. I will have to troubleshoot why the script is failing.
Thanks again for the tutorial!
Good find! The init.d script can be a tricky one due to its specific syntax, permissions, and the need to run update-rc.d afterwards. Let me know if you’re able to get it to work, as I’ve had a few other projects that needed updating recently for Raspbian Jessie, and if you’re running that you’ll see if the script still functions on it well before I can get a spare Pi set up for a clean test.
Hi. Does this project apply to the Model B? I do not get a new interface usb0 or usb1. It also does not download the tightvncserver.
Thanks, Jerry
having trouble
1. when I go to usb tethering it still says device not connected (even Though its connected)
2. when I hit begin tethering it says server timed out.
3. when I hit connect in VNC the connection times out and I keep hearing the connect beep like its trying to.
Oh yeah I’m using a rooted galaxy tab s 10.5 sm-t800
do I need to possibly flash a new ROM being my current one id fairly old?
I tried this on Raspberry pi 3. Installed Ubuntu Mate 15.10. I followed whatever you mentioned in this blog. Somehow the interface usb0 does not show up when I run ‘ifconfig’ after reboot. My phone does have a USB tethering option. The cable does work on a ubuntu machine. I mean it shows the option whether to connect as a camera or a media folder. Is there anything I am missing here?
Somehow i get the “gray screen” issue described in many online fora.
Meaning I get the connection up and running but no image except for the gray background and a cross (X) as mouse pointer. I made sure the geometry settings on both ends are similar, but no help there.
Any other fix for this? Thanks ina advance.
Could be an issue in a new version or something. Sounds like you’re getting a whole separate X11 session when you should be getting into your existing one that was created on startup with the vncboot script. Check that line 23 in /etc/init.d/vncboot includes the :1 and possibly try connecting to :0 instead. It’s possible that the connection is going to the first available open port or something along those lines instead of the running session.
Out of curiosity, since you were able to get the usb networking going if you got that far, what version of Raspbian are you running and on what type of Pi? I’ve been getting a lot of feedback that the usb network interface isn’t forming with these instructions, but haven’t been able to narrow down exactly why.
There are a lot of ways for do this, one of them is by modifying the /etc/network/interfaces and adding this lines
allow-hotplug usb0
iface usb0 inet static
address 192.168.42.10
netmask 255.255.255.0
and then save it and restart your vnc server on the static address 192.168.42.10
then you conect your phone and activate usb Tether……Enjoy
Hi i have followed the tutorial and can SSH into the Pi using VXconnectbot but when i try to connect via VNC Viewer i get “The computers domain name could not be resolved” any idea on what im doing wrong? :( also thanks for the tutorial nice and easy steps just a shame i have fallen at this hurdle haha.
i have the usb0 not found problem. vnc works ok, phone no able to enable usn tether as there is not found connection.
it seems that noone made it work over usb.
Ok, so I have solved the usb0 missing problem. As stated above, I was using a charge only microusb cable, and just found that there is such thing. I was using the kindle usb cable, which is only for charging.
After that, the tether option was available on my phone and usb0 showed up with ” ifconfig ” , and here: ” ls /sys/class/net/ “, and ” ip link “.
hope this helps someone else.
Ok so i have followed through all of the steps listed in this tutorial. If i connect my Tablet to the Pi, change to USB tethering and launch the VNC viewer app on the tablet. If i add the Pi’s IP and try to connect, it says something like: “The IP cannot be reached” and it doesnt connect it then :( Please respond with a solution
When I typed ‘sudo update-rc.d vncboot defaults’, it returned ‘update-rc.d: error initscript does not exist: /etc/init.d/vncboot’. What do I do?
Has anybody been able to solve the usb0 not in ifconfig issue yet? I’ve been searching for days and have not found much help.
Thanks for the tutorial.
I had trouble to connect.
The trouble is that in my RPi the tightvnc server was runnong at a different port.
According to
https://programmaticponderings.com/2012/12/26/installing-tightvnc-on-the-raspberry-pi/
tightvnc server runs on 5901
Use the command sudo netstat -tulpn
to see the port that the vnc server is running and
connect
In my case the result was
sudo netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:5901 0.0.0.0:* LISTEN 542/Xtightvnc
tcp 0 0 0.0.0.0:6001 0.0.0.0:* LISTEN 542/Xtightvnc
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 476/sshd
so I had to log at
192.168.42.42:5901
Good job, cheap 10 tablet as RPI display
I manage to connect through the wi fi.
Still trying with the USB cabe. The problem is that the static adress is not assigned btu the adressis 192.168.42.* not what expected, and every time the cable is pulled out, or the RPi reboots, the addres is different. needs some polishing
I append th econtets of the
/etc/network/interfaces
pi@giorgos-raspberry-Pie:~ $ cat /etc/network
network/ networks
pi@giorgos-raspberry-Pie:~ $ cat /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)
# added last lines for taBLET CONNECION
#according to
# http://joshuawoehlke.com/android-raspberry-pi-display-over-usb/
# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and ‘man dhcpcd.conf’
# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d
auto lo
iface lo inet loopback
iface eth0 inet manual
allow-hotplug wlan0
iface wlan0 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
allow-hotplug wlan1
iface wlan1 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
################################
# added lines
# according to
#https://www.raspberrypi.org/forums/viewtopic.php?f=36&t=18916
#by vslash » Thu Sep 25, 2014 10:52 am
#allow hotplug usb0
#auto usb0
iface usb0 inet static
address 192.168.42.42
netmask 255.255.255.0
network 192.168.42.0
#followinf line added accordint to
#http://rwhitmire.com/2014/03/25/android-tablet-raspberry-pi.html
gateway 192.168.1.1
broadcast 192.168.42.255
ifconfig usb0 192.168.42.42
#allow-hotplug usb0
#iface usb0 inet static
#address 192.168.42.10
#netmask 255.255.255.0
#
#When you would have already connected all devices,
#and you do not want to reboot, just restart the network:
# /etc/init.d/networking restart
#
#according to
#http://elinux.org/How_to_use_an_Android_tablet_as_a_Raspberry_Pi_console_terminal_and_internet_router
# teh following lines
#allow-hotplug usb0
#iface usb0 inet dhcp
end of file
thanks again for the help
Each time the RPi reboots or the cable is diconnected the IP is lost. Next time a different IP is assigned.
Also each time you diconnect you have to re enablel the tethtering from the tablet side
One solution is to use network utilities (from App Store) to discover the IP that the RPI is using for the usb connection.
My minor problem is that the tablet has one USB connection and I cannot use a keyboard but the soft keyboard.
Thanks for the info.
I’m getting a different IP address every time the PI restarts, so the static part is not working. Is anyone having the same problem, or found a solution?
So when I type “sudo chmod 755 vncboot” to give the files the permissions, it returns nothing. So when I run “sudo update-rc.d vncboot defaults”, it says that vncboot missing lsb tags and ovverrides.
I found your blog after I hit an issue with something built on same strategy. Still debugging that. What happened , android has two dns range 192.168.42.255 and 192.168.43.255
Now I tried to create 2 virtual usb with following static ips
192.168.42.11
and
192.168.43.11
and assume one of them will be accessible , but now mobile is giving me a dynamic ip with same config as yours. Any idea how to handle this. I am basically running an http app on rpi and check in app which of the ip is up 42.11 or 43.11 via simple http get and launch whichever comes up. If I do networking reboot once tethering is on, this works.
Hi guy’s ,
Just a little remark if you have a PI 3 B you need to leave /etc/network/interfaces allone
when you need a static IP . But instead go and add e.g.
interface eth0
static ip_address = 192.168.xxx.xxx/24
to the /etc/dhcpcd.conf file ..
more info can be found at : https://www.raspberrypi.org/forums/viewtopic.php?f=66&t=140252
I hope this helps
Hi guy’s,
So I have done all steps from this blog , but after the reboot I only see “lo” in the ifconfig (before the Reboot everything was fine, I also have seen USB0) and my wireless interface is gone too.
Hey!
Great tutorial.
Only thing to edit is that when you are editing the network/interfaces file
the following line needs to be added to the top :
allow-hotplug usb0
Thank you for taking the time to post this thread. It’s people like you who truly have a great impact on the development of tech. My hats off to you. Keep up the great work !
This has, so far, been a very frustrating journey.
I *think* my issue is that my home network is a 10.0 and not a 192.168 and I’ve not quite hit the correct combination yet.
If I set up like this and other posts state then I see the usb0 interface and it has the 192.168.42.42 address but the tablet can never connect there – either ‘no route to host’ or the connection just times out.
If I ssh into the pi on it’s assigned address then I can ping the 192.168.42.42 address and I get a response.
I’ve tried changing the 192.168 to 10.0 in all the examples but so far no joy.
I know just enough to be dangerous but if anyone can give me a pointer in this case I’d greatly appreciate it.
Thanks for the article. Updated steps for Buster is here
https://ten0s.github.io/blog/2020/02/22/raspberrypi-static-ip-address-for-android-usb-tethering
TL;DR
Leave /etc/network/interfaces as it is
Add to /etc/dhcpcd.conf
interface usb0
static ip_address=192.168.42.42/24
static routers=192.168.42.129
static domain_name_servers=192.168.42.129
Restart the Dhcpcd service
$ sudo systemctl restart dhcpcd