侧边栏壁纸
  • 累计撰写 185 篇文章
  • 累计创建 77 个标签
  • 累计收到 17 条评论

目 录CONTENT

文章目录

Linux中通过命令连接指定WiFi

码峰
2022-08-21 / 0 评论 / 0 点赞 / 908 阅读 / 1,029 字 / 正在检测是否收录...
广告 广告

前言

Linux中,尤其是在没有GUI界面的Linux系统中,需要连接WiFi时只能通过命令来完成,大致过程如下:

安装软件

连接WiFi需要用到iw,wpa_supplicant和net-tools中的一些工具,一些嵌入型Linux中可能已经在生成系统时集成了这些工具,以yum包管理为例,安装命令如下。

yum -y install iw
yum -y install wpa_supplicant
yum -y install net-tools

查看无线网接口

iw dev

查看接口连接信息

加入iw dev命令查询到的无线网卡名称为wlan0,通过一下命令查看该网卡的连接信息:

iw wlan0 link

启用WiFi网卡

ip link set dev wls2 up

扫描当前范围的WiFi热点

iw dev wlan0 scan |grep SSID

连接指定WiFi热点

wpa_supplicant -B -i wlan -c <(wap_passphrase "ssid" "password")

这里是通过wpa_supplicant命令直接连接指定的ssid,重启系统后,这个连接信息并不能自动连接,如果需要保存连接,使得系统每次重启后都能自动连接指定WiFi,可以把连接信息写入wpa_supplicant.conf文件中,具体操作如下:

ctrl_interface=/var/run/wpa_supplicant
ctrl_interface_group=wheel
update_config=1
 
network={
	ssid="xxxx"
	scan_ssid=1
	psk=xxxxxxxxx37bca5cf24a345f514d319211822f568bba28f8f0b74c894e7644
	proto=RSN
	key_mgmt=WPA-PSK
	pairwise=CCMP
	auth_alg=OPEN
}

解释一下上面的比较容易困惑的地方:
签名三句应该是个模板,没仔细研究过。

network=开始是无线接入点的具体配置,一般的无线接入点可以用wpa_passphrase来自动生成,语法是
继续解释最开始的例子,network=后面的内容
ssid :接入点名称,这个没什么好说的
scan_ssid=1 :这个很容易错过,如果你的无线接入点是隐藏的,那么这个就是必须的,亲身经历,折腾了好久才搞定,记之备忘。
psk=xx :是加密后的密码,用wpa_passphrase自动生成的
proto=RSN WPA2就选这个,参考官方配置文件例子里面的:

# proto: list of accepted protocols ---支持的协议列表
# WPA = WPA/IEEE 802.11i/D3.0  
# RSN = WPA2/IEEE 802.11i (also WPA2 can be used as an alias for RSN) ---也能使用WPA2,它只是RSN的一个别名而已
# If not set, this defaults to: WPA RSN   ---如果不设置,默认就是WPA RSN,即全部支持

key_mgmt= 认证密钥管理协议:

# key_mgmt: list of accepted authenticated key management protocols  ---支持的协议列表
# WPA-PSK = WPA pre-shared key (this requires 'psk' field)     ---一般都是这个,这就包括了WPA、WPA2开始的那些方式
# WPA-EAP = WPA using EAP authentication    ---这个就是WEP开头的,猜的,求验证
# IEEE8021X = IEEE 802.1X using EAP authentication and (optionally) dynamically
#	generated WEP keys
# NONE = WPA is not used; plaintext or static WEP could be used  ---这个是开放的,没密码,联通、电信之类的就这个
# WPA-PSK-SHA256 = Like WPA-PSK but using stronger SHA256-based algorithms
# WPA-EAP-SHA256 = Like WPA-EAP but using stronger SHA256-based algorithms
# If not set, this defaults to: WPA-PSK WPA-EAP ---如果未设置,默认支持WAP、WEP开头那些

pairwise= 这个就是加密方式:

# pairwise: list of accepted pairwise (unicast) ciphers for WPA   ---WPA可用的加密方式列表
# CCMP = AES in Counter mode with CBC-MAC [RFC 3610, IEEE 802.11i/D7.0]  ---看到没,这个就是AES,换了马甲而已
# TKIP = Temporal Key Integrity Protocol [IEEE 802.11i/D7.0] ---TKIP 这个倒是没变
# NONE = Use only Group Keys (deprecated, should not be included if APs support
#	pairwise keys)   ---这个估计很少用
# If not set, this defaults to: CCMP TKIP  ---不设置的话是CCMP TKIP,看似正确,其实有些路由器无法自动识别,只能二选一,这里有点坑。

将以上配置文件保存为/etc/wpa_supplicant/wpa_supplicant.conf,然后执行以下命令即可链接。

wpa_supplicant -B -i wlan -c /etc/wpa_supplicant/wpa_supplicant.conf

通过DHCP获取IP地址

dhclient wlan0

查看ip

ip addr show wlan0
#或者ifconfig列出所有网卡的配置信息
ifconfig
0
广告 广告

评论区