200

I need to SSH my Pi over wifi but because it is a model A board (using a usb hub is not possible - ever) and I have no ethernet, i can't configure the Pi to connect to my secured wifi network. I want to have the SD card plugged into my laptop and I want to edit a file with the wifi configuration information in it so my Pi will connect to my network automatically at start-up so I can then SSH it to get control. I know how to enable SSH on a headless system thanks to this answer.

Edit. Ive been searching around and I'm wondering if I'm able to just edit the file /etc/network/interfaces while the SD card is in my PC and put in all the network ssid, psk and wlan0 stuff in it. Will this work? Thanks

3
  • You are faced with quite a predicament there. I would suggest using a USB LAN temporarily to connect over SSHS. Most drivers are included for those. Insert your WiFi and then follow this answer to set up your WiFi on the Pi. After wards you reboot, unpluggin the LAN and boot in using Wifi. It should auto connect within a few minutes.
    – Piotr Kula
    Oct 22, 2013 at 8:29
  • 2
    Low cost way to set up is to use a usb-serial adapter to connect to the serial on the GPIO header. You don't even need to enable ssh to do this Oct 23, 2013 at 5:36
  • see my answer [here][1] for an alternative method [1]: raspberrypi.stackexchange.com/a/31397/30719
    – memebrain
    May 9, 2015 at 21:30

11 Answers 11

307

There are some great answers here, but many are out of date. Even this one has aged somewhat. There are now (at least) two ways of doing this:

1: Raspberry Pi Imager

The Raspberry Pi Foundation's Raspberry Pi Imager now has an advanced options menu which is accessed by the keyboard shortcut Ctrl+Shift+X:

Raspberry Pi Imager wifi setting option

You can set hostname, allow SSH (including changing user password), configure wifi and set locale. Note that this tool also appears to have telemetry built in, which can be turned off from the GUI.

2: Traditional CLI method

Since May 2016, Raspbian has been able to copy wifi details from /boot/wpa_supplicant.conf into /etc/wpa_supplicant/wpa_supplicant.conf to automatically configure wireless network access:

If a wpa_supplicant.conf file is placed into the /boot/ directory, this will be moved to the /etc/wpa_supplicant/ directory the next time the system is booted, overwriting the network settings; this allows a Wifi configuration to be preloaded onto a card from a Windows or other machine that can only see the boot partition.

The latest update to Raspbian - Raspberry Pi, 2016-05-13

Since the /boot partition is accessible by any computer with an SD card reader, wifi configuration is now much simpler.

A typical wpa_supplicant.conf file is:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=«your_ISO-3166-1_two-letter_country_code»

network={
    ssid="«your_SSID»"
    psk="«your_PSK»"
    key_mgmt=WPA-PSK
}

Replace «your_ISO-3166-1_two-letter_country_code» with your ISO Country Code (such as CA for Canada), «your_SSID» with your wireless access point name and «your_PSK» with your wifi password.

If you don't wish to store your wifi password in plain text, you can use the wpa_passphrase utility to store the hashed password:

wpa_passphrase «your_SSID»

The utility will prompt for the password, and will return the hexadecimal hash value. Note that this hashed password is stored without quotes in the /boot/wpa_supplicant.conf, eg:

psk=b76dc68cb76bfea7232e32634f859ca760cd8abeee318a346b51d3bde00b8965

Note that the country, ctrl_interface and update_config lines are required in file as created in /boot: if they are missing the system will not connect to the network. The above process can be repeated to correct the omissions.

This process has been tested with both Raspbian and Raspbian Lite as of the November 2018 release.


In addition, you may wish to add Secure Shell (SSH) access to your headless Raspberry Pi. SSH is disabled on Raspbian by default for security reasons. You can enable it by creating a file called /boot/ssh:

For headless setup, SSH can be enabled by placing a file named 'ssh', without any extension, onto the boot partition of the SD card. When the Pi boots, it looks for the 'ssh' file; if it is found, SSH is enabled and then the file is deleted. The content of the file doesn't matter: it could contain either text or nothing at all.

SSH (Secure Shell) - Raspberry Pi Documentation

If you enable SSH access, you should change the password of the standard pi user as soon as possible. As the default password (raspberry) is widely known, your Raspberry Pi is effectively open to everyone on your network until you change the password. You can do this with the passwd command.

18
  • 2
    There are two restrictions for this solution (tested with 2016-09-23 images): 1. The file must be formatted with Linux style line endings (LF instead of CR LF). 2. It did not work with the Raspbian light image on my Raspberry Pi 3.
    – Cellcon
    Nov 23, 2016 at 20:24
  • 1
    @user2154065 1. Covert file to LF with Notepad++ by right-clicking on "Dos\Windows" in bottom right and picking "UNIX/OSX format". 2. The wpasupplicant package probably isn't included in Raspbian Lite.
    – Hydraxan14
    Mar 3, 2017 at 0:24
  • 1
    Now I've got a couple of Raspbian Lite machines running, I'm pretty sure my comment was wrong: wpa_supplicant is included, or can be easily installed.
    – scruss
    Mar 4, 2017 at 3:34
  • 6
    @kleinfreund don't include the passphrase in clear text. Run "wpa_passphrase SSID" (with your SSID) and enter the passphrase when prompted. Copy the output into your wpa_supplicant.conf file, removing the line that includes the cleartext and leaving just the hashed version. (You can do this on a different system so you have the file ahead of time when setting up your micro SD for the Pi.) Jul 12, 2017 at 0:09
  • 4
    @scruss - your answer is almost correct, please add this line to the wpa_supplicant.conf sample above: ctrl_interface=/var/run/wpa_supplicant I just tried on a new Zero W with Raspbian Strech Lite
    – ctekse
    Sep 5, 2017 at 20:36
27

Some specific Instructions:

Contents of /etc/network/interfaces:

auto lo
iface lo inet loopback

allow-hotplug eth0
iface eth0 inet dhcp

allow-hotplug wlan0
iface wlan0 inet manual
    wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
    post-up ifdown eth0
iface default inet dhcp

Contents of /etc/wpa_supplicant/wpa_supplicant.conf:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="YOUR_SSID_HERE"
    psk="YOUR_SECRET_PASSPHRASE_HERE"
    id_str="SOME_DESCRIPTIVE_NAME"
}

Honest. Those two files, with given contents are what I use on all my pi's. They boot and immediately connect to my wireless router. DHCP negotiation provides an address, and my router resolves the hostname to the proper IP address. Make sure to name each PI appropriately via /etc/hostname.

The weirdness in the interfaces file in the trailing iface default... is needed, otherwise the wireless WPA connection won't come up.

The wpa_supplicant.conf file can have multiple network={ entries too, I used to take my pi to work... plug it in and voila, it connected automagically there too, work's configuration was a bit more convoluted though. Included here as an example, add/replace the following in the wpa_supplicant.conf file:

network={
    ssid="THE_OFFICE"
    scan_ssid=1
    key_mgmt=WPA-EAP
    eap=PEAP
    identity="WORK_USERNAME"
    password="WORK_PASSWORD"
    phase1="peaplabel=0"
    phase2="auth=MSCHAPV2"
    id_str="SOME_DESCRIPTIVE_NAME"
}

Essentially, it scans the wpa_supplicant.conf file and connects to the first network it finds that matches. Very handy. It's possible to make it connect to any 'open' network automatically this way too. Not the smartest thing to do, but doable.

4
  • I need to have a static ip also as its headless and i wont be able to ssh it without knowing its ip address. I currently have id_str="home" in wpa_supplicant.conf and iface home inet static (static ip info underneath this)in the interfaces file, this gives me a static ip but im not sure if this is the best way to have it set up and its a bit confusing to set up(and i get a error "Cannot find device "home"" on boot).
    – 11chubby11
    Oct 30, 2013 at 10:21
  • 1
    I use dyn.com (or dyndns.com) to allow a proper name to access my pi(s), use ddclient to automatically set the name to the proper address. Alternately, use your router to specify a static dhcp address, much more useful. I also use upnpcd to dynamically set up upnp settings to allow me to connect to my systems from outside my network, no static IP's ever needed now, it sets itself up where-ever it is. (iface stanza can only have lo/eth0/wlan0/default as option, 'home' won't work)
    – lornix
    Oct 30, 2013 at 10:26
  • How can I add /etc/network/interfaces in the SD boot? Just put it anywhere and it will copy it?
    – denislexic
    Aug 31, 2017 at 21:31
  • Basically a comment to the answer by lornix - whose solution I used successfully - since I don't have rep to write comments. In order to edit the root file system, you need to mount the second partition, not the first, boot partition. I did: sudo mount /dev/sdc2 /mnt after which I could edit files under the /mnt root, i.e. /mnt/etc/wpa_supplicant/wpa_supplicant.conf and /mnt/etc/network/interfaces and /mnt/etc/hostname (again using sudo). When done, cd out of the /mnt hierarchy, and do sudo umount /mnt You can then insert the SD-card in the Rπ and boot. Note that this requires a Linux machine, Feb 12, 2018 at 19:32
15

Since November 2016 SSH has been disabled by default in standard Raspbian images for security reasons.

To re-enable it, create a blank file called either ssh or ssh.txt on the sd card boot folder ( fat32 partition accessible from windows)

To preconfigure the wifi settings, you also create a file called wpa_supplicant.conf there containing:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={ 
ssid="MySSID" 
psk="MyPassword" 
}

Both these files can be created on windows, and edited using notepad.

Then insert the SD card into the pi, and you should be able to SSH right in.

1
  • Nice to see they've simplified the setup greatly! Great Answer!
    – lornix
    Feb 19, 2018 at 19:09
4

As of RASPBIAN STRETCH you must include ctrl_interface and update_config in wpa_supplicant.conf

To get wifi with ssh on boot do these simple steps on your SD card:

  1. touch /<sd mount point>/boot/ssh
  2. nano <sd mount point>/boot/wpa_supplicant.conf

With the contents:

country=AU  #omit if US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
    ssid="YourNetworkSSID-1"
    psk="passwordOne"
}
  1. Unmount SD card, put in pi, boot up pi
2

You can use the interfaces file. Load it up with all the network information and put it in a thumb drive and reboot the RPi with that. The RPi takes the settings and sets up the WiFi without the need for extra hardware or a monitor.

After that you can SSH into Pi and install VNC to get a GUI. If you want a step by step instruction for it, check out Option 3: Connect with WiFion http://www.dexterindustries.com/BrickPi/getting-started/using-the-pi/

2
  • Couldn't get this to work. Cant find any other info on the internet about this way of setting up wifi.
    – 11chubby11
    Nov 1, 2013 at 2:41
  • Forgot to mention the script used to automate this. Here it is
    – karan
    May 14, 2014 at 6:31
2

You can also configure an SD card that will automatically install Rasbian with a preconfigured WiFi connection on your Raspberry pi (Raspberry Pi headless setup). This allow your pi to be justconnected to power for its first boot.

Step 1: Download Noobs Get and unzip the latest Noobs software (version Offline and network install) on https://www.raspberrypi.org/downloads/

Alternative : direct link to the file : http://downloads.raspberrypi.org/NOOBS_latest

At that time of writing, the latest NOOBS version is 1.4.0

Step 2: Headless configuration In the os/ directory, delete every other folders than rasbian/. In fact you should have only one other folder named Data_Partition.

In the rasbian/ folder, edit the flavours.json file. You should see following:

{
  "flavours": [
    {
      "name": "Raspbian - Boot to Scratch",
      "description": "A version of Raspbian that boots straight into Scratch",
      "supported_hex_revisions": "2,3,4,5,6,7,8,9,d,e,f,10,11,12,14,19,1040,1041"
    },
    {
      "name": "Raspbian",
      "description": "A Debian wheezy port, optimised for the Raspberry Pi",
      "supported_hex_revisions": "2,3,4,5,6,7,8,9,d,e,f,10,11,12,14,19,1040,1041"
    }
  ]
}

Delete the “Raspbian – Boot to Scratch” part, so the file should look like this at the end:

{
  "flavours": [
    {
      "name": "Raspbian",
      "description": "A Debian wheezy port, optimised for the Raspberry Pi",
      "supported_hex_revisions": "2,3,4,5,6,7,8,9,d,e,f,10,11,12,14,19,1040,1041"
    }
  ]
}

Edit the recovery.cmdline file in the root NOOBS directory and append silentinstall to the arguments list. The file should look like this at the end:

runinstaller quiet vt.cur_default=1 elevator=deadline silentinstall

Step 4 (optional): Configure custom Keyboard layout and Language If you want to change the default keyboard layout (us) and/or default language (default is ‘us’) append lang=XX keyboard=XX in the arguments list of the recovery.cmdline file (XX should be replaced by your language and keyboard code, which could be fr, de, … Default is us).

The file should look like this at the end:

runinstaller quiet vt.cur_default=1 elevator=deadline silentinstall lang=fr keyboard=fr

Step 5 (optional): Configure Wifi Noobs Config is a tool that will copy/past some files just after the installation of Rasbian. Among many other things, this tool allows you to pre-configure Wifi on your Pi. This nevertheless assumes that the wifi dongle you use is directly supported by Raspbian.

First thing is to download Noobs Config on GitHub (https://github.com/procount/noobsconfig/archive/master.zip)

Then unzip noobsconfig.zip and :

  • copy “customise.sh” in the root directory of your Noobs files

  • copy “os/Rasbian/partition_setup.sh” to your “os/Rasbian/” directory of your Noobs files.

  • create a folder named “wifi” under “os/Rasbian/”

  • copy “Examples/Rasbian/wifi/wifi/wpa_supplicant.conf” to
    “os/Rasbian/wifi/” directory of your Noobs files.

  • copy “Examples/Rasbian/wifi/Raspbian_root.txt” to “os/Rasbian/”
    directory of your Noobs files.

Last thing is to configure your wifi ssid and psk (password) in wpa_supplicant.conf :

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
ssid="MyNetwork"
psk="MyPassword"
}

Step 5 (optional): Change the default password Even if this step is optional, it’s highly recommended to change the default password of your Rasbian setup.

Edit the os/Rasbian/os.json file. It should look like this :

{
  "name": "Raspbian",
  "version": "wheezy",
  "release_date": "2015-02-16",
  "kernel": "3.18",
  "description": "A community-created port of Debian wheezy, optimised for the Raspberry Pi",
  "url": "http://www.raspbian.org/",
  "username": "pi",
  "password": "raspberry",
  "supported_hex_revisions": "2,3,4,5,6,7,8,9,d,e,f,10,11,12,14,19,1040,1041"
}

Let’s change the default password on the line 9.

Step 5: Copy files on your SD card and boot your Raspberry pi Copy/past files on a FAT formated SD card.

Step 6: Boot and connect Boot on our SD card, after a few minutes (depending on your SD card, the installation process can take more than 30 minutes) and connect to your Pi via SSH.

That it...

1
  • These instructions look promising, but as of NOOBS v1.9.2 there's no flavours.json file in the os/Raspbian directory. Since the main reason to edit it was to remove an alternate version, I'm thinking it's safe to simply skip that step when not needed.
    – natevw
    Aug 21, 2016 at 16:11
2

The process for headless setup has slightly changed for Raspbian Stretch and newer versions of RetroPie.

inside of boot directory...

touch ssh

nano wpa_supplicant.conf

add this to wpa_supplicant.conf

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
network={
    ssid="YOUR_NETWORK_NAME"
    psk="YOUR_PASSWORD"
    key_mgmt=WPA-PSK
}

unmount sd card and insert in pi for boot up

give it a minute and find your pi's ip address

ssh into the pi

ssh pi@pi-ip-address

the default password is raspberry

1

You need to edit the wpa_supplicant.conf file on the sdcard (it's in /etc or /etc/wpa_supplicant depending on your distribution version). The format of the file is explained here: http://linux.die.net/man/5/wpa_supplicant.conf

4
  • But if wireless drops out it wont auto reconnect using the basic settings. It requires you use manual mode and roaming with extra configuration files.
    – Piotr Kula
    Oct 22, 2013 at 12:44
  • Well it should be enough to setup, right? As it will definitely connect when wpa_supplicant starts if the network is available. I also thinks wpa_supplicant should automatically reconnect if you loose network.
    – hogliux
    Oct 22, 2013 at 13:01
  • Further to my last comment, I'd like to add that I've used this method myself on a raspberry model A with the newest debian.
    – hogliux
    Oct 22, 2013 at 13:07
  • WPA Supplicant will not reconnect if you use AUTO, you need to use STATIC with ROAMING and define SSID to connect to... which is a bit tricky to set up.
    – Piotr Kula
    Oct 22, 2013 at 15:28
1

I have created a shell script tool (unfortunately only runs on Linux), it automates the entire process of downloading the latest Raspbian image, unpacking the image, embedding the wi-fi and ethernet settings (provided by the user) in the image, repack the image and burn it on the SD card. When you boot your Pi with the SD card, it straight away connects with your network. The script also has the capability to searching for a freshly configured Pi on the network.

You can directly connect a Wi-Fi dongle with your Pi, use the RaspImgConfig.sh script to embed the Raspbian Image with wi-fi credentials and burn the image on the card. No need for any monitor, mouse or keyboard.

Youtube video of using the tool: https://youtu.be/WdYb1L2WzjM

Regards, Subhajit Ghosh

-2

You can set up your pi to work with a password protected wifi and automatically enable ssh without booting the pi, with config files in the /boot partition.

I used this for my AIY and scanned the network using nmap to find my pi.

From there you can just connect to the pi.

1
  • You should at least explain the file names needed and their contents, as well as a link to further information. May 14, 2018 at 22:48
-3

I think all these answers are missing the point that the RPi model A HAS NO Ethernet, no wi-fi, so no matter what you do, unless you get an external dongle, you wont be able to connect to the RPi through ssh.... your best bet is either to use it directly on a TV or to use it through the serial port on the headers, or as I said, get an external dongle for wifi.

1
  • 5
    I think it's implied that he is already using one. One would assume that the OP is aware that he cannot connect to a wireless network without some sort of adapter.
    – Impulss
    Dec 8, 2013 at 22:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.