一、配置无线的注意事项
1 树莓派主要是用来折腾的,所以难免有些bug , 不像正规的Linux发行版,遇到问题都有正规的解决方案。无线网络的配置需要编辑/etc/dhcpcd.conf文件才能生效,而/etc/network/interfaces这个文件配置ip是不会生效的,这种情况适用于raspberry Jessie版本,而另一个常用的wheezy版本则相反。
具体诡异的解释请看 https://blog.csdn.net/yjbaobo/article/details/75146840
2 无线需要先关联硬件,再配置网络信息,如ip、网关、DNS等。
二、配置过程
1 配置硬件关联
编辑/etc/wpa_supplicant/wpa_supplicant.conf,填入内容如下,
	pi@raspberrypi:~ $ sudo cat /etc/wpa_supplicant/wpa_supplicant.conf
	country=GB
	ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
	update_config=1
	network={
	        ssid="S6"
	        psk="11111111"
	    priority=5
	 }
	network={
	        ssid="CONUNDRUM"
	        psk="11111111"
	    scan_ssid=1
	       # key_mgmt=WPA-PSK
	    priority=3
	 }
	network={
	        ssid="2HND"
	    scan_ssid=1
	        psk="11111111"
	       # key_mgmt=WPA-PSK
	    priority=1
	 }
ssid --- WIFI名称
psk --- WIFI密码
priority --- 优先级,越大优先级越低
scan_ssid --- 是否扫描隐藏网络,为1表示扫描,如果你的无线隐藏了,需要开启此选项
2 配置网络信息
编辑/etc/dhcpcd.conf文件,按照以下格式填入,
	pi@raspberrypi:~ $ sudo cat /etc/dhcpcd.conf  
	# A sample configuration for dhcpcd.
	# See dhcpcd.conf(5) for details.
	# Allow users of this group to interact with dhcpcd via the control socket.
	#controlgroup wheel
	# Inform the DHCP server of our hostname for DDNS.
	hostname
	# Use the hardware address of the interface for the Client ID.
	clientid
	# or
	# Use the same DUID + IAID as set in DHCPv6 for DHCPv4 ClientID as per RFC4361.
	#duid
	# Persist interface configuration when dhcpcd exits.
	persistent
	# Rapid commit support.
	# Safe to enable by default because it requires the equivalent option set
	# on the server to actually work.
	option rapid_commit
	# A list of options to request from the DHCP server.
	option domain_name_servers, domain_name, domain_search, host_name
	option classless_static_routes
	# Most distributions have NTP support.
	option ntp_servers
	# Respect the network MTU.
	# Some interface drivers reset when changing the MTU so disabled by default.
	#option interface_mtu
	# A ServerID is required by RFC2131.
	require dhcp_server_identifier
	# Generate Stable Private IPv6 Addresses instead of hardware based ones
	slaac private
	# A hook script is provided to lookup the hostname if not set by the DHCP
	# server, but it should not be run by default.
	nohook lookup-hostname
	   interface eth0
	      static ip_address=172.17.2.176/24
	      static routers=172.17.2.254
	      static domain_name_servers=223.6.6.6
	      static domain_name_servers=223.5.5.5
	   interface wlan0
	      dhcp
	      #static ip_address=172.17.2.177/24
	      #static routers=172.17.2.254
	      #static domain_name_servers=223.6.6.6
	      #static domian_name_servers=223.5.5.5
	
	我配置的是自动获取ip,选择的是dhcp
如果你想设置静态ip,可以使用上面注释掉的static示例
3 配置开机启动
原本以为在rc.local里敲几个命令实现开机配置无线网络,可是有BUG,每次都无法生效。
使用另外一个方法,在/etc/init.d目录建立一个脚本,内容如下,
	pi@raspberrypi:~ $ cat /etc/init.d/init-app
	#!/bin/sh
	#/etc/init.d/init-app
	 
	### BEGIN INIT INFO
	# Provides:          init app
	# Required-Start:    $remote_fs $syslog
	# Required-Stop:     $remote_fs $syslog
	# Default-Start:     2 3 4 5
	# Default-Stop:      0 1 6
	# Short-Description: init-app
	# Description: This service is used to start init shell
	### END INIT INFO
	case "$1" in
	    start)
	        echo "Starting init.sh"
	        sh /srv/sh/init.sh
	        ;;
	    stop)
	        echo "Stop"
	        ;;
	        
	    *)
	        echo "Usage: service init-app start|stop"
	        exit 1
	        ;;
	esac
	exit 0
	
	加上执行权限,
pi@raspberrypi:~ $ sudo chmod +x /etc/init.d/init-app
具体的启动脚本放在/srv/sh/init.sh里,内容如下,
	pi@raspberrypi:~ $ cat /srv/sh/init.sh
	#!/bin/sh
	LOG="/var/log/init.log"
	LOG_SIZE="${du -m ${LOG} 2>/dev/null | awk '{print $1}'}"
	host="peloo.cn"
	echo -e "\n----------------$(date)\n" >>${LOG}
[ -e ${LOG} ] || touch ${LOG}
	#empty LOG file if > 10MB
	[ ${LOG_SIZE} -gt 6 ] && >${LOG}
start_wlan() {
	echo -e "\n----------------$(date)\n" >>${LOG}
	wpa_supplicant -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf  >>${LOG}
	dhcpcd wlan0 >>${LOG}
}
start_wlan
	echo -e "\n----------------$(date)\n" >>${LOG}
	    
	exit 0
蓝色部分解释:
A 关联无线设备
B 调用dhcpcd配置网络
此时配置完毕,重启树莓派,sudo init 6 , 查看ip地址如下,
	 
查看硬件关联情况,
	 
至于后面无线稳不稳定,还需要长期观察,如果出问题,大不了编写个脚本自动重连。
PS: 树莓派真够折腾人的。




