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

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.
– ppumkinOct 22 '13 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
– John La RooyOct 23 '13 at 5:36

11 Answers
11

There are some great answers here, but many are out of date. 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.

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.

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.

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.
– user2154065Nov 23 '16 at 20:24

@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.)
– Peter HansenJul 12 '17 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
– ctekseSep 5 '17 at 20:36

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:

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.

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).
– 11chubby11Oct 30 '13 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)
– lornixOct 30 '13 at 10:26

How can I add /etc/network/interfaces in the SD boot? Just put it anywhere and it will copy it?
– denislexicAug 31 '17 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,
– Ketil MaldeFeb 12 at 19:32

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.

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.

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).

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.

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.
– natevwAug 21 '16 at 16:11

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.
– ppumkinOct 22 '13 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.
– hogliuxOct 22 '13 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.
– hogliuxOct 22 '13 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.
– ppumkinOct 22 '13 at 15:28

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.

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.

Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).