How to recover wifi by hand on Linux

So everything crashed, your network manager (in my case wicd) ceased to function, you have no access to the wifi network and all instructions you found on the net are outdated? Here is (mostly as a note to self) how I got it back. This is on a Debian testing machine. You need the wpa_supplicant program, which I assume you have, because it is used by every other wifi manager out there.

  1. Test that the wlan0 interface exists (o any other name):

In my case it shows this:

wlan0     IEEE 802.11  ESSID:None  
          Mode:Managed  Frequency:2.462 GHz  Access Point: 3C:A6:2F:19:44:F3   
          Bit Rate=72.2 Mb/s   Tx-Power=20 dBm   
          Retry short limit:7   RTS thr=2347 B   Fragment thr:off
          Power Management:on
          Link Quality=70/70  Signal level=-35 dBm  
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:0  Invalid misc:0   Missed beacon:0
  1. Create a wpa supplicant config file:
echo "YOUR_WIFI_PASSWORD" | wpa_passphrase YOUR_WIFI_ID > wpa_suplicant.conf
  1. Edit wpasupplicant.conf and add a few things:
ctrl_interface=DIR=/var/runwpa_supplicant GROUP=netdev

# reading passphrase from stdin
  1. Move wpa_supplicant.conf to a default location (might be useful later on):
sudo mv wpa_supplicant.conf /etc/wpa_supplicant/

You might also want to chmod 600 wpa_supplicant.conf as it contains a password, to make it only readable by the sudo account

  1. Launch everything:
wpa_supplicant -c /etc/wpa_supplicant/wpa_suplicant.conf -iwlan0 -B

iwconfig should now show that wlan0 is connected to YOUR_WIFI_ID

  1. Run a dhcp query to get all the network config automatically from the router:
sudo dhclient eth0
  1. Check that wlan0 has received an ip address:
ip a

Which in my case gives:

3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 6e:6a:7c:c2:35:87 brd ff:ff:ff:ff:ff:ff permaddr 20:68:9d:f9:31:13
    inet brd scope global dynamic wlan0
       valid_lft 863013sec preferred_lft 863013sec
  1. Check that you can reach google, for example (Ctrl+C to cancel once you see it working):

I have to run sudo dhclient eth0 regularly otherwise it disconnects from the network, not sure why, but since this is an emergency operation, it is not important.