Connect to WIFI via command line in Linux environment

I. Introduction

When debugging ARTIK, because the Ubuntu system is not a graphical interface, it is found that it is not connected to the Internet when it needs to download the relevant installation package. Therefore, it summarizes the specific operation steps of using the command line to connect to wifi under Linux, and solves the related problems encountered in the operation process. The method is introduced, and some simple command lines in wireless network debugging under LINUX are summarized.

2. Operation steps

The following operation steps are performed in the administrator mode. You can first enter the administrator mode through su, or add sudo before the command line, otherwise it will prompt that the permissions are insufficient.

  1. Use the iwconfig command to view the information of the wireless network card, and confirm that the wireless network card exists and the corresponding driver has been installed:
	iwconfig

insert image description here

  1. Start the wireless network card item:

The wireless network card can be started by ifconfig wlan0 up or ip link set wlan0 up command

	ifconfig wlan0 up
	ip link set wlan0 up	

Use the ip link show wlan0 command to view the status of the wireless network card. If the keyword UP is displayed in < >, the wireless network card has been activated.

	ip link show wlan0 

insert image description here

  1. Scan for detected wireless networks:

Use iw dev wlan0 scan to search for nearby WIFIs, where SSID is the name of each WIFI; or use iw dev wlan0 scan | grep [SSID] to search for nearby WIFIs named [SSID]

	iw dev wlan0 scan
	iw dev wlan0 scan | grep [SSID]

insert image description here

  1. wireless network connection
  • unencrypted network

Connect directly through the following command line, SSID is the name of the WIFI to be connected

	iw dev wlan0 connect [SSID]
  • Low-level encrypted network (WEP encryption)

Connect through the following command line, SSID is the name of the WIFI to be connected, and enter the WEP key in the following location

	iw dev wlan0 connect [SSID] key 0:[WEP密钥]
  • WPA/WPA2 encrypted network

WPA/WPA2 encrypted network connection usually uses wpasupplicant to connect, if there is no wpasupplicant, it needs to be installed

	apt install wpasupplicant

After installing wpa_supplicant, enter the /etc/wpa_supplicant/ directory to see the wpa_supplicant.conf file

insert image description here

Edit wpa_supplicant.conf, here use the vim editor, must run in the administrator mode, and improve according to the following content (mainly improve the SSID and PSK in the network, where SSID is the WIFI name, PSK is the WIFI password, priority is in Connection priority can be set when there are multiple networks):

	ctrl_interface=/var/run/wpa_supplicant
	ctrl_interface_group=netdev
	update_config=1
	config_methods=push_button
	
	network={
    
    
	        ssid="[SSID name]"
	        psk="[WiFi password]"
	        priority=1
	}

Note: The content of ssid and psk must be included with " ", otherwise an error will be reported

insert image description here
:wq! Save the edited content and start wpa_supplicant from this configuration file

	wpa_supplicant -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf &

After the connection is successful, as shown in the figure below

insert image description here

  1. Verify the connection was successful

Verify whether the WIFI is successfully connected through ipconfig or iw dev wlan0 link

	iw dev wlan0 link

After the connection is successful, as shown in the figure below

insert image description here

3. Frequently asked questions

wpa_supplicant启动报错“Delete ‘/var/run/wpa_supplicant/wlan0’ manually if it is not used anymore”

After configuring wpa_supplicant.conf, run the command wpa_supplicant -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf & start wpa_supplicant, the error shown in the figure below appears

insert image description here

The reason for this error is that wpa_supplicant has been started or is occupied, stop the running wpa_supplicant through systemctl stop wpa_supplicant and restart it to work normally

	systemctl stop wpa_supplicant

4. Summary of command lines related to wireless network in Linux environment

	
	ifconfig wlan0 up		#打开无线网卡
	ifconfig wlan0 down		#关闭无线网卡
	ip link set wlan0 up	#打开无线网卡
	ip link set wlan0 down	#关闭无线网卡
	ip link show wlan0		#检测网卡状态 已激活网卡<>内显示包括UP的关键字
	iwconfig				#产看网卡信息
	iw dev					#检查可用网卡
	iw list					#列出WIFI网卡的性能
	iw dev wlan0 scan 		#扫描WIFI
	iw dev wlan0 scan | grep [SSID] #扫描名为SSID的WIFI 此处SSID输入期望的WIFI名
	iw dev wlan0 link		#查看连接状态
	iw dev wlan0 connect [SSID]		#不加密时可直接连接 此处SSID输入连接的WIFI名
	iw dev wlan0 connect [SSID]	key 0:[WEP密钥]	#WEP加密WIFI连接
	iw wlan0 connect linux	#连接WIFI:LINUX
	iw wlan0 disconnect		#关闭WIFI连接
	wpa_supplicant -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf & #WPA加密配置文件wpa_supplicant重启
	dhclient wlan0			#为网卡分配IP地址
	

Guess you like

Origin blog.csdn.net/weixin_43361652/article/details/128441233