Raspberry Pi Tutorial – Connect to WiFi or Create An Encrypted DHCP Enabled Ad-hoc Network as Fallback

In this post I describe how I have configured my Raspberry Pi (RPi) to first attempt to connect to WiFi and if that fails, create and use an ad-hoc network as fallback (in this way I can always reach the RPi via SSH). The blog post is based on the following “How To” from the Raspberry Pi forum: http://www.raspberrypi.org/phpBB3/viewtopic.php?t=19517&p=190855 – however, I have introduced a level of more detail and a couple of modifications in order to get faster boot time and support for multiple wireless networks (see my previous RPi blogt post suvery for details on which parts of that “How To” I think are good as well as which I think can be improved).


  • The WiFi adapter I have used for this tutorial is the Edimax EW-7811Un (more details here) which seems to be one of the more popular WiFi adapters for the RPi (at least based on the amount of forum posts where it appears).


This tutorial has been tested with success on:

  • 2012-09-18-wheezy-raspbian
  • 2012-10-28-wheezy-raspbian

What We Are Going To Do

To reach our goal, I will walk through the following 7 steps:

  1. Getting Wireless Interface Name and Hardware Address
  2. Connect to WiFi
  3. Install and Configure DHCP Server
  4. Update interfaces Config File
  5. RPi Network Conf Bootstrapper
  6. Prevent DHCP From Starting At Boot
  7. Reboot and Test

So basically, the main idea is that we use wpa_supplicant for maintaining a connection to WiFi, and we use an ad-hoc network as fallback if we cannot connect to WiFi on boot. Further, to make it easier to connect and communicate with the RPi via the ad-hoc network, we setup a DHCP server for that network as well.

 Step 1 – Getting Wireless Interface Name and Hardware Address
In the coming steps we are going to use the name and hardware address (MAC address) of your wireless device/interface. To see that you can use:

… or:


The name of my wireless interface is: wlan0

… and the pattern of its hardware address is: 00:aa:11:bb:22:cc

Step 2 – Connect to WiFi

First of all,  use wpa_cli or the “WiFi Config” GUI in lxde to setup any WiFi connections on the RPi.

The GUI is self-explanatory, but the command line approach can be a bit tricky if you don’t know where to start. Based on:

… the steps are:

Open the command line interface (cli) for wpa_supplicant:


Make it scan for networks:

> scan

List the results:

> scan_results

From the list of networks, you should be able to see the SSID of the network you are going to connect to. If that is the case, we are now going to add that network to wpa_supplicant. Make sure to note the number returned by the add_network command as that identifies the new network (if you forget, you can use list_networks to view the list of networks known to wpa_supplicant):

> add_network

Then we need to tell about the SSID and the WPA/WPA2 key of the network:

> set_network 0 ssid "mySSID"
> set_network 0 psk "myKey"

I have seen some guides saying that at this point, wpa_supplicant should automatically start connecting to the specified network, however, that is not what I have experienced. So, if wpa_supplicant does not automatically start to connect after pressing the commands above, use the following command to “force” it to connect:

> enable_network 0

Then, in order to persist the settings, we type:

> save_config

And finally, we quit the wpa_cli:

> quit

At this point, you should be connected to the desired WiFi.

If not, you might need to request an IP-address from the DHCP server on the WiFi network using:

sudo dhclient -1 wlan0

Step 3 – Install and Configure DHCP Server

Next, we are going to install the isc-dhcp-server DHCP server using apt:

sudo apt-get update
sudo apt-get install isc-dhcp-server
The installation itself should go smoothly, however, when the DHCP server is started automatically after the installation it will probably report a failure saying that it is “Not configured to listen on any interface” – which makes sense as we haven’t configured it yet. This error message is seen by taking a look at /var/log/syslog btw.
When the installation is done, we need to configure some defaults for the DHCP server for when it is started:
sudo nano /etc/default/isc-dhcp-server
The only change we need to do in this file is to add the name of our wireless interface to the INTERFACES setting, like:
# On what interfaces should the DHCP server (dhcpd) serve DHCP requests?
# Separate multiple interfaces with spaces, e.g. "eth0 eth1".
In this way, we specify that we want the DHCP server to serve DHCP requests on the interface: wlan0
Next up is the config file for the DHCP server:
sudo nano /etc/dhcp/dhcpd.conf
My config file looks like this – see the added comments for details:
DHCPDARGS=wlan0; #args for the dhcpd daemon -> limit DHCP to the wlan0 interface
default-lease-time 600;
max-lease-time 7200;

option subnet-mask;
option broadcast-address;
option domain-name "RPi-network";
option routers; #default gateway

subnet netmask {
    range; #IP range to offer

#static IP-assignment
host myLaptop {
    hardware ethernet 11:aa:22:bb:33:cc;
Step 4 – Update interfaces Config File

In this step we are going to configure the network interface configuration information:

sudo nano /etc/network/interfaces

My interfaces file looks like this (see the added comments for details):

# start interfaces upon start of the system
auto lo wlan0

# register loopback interface
iface lo inet loopback

# use dhcp and allow interface to be started when kernel detects a hotplug event
allow-hotplug eth0
iface eth0 inet dhcp

# use manual ip configuration for wlan0 interface and allow hotplug as well
allow-hotplug wlan0
iface wlan0 inet manual

Step 5 – RPi Network Conf Bootstrapper

In this step we are going to do a bit of bash scripting in order to the tell RPi to create an ad-hoc network if it cannot connect and obtain an IP-address from one of our already known WiFi networks.

We want this code to be executed when the RPi has booted and “is ready for use”, so we use rc.local as the container of our script:

sudo nano /etc/rc.local

The script I provide below requires that you use bash as the interpreter for the rc.local file – to do this, you need to change the very first line (the shebang) to:


The script I provide here (RPi Network Conf Bootstrapper) should be put at the end of the rc.local file – the script goes as follows:

# RPi Network Conf Bootstrapper

    echo "Creating ad-hoc network"
    ifconfig wlan0 down
    iwconfig wlan0 mode ad-hoc
    iwconfig wlan0 key aaaaa11111 #WEP key
    iwconfig wlan0 essid RPi      #SSID
    ifconfig wlan0 netmask up
    /usr/sbin/dhcpd wlan0
    echo "Ad-hoc network created"

echo "================================="
echo "RPi Network Conf Bootstrapper 0.1"
echo "================================="
echo "Scanning for known WiFi networks"
ssids=( 'MyWlan' 'MyOtherWlan' )
for ssid in "${ssids[@]}"
    if iwlist wlan0 scan | grep $ssid > /dev/null
        echo "First WiFi in range has SSID:" $ssid
        echo "Starting supplicant for WPA/WPA2"
        wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf > /dev/null 2>&1
        echo "Obtaining IP from DHCP"
        if dhclient -1 wlan0
            echo "Connected to WiFi"
            echo "DHCP server did not respond with an IP lease (DHCPOFFER)"
            wpa_cli terminate
        echo "Not in range, WiFi with SSID:" $ssid

if ! $connected; then

exit 0

Step 6 – Prevent DHCP From Starting At Boot

As we only want it to start if an ad-hoc network has been created, we need to tell the DHCP server not to start on boot:

sudo update-rc.d -f isc-dhcp-server remove

Step 7 – Reboot and Test

Now, everything should be setup and ready to work – so reboot the RPi and pay attention while it boots to make sure everything looks right.

sudo reboot

Finally, try to connect to the same network as the RPi and see if it responds to ping requests. Also, try to connect to its ad-hoc network and verify that the DHCP server is leasing you an IP.


If everything looked okay, and your RPi is either connected to your WiFi or emits and ad-hoc network, everything should be working as expected 🙂 Otherwise, if you are experiencing problems, walk through the tutorial again and make sure that all your config files matches what is written in the tutorial – if that do not solve your problem, feel free to send me an e-mail or ask further question at the Raspberry Pi forum.

112 Responses to “Raspberry Pi Tutorial – Connect to WiFi or Create An Encrypted DHCP Enabled Ad-hoc Network as Fallback”

  1. Chris Hansen January 5, 2013 at 10:41 pm #

    I’m getting a syntax error on ssids=( ‘MyWlan’ ‘MyOtherWlan’ ) Any clue what that could be about?

    • Lasse Christiansen January 5, 2013 at 10:46 pm #

      Have you remembered to change the script interpreter to bash? (That is, changing the very first line of the rc.local script to: “# /bin/bash”)

      • Chris Hansen January 5, 2013 at 11:08 pm #

        Just double checked, and I had a 2nd SD card laying around so I ran through the instructions again on a vanilla Wheezy image. Set for bash and still the same error on boot.
        Right after the echo for Scanning for known Wifi networks.
        /etc/rc.local: 32: /etc/rc.local: Syntax error: “C” unexpected

        • Chris Hansen January 5, 2013 at 11:13 pm #

          Correction: /etc/rc.local: 32: /etc/rc.local: Syntax error: “(” unexpected

          • Lasse Christiansen January 5, 2013 at 11:17 pm #

            Oh, my bad – I’ve just realized that I have included the wrong syntax of how to specify the interpreter. Instead of “# /bin/bash” try this: “#!/bin/bash” – I was missing the exclamation mark (this has been updated in the blog post as well). Try it, and let me know if that made any difference 🙂

        • Chris Hansen January 5, 2013 at 11:23 pm #

          I missed that twice >.< Works perfectly now.

          • Lasse Christiansen January 5, 2013 at 11:26 pm #

            Great! Thanks for stopping by and letting me know 🙂

  2. Lucas January 11, 2013 at 1:44 am #

    I am getting an error on boot up and down saying that there is something wrong with /etc/network/interfaces.
    I deleted everything in that file and pasted in your code. Is that what I was supposed to do?

    • Lucas January 11, 2013 at 1:52 am #

      Here is the error I get. Any help is appreciated.

      [….] Configuring network interfaces…/etc/network/interfaces:3: misplaced option ifup: couldn’t read interfaces file “/etc/network/interfaces”

      • Lasse Christiansen January 11, 2013 at 7:29 am #

        Hi Lucas, thanks for your comment.

        I will double check the correctness of my interfaces file when I get my RPi booted later today.

        However, I briefly searched for this issue, and it appears to me that the typical main root cause of this, is a syntax error in the interfaces file. So, first of all, are you absolutely, 100% sure, that the file you are using are identical to the one I show in this post?

        If so, you could try again with an empty interfaces file and instead of copy pasting, try writing the exact content in the file – I have sometimes seen that doing a copy paste from one platform to the other induces some unwanted side-effects.

        • Lasse Christiansen January 11, 2013 at 8:07 pm #

          Hi Lucas, I have now compared the provided interfaces file in this blog post with the one currently running on my RPi – and they are identical. I also replaced my interfaces file on the RPi with a copy paste of the one from my blog post, and the interfaces settings loads without any issue on boot. So ye, currently, my only suggestion is that you start verifying that the interfaces file in this post is 100% equal to the one you use on your RPi 🙂 Let me know how it turns out 🙂 Thanks.

  3. Owen January 17, 2013 at 10:22 pm #

    I am so happy that this works.

    First thing i did with my pi was make it an airplay receiver.

    Second thing was this!

    I now have a standalone device that i can plug into my Aux in my car or work and play music!! 😀

  4. Dan January 20, 2013 at 3:52 pm #

    Thanks for the tutorial! Needed this for a project where the pi is in my car and needs to pull the latest code on boot if possible. Thanks a million

  5. Michael King February 5, 2013 at 12:12 am #

    First of all thank you for these detailed instructions. I am having an issue where ad-hoc wifi network is no longer available when the device connected to the ad-hoc network disconnects. This seems like perhaps there is another setting I need to make to tell the ad-hoc network to persist when a device disconnects?

    When this happens, I have to reboot the Raspberry Pi which allows me to re-establish the ad-hoc connection. Note if I have the Raspberry Pi connected to an access point, the connection to the Raspberry Pi can always be re-established.

    So it is as if the networking stack has crashed when in ad-hoc mode and a device disconnects.

    Any advice here to help resolve this would be greatly appreciated.

  6. Matt February 5, 2013 at 1:51 am #

    Thanks for a great tutorial. Very useful. It’s worked fine for me up until the part when i try to connect other devices to my ad-hoc network. They just time out when try to lease an ip address. If I allow my android phone (for instance) to use it’s own ip settings, it finds an address (albeit outside the range supposed to be leased by the dchp), but i can’t connect to the default gateway.

    Any suggestions!? Many thanks!

  7. Lasse Christiansen February 6, 2013 at 8:54 am #

    Hi Matt and Michael, thanks for your comments – I will take a look at both during the weekend 🙂

  8. Adrian Cristian Pop February 8, 2013 at 2:38 pm #

    Thanks for the effort put into making this tutorial. I’ve done a little bit more and added a router script with iptables rules in case it makes a hotspot.

    • Lasse Christiansen February 8, 2013 at 2:42 pm #

      Hi Adrian, that sounds great! Just out of interest, could you please share a link to a blog post describing your script (if a such exist)? 🙂

  9. Lasse Christiansen February 10, 2013 at 10:53 pm #

    Hi Matt and Michael, I have unfortunately been fully occupied the entire weekend, but if I get some more spare time in the next week, both of your “issues” are on my list and I will look into it. 🙂

  10. Warren February 14, 2013 at 5:43 pm #

    I followed the tutorial but and I was observing erratic behavior when the rc.local script creates the ad-hoc network and executes the ‘iwconfig wlan0 mode ad-hoc’ command. I was getting:
    Error for wireless request “Set Mode” (8B06):
    SET failed on device wlan0 ; Device or resource busy.

    The strange thing was that this would occur only once every 2 or 3 times upon full (power-unplugged) reboot. I was using a Tenda Wi-Fi dongle plugged into a powered usb hub, but found that using a BT-Micro3H2X (combination Bluetooth WiFi) dongle plugged directly into the RPi, the problem has not reappeared.

    Excellent tutorial; many thanks!

  11. Lasse Christiansen February 17, 2013 at 11:31 am #

    @Michael – I just tried connecting to my RPi, then disconnect and re-connect again – thus, I cannot reproduce the exact same problem. Which type of WiFi device are you using?

    • Jacob Rawls May 5, 2017 at 10:51 pm #

      Hi Lasse!! This has been a great tutorial so far, very successful other than having the same problem as Michael in that the Ad-Hoc network disappears after my pic disconnects from it, requiring a reboot. I am using a rtl cu8818 adapter and have checked over the tutorial, having only changed the key and ssid in the rc.local file.
      Also, my ubuntu pc will not connect to the ad-hoc network. It will see it, it will remember it, but it won’t connect. How can I check for errors in the process to identify the problem?

  12. Lasse Christiansen February 17, 2013 at 11:36 am #

    @Matt – have you tried connecting using a PC? As far as I know, Android devices are still not able to connect to Ad Hoc networks out of the box.

    • Matt February 25, 2013 at 7:55 pm #

      Hi Lasse, thanks for your reply. Yes, I have tried it from a PC also, but I get a similar problem. The PC says that it is connected, but it is not leased an IP address, and does not receive any packets from the RPi. I’ve followed your tutorial exactly….very odd!

      • Lasse Christiansen February 25, 2013 at 9:10 pm #

        Hi Matt, thanks for your reply 🙂 I have a couple of questions that might help narrow the issue down – have you checked that the DHCP server is actually running? Is the DHCP server complaining about an invalid config file when started? Which WiFi device are you using? Have you tried starting the “RPi Network Conf Bootstrapper” from the terminal and checked that everything went well (that no errors were returned?)

  13. Hans March 3, 2013 at 12:19 pm #

    Hi Lasse, first i want to say is THANK YOU :). I have RPi (rasbian wheezy, but 256MB version) and same wifi usb adaptor Edimax EW-7811Un. I would like create adhoc “server” on windows 7 ( in this post – http://raspberrypi.org/phpBB3/viewtopic.php?f=63&t=34895 ) but i was unsuccessful. So i found your really great tutorial and do this way ( “server” on raspberry ) and this works. But when i start (boot) RPi, it stay while on “Obtaining IP from DHCP” then write on screen ” DHCP server did not respond with an IP lease (DHCPOFFER) ” and then “Failed to connect to wpa_supplicant – wpa_ctrl_open: No such file or directory ” and finaly Creating ad-hoc network. This problem is now OK for me, because i need only Ad-Hoc network ( for internet (for update) i use cable). Is there any way to choose what i want. I think in script will be something like this – echo “Choose: For wifi write “w” and for AdHoc write “a” ” then user write W or A for option that he want. But that is just detail. More main thing i would like to change is transfer speed, edimax can work in 802.11 N mode, but when i wrote ” iwconfig wlan0 ” it show me only 54mb/s (so it is 802.11 G mode?). Is there any way to change transfer speed, because i stream video from USB camera with MJPG-STREAMER (by BobTech) and there is 3 seconds delay between reality and video, so i think that higher transfer speed is better.

    THANK YOU AGAIN for this tutorial

    • Lasse Christiansen March 3, 2013 at 10:10 pm #

      Hi Hans, thanks for your response – I’m glad the post have helped several RPi users out there 🙂 Regarding your questions – I think a simple way of solving the “Failed to connect to wpa_supplicant – wpa_ctrl_open: No such file or directory” error is to simply call “createAdHocNetwork” directly in the bootstrapper and skip the bootstrapper’s attempt to connect to an existing wireless network (which I understand is okay, since your are using cable for Internet access). In regards to the transfer rate of the network connection, I have seen a couple of blog posts mentioning that WEP is not supported on the N standard, meaning that if WEP is enabled, the device will automatically fall back to the g standard. So, you could try editing the script such that it do not set a WEP key, then connect and see what speeds are reported? Further, I have seen that it should be possible to “force” the device to use a certain standard by using the “rate” option for iwconfig – so that would also be an option to look further into 🙂

      • Hans March 5, 2013 at 12:05 pm #

        Thank you for fast response. I change rc.local, so i “commented” (write ” # ” behind every text from echo bootstraper to end and i ” call ” function createAdHocNetwork, where i comment line about password. Re-boot and network is created but “iwconfig wlan0 ” show me same Bit Rate: 54 Mb/s and show in first line IEEE 802.11 bg. I look at this in Thursday, but if you have some information about this ” switch to N mode ” i will be glad.

        Wifi connection was good, before i re-write interfaces and wpa_supplicat. In interfaces for wlan0 interfaces was before line “wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf ” but this is maybe for this tutorial.
        As i wrote this is not realy important. More important is N mode.

        In previous question (about WiFi connection or Ad-Hoc) i thought it is possible in script something like this. Before creating any connection, that script “Ask User What He Want” – like “echo “Are you want create AdHoc (A) or Wifi (W) network”” .. Script after this wait for what user type – A or W – and next scrit do function createAdHocNetwork if user type A or connect to existing wifi network if user write W. But this is only detail, which i would like to solve in future. I ask you if script (in rc.local) can “Read what user write with keyboard to command line”.

        Realy thank you for your time and answers.

        • Lasse Christiansen March 10, 2013 at 12:16 pm #

          Hi Hans, I have been away for a couple of days, so maybe you could give me an update on how you progress – are things starting to work? 🙂 Regarding the N mode – I do not have that much time for trying to solve it since I’m doing my Master thesis these days, but hopefully, some day, I will have more time and be able to look into it 🙂

          • Hans March 18, 2013 at 5:39 pm #

            Hi after a long time. I was away for few days too. I was and still i am ill, so i am not progressed as i would like. As i wrote i have working WiFi network, after boot RPi start “AP” i can connect via SSH and start script for streaming video from USB webcam.
            I write to Edimax tech support, but their answer did not help me.
            Their answer:
            ” Dear Sir, You will need to check in EW-7811Un driver properties if there’s possibility to change its band.
            Best Regards,”

            But i have RPi very short time, so i dont know where i find driver for editing. Did you know where is placed?

            I read that Ad-Hoc network is not standardized (IEEE standard) in N mode. If NIC can create this network, it is guaranteed only B/G mode.
            N mode is only additional option that manufacturer can (not must) add to device settings.

            Although there is 3s lag, WiFi connection and video streaming works. So I will probably solve another problem, which is sending text information over wifi. If you have some experiencies with text stream i will glad for some information about this 🙂

  14. james March 12, 2013 at 2:56 am #

    Thank you so much for these instructions! I was trying to figure out a way to set up an ad-hoc connection with static ip’s and it just was not working. I was worried about opening the whole dhcp can of worms since I’m still learning linux. Your method is great because it only enables dhcp if it’s unable to connect to a wireless network, brilliant!

    I’m making a little wifi boombox and I was stumped on how to make the setup process easier, if possible I wanted to have it configurable via smartphone instead of using buttons and the lcd. This gets me one step further!

    I still need to work out a way to make the wpa_supplicant part easier for people, I was thinking I could maybe write a little program that a user can run after ssh’ing into it that just asks “Enter SSID, Enter Encryption type 0=Open 1=WEP 2=WPA, then if applicable Enter Passkey”. If you’re interested in helping me with that feel free to email me. Once my project is done I’d like to post an all in one tutorial from getting MPD setup to stuff like this.

    Thanks again!

    • james March 12, 2013 at 4:13 am #

      After initial success I hit a few snags when trying to customize this a little. I didn’t realize at first that you need to specify your existing wifi networks in the rc.local file.

      I wonder if there is a different way to approach this so that ssid’s don’t have to be hard coded in? Issue some query to determine if the Pi is connected to an SSID, if not sleep 5 check again, do x number of times then if still not connected start ad-hoc mode? I realize this is out of the scope of your instructions, I’m just trying to figure out a way to limit users having to edit files as much as possible avoid hardcoding if possible.

      Lastly, in your script you have a bunch of feedback being echoed to show the status of the script, however I see none of it. I’m connected via serial so I see all of the boot process. The output of the boot seems to hang for awhile after “bcm2835 ALSA chip created!” which is where your script is running I assume, then I get the login prompt, no echoes from the script. Does something need to point at 2>&1 maybe? I assume you put the debug info in for a reason, just curious how you were viewing it?

      Again I’m very greatful for this tutorial, I’m just trying to adapt it a little to my needs and maybe in the process it can help improve yours as well.


      • james March 24, 2013 at 11:21 pm #

        Just FYI, here is my modified version without the hardcoded ssid’s

        # rc.local
        # This script is executed at the end of each multiuser runlevel.
        # Make sure that the script will “exit 0” on success or any other
        # value on error.
        # In order to enable or disable this script just change the execution
        # bits.
        # By default this script does nothing.

        # Print the IP address
        _IP=$(hostname -I) || true
        if [ “$_IP” ]; then
        printf “My IP address is %s\n” “$_IP”

        # RPi Network Conf Bootstrapper

        echo “Creating ad-hoc network”
        ifconfig wlan0 down
        iwconfig wlan0 mode ad-hoc
        iwconfig wlan0 key aaaaa11111 #WEP key you can change this but keep it 10 digits
        iwconfig wlan0 essid Boombox_Ad-Hoc #SSID set this to whatever you want
        ifconfig wlan0 netmask up
        /usr/sbin/dhcpd wlan0
        echo “Ad-hoc network created”
        echo “The server ip is”

        echo “=======================================================”
        echo “RPi Network Conf Bootstrapper 0.2 – JPH Photography mod”
        echo “=======================================================”
        echo “Scanning for known WiFi networks”
        for i in 1 # Initially was for i in 1 2 3 but found it took too long to failover to adhoc
        if connected=false
        echo “Starting supplicant for WPA/WPA2”
        wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf > /dev/null 2>&1
        echo “Obtaining IP from DHCP”
        if dhclient -1 wlan0
        echo “Connected to WiFi”
        iwconfig 2> /dev/null | grep ESSID > /etc/wpa_supplicant/my_current_ssid.txt
        cat /etc/wpa_supplicant/my_current_ssid.txt
        echo “My IP address is:”
        hostname -I
        echo “DHCP server did not respond with an IP lease (DHCPOFFER)”
        wpa_cli terminate
        echo “No ssid’s listed in your wpa_supplicant.conf file were in range”

        if ! $connected; then

        exit 0

        • Jesse September 24, 2013 at 3:10 am #

          What is /etc/wpa_supplicant/my_current_ssid.txt?
          Could you tell me how to make?

  15. Nakej Hrabe April 6, 2013 at 3:20 pm #

    Thanks fot amazing tutorial! I’m stuck on DHCP server – connecting to adhoc wifi is OK, but never got IP address

  16. Nakej Hrabe April 7, 2013 at 9:18 am #

    Iˇve followed your guide on my xbian and then on vanilla Raspbian “wheezy” installation with the same result: Devices can connect to ad-hoc RPi network, but any never receives IP address. Devices are able to see each other, but not Raspberry PI.

  17. Elmar April 9, 2013 at 2:13 pm #

    Hi Lasse,

    thanks for the great tutorial!!
    For me everything works except the DHCP server. I can connect to the ad-hoc WiFi, but do not get an IP address.
    (I did not use your bootstrapped, but did the same steps manually.)

    I hope you can help…!

  18. Lasse Christiansen April 10, 2013 at 2:40 pm #

    Hi Nakej and Elmar,

    Thanks for your comments – I’m glad you found the tutorial useful.

    In regards to your issues with the DHCP server not leasing an IP adress I have some follow up questions:

    – Do you get any error messages?

    – Have you tried running the logic in the “createAdHocNetwork()” function in a shell and checked that it succeeds without any error messages?

    – Have you tried using static IP assignments on any of the connecting devices (in “/etc/dhcp/dhcpd.conf”)?

  19. Lasse Christiansen April 13, 2013 at 5:02 pm #

    Hi Aikku,

    Thanks for your comments – I’m glad you could use the blog post to get started 🙂

    I’m currently in the process of writing my master thesis, so I don’t have that much time for other activities – but when I have finished my thesis I am planning to get back on this, and, based on the provided comments, write an update to the post above.

    I’m glad you solved it out and everything is working.

  20. aikku April 19, 2013 at 1:25 pm #

    At the end of the day from trial and error, I found that I am still having problems despite my earlier posts. In order to prevent giving misleading info to others (by me), could you please REMOVE my last two posts above?
    Sorry for the inconvenience – and good luck with your thesis.

    • Lasse Christiansen April 19, 2013 at 1:36 pm #

      Hi Aikku,

      Thanks for you comment – I’m glad you informed me about that 🙂 I have now removed both of your comments.

      As I mentioned in an earlier comment, I am planning to revisit this topic at some point in time after I have finished my thesis. So hopefully, we will manage to get your setup working properly 🙂

  21. Richard April 21, 2013 at 2:10 pm #

    I am using my raspi as an access point with AirPlay in my car. How do I stop my iPhone from thinking the ap has Internet? I.e. iPhone connected to ap but still access 3/4G?

    • joe June 11, 2013 at 3:14 am #

      probably iOS is configured to set the default route to the wifi connection if it is associated with a base station. you’d probably have to jailbreak your iphone so that you can log into it and fix it so all traffic except the traffic destined for the raspberry pi’s access point goes out the cellular modem…

  22. Jose April 22, 2013 at 3:24 pm #

    Thanks for the tutorial! Very smooth process.

    I’m just having a few issues with the bootstrapper. On boot and load it gives me a syntax error:

    syntax error near unexpected token ‘done’
    [FAIL] startpar: service(s) returned failure: rc.local … failed!

    And I also noticed this:

    Configuring network interfaces…ioctl[SIOCSIWAP]: Operation not permitted
    ioctl[SIOCSIWENCODEEXT]: Invalid argument
    ioctl[SIOCSIWENCODEEXT]: Invalid argument
    ifup: interface eth0 already configured

    I’ll go back and check again to see if I missed anything.
    Thanks again.

    • Jose April 22, 2013 at 5:35 pm #

      After going back and checking the code, I found out that copying and pasting the bootstrapper wasn’t a very good idea. If anyone has any issues with syntax errors after following this tutorial you either copy/pasted the code or you wrote it wrong. In the end, I also was able to solve my othe issues with the network interface conf file. I went back and deleted everything and wrote it again from scratch exactly how you had it written in the tutorial. I guess, I’m just stubborn and wanted to start picking at the code and only adding the things I want (or at least thought I needed)

      Thank you very much for the tutorial! I finally got the adhoc network to be created and broadcast. I just need to test the connection to make sure it’s working properly.

  23. Mike May 5, 2013 at 2:16 am #

    Hmm, my raspberry pi isn’t working and it doesn’t look like rc.local is running at boot. I don’t see any of rc.local’s echo statements in the terminal, syslog, or user.log. Is there somewhere else I should be looking? And the raspberry pi is not connecting to my home network or creating an ad-hoc network.

    Any advice?

    • james July 3, 2013 at 6:53 am #

      @ Mike (May 5 2013) Just FYI you only see those echoes if you have a display connected directly to the Pi, they do not show up over serial connections. If you’ve deleted the original rc.local file and created one from scratch I think you’ll have to make it executable by doing the following.

      sudo chmod +x /etc/rc.local

      As for not connecting to your home network you need this working first. One thing to try is using wpa_passphrase which I’ve found much easier than editing the wpa_supplicant.conf file manually.

      Even though your wpa_supplicant.conf file isn’t working right now it’s probably a good idea to backit up first.

      sudo cp /etc/wpa_supplicant/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.confBAK
      sudo wpa_passphrase ssid password > /home/pi/wpatemp
      sudo cp /home/pi/wpatemp /etc/wpa_supplicant/wpa_supplicant.conf

      Hope this helps. I’ve learned quite a bit over the last few year and figured I’d try to help a few people since others were nice enough to help me 🙂

  24. Bert Popma May 17, 2013 at 10:50 am #

    Hi Lasse,

    Thanks for the great tutorial, got the Pi finally working in WPA-mode. Now I have a challenge: On our campinggrouns internet-access is provided by means of a username and password you have to fill-in in your browser, a sort of public AP. No encryption is used (shame) but I was wondering if it could be possible to add the login to a file somewhere so the Pi connects automatically.

    Please share you’re thougts with me…

    Regards, Bert

    • Lasse Christiansen May 18, 2013 at 10:39 am #

      Hi Bert, hmm, maybe you could try creating a simple bash script which uses wget to post the login credentials?

      First, you need to figure out the pattern/syntax of the url that is requested from the server when you press login through your browser. There are different ways to do that, e.g., using Chrome’s developer console (ctrl + shift +j, then select “Network”) or using a tool like Fiddler. I think the easier way will be using Chrome’s developer console 🙂 Anyway, lets assume you use Chrome for this – then while at the login page, go to the “Network” tab in the developer console, then fill in the login form and submit it as you normally do – then, after you have pressed submit you should be able to see that Chrome has logged the network traffic / you request, and the url/path should now be visible in the developer console (along with the needed form data).

      With the url in hand, I suggest you create a bash script that uses wget to post the form after you have connected to the camping grounds network. You can use advise on how to configure wget for that purpose, here: http://stackoverflow.com/a/1432161/700926

      And when the script is working, you could call it from line 30 of the rc.local script I have provided in the blog post. At that line, we know we got an IP from the DHCP server, so from that point, the wget trick should be able to work.

      Give it a try and let me know how it goes 🙂

  25. Myles July 2, 2013 at 4:57 pm #

    I had a question, how do you turn off the ad-hoc network, and when I turn it back on do I have to repeat those steps?

  26. james July 3, 2013 at 6:58 am #

    Hi Lasse, I posted my version of the script in these comments a few months ago and just thought I’d come back and follow up. I used my modified version in my pi boombox project (now at pi-fidelity.blogspot.ca) and I credited you in the start of the script.

    Recently I’ve been rewriting it again so that it first checks to see if any wlan0 devices even exist first, if not then it tries eth0. I’ve got it calling custom scripts for my project but if you want I could strip those out and send it to you if you’d like. You’ve got my email address from this comment.


  27. Rnhmjoj August 17, 2013 at 4:44 pm #

    Thanks for the guide: it’s exactly what I was looking for.
    The configuration is working but I unfortunately have 2 problems: /etc/rc.local is not executed at boot time (I checked the permissions, shebang, syntax errors: all ok) and when a password is set (with iwconfig wlan0 key s:mypassword) the dhcp server is not working (I don’t receive an ip address).

    • Rnhmjoj September 9, 2013 at 11:13 am #

      I solved the first issue moving rc.local to /etc/init.d/rc.local. Now the script is starting correctly at boot but I still can’t have dhcp and wep at the same time.

  28. alex September 5, 2013 at 1:23 am #

    I have 2 Rpi’s with Edimax USB wifi dongles: I’m trying to get the one that boots first to create the ad-hoc network and the second one to connect to that network.

    The first one up indeed does create the ad-hoc network, but when the second one comes up it hangs on the “dhclient -1 wlan0” statement, so it never connects. This hang happens even if I try it from the terminal command line.

    Any ideas?

    • Lasse Christiansen September 15, 2013 at 4:13 pm #

      alex, thanks for your comment. It sounds like the DHCP server on the “host” RPi is the problem. Have you checked the DHCP server logs on the “host” RPi? (/var/log/syslog) – alternatively, you could do: “tail -f /var/log/syslog” in another session and check what is going on when you are working with the DHCP server. Let me know how it turns out 🙂

  29. Aberson September 15, 2013 at 2:11 am #

    Lasse, Thank you so much for this setup!!! i looked high and low and couldn’t find something to do just this… I am using this to configure the wifi on my Rpi but needed a way to wirelessly connect if there was no successful wifi connection.

    My only problem is i am unable to ssh into the Rpi through the ad-hoc network. I have an ip address of and used all your config files copy and pasted.

    how do i ssh into the Rpi?

    Thank you

    • Lasse Christiansen September 15, 2013 at 4:07 pm #

      Aberson, thanks for your feedback 🙂 You write: “I have an ip address of and used all your config files copy and pasted.” – If you did so the IP address of the RPi is when the Ad Hoc network is up and running – so try ssh to that IP. Let me know how it turns out 🙂

  30. Jesse September 24, 2013 at 1:43 am #

    Thanks for this tutorial Lasse! Is it possible to scan and connect to a network after ad-hoc has been made… while connected to ad-hoc via ssh? 🙂
    If not, how would I add a new network and passphrase to scan for on the next reboot?
    Thanks again!

    • Per October 6, 2013 at 9:31 pm #

      I’m interested in this aswell. Ideally I’d like to ad-hoc to my RPi, while connected to it, have it look for available networks. Then it should let me input credentials for the selected network, reboot and connect to the network I selected. Would be amazing if it’d work.

  31. Dan P October 12, 2013 at 1:06 am #


    I altered your excellent script so that you do not need to hard code in your wifi ssids, but it will simply look at your existing set up in wpa_supplicant.conf and find the ssids in there. It assume that the format is ssid=”SSID here” including the quotes.

    Replace line 18 with
    ssids=($(grep ssid /etc/wpa_supplicant/wpa_supplicant.conf | sed -r ‘s/^.{7}//’ | sed -r ‘s/.{1}$//’)) #assumes that it begins ssid=” and ends ”


    • Lasse Christiansen October 12, 2013 at 2:49 pm #

      Hi Dan,

      Thank you very much for that input 🙂 The less hard coded values the better 😉 I will definitely use that in the bootstrapper next time. Maybe I should add the project to github – simply to make it easier to track and manage suggested changes / updates to the script – I will consider that.

      Anyway, thanks again for your input.

    • Paul November 25, 2013 at 1:45 am #

      Dan, that doesn’t workif you have a space in the SSID in the spa_supplicant.conf – i.e., say you have
      ssid=”My Home WiFi”
      what you get is an entry in the array for ‘My’, one for ‘Home’ and one for ‘WiFi’. It looks like when the results is assigned to the ‘ssid=’ it breaks it apart by spaces. If in the cli I say
      sudo grep ssid /etc/wpa_supplicant/wpa_supplicant.conf | sed -r ‘s/^.{7}//’ | sed -r ‘s/.{1}$//’
      Then ‘My Home WiFi’ is returned, but the array is messed up.

      Any thoughts on how to handle it (being a noob to bash, I’ve tried all sorts of things and haven’t been successful.

  32. tony October 18, 2013 at 11:52 pm #

    Great article but I’m having trouble. In the rc.local Bootstrapper line 10 points to a file at /usr/sbin/dhcpd wlan0 . What is this file as it doesn’t exist on mi Pi.
    Many thanks

    • Lasse Christiansen October 18, 2013 at 11:59 pm #

      Hi Tony,

      Thanks for your comment 🙂 dhcpd is the DHCP daemon on your Linux box. Try typing: $ which dhcpd to get the path of where it is located (I guess it might have another location on the Linux distribution you are running) 🙂

      • Tony October 20, 2013 at 10:22 pm #

        Lasse, thank you for replying. After reinstalling a few files I now have a /usr/sbin/dhcpd.
        If I sudo /etc/rc.local I get a list of error messages as follows:
        /etc/dhcp/dhcpd.conf line 5: expecting number.
        /etc/dhcp/dhcpd.conf line 8: ? (194): expecting IP address or hostname
        option subnet-mask

        This carries on for ever line in my dhcpd.conf file. I see no errors as it is a copy of your file above.

        One other error I have is from the Bootstrapper file, it doesn’t like the brackets () at line 18 ssids=( ‘MyWlan’ ‘MyOtherWlan’ ).

        I hope you can make sense of this?


        • Lasse Christiansen October 21, 2013 at 9:33 pm #

          Hi Tony,

          Regarding the dhcpd.conf errors – to me it sounds like errors with the “copy-paste” you have done from my blog to the config file. Could you please check that everything is exactly as described in the blog post? Further, regarding line 18 in the bootstrapper script – in order for that to work you need to change the shebang in rc.local (as covered in the paragraph before the bootstrapper snippet) – that is, you need to change the very first line of rc.local to: #!/bin/bash. Let me know how it goes 🙂

  33. Stefan November 11, 2013 at 4:36 pm #

    Hallo Guys,

    thank you Lasse, that is awesome!

    Sadly I’ve got problems because of my SSID.

    Its named “FRITZ!Box Fon WLAN 7390 2GHz“ „otherWlan“

    when I add it like this:
    ssids=( “FRITZ!Box Fon WLAN 7390 2GHz“ „otherWlan“)

    the Output looks like this, if I start the script manualy in ssh console:

    Grep: Fon: No such file or directory
    Grep: WLAN: No such file or directory
    Grep: 7390: No such file or directory
    Grep: 2GHz: No such file or directory

    so apparently there seems to be a problem with the exclamation mark or the spaces.

    I searched for and tried quite a few grep and regex parameters but couldn’t fix it.

    Could some body help me out?

    Thx a lot,


    • Stefan November 21, 2013 at 1:41 pm #

      Does anybody got a clue?

    • Lasse Christiansen November 24, 2013 at 7:31 pm #

      The problem is that the SSID is not surrounded within regular double quotes, like this: "My SSID". Double quotes are needed to allow spaces to appear in the SSID name.

      To illustrate the problem, you can try execute the following bash script:

      ssids=( “FRITZ!Box Fon WLAN 7390 2GHz“ „otherWlan“ )
      for ssid in "${ssids[@]}"
      	echo $ssid
      # gives:
      # “FRITZ!Box
      # Fon
      # WLAN
      # 7390
      # 2GHz“
      # „otherWlan“
      ssids=( "“FRITZ!Box Fon WLAN 7390 2GHz“ „otherWlan“" )
      for ssid in "${ssids[@]}"
      	echo $ssid
      # gives:
      # “FRITZ!Box Fon WLAN 7390 2GHz“ „otherWlan“

      Let me know if that solves the problem.

  34. Lewis Rodger November 29, 2013 at 6:03 pm #

    Hello! i know this thread is a little old now, but im having some problems with the script. im using 2013-09-25-wheezy-raspbian and seems to just ignore the entire script, when you start it up none of the stuff we typed in comes up! what am i missing??


  35. Lewis Rodger November 29, 2013 at 6:03 pm #

    Hello! i know this thread is a little old now, but im having some problems with the script. im using 2013-09-25-wheezy-raspbian and seems to just ignore the entire script, when you start it up none of the stuff we typed in comes up! what am i missing??


  36. Ilkka December 10, 2013 at 1:04 pm #

    Thanks for a great tutorial!
    Thing works! This is very useful for my project, where i’m setting up my Pi in random locations and configuring it with MacBook instead of a keyboard or a display. After configuring it using AdHoc, I will connect it to local Wi-Fi and then access Pi remotely. Accessing remotely is tricky because of NAT, but hopefully I can solve this using PageKite.

    Thanks again!
    – Ilkka

  37. Gaston January 31, 2014 at 5:39 pm #

    Hello. Thank you for the post. I am using Linux Version 3.6.11+ on RPi Hardware BCM2708. I believe that I am running on Raspbian,
    I followed all the steps, and my RPi continue booting and connection to the wlan0 default as before making all of this. I looked at the log file, but I cannot identify anything as shown in previous questions and answers.
    I am assuming that this procedure will make the RPi as the server, is that correct? I am a little confused about the host myLaptop with the MAC address of your RPi.
    If this works, the RPi should be broadscasting a ‘MyWlan’ ‘MyOtherWlan’ as SSID, is that correct?

    I am not sure what else to ask or describe about my problem. Just too new in this…

    Thank you,

  38. NovaNostroN March 2, 2014 at 12:02 am #

    Thanks for this…. after much googling I found it… concise and easy to follow.
    I work with people that can not write instructions this clearly, and it is their job!

  39. Joe March 13, 2014 at 10:32 am #

    To read the SSIDs dynamicaly out of the wpa_supplicant I changed the for loop to:

    for ssid in $(grep ssid /etc/wpa_supplicant/wpa_supplicant.conf |awk -F ‘”‘ ‘{printf” \””$2″\” “;}’|tr -d ‘”‘)

    This works until you have spaces in your SSID’s..

    Then it loops all the SSIDs instead of using a list in rc.local you have to manage each time you config a new wifi…


  40. Bryan092785 May 5, 2014 at 5:52 am #

    Thanks for the write up. Very nice! One question tho, would you happen to know why shairport is not working on the ad hoc network? Most likely simple but I’m not sure where to even start. I have it run on boot but I went ahead and started it via ssh and my ios device still does not see it. Any comments would be greatly appreciated to get started in the right direction

  41. piet June 1, 2014 at 12:13 am #

    Thanks for the great tutorial, Lasse

    When it connect to my wifi network at boot, everything works great, I’m having a problem when in ad hoc mode. I can see the raspi’s ad hoc network, ‘RPi’, when I scan, but I can’t log in from my lap top. The laptop runs Ubuntu and WICD can’t seem to authenticate using the passphrase ‘aaaaa11111’. Does anyone have any ideas?

    Thanks, Piet

  42. AndyM July 10, 2014 at 2:02 pm #

    Hi, I get an error on boot:

    “/etc/rc.local: line 29: connected: command not found”

    from the initial connected = false statement.

    Any ideas?

  43. David July 25, 2014 at 1:23 pm #


    I got your script working, but it seems unable to connect to a known network. Everytime the pi sets up an adhoc network. When I run the script when the pi is already booted, I see that the wpa_supplicant might be the problem. Could it be that it causes problems because wpa_supplicant starts at boot?

    • David July 25, 2014 at 1:28 pm #

      When I direct the output of the wpa_supplicant command in your script to a log file, I get this output:

      ioctl[SIOCSIWAP]: Operation not permitted
      ioctl[SIOCSIWENCODEEXT]: invalid arugment
      ioctl[SIOCSIWENCODEEXT]: invalid arugment

      • David July 25, 2014 at 1:49 pm #

        Oh my, nevermind I misread dhclient -1 wlan0, I thought the “1” was a letter “l”

  44. David July 25, 2014 at 1:55 pm #

    Sorry for my previous comments, very dumb mistake from my part. I still have one question though. Is it possible to leave wpa_supplicant running even if you have an adhoc network running? I would like to implement a program where I can log in to my pi ad-hoc and then configure the WiFi network from there with wpa_ctrl.c documented here:

  45. blutoh July 26, 2014 at 7:00 pm #

    Thanks, Lasse, this is a great tutorial, it saved me from having to try and figure all of this out by myself! I have it working reliably now. I noticed two things, one of which has been mentioned here but I don’t see a solution posted, and one that has not been mentioned:

    1. Wireless channel. How can you set it to a different wireless channel? I tried modifying createAdHocNetwork() by adding:

    iwconfig wlan0 channel 5

    But the ad-hoc network still loads on channel 1. Should channel configuration be setup somewhere else?

    2. When running DHCP on the ad-hoc network, there is no WPA or WEP security. Has anyone found a way to patch that?


  46. blutoh July 26, 2014 at 7:03 pm #


    Try logging in without security. I was having this same issue until I desocvered that the security doesn’t work when DHCP is running. Logging in without a passphrase works for me

  47. Jithin July 29, 2014 at 5:14 pm #

    Sir, THanks in advance.

    I m usng EDUP nano adapter. i tried to configure wireless access point using hostapd, with the basic setting my access point was able to detected by other systems, like laptop, smartphones.

    With this configuration none of my devices are detecting the “RPi” adhoc.

    I m using a fresh iso.

    Please reply

  48. Francesco August 20, 2014 at 11:10 pm #

    i’ve followed your tutorial (multiple times) on a fresh debian raspberry, and each time i end up with a perfectly working script (thanks a lot for it) but when it comes to leasing an ip in ad-hoc mode the client device never receives it, nor is able to communicate in any way to the pi board.
    i’ve tried manually rewriting all of your config files, manually starting ad-hoc and dhcp, all without errors, but nothing changed, and in the log file there is nothing that to me makes some sense, do you have any idea what can be causing this?
    here is the log file if it’s useful:

    Aug 20 22:45:32 raspberrypi dhcpd: Wrote 0 leases to leases file.
    Aug 20 22:45:32 raspberrypi dhcpd: Listening on LPF/wlan0/00:1f:1f:bf:44:ff/
    Aug 20 22:45:32 raspberrypi dhcpd: Sending on LPF/wlan0/00:1f:1f:bf:44:ff/
    Aug 20 22:45:32 raspberrypi dhcpd: Sending on Socket/fallback/fallback-net
    Aug 20 22:45:32 raspberrypi ifplugd(wlan0)[1647]: Link beat detected.
    Aug 20 22:45:32 raspberrypi ifplugd(wlan0)[1647]: Executing ‘/etc/ifplugd/ifplugd.action wlan0 up’.
    Aug 20 22:45:32 raspberrypi ifplugd(wlan0)[1647]: client: /sbin/ifup: interface wlan0 already configured
    Aug 20 22:45:33 raspberrypi ifplugd(wlan0)[1647]: Program executed successfully.
    Aug 20 22:45:33 raspberrypi ntpd[2169]: Listen normally on 3 wlan0 UDP 123
    Aug 20 22:45:33 raspberrypi ntpd[2169]: peers refreshed

    Thanks in advance

    • Jim Lind September 22, 2014 at 1:16 am #

      Do you have any wpa_supplicant stuff in your interfaces file?
      You usually end up with pieces there by default and it would configure your wlan0 automatically.

  49. Jim Lind September 22, 2014 at 1:13 am #

    I followed your directions on 2014-06-20-wheezy-octopi-0.9.0 and it worked PERFECTLY! Great way to make my 3D printer mobile.

    Thanks a thousand times.

  50. Kostas September 28, 2014 at 1:46 pm #

    Thank you for the tutorial.It took a while to come at the point where I can connect to my home network when that is found and to kick off the ad hoc when not.

    When the ad hc is up and I run
    iwlist wlan0 scan
    I see the it is broadcasting on channel 11 ans has an ESSID: RPi

    however I cannot get my laptop to find this network.
    Both adapters, laptop (windows 8) and Pi use Wireless-N

    The network is recognized by my iphone btw.

    Does anyone have a tip how to get my laptop recognize and connect to the RPi?

  51. victor October 17, 2014 at 1:52 am #

    it is a great article. thanks!
    However for the adhoc part I can’t get it work using my macbook pro. my laptop is running mavericks. I don’t know if there is an issue with my pc part?

  52. sammy singh December 2, 2014 at 4:07 pm #

    Well even though the OP has not responeded to the comment for close to a year, I still am posting. I was hoping to use this in with Airplay installed on Rpi to be able to use it my car as lots of people have done. Well for me I installed Airplay first and tested on rpi on my LAN and it was woking good. I then installed the ad hoc as described in this tut, and though I am able to connect to the ad hoc network successfully, the devices (also connected to same ad hoc) does not see airplay running and so I can not stream any song ….. The moment I connect rpi on my wifi all devices see airplay and I am able to stream………so even though both are working I still cannot use rpi in my car since my iphone would not see airplay running on rpi if connected to AD HOC network rpi creates …………….. anyone can help me on this …

  53. Salvatore May 11, 2015 at 3:30 pm #

    Hi Lasse,
    thank you for your tutorial, it is exactly what I was looking for. However, even if I repeated the entire procedure many times I cannot connect to infrastructure network or create an ad-hoc network.
    I have Raspberry Pi with Raspian upgraded to Jessie 8.0 and I’m using TP-Link TL-WN725N V2 with rtl8188eu chip. I’m encountering different problems. The first is related to wpa_cli which I cannot use since it gives me the error “Could not connect to wpa_supplicant: (null) – re-trying”. However, this is not related to your tutorial and I’m looking for a solution. So I manually configured wpa_supplicant.conf file by following this guide https://www.raspberrypi.org/documentation/configuration/wireless/wireless-cli.md.
    I used your rc.local file and rc.local file modified by james, but I get the same error. I cannot connect to infrastructure network and I think that the problem is related to sudo dhclient -1 wlan0 my system does receive any IP. Moreover, the system can setup an adhoc network with this ifconfig
    wlan0 Link encap:Ethernet HWaddr e8:de:27:9d:ed:f0
    inet addr: Bcast: Mask:
    RX packets:0 errors:0 dropped:0 overruns:0 frame:0
    TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:1000
    RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
    and this iwconfig
    wlan0 IEEE 802.11bg ESSID:”RPi” Nickname:””
    Mode:Ad-Hoc Frequency:2.412 GHz Cell: 02:11:87:04:94:FF
    Bit Rate:54 Mb/s Sensitivity:0/0
    Retry:off RTS thr:off Fragment thr:off
    Power Management:off
    Link Quality:0 Signal level:0 Noise level:0
    Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
    Tx excessive retries:0 Invalid misc:0 Missed beacon:0
    but there is not visible wifi network.
    Finally, if I execute sudo /etc/rc.local the message is this
    RPi Network Conf Bootstrapper 0.1
    Scanning for known WiFi networks
    First WiFi in range has SSID: MYNET
    Starting supplicant for WPA/WPA2
    Obtaining IP from DHCP
    DHCP server did not respond with an IP lease (DHCPOFFER)
    Failed to connect to non-global ctrl_ifname: (null) error: No such file or directory
    Creating ad-hoc network
    Internet Systems Consortium DHCP Server 4.3.1
    Copyright 2004-2014 Internet Systems Consortium.
    All rights reserved.
    For info, please visit https://www.isc.org/software/dhcp/
    Config file: /etc/dhcp/dhcpd.conf
    Database file: /var/lib/dhcp/dhcpd.leases
    PID file: /var/run/dhcpd.pid
    Wrote 0 deleted host decls to leases file.
    Wrote 0 new dynamic host decls to leases file.
    Wrote 0 leases to leases file.
    Listening on LPF/wlan0/e8:de:27:9d:ed:f0/
    Sending on LPF/wlan0/e8:de:27:9d:ed:f0/
    Sending on Socket/fallback/fallback-net
    Ad-hoc network created
    There’s already a DHCP server running.

    If you think you have received this message due to a bug rather
    than a configuration issue please read the section on submitting
    bugs on either our web page at http://www.isc.org or in the README filevolumio@volumio:~$
    before submitting a bug. These pages explain the proper
    process and the information we find helpful for debugging..


    Thank you

  54. Nathan May 27, 2015 at 5:01 am #

    Thanks for this great tutorial! It really is what I needed.

    I am having a problem though. It looks like it creates the ad-hoc network and iwconfig shows that the mode is in Ad-Hoc, but I can’t get any of my wireless devices to see it.

    I’m not quite sure where to begin troubleshooting, any help or suggestions that you can offer would be great, thanks!


  55. John August 18, 2015 at 5:47 pm #

    you wrote

    “Then, in order to persist the settings, we type:

    > save_config”

    When I enter save_config I get: FAIL

    Any idea??

  56. Philip September 1, 2015 at 10:45 pm #

    Is it possible to scan for a list of specific Wifi’s and connect if pressent, but if no Wifi is present create an access point ?

  57. Gary January 9, 2016 at 5:22 am #

    Hi Mr. Lasse, does your project work until now? ‘Cause I’m thinking this would be very helpful to us doing our project. We’re thinking that what if there is no Internet connection and the raspberry pi will be the server of our planned network.
    BTW, great tutorial. If I could spare some free time I would make one of this.

    • Lasse Christiansen January 10, 2016 at 7:59 pm #

      Hi Gary,

      I have not worked on this project since I wrote the blogpost. _I guess_ it still works but it might require some minor modifications to get things rolling.


  58. Sam Poirier March 18, 2016 at 6:27 pm #

    Your guide was very useful, and I managed to get it working (with a few minor changes).
    However, I have encountered a problem. My android device cannot see the ad-hoc network. I know that it is able to see them, as it could see the one from my windows 10 PC, and I know that the network is created because a friend’s iPhone was able to see the connection.

    Is there some kind of mode or setting I need to change?

    Thanks a lot,

  59. John April 11, 2016 at 3:25 am #

    I bought a PI, and an Edimax WIFI dongle, and after days, managed to get it to connect to a WiFi Point, and then eveeeeeeennnnnntttuuuuually I managed to get it to act as a hot spot, thanks to articles like this.

    But NOT both together and I am wondering how Virtual Router does it on Windows. It makes the laptop act as a hot spot AND you can still connect to wifi for internet access. That is nice but you now need a cable to get internet, that kind of spoils it for me, Yah! I got WIFI, and I need a cable, nah! What I want is both, like Virtual Router does. So I can power my PI… with no network cables as a hot spot with internet from another hot spot.

    Is this just an impossible thing to do with ONE EDIMAX?
    EDIMAX is a WIFI dongle, but it seems impossible with any dongle on Linux.

    Its amazing how you gurus figure this stuff out… I’m hoping someone can at least just tell me why one WIFI device cant do both functions, like the laptop does… there seems to be nothing out there that actually mimics what Virtual Router is doing on Windows… why?

    Do I have to put two WIFI dongles on the PI? Why?

    Thanks… Completely Clueless

  60. joeR August 8, 2016 at 2:33 pm #

    I am using two Raspberry Pi 3 Model 8 V1.2
    On the first one I followed all the steps from your tutorial.
    After using “/usr/sbin/dhcpd wlan0” and checking if dhcp is active with “service isc-dhcp-server status” it shows as inactive. Using “service isc-dhcp-server start or restart” after, works and so the state shows active. Any ideas why it won’t start up with just “/usr/sbin/dhcpd wlan0”?

    When listing available networks on the second PI my adhoc network seems to be visible, but I am unable to obtain an IP address. I am not using a key for encryption, I want it to be an open network. Using “wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/test.conf” and “dhclient -1 wlan0” gives me the error “No DHCPOFFERS received” when using -d. “Unable to obtain a lease on first try. Exiting.
    My /etc/wpa_supplicant/example.conf on the second PI
    ctrl_interface=DIR=/run/wpa_supplicant GROUP=netdev


    Any leads on what might be the problem?

  61. Jerry Isdale October 17, 2017 at 7:40 pm #

    Aloha and Mahalo for the tutorial.
    I’m happy to report that (with minor tweak) it just worked on my Raspberry Pi Zero W (October 2017).
    Tweak is that when setting the iwconfig key in rc.local, a text value needs a s: prefix and must be either 5 or 13 characters exactly
    so using
    iwconfig wlan0 key s:foobr
    should work.
    more later if i can. will try to linkback to this page in our project documentation


  1. Raspberry Pi Prank Tutorial – Making Your Co-Worker’s Desk “Magically” Go Up and Down | Lasse Christiansen Development - December 6, 2012

    […] I have covered this topic in one of my previous blog posts, here: Raspberry Pi Tutorial – Connect to WiFi or Create An Encrypted DHCP Enabled Ad-hoc Network as Fall…. […]

  2. api.danielvagg.com – functional(ish) | Snapshots of Thoughts - January 26, 2013

    […] So, I have written a wee chunk of code in ‘/etc/rc.local‘ using advice from this blog here! The  endpoints are served using a simple enough Python service which accepts POST and GET. The […]

  3. Doch alles nicht so einfach: Gphoto2 , Canon und Capische | Arduino Photo Bot - September 3, 2013

    […] ich nicht im Heimnetz bin. Das geht in der Datei /etc/rc.local. Wie genau steht in dem Tutorial von JPH Photography. Man muss nicht nur ein Wlan mit SSID aufbauen und verschlüsseln, sondern auch noch eine IP […]

  4. Turning Raspberry Pi into a Spycam using just a webcam (my first adventure with pi) | Phillip Moxley - October 24, 2013

    […] http://lcdev.dk/2012/11/18/raspberry-pi-tutorial-connect-to-wifi-or-create-an-encrypted-dhcp-enabled&#8230; […]

  5. The RocketPi – A RaspberryPi powered water rocket launcher | Francesco Servida's Blog - September 14, 2014

    […] to connect to it with the Mac. Easy to say not so easy to get working, at least at first. I found this great tutorial from Lasse Christiansen on how to create a wi-fi hotspot in no knew network is […]

  6. Pure Pi: Digital Effects with Pure Data running on a Raspberry Pi | Munch Audio - April 22, 2015

    […] 2. Configure your Pi to either connect to WiFi or create an Ad-Hoc network […]

  7. Enabling mesh (ad-hoc) network on multiple Raspberry Pi’s | Scalable software, SaaS and communication technology blog - August 24, 2015

    […] initially I used settings as per this tutorial: http://lcdev.dk/2012/11/18/raspberry-pi-tutorial-connect-to-wifi-or-create-an-encrypted-dhcp-enabled… Executing from command line: ifconfig wlan0 down     iwconfig wlan0 mode ad-hoc […]

  8. Setup automatic switch from WiFi to Hotspot | Arduino, Raspberry and Mindstorm - September 4, 2015

    […] used this guide to test at startup if it could connect to a known WiFi and otherwise setup a […]

  9. WP Stacker link collection: October with 29 links | MakerCave - October 3, 2015

    […] Raspberry Pi Tutorial – Connect to WiFi or Create An Encrypted DHCP Enabled Ad-hoc Network as Fallback: From the list of networks, you should be able to see the SSID of the network you are going to connect to. If that is the case, we are now going to add that network to wpa_supplicant. – by Lasse Christiansen – Tags: makercave, raspberry pi, things to make, wifi – http://lcdev.dk/2012/11/18/raspberry-pi-tutorial-connect-to-wifi-or-create-an-encrypted-dhcp-enabled&#8230; […]

  10. Connect to WiFi or Create An Encrypted DHCP Enabled Ad-hoc Network as Fallback – Raspberry Pi - December 9, 2018

    […] Connect to WiFi or Create An Encrypted DHCP Enabled Ad-hoc Network as Fallback […]

  11. Raspberry Pi Tutorial – Connect to WiFi or Create An Encrypted DHCP Enabled Ad-hoc Network as Fallback – Raspberry Pi - December 9, 2018

    […] Raspberry Pi Tutorial – Connect to WiFi or Create An Encrypted DHCP Enabled Ad-hoc Network as Fall… […]

Leave a Reply to Adrian Cristian Pop