Linux下实现WiFi无线网络连接(linux打开wifi)
Linux操作系统支持WiFi无线网络连接。这对用户来说是很方便的,不需要使用有线连接来获取外部网络,只需要设备配置Wifi功能并连接到相应的Wifi热点,就可以使用它访问网络了。下面就此给出Linux系统实现Wifi无线网络连接的步骤:
### 一、查看无线设备
使用`iwconfig`命令可以查看服务器有哪些Wifi无线网卡:
“`sh
# iwconfig
eth0 no wireless extensions.
lo no wireless extensions.
wlan0 IEEE 802.11bg ESSID:”OpenWrt” Nickname:””
Mode:Managed Frequency:2.462 GHz Access Point: 10:CA:B1:A1:83:0E
Bit Rate:54 Mb/s
RTS thr:off Fragment thr:off
Encryption key:off
Power Management mode:All packets received
Link Quality=100/100 Signal level=0 dBm
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:0 Invalid misc:0 Missed beacon:0
可以看到这里有一块`wlan0`的Wifi卡。
### 二、查看Wifi可用热点使用`iwlist`命令可以查看本地有哪些可用的Wifi热点:
# iwlist wlan0 scan
会输出如下信息:
``` Cell 01 - Address: 10-CA-B1-A1-83-0E
ESSID:"OpenWrt" Protocol:IEEE 802.11bg
Mode可以连接:Managed Frequency:2.462 GHz (Channel 11)
Quality=100/100 Signal level=0 dBm
可以看到,本地有一个叫`OpenWrt`的Wifi信号,它使用802.11bg协议和2.462 GHz频率(对应信道11),可以用来建立连接。
### 三、添加Wifi配置文件
在连接Wifi前还需要在`/etc/wpa_supplicant/wpa_supplicant.conf`中添加Wifi配置文件:
# /etc/wpa_supplicant/wpa_supplicant.conf
network={ ssid="OpenWrt"
psk="test12345"}
### 四、启用Wifi
使用`ifconfig`命令可以启用Wifi功能,使用`iwconfig`命令可以检测会否启用成功:
“`sh
# ifconfig wlan0 up
# iwconfig
eth0 no wireless extensions.
lo no wireless extensions.
wlan0 IEEE 802.11bg ESSID:”OpenWrt” Nickname:””
Mode:Managed Frequency:2.462GHz Access Point: 10:CA:B1:A1:83:0E
Bit Rate:54 Mb/s
RTS thr:off Fragment thr:off
Encryption key:test12345
Power Management:off
Link Quality=100/100 Signal level=0 dBm
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:0 Invalid misc:0 Missed beacon:0
### 五、连接Wifi使用`wpa_supplicant`命令可以连接 Wifi 热点:
# wpa_supplicant -Dwext -iwlan0 -c/etc/wpa_supplicant/wpa_supplicant.conf
Successfully initialized wpa_supplicant
wlan0: Associated with 10:CA:B1:A1:83:0E
wlan0: Authentication with 10:CA:B1:A1:83:0E timed out.
可以看到`wlan0`已经被正确连接了。
### 六、设置IP使用`dhclient`命令可以动态获取服务器的ip地址:
# dhclient wlan0
Internet Systems Consortium DHCP Client 4.2.2
Copyright 2004-2011 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Listening on LPF/wlan0/a0:f3:c1:2d:30:93
Sending on LPF/wlan0/a0:f3:c1:2d:30:93
Sending on Socket/fallback
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 3 (xid=0x490f55e2)
DHCPOFFER of 10.46.145.205 from 10.0.0.1
DHCPREQUEST of 10.46.145.205 on wlan0 to 10.0.0.1
DHCPACK of 10.46.145.205 from 10.0.0.1
RTNETLINK answers: File exists
bound to 10.46.145.205 — renewal in 1567 seconds.
启用 DHCP 后系统可以正常工作,并获取 10.46.145.205 这个 IP 地址。
最后,使用`ping`命令就可以和外部的网络端联通了:
# ping www.baidu.com
PING www.a.shifen.com (220.181.57.216) 56(84) bytes of data.
64 bytes from 220.181.57.216: icmp_seq=1 ttl=56 time=81.4 ms
64 bytes from 220.181.57.216: icmp_seq=2 ttl=56 time=106 ms
通过以上的步骤,Linux操作系统就可以无线形式连接到外部网络了。本文具体介绍了Linux系统实现Wifi无线网络连