Monday, December 24, 2012

Raspberry Pi newbies

***NOTE: I'm using Model B Rev 2***

I recently got a Raspberry Pi computer that I'm going to use as an embedded system to control a aircraft sensor pod I'm working on. I'm using one of the Linux operating systems, since I know Linux, and have never used RISC OS, the main alternative for the RasPi.

There's a lot of people buying these things for use as a personal computer. It will probably handle some light web browsing and word processing, but it's really not what these things are for. They're intended to be used in a learning environment, but obviously also have potential for use as an embedded computer system used to control devices, sensors, file servers, etc.

I see a lot of posts from people who really want to use these things, but they have trouble even getting out of the gate. I'm hoping that I can shed a little light on how to get these little things up and running. I'm also going to assume that the people making these posts are Windows users, since that's what their questions would indicate. (I'm not anti-windows. The only reason I have learned / am learning Linux, is because of how often I encounter it in different places, doing things that void manufacturer's warranties)

If you have a Windows or Linux computer, and you're at your home network, where you have access to your router, then all you need is a network cable and USB power cord. Whenever I get a new cellphone, it comes with a new charging cord. I always keep the old ones, and since they're pretty much all USB Mini-B, that's what I'm using to power my Pi.

Power up your Pi, and use an Ethernet cable to connect it to your router. Then you should be able to determine the IP address that your router has assigned to the Pi using its web-interface. (Consult your router's documentation on how to do this). Look for the device with the hostname "raspberrypi". The IP address for that device is what you're looking for.

The next step is to log in via SSH. The default username is pi and the default password is raspberry.  If you're using Windows, you'll have to download, install, and run  a free program called PuTTY. You can also use PuTTY if you're running Linux, but SSH is included in most distributions.

In PuTTY, make sure that the SSH radio button is selected, and enter your Raspberry Pi's IP address. We'll use 192.168.2.111 as an example. Now press enter.

If you're using a Linux terminal, do:
   ssh pi@192.168.2.111

Now you are logged into the Pi. But what the heck are you looking at? It's the command-line interface of your Pi; if you're a Linux person, you should know what this means, but if not, think of it as a terminal. If you're a Windows person, think of it as MS-DOS (a.k.a. dosprompt, command prompt), although most MS-DOS commands aren't going to work here.

But big deal, right? You wanted to have a pretty graphical user interface, or GUI, for your Pi, right? Well before we get into that, there are a couple of security tasks that we need to handle first. Most importantly, we need to change our password. Whether or not you've logged in from Linux or Windows, you're controlling the Pi with Linux, so we're going to be talking about Linux commands from now on.

Changing the username and password


To change your password, use the PASSWD command. do:
  sudo passwd

The Pi may ask you for your current password, which is raspberry. Enter that, and then you can specify a new password.

Good. That's changing one of the locks on the Pi's back door (the password), but not the other one (the username). Contrary to popular belief, running Linux does not make you impervious to hackers, viruses, or other bad-guys and their tools. So the second step is to change the user name, too.

The official method of doing this involves creating another user, logging out of your current user, logging into that new user, using that new user to change your user name, and then logging out of that user, logging back into the original user account with the new name, and then removing the user that you just created.

Sound like a load of bullshit to you? Yeah, me too. The method I prefer is temporarily enabling the root user account. (Root is kind of like the Administrator account in Windows). Be sure that you don't leave your root account enabled after you're done using it, otherwise there's no point in changing any username at all.

To enable the almighty root account, do:

   sudo passwd root

Now give it a password. Now log out of your Raspberry Pi, and then log back in using the method you used before, but use root as the username, and whatever password you just gave root. Now that you've done that, it's time to rename the main user account on the Raspberry Pi - pi. To do this, we'll use the usermod command. Let's say that we want to change the username pi to the username nozedive. To do this, do:

  sudo usermod -l nozedive pi

or, for a less specific example

  sudo usermod -l newusername oldusername

Now, immediately log out and log back in, but this time, use the new username that you just created. As soon as you do that, you need to disable the root account. Do:

  sudo passwd -dl root

Okay, now you've changed both the locks on your doors. No that doesn't mean that some one can't kick the door in, but at least now not everyone has keys to your house!

*NOTE* The above method of changing your user name does not rename your home directory. You can do that if you want, and it's probably a good idea for a multi-user environment, but since I know that I'm the only one who'd going to be logging into my Pi 95% of the time, I'm leaving my home directory as pi.

Creating a remote graphical user interface via a virtual network connection


Now, surely, a gui of sorts can be accomplished by tunneling X Window through SSH, but I want a solution that will work for both Windows and Linux users.

VNC connections are NOT encrypted, so do NOT use the same password for your VNC sessions that you use for user authentication (logging in). but first, you don't even have any VNC! Step one for this is going to be installing a VNC server on your Pi. Luckily for us, the Pi comes installed with a program called APT and a library of a crap-ton of different programs. So, assuming that you have internet access, do:

  sudo apt-get install x11vnc

This will install a VNC server on your machine. Now comes time to set a VNC password. Do:

  x11vnc -storepasswd

Enter a password that you DON'T use for anything else (like authentication) since it will be transmitted in plain-text.

Oh man, we're almost there! Two things have to happen in order for you to remotely connect to the Raspberry Pi's graphical user interface:

  1. You must start an X Window session
  2. You must run a VNC server
So you'd think that the next step would be to start an X Window session, and then a VNC server, right? Wrong! First, we're going to create a script that will automate this for us! We don't want the X Window session to start every time at boot-up, and we don't want to start the VNC server every time at boot-up, either. (Besides, maybe you won't need it everytime)

We only want that VNC password to be transmitted the minimum number of times, and we don't even want those doors to exist until we're ready to use them, so while X Window and VNC are not running, they're not even doors, they're brick walls.

Let's talk about the script, shall we? We're still in the command-line interface, so we're going to use a CLI text editor. I like nano. It's not the one the real nerds use, but it's the fastest and easiest for small files, so it's what I use.

Since we haven't changed directories, the pwd command should display something like this:

  /home/pi

That's where we'll start out everytime we SSH into our Pi, so this is where we want to create our script. Do:

   sudo nano startgui.sh

This is what your script should look like:

  #!/bin/bash
  #The above line tells Linux how to handle this file
  startx & x11vnc --geometry 1024x768 -rfbauth ~/.vnc/passwd
  #that line starts X Window and x11vnc with specific screen size and the password you set

Once you've typed all that in, press ctrl+o to save the file, and then ctrl-x to return to the command line.

Let's run through what will happen when you're logging in to your Pi before we actually do anything else.

Plugging the Pi into a router and into a power source will boot the Pi up, and request a DHCP lease and IP address from your router. You check your router's web-interface to determine the IP address of your Pi. You use PuTTY or SSH to securely connect to the Pi's command line interface. You run the graphical user interface script that we created to start an X Window session, and VNC server. You connect to the VNC server via a VNC viewer.

Okay so let's get that VNC viewer. For Windows users, I recommend downloading UltraVNC, but for Linux users, I recommend xtightvncviewer. (sudo apt-get install xtightvncviewer).

Download, install, and open your VNC viewer according to its documentation. Then, using ssh or PuTTY, log into your Pi. Now, do:

  sh startgui.sh

This will take over your command line interface, but that's okay, since once you connect via the gui, you can open many terminals on your desktop. You opened your VNC viewer, right? Connect to the IP address of your Pi, and if necesary, specify a port number (probably 5900 or 5901).

Bob's your uncle, you should now see the Raspberry Pi desktop.

If you're asking yourself "What's the point of needing a computer to use another computer, I can just stick with the other computer and not even need a Raspberry Pi." Then you're missing the point of the Pi altogether. If you're thinking "Hey, this will help me configure and later connect to that command and control system for the unmanned areal vehicle I'm building." then you're on the right track.

If you've got any questions about Windows, Linux, or the Raspberry Pi, please feel free to ask, and I'll get back to you, BUT FOR THE LOVE OF GOD GOOGLE IT FIRST! If I get a question about something that Google could have answered in less than a second's time, then replying will be put on the bottom of my day's to-do list. Usually, the last ten items on that list are deferred until the next day, sometimes indefinitely. If you Googled a topic, but want my personal opinion anyway, that's different, but you have to specify that, and give me your opinion first, or it's going at the end of my list.

In addition to Google, one thing that Linux got right is having the instruction manual built in. Have a question about nano? do:

  man nano

for an user's guide that you can exit by pressing q. Often times, the very end of the man page you're looking at will refer you to a text file in a specific directory, or it will direct you to the core utilities documents. For more on that, do:

  info coreutils

Finally, if you're looking for a program to do a certain task, your first stop should be Google, but you can also check APT. Let's say I want to install a media player, I could do:

    apt-cache search media player

That will generate a list of media player applications, programs, plugins, and utilities that can be installed.

l8r
»Tony

No comments:

Post a Comment