Sunday, 9 March 2014

Connect to the Raspberry PI from the HP Chromebook

Posted by Gary Newell  |  at  23:32 17 comments

Introduction

Today's tutorial will show you how to control your Raspberry PI from your HP Chromebook (and to be honest pretty much any other Chromebook).

What you will need

For this tutorial you will need a ).

You will also need a working internet connection on the Raspberry PI. (Read this guide which shows how to set up a wireless connection on the Raspberry PI).

Last but not least, you will need a Chromebook.

SSH


There is some preparatory work that needs to be done on the Raspberry PI to enable you to connect to it from the Chromebook.

On the Raspberry PI run raspi-config from a terminal window. If you don't normally run a graphical desktop you can run this straight from the terminal window. If you run from a graphical desktop open a terminal by clicking on the LXTerminal icon on the desktop.

Enter the following into the terminal window:

sudo raspi-config

A screen similar to the one above should appear.

The "SSH" menu item is now hidden in "Advanced Options" so scroll down to option 8 and press the return key on the keyboard.


You will now see the option for "SSH" as menu item A4. Select this option and press return.


When the "SSH" screen appears make sure that it is set to enabled.

A message will appear telling you that SSH is now enabled. Exit out of the raspi-config tool.

To be able to connect to the Raspberry PI from the Chromebook you will need to know its IP address. From within the terminal window type the following:

ifconfig


If you are connecting via ethernet cable the IP address will appear under eth0. If you are connecting via wireless the IP address appears under wlan0.

If you look at the image above you will see the IP address for my Raspberry PI shows as 192.168.1.108. If you reboot your Raspberry PI at any point that IP address may change so you cannot guarantee it is the same IP address every time.

I have a method to get around this which I will show you later.

SSH to the Raspberry PI from the Chromebook

You will need an SSH client on the Chromebook in order to connect to the Raspberry PI.

Open up the Chromebook store by clicking the applications launcher in the bottom left corner and then click on the "Store" icon.

 
When the store opens type "ssh" into the search box in the top left corner.

There are a number of tools available. I installed the one called "Secure Shell".

After installing the secure shell application open it by clicking on the application launcher. It usually appears straight away in the launcher but if it doesn't, try clicking on the tabs at the bottom until you find it.



To connect to the Raspberry PI enter the username you normally use to connect to the pi. (It is the box on the left, in the image above the user I use to connect is shown as pi).

In the second box enter the IP address for the Raspberry PI. Enter 22 as the port number.

Click on "Connect".

You may receive a message stating that the authenticity of the connection cannot be established. Enter "yes" to continue.

You will then be asked to enter the password for the account you wish to connect to.


Installing VNC Server on the Raspberry PI

You should now have a connection to the Raspberry PI from the Chromebook using SSH. This is great for doing command line stuff but if you want to do things in a graphical environment you will need a few more tools installed.

The first thing that needs to be installed is VNC Server.  Make sure you are connected using SSH.

Type the following to install the VNC Server:

sudo apt-get install tightvncserver

You will be asked if you are sure you want to continue. Press "Y" to continue.

The VNC Server will be installed.

At this point I would advise writing a script that you can run whenever you want to as typing the whole command that you need to run the VNC server is something you only really want to do once.

Type the following to open the nano editor:

sudo nano /etc/init.d/vncserver

Enter the following script into the editor:

#!/bin/sh -e
export USER="pi"
# parameters for tightvncserver
DISPLAY="1"
DEPTH="16"
GEOMETRY="1200x720"
NAME="VNCserver"
OPTIONS="-name ${NAME} -depth ${DEPTH} -geometry ${GEOMETRY} :${DISPLAY}"
. /lib/lsb/init-functions
case "$1" in
start)
log_action_begin_msg "Starting vncserver for user '${USER}' on localhost:{$DISPLAY}"
su ${USER} -c "/usr/bin/vncserver ${OPTIONS}"
;;
stop)
log_action_begin_msg "Stopping vncserver for user '${USER}' on localhost:{$DISPLAY}"
su ${USER} -c "/usr/bin/vncserver -kill :${DISPLAY}"
;;
restart)
$0 stop
$0 start
;;
esac
exit 0
Press CTRL and O to save the script and CTRL and X to exit nano.

I don't advise copying and pasting scripts into your computer without knowing what they are doing so I will do my best to explain.

The VNCServer takes a number of parameters. In the script above we use three of them. "DISPLAY" is used to determine which port number to use. VNCServer by default runs on port 5900. By setting display to 1 you are specifying that VNCServer will run on port 5901. The DEPTH variable specifies the colour depth. Finally the GEOMETRY option specifies the screen resolution to use.

By specifying these options early in the script it makes it easy to adjust them to suit your needs.

The OPTIONS line takes all of the parameters and appends them as input parameters for the VNCServer command.

There is now a case statement. The case statement will look at the argument provided when you run the script and will run the appropriate code dependent upon the argument you specified. For example if you type vncserver start the code after start) will run. If you run vncserver stop the code after stop) will run. I think you get the idea now.

When you specify the start option a message is sent to the terminal stating that the VNC server is starting. The user is then switched to the user account specified by the export USER command and the VNC Server is started with the options specified as that user.

The stop option kills the server. The restart option, stops and restarts the VNC Server.

In order to be able to run the script at all you need to make it executable. To do this type the following:

sudo chmod +x /etc/init.d/vncserver

To make sure you haven't made any mistakes enter the following:

sudo /etc/init.d/vncserver start

If any errors appear then the script hasn't been entered correctly. Make sure the script matches the script above and try again.

It is likely that as this is your first time running VNC server that you will be asked to enter a password for connecting to the PI. You will also be asked if you want to create a readonly password. It is up to you if you want to do this.

VNC to the Raspberry PI from the Chromebook

To VNC onto the Raspberry PI you will need to install a VNC client onto the Chromebook. To do this click the application launcher and open the store.




When the store loads enter "vnc" into the search box in the top left corner. I installed the "VNC Viewer for Google Chrome" application.

To run the VNC Viewer open the application launcher and click on VNC Viewer. If it doesn't appear straight away click on the tabs at the bottom until it appears.


Enter the IP address for the Raspberry PI into the address field and click "Connect". For example enter 192.168.1.101:5901. 


You will receive a warning about security as VNC isn't the most secure way to connect two devices together.

Click "Connect" to continue.

You will be asked to enter the password required to connect to the VNC Server.


You should now be able to see a visual representation of your Raspberry PI. To get a full screen view click on the second icon from the right (looks like 4 arrows).

Running the VNC Server at startup

The VNC server is currently running but as soon as you shut down the Raspberry PI that is it. The next time you start the Raspberry PI you won't be able to VNC to it.

There are two things you can do. The first is to SSH onto the Raspberry PI and run the startup script again. The second is to set the VNC Server to start when the Raspberry PI starts.

My preferred method is to use SSH. Simply connect via SSH to the Raspberry PI and then enter the following command when you want to VNC onto the Raspberry PI:

sudo /etc/init.d/vncserver start

I think this is the best way as it only leaves VNC open for connection when you actually plan to use it as opposed to leaving it open all the time.

If however you want to always be able to connect via VNC you can run the following command into the terminal just once and the VNC server will start when the Raspberry PI starts:

sudo update-rc.d vncserver defaults

Get your IP address sent to your email account

As I mentioned earlier one of the problems you will encounter is that your Raspberry PI's IP address can and will change when you restart it.

You can use trial and error to find the Raspberry PI if you wish by trying 192.168.1.101 and then 192.168.1.102, 192.168.1.103 and so on until you get connected.

The next bit is entirely optional but will help you if you need your PI's IP address.

First things first you will need a piece of software called SSMTP installed. Connect to the Raspberry PI via SSH and enter the following:

sudo apt-get install ssmtp

The conf file needs to be edited to be able to set up the outgoing email settings. Type the following:

sudo nano /etc/ssmtp/ssmtp.conf

Add the following lines to the end of the file:

root=postmaster 
mailhub=smtp.gmail.com:587
hostname=raspberrypiAuthUser=myemailaddressAuthPass=myemailpasswordUseSTARTTLS=YE
The mailhub line needs to be replaced with your outgoing smtp server. The bit that says myemailaddress needs to be replaced with your email address and myemailpassword needs to be replaced with the password from your email address.

Press CTRL + O and CTRL + X to save and exit the file.

Now you will need to edit the file /etc/ssmtp/revaliases. To do this type the following:

sudo nano /etc/ssmtp/revaliases

Add the following line somewhere within the file:

pi:pi@everydaylinuxuser.com:smtp.gmail.com:587

Replace the bit that says pi@everydaylinuxuser.com with a user and host name that you want emails to look like they come from. Replace the smtp.gmail.com:587 with the smtp details of your outgoing mail server.

Again save the file by pressing CTRL + O and CTRL + X.

Change the permissions on the ssmtp.conf file to enable emails to be sent:

sudo chmod 774 /etc/ssmtp/ssmtp.conf

Now create a file called sendmyIP.sh by typing the following:

sudo nano /etc/profile.d/sendmyip.sh

Enter the following into the editor:

ifconfig | mail -s "Your PI IP " emailaddresstosendmailto.com

Replace the emailaddresstosendmailto.com to your email address.

Press CTRL + O and then CTRL + X to save and exit the file.

Change the permissions to enable the script to run by typing the following:

sudo chmod +x /etc/profile.d/sendmyip.sh
 
Now every time your PI starts it should send you an email with the internal IP address for the PI.

Another option is to set the IP address for the Raspberry PI as static. Follow this guide to find out how to set a static IP address.

Summary

You should now be able to connect to the Raspberry PI from your Chromebook. Feel free to adjust the geometry settings but they seem to work well enough on the HP Chromebook 14.

These instructions should work from any computer or device but you will need to have an SSH client and VNC client installed.

Thankyou for reading.











17 comments:

  1. Really cool capabilities, being able to connect remotely to your Pi by several methods. However, e-mailing an IP address is really overly complicated and unnecessary. Even setting a static IP is a bit more complicated than it needs to be. Your Pi (and every other device on your home network) gets its IP address assignment from your home router/firewall. Just assign DHCP reservations for devices like your Pi and they'll always get the IP you expect. Further, if you want to change IP address assignment in the future, it's easier to go to your router and change reservations than making individual static changes on many devices.

    ReplyDelete
  2. I agree that emailing the ip address is overkill. Using the router's built-in DHCP to manage assignments is the way to go. However some people don't like messing with their routers, so I can understand the need for this kind of utility - but only if it is a one-time thing, I'd say. Figure out the IP, then make it static, or learn how to use your router's DHCP interface. I don't even like using my router's DHCP, though, so I disable it. Instead, I have my own DHCP server on the local subnet, which is fully under my control, and I bind each client (by MAC) to an IP of my choosing. And I use dnsmasq for local DNS. I recommend these!

    But speaking technically (about the article), I love the mention of SSMTP and the simple, clear code examples. I've never heard of it before, but I will definitely start using it...so thanks!

    ReplyDelete
    Replies
    1. I agree with both of these comments. Emailing the ip address is overkill. I generally log on to my router and I can see the IP address listed.

      I wanted to give a way of obtaining the IP address everyone can use. Getting it emailed is just one way and I listed it as optional.

      Delete
  3. whenever I try starting VNC, I get this message...

    vncserver: 24: exit: Illegal number: 0#!/bin/sh

    any ideas?

    ReplyDelete
    Replies
    1. Hi Steven,

      Can you send me a copy of the vncserver script in /etc/init.d/vncserver?. (Email is everydaylinuxuser at gmail dot com)

      Delete
  4. How do you find out what your password is, because I made a typo, and now it is wrong...

    ReplyDelete
  5. Hey,

    great walk through! Thank you!

    Just ant get it working... :-(

    Copied the script 100%. When trying to start (or stop or restart) get this message:

    Starting vncserver (via systemctl): vncserver.serviceFailed to start vncserver.service: Unit vncserver.service failed to load: No such file or directory.
    failed!

    Can you help?

    ReplyDelete
    Replies
    1. I was encountering the same (or it was similar, I think) error and it was because I wasn't formatting the line 12 all in one line- I had it start a new line with {$DISPLAY}" when it should have been on the previous line.

      Line 12 should read without any breaks as the following (as the guide describes).

      log_action_begin_msg "Starting vncserver for user '${USER}' on localhost:{$DISPLAY}"

      This also applies to line 16. I don't know if what I'm saying is correct but it seemed to fix my error message!

      Delete
    2. Hey sevarre,

      thank you for your hint. Unfortunately it didn't solve the problem for me though. I made sure all lines were without breaks and still encounter the same error...

      Delete
    3. I am encountering the exact same problem with the same failed message (No such file or directory). I followed the directions outlined above to verify formatting for lines 12 and 16, and they are correct. I really don't know what to do from here. Do I need to save the file someplace special when I do the ctrl+o?

      Delete
    4. I think the best thing I can do is provide the script as a downloadable file. Give me a day or so and I will make it available.

      Delete
  6. Thanks for the great guide, Garry! I was able to successfully complete all the steps with a little perseverance, but all succeeded. Thank you .

    ReplyDelete
  7. Excellent work! I was having the same issues as Tommy G. above. I am not sure if there was typing issue or not on lines 12 and 16, but removing that code allowed the script to work. Not sure where hang up was but imagine around this line:

    . /lib/lsb/init-functions ;and

    log_action_begin_msg "Starting vncserver for user '${USER}' on localhost:{$DISPLAY}"

    I compared your script with the VNC document page at raspberry.org and removed those two lines and the above issue quite and I was able to get the service started. Any insight?

    ReplyDelete
    Replies
    1. Had same problem here. Removed both . /lib and log_action lines and managed to start it

      Delete
  8. How do you find the hostname? I've tried using the ip address and rasberrypi but it just says the connection's refused. Is this a problem with the hostname or just a firewall?

    ReplyDelete
  9. Hello! for some odd reason i keep get this error message even after deleting lines 12 and 16 as well as this line: . /lib/lsb/init-functions ;

    bash: /user/bin/vncserver: No such file or directory


    this seemed to fix the issue on my first raspberry pi, but on my second one i keep getting this error message

    please help!

    kind regards,

    Lannon

    ReplyDelete
  10. Hello everyone,
    I'm totally stuck at first ssh attempt connection..
    Using Secure shell on an Acer Chromebook, i do get this error message :
    " Connecting to pi@192.xxx.0.xx... ( put x instead of numbers )
    Loading NaCl plugin... done.
    ssh_exchange_identification: Connection closed by remote host
    NaCl plugin exited with status code 255.
    (R)econnect, (C)hoose another connection, or E(x)it? "
    Following some advice on a forum I tried adding to the ssh arguments :
    " -o TCPKeepAlive=yes "
    But I still get the same error..
    If anyone could point me to the right direction I'd be thankful.
    Thx.

    ReplyDelete

Feel free to comment on any of the blog posts. Please try to be constructive.

Offensive messages will be removed as will blatant adverts for misleading products and sites.

Thanks for visiting my blog

    Popular Posts



back to top