Tuesday, July 3, 2018

How to connect to wifi from Raspberry Pi - CLI


Although there are easy ways connect to wifi using "sudo raspi-config" , I have decided to stick with CLI so that we will know what is happening behind the fancy screens ;) Come on, you dont get high when it is easy :).

On a serious note, it doesnt encypt the PSK - that is the actual reason..

Following are the steps I followed  :


Step 1 - Scan the Network by issuing the following command

                sudo iwlist wlan0 scan

Step 2 - Generate an encrypted PSK (pre-encrypted 32 byte hexadecimal number) and then you will be asked for the password of the WiFi network

               wpa_passphrase



               example :

                       wpa_passphrase "test"
                                   
                       Then you will be asked for the password of the WiFi network (in this case  testingPassword). The output is as follows:

  network={
      ssid="test"
      #psk="testingPassword"
      psk=131e1e221f6e06e3911a2d11ff2fac9182665c004de85300f9cac208a6a80531
  }

You can ideally copy paste this to /etc/wpa_supplicant/wpa_supplicant.conf, but one should be nuts id you don't delete the clear text password (commented in above example)

Also, you can do this in a diff way when you are more lazy (again delete the clear text as needed, dont be that ;))

sudo su

wpa_passphrase "testing" >> /etc/wpa_supplicant/wpa_supplicant.conf

Now save the file by pressing Ctrl+X, then Y, then finally press Enter.

Reconfigure the interface by issuing the following command

wpa_cli -i wlan0 reconfigure

You can verify whether it has successfully connected using ifconfig wlan0. If the inet addr field has an address beside it, the Raspberry Pi has connected to the network. If not, check that your password and ESSID are correct.

On the Raspberry Pi 3 Model B+, you will also need to set the country code, so that the 5G networking can choose the correct frequency bands. You can either use the raspi-config application and select the localisation option, or edit the  wpa_supplicant.conf file and add the following. (Note you need to replace 'GB' with the ISO code of your country. See Wikipedia for a list of country codes.)

country=US

Its time for QA - 

What about unsecured networks ?

If the network you are connecting to does not use a password, the  wpa_supplicant entry for the network will need to include the correct  key_mgmt entry. e.g.

network={
    ssid="test"
    key_mgmt=NONE
}

What about hidden networks ?

If you are using a hidden network, an extra option in the wpa_supplicant file,  scan_ssid, may help connection.

network={
    ssid="yourHiddenSSID"
    scan_ssid=1
    psk="Your_wifi_password"
}
You can verify whether it has successfully connected using ifconfig wlan0. If the inet addr field has an address beside it, the Raspberry Pi has connected to the network. If not, check your password and ESSID are correct.

What about adding multiple wireless network configurations?

On recent versions of Raspbian, it is possible to set up multiple configurations for wireless networking. For example, you could set up one for home and one for school.

For example

network={
    ssid="SchoolNetworkSSID"
    psk="passwordSchool"
    id_str="school"
}

network={
    ssid="HomeNetworkSSID"
    psk="passwordHome"
    id_str="home"
}
If you have two networks in range, you can add the priority option to choose between them. The network in range, with the highest priority, will be the one that is connected.

network={
    ssid="HomeOneSSID"
    psk="passwordOne"
    priority=1
    id_str="homeOne"
}

network={
    ssid="HomeTwoSSID"
    psk="passwordTwo"
    priority=2
    id_str="homeTwo"
}

Finally, what about me ? 


Well, thats THE same... Still counting on you, will try my best before I call it..... On a serious note, that was a silly question, seldom cares.. Thanks for asking - see you next time ?


Courtesy: https://wiki.debian.org/WiFi/HowToUse