CentOS 命令行下连接加密模式为 WPA/WPA2-PSK 的无线网络 – 周丕中的生活技术博客

记录了 CentOS 最小化安装后如何配置和连接无线网络,当时的情况是路由器使用的 WEP 加密方式,所以设置密码的时候可以直接使用明文密码。

后来当把路由器的加密模式设置为 WPA/WPA2-PSK的时候,发现已经无法连接无线网络了,原因是当使用 WPA/WPA2-PSK 加密模式之后客户端不能直接发送明文密码,而是要使用加密之后的密码,既然我们知道了原因我们该怎么做呢。

首先我们要安装一个帮我们生成加密密码并配置网卡的工具包 wpa_supplicant

yum install -y wpa_supplicant

然后找到我们路由的 ESSID,比如我是 long,然后生成密码配置文件,

<span class="pln">wpa_passphrase </span><span class="kwd">long</span><span class="pln"> </span><span class="str">'my password'</span>

它会输出如下内容

<span class="pln">network</span><span class="pun">={</span><span class="pln"></p><p>	ssid</span><span class="pun">=</span><span class="str">"long"</span><span class="pln"></p><p>	</span><span class="com">#psk="my password"</span><span class="pln"></p><p>	psk</span><span class="pun">=</span><span class="lit">350fb537ccec9b6de427eb4a43b7e02ae0492a59ce0a095ac6b527aef2c40f94</span><span class="pln"></p><p></span><span class="pun">}</span>

将输入的内容追加到 /etc/wpa_supplicant/wpa_supplicant.conf

或者直接执行

<span class="pln">wpa_passphrase </span><span class="kwd">long</span><span class="pln"> </span><span class="str">'my password'</span><span class="pln"> </span><span class="pun">&gt;&gt;</span><span class="pln"> </span><span class="str">/etc/</span><span class="pln">wpa_supplicant</span><span class="pun">/</span><span class="pln">wpa_supplicant</span><span class="pun">.</span><span class="pln">conf</span>

再把其中 ssid= 和 psk= 加入到wlan0 的配置文件,或执行一下命令

<span class="pln">wpa_passphrase </span><span class="kwd">long</span><span class="pln"> </span><span class="str">'my password'</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> grep </span><span class="pun">-</span><span class="pln">v </span><span class="str">'{\|}'</span><span class="pln"> </span><span class="pun">&gt;&gt;</span><span class="pln"> </span><span class="str">/etc/</span><span class="pln">sysconfig</span><span class="pun">/</span><span class="pln">network</span><span class="pun">-</span><span class="pln">scripts</span><span class="pun">/</span><span class="pln">ifcfg</span><span class="pun">-</span><span class="pln">wlan0</p><p>echo </span><span class="str">'WPA=yes'</span><span class="pln"> </span><span class="pun">&gt;&gt;</span><span class="pln"> </span><span class="str">/etc/</span><span class="pln">sysconfig</span><span class="pun">/</span><span class="pln">network</span><span class="pun">-</span><span class="pln">scripts</span><span class="pun">/</span><span class="pln">ifcfg</span><span class="pun">-</span><span class="pln">wlan0</span>

我的配置文件内容如下:

<span class="pln">DEVICE</span><span class="pun">=</span><span class="pln">wlan0</p><p>TYPE</span><span class="pun">=</span><span class="typ">Ethernet</span><span class="pln"></p><p>ONBOOT</span><span class="pun">=</span><span class="pln">yes</p><p>NM_CONTROLLED</span><span class="pun">=</span><span class="pln">yes</p><p>BOOTPROTO</span><span class="pun">=</span><span class="kwd">static</span><span class="pln"></p><p>IPADDR</span><span class="pun">=</span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">1.5</span><span class="pln"></p><p>NETMASK</span><span class="pun">=</span><span class="lit">255.255</span><span class="pun">.</span><span class="lit">255.0</span><span class="pln"></p><p>GATEWAY</span><span class="pun">=</span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">1.1</span><span class="pln"></p><p>WPA</span><span class="pun">=</span><span class="pln">yes</p><p>SSID</span><span class="pun">=</span><span class="str">"long"</span><span class="pln"></p><p>PSK</span><span class="pun">=</span><span class="lit">350fb537ccec9b6de427eb4a43b7e02ae0492a59ce0a095ac6b527aef2c40f94</span>

然后,我们可以启动网络设备

<span class="pln">ifup wlan0</span>

再启动 wpa_supplicant

<span class="pln">wpa_supplicant </span><span class="pun">-</span><span class="pln">iwlan0 </span><span class="pun">-</span><span class="pln">B </span><span class="pun">-</span><span class="pln">c </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">wpa_supplicant</span><span class="pun">/</span><span class="pln">wpa_supplicant</span><span class="pun">.</span><span class="pln">conf</span>

我们需要随机启动的话,加入到 /etc/rc.local

<span class="pln">cat </span><span class="pun">&gt;&gt;</span><span class="pln"> </span><span class="str">/etc/</span><span class="pln">rc</span><span class="pun">.</span><span class="kwd">local</span><span class="pun">&lt;&lt;</span><span class="pln">EOF</p><p>wpa_supplicant </span><span class="pun">-</span><span class="pln">iwlan0 </span><span class="pun">-</span><span class="pln">B </span><span class="pun">-</span><span class="pln">c </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">wpa_supplicant</span><span class="pun">/</span><span class="pln">wpa_supplicant</span><span class="pun">.</span><span class="pln">conf</p><p>EOF</span>

参考:http://wiki.centos.org/zh/HowTos/Laptops/WpaSupplicant

来源URL:http://zpz.name/2292/