Simple Wifi Configuration for Raspberry Pi /etc/network/interfaces

Sometimes, when I’m troubleshooting or initially setting up a Raspberry Pi,  I like to create a dead simple configuration to connect to my home wifi network. This skips using the wpa_supplicant and has all the information in the intitial networking file for simplicity.

From the command line on your Pi I do the following:

  1. cd /etc/network
  2. sudo cp interfaces interfaces-wifi

    This makes a copy of our existing interfaces file for a backup.

  3. sudo nano /etc/network/interfaces

    We open the file in a text editor to make our changes.

Update the code in this file so that it contains the code following this paragraph:
Be sure to insert the name of your network in place of ssid inside the quotes as the value for wpa-ssid and password in the appropriate place as the value for wpa-psk, within the code. You may already some of these lines in there for your ethernet (eth0) so you can keep it or replace it.

auto lo
iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
auto wlan0
iface wlan0 inet dhcp
wpa-ssid "ssid"
wpa-psk "password"

After your file looks like this, hit Ctrl-X, then y for yes and enter.
I usually reboot at this point with a command of:

sudo reboot

Note: If something happens and you can’t log onto your wifi network, don’t despair. Just hook up your monitor and keyboard and go back to this file, change it back or rename your backup file to interfaces and you are back where you started. To change back to your original interfaces file that we renamed in step #2, just type this on the command line.

sudo cp /etc/network/intefaces-wifi /etc/network/interfaces

When finished doing this, reboot again.

sudo reboot

Creating an Ad-hoc network for your Raspberry Pi

I have found this setup to be very useful for my Raspberry Pi.
Instead of connecting to an existing wireless network, you have the Pi create it’s own. That way you can login to the Pi even if a wi-fi network is not available. I’m using it for my Pi Powered robots outside.

Hopefully, this little snippet of code will be useful to others:

Here are the steps:
=====================

  1. From the command line (LX terminal or SSH) go to the following directory.
    cd /etc/network
  2. Copy the existing interfaces file as a backup.
    sudo cp interfaces interfaces-wifi
  3. Create a new file for our ad-hoc network.
    sudo nano interfaces-adhoc
  4. Once this file is open, copy the code snippet below so that it reads as below.
    auto lo
    iface lo inet loopback
    iface eth0 inet dhcp
    
    auto wlan0
    iface wlan0 inet static
    address 192.168.1.1
    netmask 255.255.255.0
    wireless-channel 1
    wireless-essid RPiwireless
    wireless-mode ad-hoc
    
    Once you've entered this do a Ctrl-X, yes and enter to save the file.
  5. Now we have a interfaces-wifi and interfaces-adhoc files in the /etc/network directory along with your original interfaces file. Now, we can switch between using a wireless connection and our ad-hoc network by simply copying the appropriate file over to the interfaces file.
  6. For example, if you want to use the ad-hoc network enter the following command >
    sudo cp /etc/network/interfaces-adhoc interfaces

    To switch back to a wifi network do the following:

    sudo cp /etc/network/interfaces-wifi interfaces
  7. You’ll need to install a package to allow your Pi to assign a device connecting to it an IP address.
    sudo apt-get install isc-dhcp-server

    It creates a config file that we will need to edit. You can access it using the code below.

    sudo nano /etc/dhcp/dhcpd.conf

    Once you have this open, edit it so that it only contains the code below.

    ddns-update-style interim;
    default-lease-time 600;
    max-lease-time 7200;
    authoritative;
    log-facility local7;
    subnet 192.168.1.0 netmask 255.255.255.0 {
     range 192.168.1.5 192.168.1.150;
    }
  8. Save this config file and reboot.
  9. When you boot up now under ad-hoc mode you will need to use your computer’s wifi to search and find the new network called PiWireless. Connect to it and then use SSH or VNC to connect to the IP address we assigned of 192.168.1.1Hope this helps.