Skip to content

Commit 8df7dbe

Browse files
committed
优化 open-sshd-passwd.sh 脚本,增强 SSH 密钥生成逻辑,添加版本兼容性检查,初始化 SSH 运行环境,调整 sshd 配置,支持 root 密码设置
1 parent b189eba commit 8df7dbe

File tree

2 files changed

+98
-29
lines changed

2 files changed

+98
-29
lines changed

common/open-sshd-passwd.sh

Lines changed: 89 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,95 @@
1-
#!/usr/bin/env sh
1+
#!/usr/bin/env sh
2+
set -e # 遇到错误立即退出
23

3-
# SSHD
4-
# generate fresh rsa key
5-
if [ ! -f /etc/ssh/ssh_host_rsa_key ];then
6-
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N '' -t rsa
4+
# ===================== 基础配置与版本检测 =====================
5+
# 定义默认 SSH 端口(未传参时用 22)
6+
SSHD_PORT=${SSHD_PORT:-22}
7+
# 检测 OpenSSH 主版本号(提取数字部分,如 8.4 → 8,6.6 → 6)
8+
SSH_VERSION=$(ssh -V 2>&1 | awk '{gsub(/,|_p[0-9]+/,""); print $1}' | cut -d'.' -f1)
9+
# 兼容旧版本输出格式(部分系统 ssh -V 输出不同)
10+
if [ -z "$SSH_VERSION" ] || ! echo "$SSH_VERSION" | grep -q '[0-9]'; then
11+
SSH_VERSION=$(ssh -V 2>&1 | awk '{print $NF}' | cut -d'_' -f1 | cut -d'.' -f1)
712
fi
8-
# generate fresh dsa key
9-
if [ ! -f /etc/ssh/ssh_host_dsa_key ];then
10-
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -N '' -t dsa
13+
# 确保版本号为数字(兜底:默认按高版本处理)
14+
if ! echo "$SSH_VERSION" | grep -q '^[0-9]\+$'; then
15+
SSH_VERSION=7
1116
fi
12-
#prepare run dir
17+
18+
echo "=== 检测到 OpenSSH 主版本:$SSH_VERSION ==="
19+
20+
# ===================== 生成 SSH 主机密钥(版本兼容) =====================
21+
# 生成 RSA 密钥(全版本兼容,强制 4096 位更安全)
22+
if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then
23+
echo "=== 生成 RSA 主机密钥 ==="
24+
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N '' -t rsa -b 4096
25+
else
26+
echo "=== RSA 主机密钥已存在,跳过生成 ==="
27+
fi
28+
29+
# 生成 ED25519 密钥(OpenSSH ≥6.5 支持,优先推荐)
30+
if [ "$SSH_VERSION" -ge 6 ]; then
31+
if [ ! -f /etc/ssh/ssh_host_ed25519_key ]; then
32+
echo "=== 生成 ED25519 主机密钥 ==="
33+
ssh-keygen -f /etc/ssh/ssh_host_ed25519_key -N '' -t ed25519
34+
else
35+
echo "=== ED25519 主机密钥已存在,跳过生成 ==="
36+
fi
37+
fi
38+
39+
# 仅 OpenSSH <7.0 时尝试生成 DSA 密钥(已废弃,仅兜底)
40+
if [ "$SSH_VERSION" -lt 7 ]; then
41+
if [ ! -f /etc/ssh/ssh_host_dsa_key ]; then
42+
echo "=== 生成 DSA 主机密钥(仅兼容旧版本) ==="
43+
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -N '' -t dsa || {
44+
echo "警告:DSA 密钥生成失败,跳过(不影响核心功能)"
45+
}
46+
else
47+
echo "=== DSA 主机密钥已存在,跳过生成 ==="
48+
fi
49+
else
50+
echo "=== OpenSSH ≥7.0,跳过废弃的 DSA 密钥生成 ==="
51+
# 删除旧的 DSA 密钥配置(避免 sshd 加载报错)
52+
sed -i '/ssh_host_dsa_key/d' /etc/ssh/sshd_config 2>/dev/null || true
53+
fi
54+
55+
# ===================== 初始化 SSH 运行环境 =====================
56+
# 创建 sshd 运行目录(避免启动报错)
57+
echo "=== 初始化 SSH 运行目录 ==="
1358
mkdir -p /var/run/sshd
14-
# prepare config file for key based auth
15-
sed -i "s/UsePrivilegeSeparation.*/UsePrivilegeSeparation no/g" /etc/ssh/sshd_config
16-
sed -i "s/UsePAM.*/UsePAM no/g" /etc/ssh/sshd_config
17-
sed -i "s/\(#\s*\)*PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config
18-
sed -i "s/\(#\s*\)PasswordAuthentication.*/PasswordAuthentication yes/g" /etc/ssh/sshd_config
19-
sed -i "s/#AuthorizedKeysFile/AuthorizedKeysFile/g" /etc/ssh/sshd_config
20-
21-
if [ -n "$SSHD_PASSWORD" ];then
22-
echo '设置密码';
23-
echo root:${SSHD_PASSWORD}|chpasswd
59+
chmod 0755 /var/run/sshd
60+
61+
# ===================== 修改 sshd 配置(兼容全版本) =====================
62+
echo "=== 调整 sshd 配置 ==="
63+
# 1. UsePrivilegeSeparation:OpenSSH 7.5+ 已废弃该参数,避免配置报错
64+
if [ "$SSH_VERSION" -lt 7 ] || [ "$(echo "$SSH_VERSION" | cut -d'.' -f2)" -lt 5 ]; then
65+
sed -i "s/^UsePrivilegeSeparation.*/UsePrivilegeSeparation no/g" /etc/ssh/sshd_config 2>/dev/null || true
66+
else
67+
sed -i "/^UsePrivilegeSeparation/d" /etc/ssh/sshd_config 2>/dev/null || true
68+
fi
69+
70+
# 2. 禁用 PAM(保持原有逻辑)
71+
sed -i "s/^UsePAM.*/UsePAM no/g" /etc/ssh/sshd_config 2>/dev/null || true
72+
73+
# 3. 允许 root 登录(保持原有逻辑)
74+
sed -i "s/^#\?PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config 2>/dev/null || true
75+
76+
# 4. 启用密码登录(保持原有逻辑)
77+
sed -i "s/^#\?PasswordAuthentication.*/PasswordAuthentication yes/g" /etc/ssh/sshd_config 2>/dev/null || true
78+
79+
# 5. 启用授权密钥文件(保持原有逻辑)
80+
sed -i "s/^#\?AuthorizedKeysFile.*/AuthorizedKeysFile .ssh/authorized_keys/g" /etc/ssh/sshd_config 2>/dev/null || true
81+
82+
# ===================== 设置 root 密码(传参时) =====================
83+
if [ -n "$SSH_PWD" ]; then
84+
echo "=== 设置 root 密码 ==="
85+
echo "root:${SSH_PWD}" | chpasswd
86+
else
87+
echo "=== 未传入 SSH_PWD,跳过密码设置 ==="
2488
fi
2589

26-
# -e 显示详细信息
27-
`which sshd` -e -p ${SSHD_PORT} "$@"
90+
# ===================== 启动 sshd 服务 =====================
91+
echo "=== 启动 sshd 服务(端口:$SSHD_PORT) ==="
92+
# 查找 sshd 可执行文件(兼容不同系统路径)
93+
SSHD_BIN=$(which sshd || echo "/usr/sbin/sshd")
94+
# 启动并输出详细日志(-e),指定端口,传递额外参数
95+
exec "$SSHD_BIN" -e -p "$SSHD_PORT" "$@"

hyperf/Dockerfile.xdebug

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ ARG BASE_FORM=hyperf/hyperf:7.4-alpine-v3.11
44
FROM ${BASE_FORM}
55

66
RUN apk add --no-cache php84-pecl-xdebug && \
7-
echo "[xdebug] \
8-
zend_extension=xdebug.so \
9-
xdebug.mode=develop,debug \
10-
xdebug.start_with_request=yes \
11-
xdebug.client_host=host.docker.internal \
12-
xdebug.client_port=9003 \
13-
xdebug.log=/tmp/xdebug.log" > /etc/php84/conf.d/50_xdebug.ini && \
7+
echo "安装完成xdebug扩展" && \
8+
echo "" > /etc/php84/conf.d/50_xdebug.ini && \
9+
echo "[xdebug]" >> /etc/php84/conf.d/50_xdebug.ini && \
10+
echo "zend_extension=xdebug.so" >> /etc/php84/conf.d/50_xdebug.ini && \
11+
echo "xdebug.mode=develop,debug" >> /etc/php84/conf.d/50_xdebug.ini && \
12+
echo "xdebug.start_with_request=yes" >> /etc/php84/conf.d/50_xdebug.ini && \
13+
echo "xdebug.log=/tmp/xdebug.log" >> /etc/php84/conf.d/50_xdebug.ini && \
1414
echo "开始验证xdebug是否安装成功" && \
15-
php -m | grep xdebug
15+
bash -c "php -m | grep xdebug" \
16+
&& echo "结束"

0 commit comments

Comments
 (0)