connect Wifi in linux via cmd

All cmd run by root.

create config file via cmd:

1
wpa_passphrase SSIDofWifi passwordOfWifi > /etc/wpa_supplicant/wpa_supplicant.conf

modify config: /etc/wpa_supplicant/wpa_supplicant

1
2
3
4
5
6
7
8
9
10
11
12
ctrl_interface=/var/run/wpa_supplicant
ctrl_interface_group=root
network={
ssid="theSSID"
psk={encoded psk}
scan_ssid=1 # to connect to a hiden Wifi
proto=RSN
key_mgmt=WPA-PSK
group=CCMP
pairwise=CCMP
priority=10
}

connect to specific Wifi:

1
wpa_supplicant -c /etc/wpa_supplicant/wpa_supplicant.conf -i wlp7s0 -B

-B: run in background

if failed:

1
2
ps -aux | grep wpa # get pid of running wpa_supplicant instance
kill -9 {pid}

or just:

1
kill -9 wpa[press Tab]

get ip add via dhcp:

1
dhclient

todo:
wpa_supplicant.service

References

https://blog.csdn.net/u012503786/article/details/79541811/
https://blog.csdn.net/yunlong654/article/details/88655685
https://blog.csdn.net/u010049696/article/details/48765999