VPS的一键重装系统

 

脚本一键重装系统

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
# 本脚本至少需要 512MB 内存 + 1.5GB 硬盘,目前Debian 13安装完占用存储625M,注意其不支持 OpenVZ、LXC 等虚拟机
# 脚本会最大化利用磁盘空间:不含 boot 分区(Fedora 例外),不含 swap 分区。自动根据机器类型选择不同的优化内核,例如 Cloud、HWE 内核
wget -O reinstall.sh https://github.com/bin456789/reinstall/raw/main/reinstall.sh
bash reinstall.sh debian --password [PASSWORD] --ssh-port [PORT] --hold 2

# 以重装Debian 13为例,安装最新版系统可不输入版本号,部分参数如下:
--hold 2 # 安装结束后不立即重启,此时可以 SSH 登录修改系统内容,系统挂载在 /os (此功能不支持 Debian/Kali)
--frpc-toml /path/to/frpc.toml # 添加 frpc 内网穿透
--web-port PORT # 修改 Web 端口(安装期间观察日志用)
--ssh-port PORT # 修改 SSH 端口(安装期间观察日志用,也作用于新系统)
--password PASSWORD # 设置密码,如不设置默认用户名:root 默认密码:123@@@
--ssh-key KEY # 设置 SSH 登录公钥,当使用公钥时,密码为空,支持以下格式:
--ssh-key "ssh-rsa ..." --ssh-key "ssh-ed25519 ..." --ssh-key "ecdsa-sha2-nistp256/384/521 ..."
--ssh-key /path/to/public_key --ssh-key github:your_username --ssh-key http://path/to/public_key

# 也可选择重启到 Alpine Live OS(内存系统),可用 SSH 连接,进行备份/恢复硬盘、手动 DD、修改分区、手动安装 Alpine/Arch/Gentoo 等操作
./reinstall.sh alpine --hold=1 # 用户名 root 默认密码 123@@@,如果手动操作没有破坏原系统,再次重启将回到原系统
./reinstall.sh netboot.xyz # 或者重启到 netboot.xyz 来用商家后台 VNC 手动安装 更多系统,如果手动操作没有破坏原系统,再次重启将回到原系统

# 脚本执行完后会生成 /boot/grub/custom.cfg 文件,该配置文件会在GRUB菜单中添加一条网络启动的项,首次重启后自动进入该选项后即可在云服务商控制台VNC中操作来网络安装
# 如果首次安装失败,后续可以在GRUB菜单中手动选择 reinstall 项再次进行安装,该配置文件 /boot/grub/custom.cfg 主要内容如下:
......
menuentry "reinstall (debian 13)" --unrestricted {
insmod lvm
insmod all_video
search --no-floppy --file --set=root /reinstall-vmlinuz
linux /reinstall-vmlinuz lowmem/low=1 auto=true priority=critical url=https://raw.githubusercontent.com/bin456789/reinstall/main/debian.cfg mirror/http/hostname=deb.debian.org mirror/http/directory=/debian base-installer/kernel/image=linux-image-cloud-amd64 extra_confhome='https://raw.githubusercontent.com/bin456789/reinstall/main' extra_hold=2 extra_main_disk=45f2e6fa extra_elts=0 extra_deb_mirror=deb.debian.org/debian extra_ssh_port=22
initrd /reinstall-initrd
}

# 上面的 extra_main_disk 内容来自 blkid 中 PARTUUID 的值
/dev/vda1: UUID="67eedb9c-5877-4061-a42e-40d0aabf95a2" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="45f2e6fa-01"
# 重装后默认安装 Cloud 内核,如需切换到通用内核,重启前将上面GRUB配置文件中的 linux-image-cloud-amd64 改成 linux-image-amd64 即可

# 脚本执行后产生的相关文件
/boot/grub/custom.cfg # 新增的自定义GRUB配置
/reinstall.log # 脚本执行所产生的日志文件
/reinstall-initrd # 该文件是由 /reinstall-tmp/initrd/ 目录制作而成的
/reinstall-vmlinuz # https://deb.debian.org/debian/dists/trixie/main/installer-amd64/current/images/netboot/debian-installer/amd64/linux
/reinstall-tmp/ # https://deb.debian.org/debian/dists/trixie/main/installer-amd64/current/images/netboot/debian-installer/amd64/initrd.gz
├── initrd/ # 由上面链接官方 initrd.gz 解包后添加几个脚本和配置而成,最终会打包制作成 /reinstall-initrd 镜像
├── fdisk/ # 该目录和后面的几个文件都是软件包 fdisk-udeb 解包产生的
├── control.tar.xz
├── data.tar.xz
├── debian-binary
├── tmp.udeb
└── udeb_list

# 可将 /boot/grub/custom.cfg /reinstall-initrd /reinstall-vmlinuz 这三个文件放到其他系统上对应位置便能重装而不用执行该脚本,SSHD密钥码端口等均为上面脚本执行时设置的

# 上面系统重装后它对于SSHD的配置改动和内容在如下位置:
/etc/ssh/sshd_config.d/01-change-ssh-port.conf Port 22
/etc/ssh/sshd_config.d/01-permitrootlogin.conf PermitRootLogin yes

除此之外,它还可以添加一些密钥
root@localhost:~# tree -L 1 /etc/ssh/
/etc/ssh/
......
├── ssh_host_ecdsa_key
├── ssh_host_ecdsa_key.pub # --ssh-key "ecdsa-sha2-nistp256 ..."
├── ssh_host_ed25519_key
├── ssh_host_ed25519_key.pub # --ssh-key "ssh-ed25519 ..."
├── ssh_host_rsa_key
└── ssh_host_rsa_key.pub # --ssh-key "ssh-rsa ..."

# 如需更换安装镜像源,使用前请自己修改 reinstall.sh 文件 setos_debian() 函数中的 udeb_mirror(安装时的源)和deb_mirror(安装后要修改成的源)
# frpc的配置可参考这个:https://github.com/bin456789/reinstall/blob/main/frpc-example.toml

SSH登录安全相关

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
# 建议在 /etc/ssh/sshd_config.d/ 创建一个 conf 文件来自定义 sshd 配置,而不是直接编辑 /etc/ssh/sshd_config,防止 OpenSSH 更新后配置冲突
# 若主配置文件 /etc/ssh/sshd_config 首行为 Include /etc/ssh/sshd_config.d/*.conf ,则该目录里的子配置文件中任何项都优先于主配置文件中的
# SSHD配置修改完可先用 sudo sshd -T 看一下有效配置,免得被覆盖了都不知道,注意:prohibit-password 是 without-password 的别名

# 禁止Root用户通过密码远程登录想法是好的,但在服务器故障需要救援时就会变麻烦,所以更推荐只关闭远程密码登录而保留本地密码登录,这样挂掉时也能通过服务商VNC本地登录Root进行救援
Port 22 # 设置 SSHD 端口
PermitEmptyPasswords no # 禁用空密码登陆(默认)
PermitRootLogin prohibit-password # 禁止Root用户通过密码远程登录,no 为禁用Root登录包括本地
PasswordAuthentication no # 关闭远程密码登录
PubkeyAuthentication yes # 允许密钥登录
AuthorizedKeysFile .ssh/authorized_keys # 指定公钥路径

# 服务端生成密钥对并发送给客户端让其通过密钥登录
cd ~/.ssh # 服务端只需要留公钥,客户端则需要私钥即可
ssh-keygen -t rsa # 生成密钥对,将生成的id_rsa和id_rsa.pub备份到自己的本地电脑
mv id_rsa.pub authorized_keys # 导入公钥后建议删除服务器端的私钥以保证安全
rm id_rsa # 妥善保存id_rsa私钥后再删除,丢了服务器可就没法远程登陆了

# 也可在客户端 Windows PowerShell 中生成密钥对并发送到服务端
PS C:\Users\leux> cd $HOME\.ssh\ # ssh-keygen 的默认密钥生成路径都是:$HOME/.ssh ,生成前进入只是为了更好操作生成后的密钥
PS C:\Users\leux\.ssh> ssh-keygen -t ed25519 # 完成后会生成私钥id_ed25519和公钥id_ed25519.pub,将 id_ed25519.pub 放到服务器的 ~/.ssh/authorized_keys
PS C:\Users\leux\.ssh> ssh [username]@[ip] -p [port] # 后续只要 $HOME/.ssh/ 中存在名为 id_rsa id_ecdsa id_ed25519 的私钥文件均可通过其来登录


# 也可以通过 PAM 模块在每次SSH登录时触发脚本来实现自动发通知,编辑 /etc/pam.d/sshd 文件在末尾添加:
session optional pam_exec.so /usr/local/bin/notify_ssh_login.sh

# 这里使用推送加的API来将登录用户的信息推送到微信公众号,如下是登录时触发的脚本内容:sudo chmod +x /usr/local/bin/notify_ssh_login.sh
#!/bin/bash

# 如果登录失败则退出脚本
if [ "$PAM_TYPE" != "open_session" ]; then
exit 0
fi

# 获取登录用户的相关信息
IP=$PAM_RHOST
NAME=$PAM_USER
DATE=$(date +"%Y-%m-%d %H:%M:%S")

# pushplus API相关参数,需更改TOKEN值为你的
URL="https://www.pushplus.plus/send/"
TOKEN="You_Token"
TITLE="登录提醒"
CONTENT="<h4>登录信息:</h4><ul><li>登录时间:$DATE</li><li>登录用户:$NAME</li><li>客户端IP:$IP</li></ul>"

# 发送HTTP POST请求并将响应值存到变量
response=$(curl -s -X POST -d "token=$TOKEN&title=$TITLE&content=$CONTENT" "$URL")

# 如需额外解析响应的JSON来提取返回码或说明来调试,可安装jq解析并参考如下,响应内容示例:{"code":200,"msg":"执行成功","data":"abcdefghijklmnopqrstuvwxyz"}
# code=$(echo "$response" | jq -r '.code') # 单独获取返回码,返回码说明见:https://www.pushplus.plus/doc/guide/code.html
# echo $response # 输出所有响应值,DEMO代码参考:https://www.pushplus.plus/doc/guide/demo.html