Network - How to setup with Raspberry

From Linux - Help
Jump to navigation Jump to search
raspberry

'A quick guide, how to setup your Raspberry Network.'

Setting a Static IP Address

You want to set the IP address of your Raspberry Pi so that it does not change.

To set up the IP address of your Raspberry Pi, wheter using a wired or wireless network, you need to edit the configuration file /etc/network/interfaces.

If you view your /etc/network/interfaces file using the following command:

cat /etc/network/interfaces

It should look something like this:

auto lo
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

This is telling you that Raspbian is aware of three network interfaces on your Raspberry Pi. each starting with the word iface.

lo    # Loopback. You can ignore this.
eth0  # A network using ethernet socket. 
wlan0 # A wireless network.

Your Raspberry Pi will have a different IP address for each network connection. In this example, you will just make the IP address for the ethernet interface static.

To edit this file, type the following command:

sudo nano /etc/network/interfaces

First, decide on an IP address to use. You need to pick up one that is both unused by any other machine on the network and within the allowed range of IP addresses for your home network. In this case I will use 192.168.0.30.

Modify the contents of the file, changing the word dhcp to static and adding the following lines:

address 192.168.0.30
netmask 255.255.255.0
gateway 192.168.0.1

With the file changed as shown here, the static IP address of 192.168.0.30 has been assigned to the eth0 interface.

auto lo
iface lo inet loopback
iface eth0 inet static
      address 192.168.0.30
      netmask 255.255.255.0
      gateway 192.168.0.1
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

For most network, the netmask setting should be set to 255.255.255.0 and the gateway sould be the IP address of your home router itself. This will be the same as the IP address you use to connect to the admin console of your router.

After you edit and save the file, run the following commands to clear out all the existing DGCP leases, and restart your PI so that changes will take effect.

sudo rm /var/lib/dhcp/*
sudo reboot

Setting the Network Name of a Raspberry Pi

Changing the name of your Pi is pretty straightforward. There are just two files that need to be changed.

First edit the file /etc/hostname, you can do this by opening a Terminal and typing the command:

sudo nano /etc/hostname

Replace "raspberrypi" with a name of your choice. This should remain one word, without any punctuation or unusual characters.

Second, open the file /etc/hosts in an editor using the command:

sudo nano /etc/hosts

The file will look something like this:

127.0.0.1       localhost
::1             localhost ip6-localhost ip6-loopback
fe00::0         ip6-localnet
ff00::0         ip6-mcastprefix
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters
127.0.1.1       raspberrypi

Change the text at the end that uses the old name ("raspberrypi") to the new name.

Restart the Pi, and you should find that the name has changed when you type the following command:

hostname 

Controlling the Pi with SSH

Before you can connect to your Raspberry Pi using SSH, you must enable SSH. Use the raspi-config application. Start this by entering the following command:

sudo raspi-config

Go to the interface tab and enable the SSH option.

Now all you need to do is to open a terminal (if using windows then use Putty) and type:

ssh pi@192.168.0.30 # or ssh pi@your_hostname

Setting a DNS server in your Raspberry

Adding a DNS server in your Pi is pretty straightforward. There are just one files that need to be configured.

Type in a terminal the following command:

sudo nano /etc/resolv.conf

It should look something like this:

# Generated by resolvconf
nameserver 192.168.0.1

You need to change the ip by the DNS server you want to use (eg. 208.67.222.222, 208.67.220.220, these are opendns server) or change it with your router IP address if your router already is configured to use a DNS server.

This is it, know you have a fully network configuration to work with your Pi in your home network.