summaryrefslogtreecommitdiff
path: root/script/debian.sh
diff options
context:
space:
mode:
Diffstat (limited to 'script/debian.sh')
-rw-r--r--script/debian.sh80
1 files changed, 80 insertions, 0 deletions
diff --git a/script/debian.sh b/script/debian.sh
new file mode 100644
index 0000000..35b5fe4
--- /dev/null
+++ b/script/debian.sh
@@ -0,0 +1,80 @@
+#!/bin/bash
+
+# 该脚本用于在当前目录下创建一个 Debian 10 系统文件夹,并在其中安装 ssh 服务。
+# 如果当前目录下已经存在名为 "debian" 的文件夹,则脚本会退出。
+# 运行脚本需 root 运行
+# 依赖项:debootstrap 工具、apt-get 命令。
+# 使用清华大学的 Debian 镜像源
+
+set -e
+set -x
+
+# 进入根目录
+cd ..
+
+if [ -e "./debian" ]; then
+ read -r -p "./debian\" 已存在,是否删除? (回车确认,其他键取消)" confirm
+ if [[ $confirm == "" ]]; then
+ rm -rf "./debian"
+ else
+ exit 1
+ fi
+fi
+
+# 创建并进入目标文件夹
+mkdir debian
+cd debian
+
+# 安装 debootstrap 工具
+if ! apt-get -qy install debootstrap; then
+ echo "无法安装 debootstrap 工具"
+ exit 1
+fi
+
+# 使用 debootstrap 创建 Debian10 系统文件
+if ! debootstrap --arch=amd64 bullseye . https://mirrors.tuna.tsinghua.edu.cn/debian/; then
+ echo "创建 Debian 10系统文件时出错"
+ exit 1
+fi
+
+# 进入新创建的 Debian 系统
+if ! chroot . /bin/bash -c "apt-get update"; then
+ echo "无法进入新创建的 Debian 系统"
+ exit 1
+fi
+
+# 进入 chroot 环境, 初始化系统
+chroot . /bin/bash <<EOF
+set -e
+
+# 安装 ssh 服务
+apt-get update
+apt-get install -qy openssh-server sudo net-tools bash-completion ssh
+
+# 设置 root 用户的密码
+echo "root:root" | chpasswd
+
+# 配置 ssh 登录
+# sed -i 's/.*Port.*/Port 22/' /etc/ssh/sshd_config
+sed -i 's/^#Port 22/Port 22/' /etc/ssh/sshd_config
+# sed -i 's/.*ListenAddress.*/ListenAddress 0.0.0.0/' /etc/ssh/sshd_config
+sed -i 's/.*PasswordAuthentication.*/PasswordAuthentication yes/' /etc/ssh/sshd_config
+sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
+
+# 重启 ssh 服务
+service ssh restart
+apt clean
+EOF
+
+# 写入 init 文件并设置权限
+cp ../script/init_debian init
+chroot . /bin/bash -c "chmod u+x /init"
+
+echo "最小化 Debian 系统创建成功!"
+
+if read -r -t 10 -p "打包镜像? (10秒 超时取消)" confirm; then
+ find . -print0 | cpio --null -ov --format=newc | gzip -9 >../debian.cpio.gz
+ echo "打包完毕"
+else
+ echo "取消打包"
+fi