TranslateProject/sources/talk/5 Pointers To Supercharge Your Raspberry Pi Projects.md

12 KiB
Raw Blame History

[sailing] 5 Pointers To Supercharge Your Raspberry Pi Projects

From SSH to port forwarding, these advanced techniques will ease Raspberry Pi development.

Youre in command of the command line. Youre not a noob at setting up NOOBS. Slowly but surely, youre learning your way around the affordable, credit card-size computer for DIY tinkering, Raspberry Pi.

See also: The Quantified Fish: How My Aquarium Uses Raspberry Pi

However, theres a steep jump-off point between the basics and the intermediate stuff. When you move from “setting up your Pi” tutorials to stuff like “building a media server,” the pre-project requirements start to get a little dicey. Many intermediate Pi tutorials, including some of the ones here on ReadWrite, assume a few things about your Raspberry Pi setup.

Not every project will require all of the techniques suggested here, but knowing these procedures will make projects that do require them go much more smoothly later.

When youre ready to go beyond the basics, here are some things you can do to prepare your Raspberry Pi for any pre-requirements that a tutorial could throw at you.

1) Using SSH

SSH, which stands for Secure Shell, is a cryptographic network protocol that lets you securely transfer data between your computer and your Raspberry Pi. Projects might require it so you can control your Raspberry Pi from your computer's command line without hooking it up to a monitor or keyboard.

SSH now comes pre-installed in Raspberry Pi operating system Raspbian, so if you have installed the latest or close-to-latest version of NOOBS, you already have it.

To use SSH, first you need your Pis IP address. Boot your Pi to the command line and type:

sudo ifconfig

Three paragraphs will appear. Your IP address will show up in either the first or the third line, depending on whether your Raspberry Pi is hooked up to an ethernet cable or via a wifi adaptor. If it's ethernet, look in the first paragraph, which starts with "eth0." If it's wifi, look in the third paragraph, which starts with "wlan0."

Either way, you'll see the words “inet addr” followed by an IP—something like 192.168.2.2, a pretty common default IP address that well use for the duration of this article.

Now you have the address that'll allow you to access the Pi from your computer. If youre on a Mac, you already have built-in SSH. Launch the Terminal application and type:

ssh pi@192.168.2.2

Itll ask for your password. By default, this is always "raspberry." If youve changed it to something else, use that instead. Now, youre in!

If youre on a PC, theres an extra step.

Download and run PuTTY or another SSH client for Windows. Enter your IP address in the field, as shown in the above screenshot. Keep the default port at 22. Hit enter, and PuTTY will open a terminal window which will prompt you for your username and password. Fill those in, and begin working remotely on your Pi.

2) Remote Desktop Your Raspberry Pi

SSH is great for remote controlling your Pi from the command line. But what if youd like to work off of its Graphical User Interface (GUI)? Fortunately, this is already built in for Macs and PCs.

On your Raspberry Pis command line, (and yes, you can do this with SSH), type:

sudo apt-get install xrdp 

Xrdp is a daemon, a computer process that runs in the background that can support Microsoft Remote Desktop Client on both Mac and PC. Once it's installed on your Raspberry Pi, your Mac or PC will be able to access it through the client.

Search your computer for a program called Remote Desktop Client. Its not exactly hidden per se, but its not stored in your application folder.

Input the IP address when prompted. Next, an xrdp window will pop up, prompting you for your username and password.

If all goes well, youll see your Raspberry Pis desktop in a window on your computer screen.

3) Static IP For Your Pi On The Home Network

That Raspberry Pi IP we found earlier (in Step 1) may work for now, but theres no guarantee your router will keep assigning the same IP address to your Pi forever. That means the other computers on your network wont know which IP address the Pi lives at. So how do you assign it a static IP instead of having to use the “sudo ifconfig” command every few days, or worse, hours?

Predictably, we begin with "ifconfig." So type:

sudo ifconfig

Get a pen and paper ready, and record the following three addresses listed after eth0 (The comments after the hashes are my descriptions. They won't show up in the code.):

inet addr: 192.168.2.2 # Pi's Current IP Address

Bcast: 192.168.0.255 # The Broadcast IP Range

Mask: 255.255.255.0 # Subnet Mask Address

You still need two more pieces of information. So type the following line to get the Gateway and Destination addresses:

netstat -nr

Next, go to your routers configuration. (How you do this is dependent on which router you have, so check the manual.) Find out which addresses the Dynamic Host Configuration Protocol (DHCP) is giving out to your devices. These are off-limits addresses, since the protocol is already using them. So pick one that isnt being used—for example, 192.168.2.2. Your router may allow you to reserve an unused IP address and assign it to a local device like the Raspberry Pi. (If you have a Belkin router, this is probably the case.) If so, you can stop here.

If not, you need to let the Raspberry Pi know what its new IP address is. Well do this by editing a configuration file on the Pi in the nano text editor:

sudo nano /etc/network/interfaces

Change the line “iface eth0 inet dhcp” to “iface eth0 inet static” to inform it that its now static.

Then enter the following, but replace the X's with the addresses you copied down earlier! The top address is the one weve decided to assign after looking at what our DHCP wasnt already using.

address 192.168.2.2

netmask XXX.XXX.XXX.X

network XXX.XXX.X.X

broadcast XXX.XXX.X.X

gateway XXX.XXX.X.X

Reboot the Raspberry Pi with “sudo reboot” and run “ifconfig” one more time. Your new static settings should appear!

4) Forwarding Ports To Raspberry Pi

Some Pi projects may require you forward a specific Internet port to your Raspberry Pi. Ports are virtual pathways where information travels on the Internet. You sometimes need to forward a port in order to make a computer, like the Raspberry Pi, accessible to the Internet even if its behind a router. It's kind of like dialing an extension on a phone network.

Port forwarding can be used for projects like a Raspberry Pi web server, Voice Over IP on the Pi, or simply for peer-to-peer downloading. There are 65,000+ ports to choose from, so you can assign a different port for traffic for every Raspberry Pi project you do.

Some ports are default for certain things. Port 80 is default for HTML pages. Port 21 is default for FTP (File Transfer Protocol) transferring. Port 1194 is default for setting up a VPN server. So a lot of the time, you can just go with whichever port is set as the default.

The way to set up port forwarding is entirely dependent on your router, so its difficult to create a tutorial for port forwarding beyond what it actually does. Read the documentation that comes with your router in order to customize and define ports to forward.

Here are some port forwarding tutorials for major router manufacturers:

5) Static IP For Your Pi On The Internet

Once again, if your ISP already gives you a static IP address, you don't need to worry about this section.

Weve already set up a static internal IP address for the Raspberry Pi, which means computers inside your local network will always know where to find your Pi. But what about projects that require Raspberry Pi to be connected to the Internet? If you want to build a Web server with your Raspberry Pi, people outside your network need to know where your Pi lives, which means setting a public IP that always looks the same.

I say “looks the same” because were really setting up a pseudo-static IP. Your ISP will continue to change the address as usual. So every time somebody connects to your Pi, the script were about to write will say, “Looks like the Pi has moved! Let me redirect you to the new address."

We do this with a Dynamic DNS (DDNS), which maintains a name server that gets updated in real-time, and DDClient, a program that will correspond with DDNS directly from your Pi.

First sign up for a free dynamic host service like DNS Dynamic. Follow the instructions and create a new nameserver like Yourserver.dnsdynamic.com.

Now go to the command line on your Raspberry Pi and install DDClient with the following line:

sudo apt-get install ddclient

We need to edit the DDClient configuration with our DDNS's new name server:

sudo nano /etc/ddclient/ddclient.conf 

Every service will have slightly different configuration, but the DDNS website should tell you what you need to do to configure this file. A standard configuration for DNS Dynamic, for example, goes like this. Copy and paste it in.

Your ISP isnt going to just stop updating the Raspberry Pis IP address because of this file, however, so we need to put a daemon at the very top of this script to tell it to continually check whether or not the IP address has changed.

daemon=600
# check every 600 seconds

This one checks every 10 minutes, which is just good practice. If you set it up to check every second, your dynamic service may not appreciate it because you'll be literally spamming their servers. Press Control + X to save and exit this configuration file.

After this, type:

ddclient

See also: 12 Cool Projects For Your Raspberry Pi

Simply typing the name of the program will run it. Itll continue to run as long as your Raspberry Pi is turned on. If you reboot your Pi, write “ddclient” to start running it again.

Now your Raspberry Pi is tricked out and ready for more advanced tutorials. Have fun and learn lots!


via: http://readwrite.com/2014/04/09/raspberry-pi-projects-ssh-remote-desktop-static-ip-tutorial#feed=/hack&awesm=~oB1b7zvteUQGOV

译者:译者ID 校对:校对者ID

本文由 LCTT 原创翻译,Linux中国 荣誉推出