CoolFace
Apppublic

xmeta-dev/agent-base

sourceHugging Faceapache-2.0updated 3mo agoView on Hugging Face
0likes
configure-ssh.sh102 linesDownload Raw Back to scripts
1#!/bin/sh2set -e3 4check_ssh_login() {5    if [ -z "$SSH_PASSWORD" ]; then6        echo "[check_ssh_login] SSH_PASSWORD 未设置"7        return 18    fi9 10    if command -v sshpass > /dev/null 2>&1; then11        result=$(sshpass -p "$SSH_PASSWORD" ssh -vvv -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ConnectTimeout=5 root@localhost exit 2>&1)12        ret=$?13        echo "[check_ssh_login] sshpass exit=$ret"14        if [ -n "$result" ]; then15            echo "[check_ssh_login] ssh output: $result"16        fi17        return $ret18    fi19 20    echo "[check_ssh_login] sshpass 不可用"21    return 122}23 24if [ "$1" = "--check-login" ]; then25    check_ssh_login26    exit $?27fi28 29# 生成 SSH 主机密钥(如果不存在)30if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then31    ssh-keygen -A32fi33 34# 配置SSH35mkdir -p /etc/ssh/sshd_config.d36 37cat > /etc/ssh/sshd_config.d/99-allow-root.conf << 'EOF'38ListenAddress 0.0.0.039Port 2240PermitRootLogin yes41PasswordAuthentication yes42KbdInteractiveAuthentication yes43X11Forwarding yes44X11DisplayOffset 1045UsePAM no46EOF47 48if [ -f /etc/ssh/sshd_config ]; then49    sed -i \50        -e 's/^#\?PasswordAuthentication .*/PasswordAuthentication yes/' \51        -e 's/^#\?KbdInteractiveAuthentication .*/KbdInteractiveAuthentication yes/' \52        -e 's/^#\?PermitRootLogin .*/PermitRootLogin yes/' \53        -e 's/^#\?UsePAM .*/UsePAM no/' \54        /etc/ssh/sshd_config55fi56 57# 配置用户密码58if [ -n "$SSH_PASSWORD" ]; then59    echo "root:$SSH_PASSWORD" | chpasswd60fi61 62verify_ssh_config() {63    echo "验证 SSH 配置..."64 65    if [ -z "$SSH_PASSWORD" ]; then66        echo "错误: SSH_PASSWORD 环境变量未设置"67        return 168    fi69    echo "[OK] SSH_PASSWORD 已设置"70 71    if [ -f /etc/ssh/sshd_config.d/99-allow-root.conf ]; then72        if grep -q "PermitRootLogin yes" /etc/ssh/sshd_config.d/99-allow-root.conf; then73            echo "[OK] PermitRootLogin 已配置"74        else75            echo "错误: PermitRootLogin 未正确配置"76            return 177        fi78    else79        echo "错误: SSH 配置文件不存在"80        return 181    fi82 83    if [ -f /etc/ssh/ssh_host_rsa_key ] && [ -f /etc/ssh/ssh_host_rsa_key.pub ]; then84        echo "[OK] SSH 主机密钥已生成"85    else86        echo "错误: SSH 主机密钥未生成"87        return 188    fi89 90    if getent passwd root > /dev/null 2>&1; then91        echo "[OK] root 用户存在"92    else93        echo "错误: root 用户不存在"94        return 195    fi96 97    echo "SSH 配置验证通过"98    return 099}100 101# verify_ssh_config102