树莓派系统安装、初始化步骤

系统安装

  1. 下载烧录工具

    1. Windows 版macOS 版Ubuntu X86 版
    2. 具体烧录步骤参考:烧录步骤
  2. 插入存储卡、通电

    第一次安装需要注意:
    1. 清除配置后烧录系统
    2. 通电开机后,需要连接显示器,进行配置(涉及到用户初始化、网络设置、语言选择、时区选择等)

系统初始化

登陆

通过shell远程登录树莓派
ssh sunhao@192.168.1.107
其中:sunhao为开机后设置的用户名

更换国内源

  1. 查看系统版本:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    sunhao@raspberrypi:/etc/apt $ cat /etc/os-release
    PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
    NAME="Debian GNU/Linux"
    VERSION_ID="12"
    VERSION="12 (bookworm)"
    VERSION_CODENAME=bookworm
    ID=debian
    HOME_URL="https://www.debian.org/"
    SUPPORT_URL="https://www.debian.org/support"
    BUG_REPORT_URL="https://bugs.debian.org/"
  2. 如上,根据 PRETTY_NAMEVERSION 字段,你可以确定树莓派的操作系统版本。

  3. 根据以上,可知系统版本为bookworm

    1. 备份原始文件

      sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
      sudo cp /etc/apt/sources.list.d/raspi.list /etc/apt/sources.list.d/raspi.list.bak
    2. 编辑软件源配置

      1
      2
      3
      4
      5
      6
      7
      1. 编辑 /etc/apt/sources.list 文件(软件源),删除原来的配置,用以下配置替换
      deb https://mirrors.tuna.tsinghua.edu.cn/debian bookworm main contrib non-free-firmware
      deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free-firmware
      deb https://mirrors.tuna.tsinghua.edu.cn/debian bookworm-updates main contrib non-free-firmware

      2. 编辑 /etc/apt/sources.list.d/raspi.list 文件(系统更新源),删除原来的配置,用以下配置替换
      deb https://mirrors.tuna.tsinghua.edu.cn/raspberrypi bookworm main
  4. 更新

    #更新软件源列表
    sudo apt-get update
    #更新软件版本
    sudo apt-get upgrade
    sudo apt-get dist-upgrade
  5. 对于其他系统版本,请阅读这篇文章

安装vim

  1. 安装

    sudo apt install -y vim
  2. 配置

    1. 安装插件

      # 安装Vundle
      git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
    2. vim .vimrc

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      91
      92
      93
      94
      95
      96
      97
      98
      99
      100
      101
      102
      103
      104
      105
      106
      107
      108
      109
      110
      111
      112
      113
      114
      115
      116
      117
      118
      119
      120
      121
      122
      123
      124
      125
      126
      127
      128
      129
      130
      131
      132
      133
      134
      135
      136
      137
      138
      139
      140
      141
      142
      143
      "显示行号
      set nu

      "启动时隐去援助提示
      set shortmess=atI

      "语法高亮
      syntax on

      "使用vim的键盘模式
      set nocompatible

      "不需要备份
      set nobackup

      "没有保存或文件只读时弹出确认
      set confirm

      "鼠标可用
      "set mouse=a

      "tab缩进
      set tabstop=4
      set shiftwidth=4
      set expandtab
      set smarttab

      "文件自动检测外部更改
      set autoread

      "c文件自动缩进
      set cindent

      "自动对齐
      set autoindent

      "智能缩进
      set smartindent

      "高亮查找匹配
      set hlsearch

      "显示匹配
      set showmatch

      "显示标尺,就是在右下角显示光标位置
      set ruler

      "去除vi的一致性
      set nocompatible

      "设置键盘映射,通过空格设置折叠
      nnoremap <space> @=((foldclosed(line('.')<0)?'zc':'zo'))<CR>
      """"""""""""""""""""""""""""""""""""""""""""""
      "不要闪烁
      set novisualbell

      "启动显示状态行
      set laststatus=2

      "浅色显示当前行
      autocmd InsertLeave * se nocul

      "用浅色高亮当前行
      autocmd InsertEnter * se cul

      "显示输入的命令
      set showcmd

      "被分割窗口之间显示空白
      set fillchars=vert:/
      set fillchars=stl:/
      set fillchars=stlnc:/

      " vundle 环境设置
      filetype off
      set rtp+=~/.vim/bundle/Vundle.vim
      " vundle 管理的插件列表必须位于 vundle#begin() 和 vundle#end() 之间
      call vundle#begin()
      Plugin 'VundleVim/Vundle.vim'
      Plugin 'tomasr/molokai'
      Plugin 'powerline/powerline'
      Plugin 'Raimondi/delimitMate'
      " 插件列表结束
      call vundle#end()
      filetype plugin indent on

      " 配色方案
      set background=dark
      colorscheme molokai

      " 禁止显示菜单和工具条
      set guioptions-=m
      set guioptions-=T

      " 总是显示状态栏
      set laststatus=2

      " 禁止折行
      set nowrap

      " 设置状态栏主题风格
      let g:Powerline_colorscheme='solarized256'

      syntax keyword cppSTLtype initializer_list

      " 基于缩进或语法进行代码折叠
      "set foldmethod=indent
      set foldmethod=syntax
      " 启动 vim 时关闭折叠代码
      set nofoldenable

      "允许用退格键删除字符
      set backspace=indent,eol,start

      "编码设置
      set encoding=utf-8

      "共享剪切板
      set clipboard=unnamed

      " powerline 配置
      set rtp+=/usr/local/lib/python2.7/site-packages/powerline/bindings/vim

      " These lines setup the environment to show graphics and colors correctly.
      set nocompatible
      set t_Co=256

      " vim 头部自动生成
      " 创建sh文件时,生成以下注释模板
      autocmd BufNewFile *.sh exec ":call AddTitle()"
      " 注释模板
      function AddTitle()
      call setline(1, "#!/bin/bash")
      call append(1, "# @Author: 孙昊(Crazy Coder)")
      call append(2, "# @Email: sunhao.java@gmail.com")
      call append(3, "# @Date: " . strftime("%Y-%m-%d %H:%M"))
      call append(4, "# @Desc: .")
      call append(5, "")
      endf
      " 光标跳转到最后一行,并且按下键盘的o键,进入下一行并且是编辑模式
      autocmd BufNewFile *.sh normal G
      autocmd BufNewFile *.sh normal o
    3. 安装插件

      vim
      shift+:
      PluginInstall

安装oh-my-zsh

  1. 安装zsh

    sudo apt install -y zsh
  2. 安装oh-my-zsh

    sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
  3. 安装插件

    git clone https://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions
    git clone https://github.com/zsh-users/zsh-syntax-highlighting $ZSH_CUSTOM/plugins/zsh-syntax-highlighting
  4. 切换主题、设置alias

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    vim ~/.zshrc
    1. 修改为如下(其他不动):
    ZSH_THEME="ys"
    plugins=(
    z
    git
    zsh-autosuggestions
    zsh-syntax-highlighting
    docker
    docker-compose
    )
    2. 文件最后,新增
    alias ll='ls -a -l -G --block-size=m'
    alias ld='ls -d -l */'
    3. 完成后:source ~/.zshrc

禁用密码登陆,使用ssh登录

  1. 修改ssh端口

    1. sudo vim /etc/ssh/sshd_config
    2. #Port 22 -> Port xxx
    3. sudo service ssh restart
  2. 防火墙放行端口

    sudo ufw allow xxx/tcp
    sudo ufw enable
    sudo ufw status
  3. 添加ssh pub key

    cd ~/.ssh
    sudo touch authorized_keys
    sudo chmod 600 authorized_keys
    # add pub key to authorized_keys
    sudo vim authorized_keys
  4. 禁用密码登录

    sudo vim /etc/ssh/sshd_config
    # 修改 PasswordAuthentication yes 为如下
    PasswordAuthentication no
文章目录
  1. 系统安装
  2. 系统初始化
    1. 登陆
    2. 更换国内源
    3. 安装vim
    4. 安装oh-my-zsh
    5. 禁用密码登陆,使用ssh登录
评论