有个小需求,我希望能够随时随地开启家里的台式机,台式机安装了 centos7 跑了很多 kvm 实例,但是我只希望用的着的时候去开机,没事就关闭,省电(电费贵),家里的路由器是二级路由,不能用 nat 地址转发,也不能用ddns,路由器上我尝试使用 frp,但是没成功,从 github 上看应该是路由器的内核太老了。直接报错,不太好折腾。我还要上网。又不希望电脑一直开着,那样太费电。台式机功耗大。手上有个闲置的树莓派,我就拿来折腾了,上一篇写了怎么安装 Ubuntuserver,后来发现 Ubuntu 的 WiFi 我也折腾了没成功,还是换了官网的迷你版镜像,不带图形界面的,我不需图行界面,只要它安安静静的躺着工作就行。
写入镜像
[root@vm-50-156 opt]# dd bs=4M if=2017-04-10-raspbian-jessie-lite.img of=/dev/sdc
309+1 records in
309+1 records out
1297862656 bytes (1.3 GB) copied, 89.8965 s, 14.4 MB/s
##连接网络
搜索网络
pi@raspberrypi:~ $ sudo iwlist wlan0 scan
wlan0 Scan completed :
Cell 01 - Address: E4:F3:F5:87:D7:C6
Channel:1
Frequency:2.412 GHz (Channel 1)
Quality=26/70 Signal level=-84 dBm
Encryption key:on
ESSID:"SuperJBT"
Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 9 Mb/s
18 Mb/s; 36 Mb/s; 54 Mb/s
Bit Rates:6 Mb/s; 12 Mb/s; 24 Mb/s; 48 Mb/s
Mode:Master
Extra:tsf=0000000000000000
Extra: Last beacon: 90ms ago
IE: Unknown: 000853757065724A4254
IE: Unknown: 010882848B961224486C
IE: Unknown: 030101
IE: Unknown: 2A0104
IE: Unknown: 32040C183060
IE: Unknown: 2D1AEE1117FFFF000001000000000000000000000000000000000000
IE: Unknown: 3D1601050000000000000000000000000000000000000000
IE: WPA Version 1
Group Cipher : CCMP
Pairwise Ciphers (1) : CCMP
Authentication Suites (1) : PSK
IE: IEEE 802.11i/WPA2 Version 1
Group Cipher : CCMP
Pairwise Ciphers (1) : CCMP
Authentication Suites (1) : PSK
IE: Unknown: 7F080000000000000000
IE: Unknown: 0B05000000127A
IE: Unknown: DD180050F2020101000003A4000027A4000042435E0062322F00
IE: Unknown: 4A0E14000A002C01C800140005001900
IE: Unknown: 0706434E20010D10
IE: Unknown: DD07000C4303000000
Cell 02 - Address: 38:D5:47:2D:30:00
……
不过这个工具不太好用,可以使用一下命令
pi@raspberrypi:~ $ sudo wpa_cli
wpa_cli v2.3
Copyright (c) 2004-2014, Jouni Malinen <j@w1.fi> and contributors
This software may be distributed under the terms of the BSD license.
See README for more details.
Selected interface 'wlan0'
Interactive mode
> scan
OK
<3>CTRL-EVENT-SCAN-STARTED
<3>CTRL-EVENT-SCAN-RESULTS
> scan_results
bssid / frequency / signal level / flags / ssid
38:d5:47:2d:30:00 2442 -49 [WPA2-PSK-CCMP][ESS] wifi
38:d5:47:2d:30:01 2442 -55 [WPA2-PSK-CCMP][ESS] UPYUN
88:25:93:c7:a9:23 2472 -72 [WPA2-PSK-CCMP][ESS]
ec:26:ca:0e:49:70 2462 -81 [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS] chuanshi
8c:f2:28:58:b0:ea 2472 -84 [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS] MERCURY_B0EA
24:69:68:95:70:44 2412 -86 [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS] TP-LINK_17-3-501
a4:56:02:38:7d:32 2412 -88 [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS] Bao
9c:d2:4b:83:f2:40 2412 -89 [WPA-PSK-CCMP][WPS][ESS] ChinaNet-gsCD
40:16:9f:b0:ec:16 2437 -92 [WPA-PSK-CCMP][WPA2-PSK-CCMP][WPS][ESS] smile
>
搜索到 WiFi 后进行连接
> add_network
1
> set_network 1 ssid "ssid"
OK
> set_network 1 psk "passwd"
OK
> set_network 1 key_mgmt "WPA-PSK"
FAIL
> enable_network 1
OK
> save_config
OK
> quit
其实,他就是把配置写到了
pi@raspberrypi:~ $ sudo cat /etc/wpa_supplicant/wpa_supplicant.conf
先用有线连接获取到 ip 地址吧,然后在配置 WiFi
sudo vi /etc/wpa_supplicant/wpa_supplicant.conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=GB
network={
ssid="SSID"
psk="password" #密码带特殊字符会连不上
key_mgmt=WPA-PSK
}
好了 尽情折腾吧。
pi@raspberrypi:~ $ ifconfig
eth0 Link encap:Ethernet HWaddr b8:27:eb:c8:b3:c8
inet addr:192.168.50.77 Bcast:192.168.50.255 Mask:255.255.255.0
inet6 addr: fe80::a986:200c:9f34:dbde/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:76 errors:0 dropped:0 overruns:0 frame:0
TX packets:59 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:12270 (11.9 KiB) TX bytes:9811 (9.5 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
wlan0 Link encap:Ethernet HWaddr b8:27:eb:9d:e6:9d
inet addr:192.168.50.86 Bcast:192.168.50.255 Mask:255.255.255.0
inet6 addr: fe80::a644:42d3:dbd5:df25/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:160 errors:0 dropped:107 overruns:0 frame:0
TX packets:48 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:28311 (27.6 KiB) TX bytes:8098 (7.9 KiB)
踩到的坑
默认情况下运行
sudo raspi-config
然后开启 ssh 是可以连接的,但是如果你一来接上图形界面就修改了默认的密码
默认用户是pi 密码为raspberry
就会被坑了,如果你的密码恰好包含了@之类的,就会一直提示你密码错误。
无线密码也不要使用@。也会踩坑
可以通过 以下命令给密码加密
pi@raspberrypi:~ $ wpa_passphrase "ssid" "password"
network={
ssid="ssid"
#psk="password"
psk=44116ea881531996d8a23af58b376d70f196057429c258f529577a26e727ec1b
}
最好的办法就是使用其他终端软件连接,先用默认用户名登陆 然后在改密码。
可以参考官网的:
https://www.raspberrypi.org/documentation/configuration/wireless/wireless-cli.md
还有:http://rickgray.me/2015/08/03/useful-command-tool-for-wifi-connection.html
连接外网
参考我之前写的 frp https://awen.me/archives/687.html
远程唤醒
由于系统是大便系,所以,需要安装wakeonlan
sudo apt-get install wakeonlan
然后写个脚本赋予可执行权限扔/usr/bin/下就可了
pi@raspberrypi:~ $ cat /usr/bin/wakeonlan-156
#!/bin/bash
wakeonlan 14:dd:a9:ea:0b:96
其实吧,树莓派1g 内存,只用这么功能还是有点浪费,想想折腾点啥好。
另外,我直接不接电源,把 USB 接口插在路由器上,不然感觉自带的电源太吵了。