Skip to content

Commit 91a969a

Browse files
committed
core: 支持修改 ssh rdp web 端口
1 parent b3fef98 commit 91a969a

10 files changed

+319
-35
lines changed

README.en.md

+17-3
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ certutil -urlcache -f -split https://jihulab.com/bin456789/reinstall/-/raw/main/
126126
- On virtual machines, the appropriate official slimmed-down kernel will be automatically installed.
127127
- To install Red Hat, you need to provide the `qcow2` image link obtained from <https://access.redhat.com/downloads/content/rhel>.
128128
- Username `root`, password `123@@@`. It may take a few minutes for the password to take effect on the first boot.
129-
- When switching to key-based authentication, you also need to modify the files inside `/etc/ssh/sshd_config.d/`
129+
- After reinstalling, if you need to change SSH port or switch to key-based login, be sure to modify the files inside `/etc/ssh/sshd_config.d/`.
130+
- Optional parameters:
131+
- `--ssh-port PORT` to change the SSH port
132+
- `--hold 2` to prevent entering the system after installation. You can connect via SSH to modify system content, with the system mounted at `/os` (this feature is not supported on Debian/Kali).
130133

131134
```bash
132135
bash reinstall.sh centos 9
@@ -158,6 +161,8 @@ bash reinstall.sh centos 9
158161

159162
<summary>Experimental Features</summary>
160163

164+
The following features are experimental and may not support modifying the SSH port or other options.
165+
161166
Install Debian using a cloud image, suitable for machines with slower CPUs
162167

163168
```bash
@@ -183,6 +188,10 @@ bash reinstall.sh ubuntu --installer
183188
- Supports `raw`, `vhd` images or those compressed with `xz` or `gzip`.
184189
- When deploy a Windows image, the system disk will be expanded, and machines with static IPs will have their IPs configured. However, it may take a few minutes after the first boot for the configuration to take effect.
185190
- When deploy a Linux image, the script will not modify any contents of the image.
191+
- Optional parameters:
192+
- `--rdp-port PORT` to change the RDP port (Windows only).
193+
- `--allow-ping` to allow ping responses (Windows only).
194+
- `--hold 2` to prevent entering the system after DD completion. You can connect via SSH to modify system content, with the system mounted at `/os`.
186195

187196
```bash
188197
bash reinstall.sh dd --img https://example.com/xxx.xz
@@ -218,8 +227,12 @@ bash reinstall.sh netboot.xyz
218227
- Username `administrator`, password `123@@@`
219228
- If remote login fails, try using the username `.\administrator`.
220229
- The machine with a static IP will automatically configure the IP. It may take a few minutes to take effect on the first boot.
230+
- Optional parameters:
231+
- `--rdp-port PORT` to change the RDP port
232+
- `--allow-ping` to allow ping responses
233+
- `--hold 2` to allow SSH connections for modifying the hard disk content before rebooting into the official Windows installation program, with the hard disk mounted at `/os`.
221234

222-
![Installing Windows](https://github.com/bin456789/reinstall/assets/7548515/07c1aea2-1ce3-4967-904f-aaf9d6eec3f7)
235+
![Windows Installation](https://github.com/bin456789/reinstall/assets/7548515/07c1aea2-1ce3-4967-904f-aaf9d6eec3f7)
223236

224237
#### Method 1: Allow the script to automatically find the ISO
225238

@@ -365,9 +378,10 @@ Most ARM machines support ISO installation of Windows 11 24H2, but some machines
365378
- ✔️Azure: B2pts_v2
366379
- ✔️Alibaba Cloud: g8y, c8y, r8y (may occasionally get stuck on the boot logo during restart; force restart to resolve)
367380
- ✔️Alibaba Cloud: g6r, c6r
368-
- ✔️Oracle Cloud: A1.Flex (manual loading of the graphics driver is required after installation)
381+
- ✔️Oracle Cloud A1.Flex (Success depends on the machine's creation date; newer instances are more likely to install successfully. You will also need to manually load the GPU drivers after installation.)
369382
- ✔️AWS: T4g
370383
- ✔️Scaleway: COPARM1
384+
- ✔️Gcore
371385
- ❌Google Cloud: t2a (lacking network card driver)
372386

373387
<details>

README.md

+17-3
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ certutil -urlcache -f -split https://jihulab.com/bin456789/reinstall/-/raw/main/
126126
- 在虚拟机上,会自动安装合适的官方精简内核
127127
- 安装 Red Hat 需填写 <https://access.redhat.com/downloads/content/rhel> 得到的 `qcow2` 镜像链接
128128
- 用户名 `root` 密码 `123@@@`,可能首次开机几分钟后密码才生效
129-
- 改为密钥登录时,还要修改 `/etc/ssh/sshd_config.d/` 里面的文件
129+
- 重装后如需修改 SSH 端口 / 改成密钥登录,还要注意修改 `/etc/ssh/sshd_config.d/` 里面的文件
130+
- 可选参数
131+
- `--ssh-port PORT` 修改 SSH 端口
132+
- `--hold 2` 安装结束后不进入系统。可连接 SSH 修改系统内容,系统挂载在 `/os` (此功能不支持 Debian / Kali)
130133

131134
```bash
132135
bash reinstall.sh centos 9
@@ -158,6 +161,8 @@ bash reinstall.sh centos 9
158161

159162
<summary>实验性功能</summary>
160163

164+
以下功能为实验性质,可能不支持修改 ssh 端口等其它选项
165+
161166
用云镜像安装 Debian,适合于 CPU 较慢的机器
162167

163168
```bash
@@ -183,6 +188,10 @@ bash reinstall.sh ubuntu --installer
183188
- 支持 `raw` `vhd` 或者经过 `xz` `gzip` 压缩的镜像
184189
- DD Windows 镜像时,会扩展系统盘,静态 IP 的机器会配置好 IP,可能首次开机几分钟后才生效
185190
- DD Linux 镜像时,脚本不会修改镜像的任何内容
191+
- 可选参数
192+
- `--rdp-port PORT` 修改 RDP 端口 (仅限 Windows)
193+
- `--allow-ping` 允许被 Ping (仅限 Windows)
194+
- `--hold 2` DD 结束后不进入系统。可连接 SSH 修改系统内容,系统挂载在 `/os`
186195

187196
```bash
188197
bash reinstall.sh dd --img https://example.com/xxx.xz
@@ -218,8 +227,12 @@ bash reinstall.sh netboot.xyz
218227
- 用户名 `administrator` 密码 `123@@@`
219228
- 如果远程登录失败,尝试使用用户名 `.\administrator`
220229
- 静态机器会自动配置好 IP,可能首次开机几分钟后才生效
230+
- 可选参数
231+
- `--rdp-port PORT` 更改 RDP 端口
232+
- `--allow-ping` 允许被 Ping
233+
- `--hold 2` 在重启进入 Windows 官方安装程序前,可连接 SSH 修改硬盘内容,硬盘挂载在 `/os`
221234

222-
![Windows 安装中](https://github.com/bin456789/reinstall/assets/7548515/07c1aea2-1ce3-4967-904f-aaf9d6eec3f7)
235+
![Windows 安装界面](https://github.com/bin456789/reinstall/assets/7548515/07c1aea2-1ce3-4967-904f-aaf9d6eec3f7)
223236

224237
#### 方法 1: 让脚本自动查找 ISO
225238

@@ -365,9 +378,10 @@ Windows Server 2025 SERVERDATACENTER
365378
- ✔️Azure B2pts_v2
366379
- ✔️阿里云 g8y c8y r8y (有几率重启时卡开机 Logo,强制重启即可)
367380
- ✔️阿里云 g6r c6r
368-
- ✔️甲骨文云 A1.Flex (安装后需要手动加载显卡驱动)
381+
- ✔️甲骨文云 A1.Flex (视乎机器的创建日期,越新的越有可能成功安装,安装后还需要手动加载显卡驱动)
369382
- ✔️AWS T4g
370383
- ✔️Scaleway COPARM1
384+
- ✔️Gcore
371385
- ❌谷歌云 t2a (缺少网卡驱动)
372386

373387
<details>

cloud-init.yaml

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ runcmd:
2121
# 下面这行删除 clout-init 创建的 sshd_config
2222
- test $(wc -l </etc/ssh/sshd_config) -le 1 && cat /etc/ssh/sshd_config >>/etc/ssh/sshd_config.d/50-cloud-init.conf && rm -f /etc/ssh/sshd_config
2323
- echo "PermitRootLogin yes" >/etc/ssh/sshd_config.d/01-permitrootlogin.conf || sed -Ei 's/^#?PermitRootLogin.*/PermitRootLogin yes/' /etc/ssh/sshd_config
24+
- echo "Port @SSH_PORT@" >/etc/ssh/sshd_config.d/01-change-ssh-port.conf || sed -Ei 's/^#?Port .*/Port @SSH_PORT@/' /etc/ssh/sshd_config
2425
# 已创建的 ssh 连接会沿用旧的配置(未开启密码登录),这时即使输入正确的密码,也会提示 Access Denied
2526
# systemctl restart sshd 只会重启监听进程,不会关闭已创建的连接(子进程)
2627
- pkill sshd || true
27-
- systemctl restart sshd || systemctl restart ssh
28+
# daemon-reload 会刷新 /run/systemd/generator/ssh.socket.d/addresses.conf
29+
- systemctl daemon-reload
30+
- for s in ssh.socket ssh.service sshd.socket sshd.service; do systemctl is-enabled $s && systemctl restart $s && break; done
31+
- sed -i -e '/^[[:space:]]*password:/d' -e '/[[:space:]]*root:/d' /etc/cloud/cloud.cfg.d/99_fallback.cfg
2832
- touch /etc/cloud/cloud-init.disabled
2933
# ubuntu 镜像运行 echo -e '\nDone' ,-e 会被显示出来
3034
- printf '\n%s\n' 'reinstall done' >/dev/tty0 || true

debian.cfg

+9-1
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,16 @@ d-i partman/early_command string true; \
162162
# if [ "$link_grub_dir" = 1 ]; then mkdir /target/boot/grub2; echo 'chainloader (hd0)+1' >/target/boot/grub2/grub.cfg; fi; \
163163
d-i preseed/late_command string true; \
164164
eval "$(grep -o 'extra_link_grub_dir=[^ ]*' /proc/cmdline | sed 's/^extra_//')"; \
165+
eval "$(grep -o 'extra_ssh_port=[^ ]*' /proc/cmdline | sed 's/^extra_//')"; \
166+
165167
if [ "$link_grub_dir" = 1 ]; then ln -s grub /target/boot/grub2; fi; \
166168

167169
in-target systemctl enable ssh; \
170+
168171
echo "PermitRootLogin yes" >/target/etc/ssh/sshd_config.d/01-permitrootlogin.conf || \
169-
echo "PermitRootLogin yes" >>/target/etc/ssh/sshd_config
172+
echo "PermitRootLogin yes" >>/target/etc/ssh/sshd_config; \
173+
174+
if [ -n "$ssh_port" ] && ! [ "$ssh_port" = 22 ]; then \
175+
echo "Port $ssh_port" >/target/etc/ssh/sshd_config.d/01-change-ssh-port.conf || \
176+
echo "Port $ssh_port" >>/target/etc/ssh/sshd_config; \
177+
fi

logviewer-nginx.conf

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
server {
2-
listen 80;
3-
listen [::]:80;
2+
listen @WEB_PORT@;
3+
listen [::]:@WEB_PORT@;
44
root /;
55

66
gzip on;

reinstall.sh

+76-10
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ Usage: $reinstall____ centos 9
5959
windows --image-name='windows xxx yyy' --iso='http://xxx.com/xxx.iso'
6060
netboot.xyz
6161
62+
Options: [--ssh-port PORT]
63+
[--rdp-port PORT]
64+
[--web-port PORT]
65+
[--allow-ping]
66+
6267
Manual: https://github.com/bin456789/reinstall
6368
6469
EOF
@@ -194,6 +199,14 @@ is_use_firmware() {
194199
[ "$nextos_distro" = debian ] && ! is_virt
195200
}
196201

202+
is_digit() {
203+
[[ "$1" =~ ^[0-9]+$ ]]
204+
}
205+
206+
is_port_valid() {
207+
is_digit "$1" && [ "$1" -ge 1 ] && [ "$1" -le 65535 ]
208+
}
209+
197210
get_host_by_url() {
198211
cut -d/ -f3 <<<$1
199212
}
@@ -2315,14 +2328,21 @@ find_grub_extlinux_cfg() {
23152328
fi
23162329
}
23172330

2331+
# 空格、&、用户输入的网址要加引号,否则 grub 无法正确识别
2332+
is_need_quote() {
2333+
[[ "$1" = *' '* ]] || [[ "$1" = *'&'* ]] || [[ "$1" = http* ]]
2334+
}
2335+
23182336
# 转换 finalos_a=1 为 finalos.a=1 ,排除 finalos_mirrorlist
23192337
build_finalos_cmdline() {
23202338
if vars=$(compgen -v finalos_); then
23212339
for key in $vars; do
23222340
value=${!key}
23232341
key=${key#finalos_}
23242342
if [ -n "$value" ] && [ $key != "mirrorlist" ]; then
2325-
finalos_cmdline+=" finalos_$key='$value'"
2343+
is_need_quote "$value" &&
2344+
finalos_cmdline+=" finalos_$key='$value'" ||
2345+
finalos_cmdline+=" finalos_$key=$value"
23262346
fi
23272347
done
23282348
fi
@@ -2334,10 +2354,13 @@ build_extra_cmdline() {
23342354
# 会将 extra.xxx=yyy 写入新系统的 /etc/modprobe.d/local.conf
23352355
# https://answers.launchpad.net/ubuntu/+question/249456
23362356
# https://salsa.debian.org/installer-team/rootskel/-/blob/master/src/lib/debian-installer-startup.d/S02module-params?ref_type=heads
2337-
for key in confhome hold force cloud_image main_disk; do
2357+
for key in confhome hold force force_old_windows_setup cloud_image main_disk \
2358+
ssh_port rdp_port web_port allow_ping password; do
23382359
value=${!key}
23392360
if [ -n "$value" ]; then
2340-
extra_cmdline+=" extra_$key='$value'"
2361+
is_need_quote "$value" &&
2362+
extra_cmdline+=" extra_$key='$value'" ||
2363+
extra_cmdline+=" extra_$key=$value"
23412364
fi
23422365
done
23432366

@@ -3042,8 +3065,28 @@ else
30423065
fi
30433066
fi
30443067

3068+
long_opts=
3069+
for o in ci installer debug minimal allow-ping \
3070+
hold: \
3071+
sleep: \
3072+
iso: \
3073+
image-name: \
3074+
boot-wim: \
3075+
img: \
3076+
lang: \
3077+
ssh-port: \
3078+
rdp-port: \
3079+
web-port: \
3080+
allow-ping: \
3081+
commit: \
3082+
force: \
3083+
force-old-windows-setup:; do
3084+
[ -n "$long_opts" ] && long_opts+=,
3085+
long_opts+=$o
3086+
done
3087+
30453088
# 整理参数
3046-
if ! opts=$(getopt -n $0 -o "" --long ci,installer,debug,minimal,hold:,sleep:,iso:,image-name:,img:,lang:,commit:,force: -- "$@"); then
3089+
if ! opts=$(getopt -n $0 -o "" --long "$long_opts" -- "$@"); then
30473090
usage_and_exit
30483091
fi
30493092

@@ -3073,18 +3116,41 @@ while true; do
30733116
minimal=1
30743117
shift
30753118
;;
3119+
--allow-ping)
3120+
allow_ping=1
3121+
shift
3122+
;;
30763123
--hold | --sleep)
3077-
hold=$2
3078-
if ! { [ "$hold" = 1 ] || [ "$hold" = 2 ]; }; then
3079-
error_and_exit "Invalid --hold value: $hold."
3124+
if ! { [ "$2" = 1 ] || [ "$2" = 2 ]; }; then
3125+
error_and_exit "Invalid $1 value: $2"
30803126
fi
3127+
hold=$2
30813128
shift 2
30823129
;;
30833130
--force)
3084-
force=$2
3085-
if ! { [ "$force" = bios ] || [ "$force" = efi ]; }; then
3086-
error_and_exit "Invalid --force value: $force."
3131+
if ! { [ "$2" = bios ] || [ "$2" = efi ]; }; then
3132+
error_and_exit "Invalid $1 value: $2"
30873133
fi
3134+
force=$2
3135+
shift 2
3136+
;;
3137+
--ssh-port)
3138+
is_port_valid $2 || error_and_exit "Invalid $1 value: $2"
3139+
ssh_port=$2
3140+
shift 2
3141+
;;
3142+
--rdp-port)
3143+
is_port_valid $2 || error_and_exit "Invalid $1 value: $2"
3144+
rdp_port=$2
3145+
shift 2
3146+
;;
3147+
--web-port)
3148+
is_port_valid $2 || error_and_exit "Invalid $1 value: $2"
3149+
web_port=$2
3150+
shift 2
3151+
;;
3152+
--force-old-windows-setup)
3153+
force_old_windows_setup=$2
30883154
shift 2
30893155
;;
30903156
--img)

0 commit comments

Comments
 (0)