How to connect wifi through the command line under Linux?

  

Usually, our computers are connected by broadband, so how to set up wireless wifi? The following small series will introduce you to how to connect to wifi through the command line under Linux system, let's learn together.

Whenever you want to install a new Linux distribution system, the general advice is to let you connect to the Internet via a wired connection.

There are two main reasons for this: First, your wireless network card may not be installed correctly and cannot be used. Second, if you are installing the system from the command line, managing WiFi is very terrible. I always try to avoid dealing with WiFi on the command line. But the world of Linux should have a fearless spirit.

is set by the command line to connect to WiFi, of course there are many ways, but in this article, is also a suggestion that I use the most basic way: it is used in any released version There are programs and tools included in the "default package". Or I prefer to use this method. The obvious benefit of using this method is that this process can be reused on any machine with a Linux system. The bad thing is that it is relatively complicated.

First, I assume that you have properly installed the drivers for your wireless network card. Without this premise, everything that follows is like a mirror. If your machine is not properly installed, you should check out the wiki and documentation for your release.

You can then use the following command to check which interfaces to support wireless connections:

$ iwconfig

In general, wireless interfaces are called Wlan0. There are of course exceptions, but in this tutorial we will always use the generic term.

Just in case, you have to confirm that this interface service is up:

$ sudo ip link set wlan0 up

Once the wireless interface is confirmed It is working, you can use the following command to scan the nearby wireless network:

$ sudo iw dev wlan0 scan |  less

The result of the scan can be obtained in the name of the network (which is the SSID), information on its strength, and which it uses secure encryption (eg: WEP, WPA /WPA2) . From this point on, there will be two routes: the situation is very good, the situation is easy, and the situation is slightly more complicated.

If the network you want to connect to is not encrypted, you can connect directly with the following command:

$ sudo iw dev wlan0 connect [network SSID]

If the network It is encrypted with WEP and is very easy:

$ sudo iw dev wlan0 connect [network SSID] key 0: [WEP key]

But if the network uses WPA or WPA2 protocol Things will not work well. In this case, you have to use a tool called wpasupplicant, which is not available by default. Then you need to modify the /etc/wpasupplicant/wpa_supplicant.conf file and add the following line:

network={ ssid=“[network ssid]” psk=“[password]” priority=1}< Br>

I recommend that you add it at the end of the file and make sure other configurations are commented out. Be aware that the SSID and password strings are case sensitive. Technically you can also use the name of the access point as the SSID. If you use the wpa_supplicant tool, you will have the appropriate SSID instead.

Once the configuration file has been modified, start this command in the background:

$ sudo wpa_supplicant -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf

Finally, regardless of Whether you are connected to an open network or an encrypted secure network, you must obtain an IP address. Simply use the following command:

$ sudo dhcpcd wlan0

If all goes well, you should have obtained a brand new local IP via DHCP, which is done automatically in the background. If you want to really connect on whether the lower confirmation, you can enter the following command to check again:

$ iwconfig

Finally, I think the first step of repeatedly checking operation is Very necessary. You never know when your graphical user interface is hanging, or when you can't access the wireless connection, so now you are ready to use commands to detect it. Also, as mentioned earlier, there are many ways to manage wireless connections (such as NetworkManager, wicd, netcfg, wifi, etc.). I insist on using the most basic way, but in some cases, the tools I use may not have you, so you have to download them before. On the other hand, there are a lot of advanced applications, they are not included in the "default package", using them will greatly simplify the operation process. But the general recommendation is to start from the basics.

The above is the way to connect wifi through the command line under Linux. The Linux system management wireless wifi is not simple, and it is necessary to develop good habits. Now try it out.

Copyright © Windows knowledge All Rights Reserved