目录

树莓派

树莓派

1 概述

1.1 树莓派zero w

1.2 安装系统-旧版

#MAC 给sd卡安装系统
下载系统镜像地址: https://www.raspberrypi.org/downloads/
我选择radpbian最新版
wget http://120.52.51.14/director.downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2018-11-15/2018-11-13-raspbian-stretch-lite.zip  ~/download/

unzeip  raspbian_lite-2018-11-15/2018-11-13-raspbian-stretch-lite.zip

-rw-r--r--@ 1    1.7G Nov 13 14:02 2018-11-13-raspbian-stretch-lite.img
查看sd卡底盘分区

-w133

-w924 -w420 -w473

➜  df -h
➜  diskutil unmount /dev/disk3s1
Volume RASPBIAN on disk3s1 unmounted
➜ diskutil list

#使用dd命令将系统镜像写入,需要特别特别注意disk后的数字,不能搞错!
(说明:/dev/disk2s1是分区,/dev/disk2是块设备,/dev/rdisk2是原始字符设备)
# 用dd写系统到sd卡
➜  sudo dd bs=4m if=2018-11-13-raspbian-stretch-lite.img of=/dev/rdisk3
Password:
445+0 records in
445+0 records out
1866465280 bytes transferred in 27.727287 secs (67315106 bytes/sec)

➜ diskutil unmountDisk /dev/disk3

将sd卡接入树莓派这个位置,按进去卡槽卡紧便可

-w761

  • 以前安装过系统的盘,格式化重新安装系统
diskutil unmount /dev/disk3s1

-w848

#使用指令dd覆盖磁盘的第一个扇区512个字节
➜  ~ sudo dd bs=512 count=1 if=/dev/zero of=/dev/rdisk3
Password:
1+0 records in
1+0 records out
512 bytes transferred in 0.000280 secs (1829202 bytes/sec)

#重新插拔sd卡,会提示报错

-w427

点击初始化

-w909

点击抹掉,就和之前步骤相同

-w685

1.2 安装系统-新版

github上找到一个脚本,刷系统很方便,已测试可用
wget https://raw.githubusercontent.com/wangbokun/Raspberry-PI-SD-Installer-OS-X/master/install

➜  2020-02-13-raspbian-buster.zip ll
total 7397384
-rw-r--r--  1 bokun.wang  FAREAST\Domain Users   3.5G  2 13 16:16 2020-02-13-raspbian-buster.img
-rwxr-xr-x@ 1 bokun.wang  FAREAST\Domain Users   3.0K  2 21 13:29 install
➜  2020-02-13-raspbian-buster.zip sudo ./install  2020-02-13-raspbian-buster.img
Password:
Sorry, try again.
Password:

__________.___  .___                 __         .__  .__                
\______   \   | |   | ____   _______/  |______  |  | |  |   ___________ 
 |     ___/   | |   |/    \ /  ___/\   __\__  \ |  | |  | _/ __ \_  __ \
 |    |   |   | |   |   |  \___ \  |  |  / __ \|  |_|  |_\  ___/|  | \/
 |____|   |___| |___|___|  /____  > |__| (____  /____/____/\___  >__|   
                         \/     \/            \/               \/       

------------------------------------------------------------------------------------
   Filesystem      Size   Used  Avail Capacity iused               ifree %iused  Mounted on
------------------------------------------------------------------------------------
1) /dev/disk1s1   234Gi  216Gi  9.9Gi    96% 3536725 9223372036851239082    0%   /
2) devfs          199Ki  199Ki    0Bi   100%     689                   0  100%   /dev
3) /dev/disk1s4   234Gi  6.0Gi  9.9Gi    38%       6 9223372036854775801    0%   /private/var/vm
4) map -hosts       0Bi    0Bi    0Bi   100%       0                   0  100%   /net
5) map auto_home    0Bi    0Bi    0Bi   100%       0                   0  100%   /home
6) map -fstab       0Bi    0Bi    0Bi   100%       0                   0  100%   /Network/Servers
7) /dev/disk3s1    30Gi  2.4Mi   30Gi     1%       0                   0  100%   /Volumes/UNTITLED
------------------------------------------------------------------------------------

Select the disk to use by enetering the disk number.
*** MAKE SURE YOU SELECT THE CORRECT DISK ***
*** Refer to the Readme if uncertain ***

Use disk [ 1, 2, 3, 4, 5, 6, 7 ] #7
Unmounting Disk
Volume UNTITLED on disk3s1 unmounted
Writing image
Ctrl+T to see progress..
3612+0 records in
3612+0 records out
3787456512 bytes transferred in 111.627002 secs (33929573 bytes/sec)
Ejecting Disk
Disk /dev/rdisk3 ejected
All Done!

安装完之后如果是无显示器,usb启动的话,请执行 1.3内容

-w710

1.3 usb/以太网方式链接电脑 (mac os)

run script: https://github.com/raspberrypitools/usb_ethernet
修改两个配置文件
#1. /Volumes/boot/config.txt 在文件末尾添加一行 dtoverlay=dwc2
#2. /Volumes/boot/cmdline.txt  rootwait,在后面插入 modules-load=dwc2,g_ether
#3. 在根目录创建一个名为 ssh 文件
touch /Volumes/boot/ssh

ssh pi@raspberrypi.local
Username: pi
Password: raspberry

-w675

-w669

【ERROR 未解决】 -w404

1.4 无屏模式wifi 配置

首先搜索Realtek驱动:
apt-cache search realtek

默认网络不通,先下载deb,拷贝到树莓派
https://opsx.alibaba.com/mirror/search?q=ras&lang=zh-CN
下载后从宿主机拷贝到树莓派系统里面.
#安装
pi@raspberrypi:~ $ sudo dpkg -i  /tmp/firmware-realtek_20190114-1_all.deb
dpkg: warning: downgrading firmware-realtek from 1:20161130-3+rpt4 to 20190114-1
(Reading database ... 34605 files and directories currently installed.)
Preparing to unpack .../firmware-realtek_20190114-1_all.deb ...
Unpacking firmware-realtek (20190114-1) over (1:20161130-3+rpt4) ...
Setting up firmware-realtek (20190114-1) ...
update-initramfs: deferring update (trigger activated)
Processing triggers for initramfs-tools (0.130) ...

-w1391

pi@raspberrypi:~ $ iwlist wlan0 scan 【扫描附近wifi热点】
#修改配置文件
pi@raspberrypi:~ $ sudo cat /etc/network/interfaces
source-directory /etc/network/interfaces.d
allow-hotplug wlan0
auto wlan0

iface wlan0 inet dhcp
#ssid和pass 可以双引号,也可以不加双引号
wpa-ssid xxxxx
wpa-psk xxxxx

#重启网卡
pi@raspberrypi:~ $ sudo ifdown wlan0
pi@raspberrypi:~ $ sudo ifup wlan0
Internet Systems Consortium DHCP Client 4.3.5
Copyright 2004-2016 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/wlan0/b8:27:eb:1c:78:28
Sending on   LPF/wlan0/b8:27:eb:1c:78:28
Sending on   Socket/fallback
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 8
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 18
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 15
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 15
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 5
No DHCPOFFERS received.
No working leases in persistent database - sleeping.
Internet Systems Consortium DHCP Client 4.3.5
Copyright 2004-2016 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/wlan0/b8:27:eb:1c:78:28
Sending on   LPF/wlan0/b8:27:eb:1c:78:28
Sending on   Socket/fallback
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 5
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 14
DHCPREQUEST of 10.111.161.210 on wlan0 to 255.255.255.255 port 67
DHCPOFFER of 10.111.161.210 from 10.111.160.1
DHCPACK of 10.111.161.210 from 10.111.160.1
Too few arguments.
Too few arguments.
bound to 10.111.161.210 -- renewal in 37422 seconds.


pi@raspberrypi:~ $ ping baidu.com
PING baidu.com (123.125.115.110) 56(84) bytes of data.
64 bytes from 123.125.115.110 (123.125.115.110): icmp_seq=1 ttl=51 time=52.7 ms
64 bytes from 123.125.115.110 (123.125.115.110): icmp_seq=2 ttl=51 time=8.41 ms
64 bytes from 123.125.115.110 (123.125.115.110): icmp_seq=3 ttl=51 time=8.37 ms

1.4.1 networkmanager连接wifi

sudo apt-get install network-manager
#查看网络设备
sudo nmcli dev
#开启wifi
sudo nmcli r wifi on
#扫描附近wifi热点
sudo nmcli dev wifi
#链接到指定wifi
sudo nmcli dev wifi connect "SSID" password "PASSWORD"

1.5 修改时区

pi@raspberrypi:~ $ sudo mv /etc/localtime /etc/localtime.bak
pi@raspberrypi:~ $ sudo ln -s  /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime
pi@raspberrypi:~ $ sudo ls -al /etc/localtime
lrwxrwxrwx 1 root root 33 Mar 18 21:06 /etc/localtime -> /usr/share/zoneinfo/Asia/Shanghai
pi@raspberrypi:~ $ date
Mon 18 Mar 21:07:06 CST 2019

1.5 提升swap

pi@raspberrypi:/var $ sudo swapoff /var/swap
pi@raspberrypi:/var $ free
              total        used        free      shared  buff/cache   available
Mem:         443872       32012      119500        5920      292360      354264
Swap:             0           0           0
 
pi@raspberrypi:/var $ sudo dd if=/dev/zero of=swap bs=1M count=10240
10240+0 records in
10240+0 records out
10737418240 bytes (11 GB, 10 GiB) copied, 553.609 s, 19.4 MB/s

pi@raspberrypi:/var $ sudo mkswap swap
Setting up swapspace version 1, size = 10 GiB (10737414144 bytes)
no label, UUID=f05f671a-8650-4b43-8ac4-3de93b712695

pi@raspberrypi:/var $ sudo swapon /var/swap

pi@raspberrypi:/var $ swapon -s
Filename				Type		Size	Used	Priority
/var/swap                              	file    	10485756	0	-2

pi@raspberrypi:/var $ free -g
              total        used        free      shared  buff/cache   available
Mem:              0           0           0           0           0           0
Swap:             9           0           9


pi@raspberrypi:/var $ sudo cat /etc/fstab
...
#添加swap开启自挂载
/var/swap    swap     swap    defaults      0 1

1.6 创建内存盘

#将频繁读写的缓存等小文件放到内存,减少对SD卡的寿命消耗
mkdir /ram
mount -t tmpfs -o size=10m,mode=0777 tmpfs /ram

1.7 查找局域网树莓派的ip地址

方法一
➜  ~ ping raspberrypi.local
PING raspberrypi.local (10.111.161.210): 56 data bytes
64 bytes from 10.111.161.210: icmp_seq=0 ttl=64 time=86.265 ms
64 bytes from 10.111.161.210: icmp_seq=1 ttl=64 time=171.123 ms
64 bytes from 10.111.161.210: icmp_seq=2 ttl=64 time=7.914 ms

ssh pi@10.111.161.210

方法二:
sudo nmap -sP 192.168.1.0/24 | awk '/^Nmap/{ip=$NF}/B8:27:EB/{print ip}'

1.8 vnc

sudo raspi-config
#选择5

-w561

-w562

-w424

-w422

-w560

-w415

下载vnc客户端:
#https://www.realvnc.com/en/connect/download/vnc/macos/

vncserver

-w1149

-w593 -w123 -w893

或者链接这个地址:raspberrypi.local:1
输入用户名密码,便可登录

-w589

1.9 LT猫拨号映射公网

#首先说明下国内liantong网络默认是猫拨号->路由器->电脑
#所以要做两层NAT才可以出去.当然你可以联系客服让做成路由器直接拨号方式.

联通映射出去的地址,不能自访问,所以用手机4G网络来测试结果

#第一层NAT
登录路由器: http://192.168.0.1
高级设置->高级用户->虚拟服务器->添加端口映射

-w1254

-w1179

#第二层NAT
上图可以查到猫地址:http://192.168.1.1
登录网页,用户名密码,在猫设备背面贴纸上.这里就不展示自己找

-w966

#应用->高级NAT配置->虚拟主机配置
这里是映射到内网路由器的地址,
配置完之后,用其他网络来访问测试.成功

-w928

1.10 apt-get 国内源

对于使用buster的树莓派系统:
    
1、打开文件
    
sudo vi /etc/apt/sources.list
    
2、编辑文件
将原来的deb.......和deb-src......两行注释掉,或者直接在这两行修改。
    
deb http://mirrors.aliyun.com/raspbian/raspbian/ buster main contrib non-free rpi
deb-src http://mirrors.aliyun.com/raspbian/raspbian/ buster main contrib non-free rpi

3、dns修改 
 sudo vi /etc/resolv.conf
 nameserver 114.114.114.114

4、执行更新软件源    
sudo apt-get update

1.11 挂载移动硬盘

sudo fdisk -l
sudo mkdir /data
sudo mount /dev/sda2 /data/

-w566

【ERROR】
 /data touch a
touch: cannot touch 'a': Read-only file system

【解决方法】
默认不支持hfs格式,这个格式是mac os下面的格式.
sudo mount -t hfsplus -o force,rw /dev/sda2 /data

2

2.1 type-c

2.2 usb