Step 1: Find available WiFi adapters – WiFi network from command line
This actually help .. I mean you need to know your WiFi device name before you go an connect to a WiFi network. So just use the following command that will list all the connected WiFi adapters in your Linux machines.
Let me explain the output:
This system has 1 physical WiFi adapters.
Designated name: phy#1
- Device names: wlan0
- Interface Index: 4. Usually as per connected ports (which can be an USB port).
- Type: Managed. Type specifies the operational mode of the wireless devices. managed means the device is a WiFi station or client that connects to an access point
-
Step 2: Check device status – WiFi network from command line
By this time many of you are thinking, why two network devices. The reason I am using two is because I would like to show how a connected and disconnected device looks like side by side. Next command will show you exactly that.
You can check that if the wireless device is up or not using the following command:
As you can already see, I got once interface (wlan0) as state UP and wlan1 as state DOWN.
Look for the word “UP” inside the brackets in the first line of the output.
In the above example, wlan1 is not UP. Execute the following command to
Step 3: Bring up the WiFi interface – WiFi network from command line
Use the following command to bring up the WiFI interface
Note: If you’re using Ubuntu, Linux Mint, CentOS, Fedora etc. use the command with ‘sudo’ prefix
If you run the show link command again, you can tell that wlan1 is now UP.
Step 4: Check the connection status – WiFi network from command line
You can check WiFi network connection status from command line using the following command
The above output shows that you are not connected to any network.
Step 5: Scan to find WiFi Network – WiFi network from command line
Scan to find out what WiFi network(s) are detected
The 2 important pieces of information from the above are the SSID and the security protocol (WPA/WPA2 vs WEP). The SSID from the above example is blackMOREOps. The security protocol is RSN, also commonly referred to as WPA2. The security protocol is important because it determines what tool you use to connect to the network.
— following image is a sample only —
Step 6: Generate a wpa/wpa2 configuration file – WiFi network from command line
Now we will generate a configuration file for wpa_supplicant that contains the pre-shared key (“passphrase“) for the WiFi network.
wpa_passphrase uses SSID as a string, that means you need to type in the passphrase for the WiFi network blackMOREOps after you run the command.
Note: If you’re using Ubuntu, Linux Mint, CentOS, Fedora etc. use the command with ‘sudo’ prefix
wpa_passphrase will create the necessary configuration entries based on your input. Each new network will be added as a new configuration (it wont replace existing configurations) in the configurations file /etc/wpa_supplicant.conf.
Step 7: Connect to WPA/WPA2 WiFi network – WiFi network from command line
Now that we have the configuration file, we can use it to connect to the WiFi network. We will be usingwpa_supplicant to connect. Use the following command
Where,
- -B means run wpa_supplicant in the background.
- -D specifies the wireless driver. wext is the generic driver.
- -c specifies the path for the configuration file.
Use the iw command to verify that you are indeed connected to the SSID.
Step 8: Get an IP using dhclient – WiFi network from command line
Until step 7, we’ve spent time connecting to the WiFi network. Now use dhclient to get an IP address by DHCP
You can use ip or ifconfig command to verify the IP address assigned by DHCP. The IP address is10.0.0.4 from below.
Add default routing rule.The last configuration step is to make sure that you have the proper routing rules.
Step 9: Test connectivity – WiFi network from command line
Ping Google’s IP to confirm network connection (or you can just browse?)
Conclusion
This is a very detailed and long guide. Here is a short summary of all the things you need to do in just few line.
At the end of it, you should be able to connect to WiFi network. Depending on the Linux distro you are using and how things go, your commands might be slightly different. Edit commands as required to meet your needs.