Debian虚拟机精简系统初始化

在QEMU中运行debian 采用了只选择安装 SSH Server 和基本运维工具包。完成后进行初始化

备注

Debian/Ubuntu 不允许普通用户账号设置为 admin ,所以我设置一个普通账号 chief

串口设置

默认没有启用串口控制台,这导致虚拟机console无法使用,所以按照 Ubuntu 串口控制台 调整:

  • 创建一个 /etc/init/ttyS0.conf :

/etc/init/ttyS0.conf 配置getty
# ttyS0 - getty
#
# This service maintains a getty on ttyS0 from the point the system is
# started until it is shut down again.

start on stopped rc RUNLEVEL=[12345]
stop on runlevel [!12345]

respawn
exec /sbin/getty -L 115200 ttyS0 vt102
  • 修订 /etc/default/grub 配置 GRUB_CMDLINE_LINUX :

修订 /etc/default/grub 配置 GRUB_CMDLINE_LINUX 添加控制台
# Kernel console on both serial and kvm/local console
GRUB_CMDLINE_LINUX="console=tty0 console=ttyS0,115200n8"

# Show grub menu on both serial and kvm/local console
GRUB_TERMINAL="console serial"
GRUB_SERIAL_COMMAND="serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1"

备注

上述串口配置仅针对Linux Kernel,所以只有在Linux内核获得主机控制时才会有串口输出,这样就不包含grub自身的串口输出,会导致grub刚启动时看不到选择启动项的页面。所以还需要补充 grub 的串口配置

  • 补充 增加GRUB串口配置,修订 /etc/default/grub 配置,配置 GRUB_TERMINALGRUB_SERIAL_COMMAND :

修订 /etc/default/grub 配置 GRUB_TERMINALGRUB_SERIAL_COMMAND
# Kernel console on both serial and kvm/local console
GRUB_CMDLINE_LINUX="console=tty0 console=ttyS0,115200n8"

# Show grub menu on both serial and kvm/local console
GRUB_TERMINAL="console serial"
GRUB_SERIAL_COMMAND="serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1"
  • 更新GRUB:

更细GRUB
sudo update-grub

重启系统后, 在QEMU中运行debian 就能在运行 qemu 执行的命令行访问虚拟机控制台,方便运维

系统仓库 sources.list

由于 在QEMU中运行debian 使用CDROM ISO安装,我发现完成后仓库源文件 /etc/apt/sources.list 只配置了CDROM,所以参考 debian wiki: SourcesList 更新设置:

配置 /etc/apt/sources.list
deb https://deb.debian.org/debian bookworm main non-free-firmware
deb-src https://deb.debian.org/debian bookworm main non-free-firmware

deb https://security.debian.org/debian-security bookworm-security main non-free-firmware
deb-src https://security.debian.org/debian-security bookworm-security main non-free-firmware

deb https://deb.debian.org/debian bookworm-updates main non-free-firmware
deb-src https://deb.debian.org/debian bookworm-updates main non-free-firmware

系统软件仓库更新

  • 在安装和更新软件包之前,我们通常需要先更新软件仓库索引(update),并完成一次现有安装软件的全面更新(upgrade):

debian更新软件仓库索引,并全面更新已安装软件
sudo apt update
sudo apt upgrade -y

软件安装步骤

  • 服务器系统,安装必要的管理工具:

安装运维管理工具
# tmux: 多会话终端
# neovim: 增强型vim编辑器

# 以下软件包提供必要工具
# cron: 定时任务(可选)
# locales: Debian的Docker镜像默认缺少需要补全
# net-tools: netstat
# iproute2: ip
# dnsutils: dig/nslookup
# plocate: updatedb/locate
# gnupg2: GPG (安装rvm需要)
# git: git版本管理
# tree: tree目录检查
# unzip: unzip解压缩
# wget: wget下载工具

apt install cron tmux neovim locales net-tools iproute2 dnsutils plocate gnupg2 git tree unzip lsof wget
  • 对于纯后台服务器系统,可以采用如下精简安装:

安装vim以及服务器开发所需软件集
sudo apt install build-essential cmake vim-nox python3-dev -y
  • debian发行版中, mlocate 已经被 plocate (更快)替代( /usr/bin/locate --> /etc/alternatives/locate --> /usr/bin/plocate ): 使用方法相同 updatedb && locate XXX