This isn’t only for Raspbian running on a Raspberry Pi, as far as I know you can use it on most flavors of GNU/Linux if you want to set up WiFi using only the terminal.
This also assumes that you already have appropriate drivers installed. I have tested this with this wireless adapter and the most current version of Raspbian (at the time of posting), and I didn’t need to download any additional drivers.
Modify /etc/network/interfaces
to include the following lines:
allow-hotplug wlan0 iface wlan0 inet dhcp wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf iface default inet dhcp
Add to /etc/wpa_supplicant/wpa_supplicant.conf
the following lines:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 network={ ssid="YOURSSID" psk="YOURPASSWORD" # Protocol type can be: RSN (for WP2) and WPA (for WPA1) proto=RSN # Key management type can be: WPA-PSK or WPA-EAP (Pre-Shared or Enterprise) key_mgmt=WPA-PSK # Pairwise can be CCMP or TKIP (for WPA2 or WPA1) pairwise=CCMP #Authorization option should be OPEN for both WPA1/WPA2 (in less commonly used are SHARED and LEAP) auth_alg=OPEN }
Then, reboot.