Skip to content

提示

本文主要介绍如果从 Debian 官网下载的镜像 qcow2,导入到 KVM 中,并修改 root 密码, 能从 ssh 登录。

Debian 镜像下载

下载地址,选择合适的版本下载

下载镜像,Debian 镜像非常小,选择 qcow2 格式,大概 300多 M。

shell
ll -h
-rw-r--r-- 1 libvirt-qemu libvirt-qemu 766M 11月14日 11:55 debian-11-generic-amd64-20241111-1928.qcow2

qemu-img info debian-11-generic-amd64-20241111-1928.qcow2
image: debian-11-generic-amd64-20241111-1928.qcow2
file format: qcow2
virtual size: 2 GiB (2147483648 bytes)
disk size: 769 MiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    compression type: zlib
    lazy refcounts: false
    refcount bits: 16
    corrupt: false
    extended l2: false

镜像挂载到本地

shell
sudo virt-filesystems --long -a debian-11-generic-amd64-20241111-1928.qcow2 --all
Name        Type        VFS      Label  MBR  Size        Parent
/dev/sda1   filesystem  ext4     -      -    1941159936  -
/dev/sda14  filesystem  unknown  -      -    3145728     -
/dev/sda15  filesystem  vfat     -      -    129718272   -
/dev/sda1   partition   -        -      -    2012217344  /dev/sda
/dev/sda14  partition   -        -      -    3145728     /dev/sda
/dev/sda15  partition   -        -      -    130023424   /dev/sda
/dev/sda    device      -        -      -    2147483648

挂在镜像到本地系统:

shell
sudo mkdir /mnt/kvmdisk
sudo guestmount -a debian-11-generic-amd64-20241111-1928.qcow2 -m /dev/sda1 --rw /mnt/kvmdisk
sudo -i
cd /mnt/kvmdisk

内部是一个完整的系统,可以执行各种命令,需要修改一些配置。

root密码重置

Linux 密码文件:cat /etc/shadow, 内容如下(不同版本内容有不同,但大致如此);

log
root:*:20038:0:99999:7:::
daemon:*:20038:0:99999:7:::
bin:*:20038:0:99999:7:::
sys:*:20038:0:99999:7:::
sync:*:20038:0:99999:7:::
games:*:20038:0:99999:7:::
man:*:20038:0:99999:7:::
lp:*:20038:0:99999:7:::
mail:*:20038:0:99999:7:::
news:*:20038:0:99999:7:::
uucp:*:20038:0:99999:7:::
proxy:*:20038:0:99999:7:::
www-data:*:20038:0:99999:7:::
backup:*:20038:0:99999:7:::
list:*:20038:0:99999:7:::
irc:*:20038:0:99999:7:::
gnats:*:20038:0:99999:7:::
nobody:*:20038:0:99999:7:::
_apt:*:20038:0:99999:7:::
systemd-network:*:20038:0:99999:7:::
systemd-resolve:*:20038:0:99999:7:::
messagebus:*:20038:0:99999:7:::
uuidd:*:20038:0:99999:7:::
tcpdump:*:20038:0:99999:7:::
_chrony:*:20038:0:99999:7:::
sshd:*:20038:0:99999:7:::
systemd-timesync:!*:20041::::::
systemd-coredump:!*:20041::::::

第一行 root:*:20038:0:99999:7::: 需要修改,采用 openssl 重新生成 root 密码,密码为:123456

shell
openssl passwd -1 123456
$1$JgzOeNYD$xrQFAgaYG1El9M0zL0cvu/

将第一行修改为(注意/等特殊符号,全部 copy):

log
root:$1$JgzOeNYD$xrQFAgaYG1El9M0zL0cvu/:20038:0:99999:7:::

修改镜像其他配置

修改 sshd 服务,使支持远程登录服务。

vim sshd_config

  1. 第34行
shell
#PermitRootLogin prohibit-password
释放掉注释,并修改值为yes,调整完之后第32行为
PermitRootLogin yes
  1. 第58行
shell
#PasswordAuthentication yes
释放掉注释,调整完之后第56行为
PasswordAuthentication yes

创建 sshd 的运行目录:

shell
mkdir /run/sshd

修改APT为国内源

vim /etc/apt/sources.list

log
deb http://deb.debian.org/debian bullseye main
deb-src http://deb.debian.org/debian bullseye main
deb http://security.debian.org/debian-security bullseye-security main
deb-src http://security.debian.org/debian-security bullseye-security main
deb http://deb.debian.org/debian bullseye-updates main
deb-src http://deb.debian.org/debian bullseye-updates main
deb http://deb.debian.org/debian bullseye-backports main
deb-src http://deb.debian.org/debian bullseye-backports main

注释以上的 debian 官方源,使用国内 aliyun 的源。添加以下内容:

log
deb https://mirrors.aliyun.com/debian/ stable main non-free contrib
deb-src https://mirrors.aliyun.com/debian/ stable main non-free contrib

导入到 KVM

卸载镜像挂载

shell
sudo guestunmount /mnt/kvmdisk

再 KVM 中创建新虚拟机,并导入,启动虚拟机,输入 root 密码 123456,登录成功。

设置虚拟机静态 IP

为虚拟机设置静态IP地址,编辑 /etc/network/interfaces 文件,添加以下内容:

shell
auto lo
iface lo inet loopback

# The primary network interface
auto enp0s3
iface enp0s3 inet static
 address 192.168.122.101
 netmask 255.255.255.0
 gateway 192.168.122.1
 dns-nameservers 114.114.114.114