目录

树莓派安装Raspbian系统后的一些设置

在启动前开启SSH

For headless setup, SSH can be enabled by placing a file named ssh, without any extension, onto the boot partition of the SD card. When the Pi boots, it looks for the ‘ssh’ file. If it is found, SSH is enabled, and the file is deleted. The content of the file does not matter: it could contain text, or nothing at all.

配置无线网络

在启动前配置

参考无屏幕和键盘配置树莓派WiFi和SSH

在电脑上插入SD卡,在树莓派系统的/boot分区(即能够被正常识别的分区)下新建文件wpa_supplicant.conf,树莓派启动后会自动读取该文件里的wifi配置信息。内容格式与/etc/wpa_supplicant/wpa_supplicant.conf相同。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
country=CN
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="your_ssid"
    psk="your_password"
    key_mgmt=WPA-PSK
    priority=1
}

在启动后配置

参考官方文档

切换到root用户

1
sudo su - root

使用wpa_passphrase命令生成配置

1
wpa_passphrase "your_ssid" "your_password" >> /etc/wpa_supplicant/wpa_supplicant.conf

使生效

1
wpa_cli reconfigure

配置免密码登录

1
2
cd ~ && mkdir .ssh
cd .ssh && touch authorized_keys

将客户端生成的公钥内容粘贴到authorized_keys文件中;

修改.ssh目录权限为700,authorized_keys文件权限为600,保证其他用户没有权限查看和修改该文件

1
2
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

修改更新源并更新

编辑/etc/apt/sources.list文件,注释官方源并添加国内源。

1
2
3
# tsinghua
deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ stretch main non-free contrib
deb-src http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ stretch main non-free contrib

系统更新:

1
2
sudo apt-get update
sudo apt-get upgrade -y

raspi-config配置

执行sudo raspi-config

  1. 修改pi用户密码,修改为强密码;
  2. 修改主机名;
  3. 本地化设置,修改Localeen_US utf-8zh_CN utf-8,并设置时区;
  4. 修改启动选项到CLI模式;
  5. 扩展文件系统;
  6. 关闭UART的Console模式,启用硬件模式,使UART可以正常连接外设;
  7. 按照需要开启或关闭SPII2C等接口;

安全加固

更改/etc/ssh/sshd_config文件:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 修改端口
Port 3154

# 禁用旧版本的有漏洞的 SSH 协议
Protocol 2

# 禁止root账户通过SSH登入
PermitRootLogin no

# 只允许白名单内的用户登入
AllowUsers pi

重新生成 private host ID keys,否则将会使用系统镜像文件中默认值,因为镜像是公开的,因此容易受到中间人攻击。

1
2
sudo rm /etc/ssh/ssh_host_*
sudo dpkg-reconfigure openssh-server

重启sshd——sudo service ssh restart

安装常用软件

  • zsh
  • git
  • oh-my-zsh

备份树莓派系统镜像

备份为镜像文件

将树莓派 SD 卡通过 USB 读卡器连接电脑,Mac/Linux(使用 gzip 压缩)下执行:

1
sudo dd if=/dev/disk2 | gzip > /Users/chiqingjun/Downloads/rasbian_stretch_gzip_20180425.img

缺点是,SD 卡容量有多大,备份出来的镜像文件就有多大,并且……备份速度很慢。

备份到其他SD卡(推荐)

Github上的一个开源项目 rpi-clone,可以将树莓派系统备份到另一张 SD 卡上(不确定另一张卡容量小于当前 SD 卡容量时是否可行)。备份 SD 卡插入可直接运行系统,方便快捷。

把 SD 卡通过 USB 读卡器连接到树莓派上,然后以下代码在树莓派上执行。

clone 代码

1
git clone https://github.com/billw2/rpi-clone.git

查看 SD 卡设备名,一般是 sda。

1
sudo fdisk -l

执行备份,注意目标设备不是分区名,而是设备名。-f 选项会格式化备份 SD 卡可加可不加。

1
2
cd rpi-clone
sudo ./rpi-clone -f [sdx]