Shell脚本如何配置SSH访问白名单

wen 实用脚本 4

我来介绍几种Shell脚本配置SSH访问白名单的方法:

Shell脚本如何配置SSH访问白名单

通过 hosts.allow/hosts.deny 配置

#!/bin/bash
# SSH访问白名单配置脚本 - hosts.allow方式
# 定义白名单IP列表
WHITE_LIST=(
    "192.168.1.100"
    "10.0.0.50"
    "172.16.0.0/24"
    "203.0.113.0/24"
)
# 配置hosts.allow
configure_allow() {
    echo "配置SSH白名单..."
    # 备份原文件
    cp /etc/hosts.allow /etc/hosts.allow.bak
    # 写入SSH允许规则
    echo "# SSH白名单配置 - 自动生成" > /etc/hosts.allow
    for ip in "${WHITE_LIST[@]}"; do
        echo "sshd: $ip" >> /etc/hosts.allow
        echo "  ✓ 添加白名单: $ip"
    done
    # 拒绝其他所有
    echo "ALL: ALL" >> /etc/hosts.deny
    echo "✓ hosts.allow配置完成"
}
# 执行配置
configure_allow

通过修改sshd_config配置

#!/bin/bash
# SSH访问白名单配置脚本 - sshd_config方式
# 定义白名单用户和IP
declare -A WHITE_LIST
WHITE_LIST=(
    ["admin"]="192.168.1.100 10.0.0.50"
    ["developer"]="192.168.1.0/24"
    ["devops"]="10.0.0.0/24 172.16.0.0/16"
)
# 配置sshd_config
configure_sshd() {
    local config_file="/etc/ssh/sshd_config"
    local backup_file="/etc/ssh/sshd_config.bak"
    echo "配置SSH白名单..."
    # 备份配置文件
    cp $config_file $backup_file
    # 添加AllowUsers配置
    {
        echo ""
        echo "# SSH用户白名单配置 - 自动生成"
        echo "# 格式: AllowUsers user@ip user@ip"
        for user in "${!WHITE_LIST[@]}"; do
            ips="${WHITE_LIST[$user]}"
            for ip in $ips; do
                echo "AllowUsers $user@$ip"
            done
        done
    } >> $config_file
    # 验证配置
    sshd -t && echo "✓ 配置文件语法正确" || {
        echo "✗ 配置文件错误,恢复备份"
        cp $backup_file $config_file
        exit 1
    }
    # 重启SSH服务
    systemctl restart sshd && echo "✓ SSH服务已重启"
}
# 执行配置
configure_sshd

iptables防火墙白名单

#!/bin/bash
# SSH访问白名单配置脚本 - iptables方式
# 配置参数
SSH_PORT=22
WHITE_LIST_IPS=(
    "192.168.1.100"
    "10.0.0.50"
    "172.16.0.0/24"
    "203.0.113.0/24"
)
configure_iptables() {
    echo "配置iptables SSH白名单..."
    # 清除现有SSH规则
    iptables -D INPUT -p tcp --dport $SSH_PORT -j ACCEPT 2>/dev/null
    # 添加白名单规则
    for ip in "${WHITE_LIST_IPS[@]}"; do
        iptables -A INPUT -p tcp --dport $SSH_PORT -s $ip -j ACCEPT
        echo "  ✓ 添加白名单: $ip"
    done
    # 拒绝其他SSH连接
    iptables -A INPUT -p tcp --dport $SSH_PORT -j DROP
    echo "✓ iptables SSH白名单配置完成"
    # 显示当前规则
    echo -e "\n当前SSH规则:"
    iptables -L INPUT -n -v | grep ":22"
}
# 保存iptables规则(Debian/Ubuntu)
save_iptables_rules() {
    if command -v netfilter-persistent &> /dev/null; then
        netfilter-persistent save
        echo "✓ iptables规则已保存"
    elif command -v iptables-save &> /dev/null; then
        iptables-save > /etc/iptables/rules.v4
        echo "✓ iptables规则已保存到 /etc/iptables/rules.v4"
    fi
}
# 执行配置
configure_iptables
save_iptables_rules

综合管理脚本

#!/bin/bash
# SSH白名单综合管理脚本
# 配置存储文件
CONFIG_FILE="/etc/ssh_whitelist.conf"
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
# 初始化配置
init_config() {
    if [ ! -f "$CONFIG_FILE" ]; then
        cat > "$CONFIG_FILE" << EOF
# SSH白名单配置
# 格式: IP地址
# 支持单个IP: 192.168.1.100
# 支持CIDR: 192.168.1.0/24
192.168.1.0/24
10.0.0.0/8
EOF
        echo "初始化配置文件: $CONFIG_FILE"
    fi
}
# 读取白名单
read_whitelist() {
    if [ -f "$CONFIG_FILE" ]; then
        grep -v "^#" "$CONFIG_FILE" | grep -v "^$"
    fi
}
# 添加IP到白名单
add_to_whitelist() {
    local ip=$1
    if [ -f "$CONFIG_FILE" ]; then
        echo "$ip" >> "$CONFIG_FILE"
        echo -e "${GREEN}✓ 添加 $ip 到白名单${NC}"
    fi
}
# 从白名单移除IP
remove_from_whitelist() {
    local ip=$1
    if [ -f "$CONFIG_FILE" ]; then
        sed -i "/^${ip}$/d" "$CONFIG_FILE"
        echo -e "${YELLOW}✓ 从白名单移除 $ip${NC}"
    fi
}
# 应用firewalld配置
apply_firewalld() {
    echo "应用firewalld配置..."
    # 移除现有限制
    firewall-cmd --permanent --remove-service=ssh >/dev/null 2>&1
    # 创建白名单区域
    firewall-cmd --permanent --new-zone=ssh_whitelist >/dev/null 2>&1
    # 添加白名单IP
    for ip in $(read_whitelist); do
        firewall-cmd --permanent --zone=ssh_whitelist --add-source="$ip"
        echo -e "${GREEN}  ✓ 添加白名单IP: $ip${NC}"
    done
    # 允许SSH服务
    firewall-cmd --permanent --zone=ssh_whitelist --add-service=ssh
    # 重新加载firewalld
    firewall-cmd --reload
    echo -e "${GREEN}✓ firewalld配置完成${NC}"
}
# 显示当前白名单
show_whitelist() {
    echo -e "${YELLOW}当前SSH白名单:${NC}"
    echo "------------------------"
    if [ -f "$CONFIG_FILE" ]; then
        cat -n "$CONFIG_FILE" | grep -v "^#" | grep -v "^$"
    fi
    echo "------------------------"
}
# 主菜单
main_menu() {
    while true; do
        echo -e "\n${YELLOW}SSH白名单管理工具${NC}"
        echo "1. 显示当前白名单"
        echo "2. 添加IP到白名单"
        echo "3. 从白名单移除IP"
        echo "4. 应用firewalld配置"
        echo "5. 退出"
        read -p "请选择操作 [1-5]: " choice
        case $choice in
            1) show_whitelist ;;
            2)
                read -p "输入IP地址或CIDR: " new_ip
                add_to_whitelist "$new_ip"
                ;;
            3)
                show_whitelist
                read -p "输入要移除的IP: " remove_ip
                remove_from_whitelist "$remove_ip"
                ;;
            4) apply_firewalld ;;
            5) exit 0 ;;
            *) echo -e "${RED}无效选择${NC}" ;;
        esac
    done
}
# 初始化
init_config
# 检查root权限
if [ "$EUID" -ne 0 ]; then 
    echo -e "${RED}请使用root权限运行此脚本${NC}"
    exit 1
fi
# 运行主菜单
main_menu

使用方法

  1. 安装依赖
    # Ubuntu/Debian
    apt-get update
    apt-get install iptables-persistent

CentOS/RHEL

yum install iptables-services


2. **设置脚本执行权限**:
```bash
chmod +x ssh_whitelist.sh
  1. 运行脚本
    sudo ./ssh_whitelist.sh

注意事项

  1. 安全备份:修改配置前务必备份原文件
  2. 测试连接:配置完成后,用另一个终端测试连接
  3. 永久生效:iptables规则需要保存才能在重启后生效
  4. 兼容性:不同Linux发行版配置方式可能不同

建议根据你的系统环境选择合适的配置方式,推荐使用firewalld或iptables方式,因为它们更灵活且不易出错。

抱歉,评论功能暂时关闭!