本文目录导读:

在Shell脚本中限制用户连接数有多种方法,我为您介绍几种常用的实现方式:
使用SSH配置限制(推荐)
修改SSH配置文件
# 编辑/etc/ssh/sshd_config sudo vi /etc/ssh/sshd_config # 添加或修改以下配置 MaxSessions 5 # 全局最大会话数 MaxStartups 10:30:100 # 并发连接限制(开始:概率拒绝:最大)
按用户限制
# 在sshd_config中添加
Match User username1,username2
MaxSessions 2
MaxStartups 2
重启SSH服务:
sudo systemctl restart sshd
Shell脚本实现连接数监控
#!/bin/bash
# 连接数限制脚本
MAX_CONNECTIONS=3
USER="target_user"
while true; do
# 获取指定用户的SSH连接数
current_connections=$(ss -tn src :22 | grep -c "ESTAB")
user_connections=$(who | grep -c "^$USER")
if [ $user_connections -gt $MAX_CONNECTIONS ]; then
echo "用户 $USER 连接数超限"
# 踢出多余的连接
who | grep "^$USER" | head -n -$MAX_CONNECTIONS | while read line; do
pid=$(echo $line | awk '{print $2}' | xargs ps -u $USER -o pid=)
kill $pid 2>/dev/null
done
fi
sleep 10
done
使用PAM模块限制
# 安装pam_limits sudo apt-get install libpam-modules # Debian/Ubuntu sudo yum install pam # CentOS/RHEL # 编辑/etc/security/limits.conf sudo vi /etc/security/limits.conf # 添加限制规则 username hard maxlogins 3 @groupname hard maxlogins 5 * hard maxsyslogins 10
使用iptables限制(高级)
#!/bin/bash
# iptables连接数限制
MAX_SSH=3
CHAIN_NAME="SSH_RATE_LIMIT"
# 创建自定义链
iptables -N $CHAIN_NAME 2>/dev/null
# 限制每个IP的连接数
iptables -A $CHAIN_NAME -p tcp --syn --dport 22 -m connlimit \
--connlimit-above $MAX_SSH --connlimit-mask 32 \
-j REJECT
# 应用到INPUT链
iptables -A INPUT -p tcp --dport 22 -j $CHAIN_NAME
# 特定用户的连接数限制(需要xtables-addons)
iptables -A $CHAIN_NAME -m owner --uid-owner 1000 -m connlimit \
--connlimit-above 2 -j REJECT
完整的监控脚本示例
#!/bin/bash
# 连接数限制监控脚本
CONFIG_FILE="/etc/user_connection_limits.conf"
LOG_FILE="/var/log/connection_monitor.log"
# 创建配置文件示例
create_config() {
if [ ! -f "$CONFIG_FILE" ]; then
cat > "$CONFIG_FILE" << EOF
# 用户连接数限制配置
# 格式: 用户名:最大连接数
root:2
www-data:3
mysql:5
EOF
fi
}
# 检查并限制连接
check_connections() {
while IFS=':' read -r user max_conn; do
# 跳过注释和空行
[[ "$user" =~ ^#.*$ || -z "$user" ]] && continue
# 获取当前用户连接数
current=$(who | grep -c "^$user ")
if [ "$current" -gt "$max_conn" ]; then
echo "$(date): 用户 $user 连接超限 ($current > $max_conn)" >> "$LOG_FILE"
# 踢出超出的连接
who | grep "^$user " | tail -n +$((max_conn + 1)) | \
awk '{print $2}' | while read tty; do
pid=$(ps -t "$tty" -o pid= 2>/dev/null)
if [ -n "$pid" ]; then
kill -9 $pid
echo "$(date): 踢出用户 $user 的连接 (PID: $pid, TTY: $tty)" >> "$LOG_FILE"
fi
done
fi
done < "$CONFIG_FILE"
}
# 发送警报(可选)
send_alert() {
local user=$1
local count=$2
# 可以使用邮件、微信、钉钉等通知
# echo "用户 $user 连接数超限: $count" | mail -s "连接数警报" admin@example.com
}
# 主循环
main() {
create_config
while true; do
check_connections
sleep 30
done
}
# 启动监控
main &
# 添加系统服务支持
# 保存为 /etc/init.d/connection-limiter 或 systemd服务
使用systemd服务管理
创建systemd服务文件 /etc/systemd/system/user-connection-limiter.service:
[Unit] Description=User Connection Limiter After=network.target [Service] Type=simple ExecStart=/usr/local/bin/connection-limiter.sh Restart=always User=root [Install] WantedBy=multi-user.target
启用服务:
sudo systemctl daemon-reload sudo systemctl enable user-connection-limiter sudo systemctl start user-connection-limiter
最佳实践建议
- 配置文件管理:使用独立的配置文件管理限制规则
- 日志记录:记录所有限制操作以便审计
- 警报机制:整合通知系统(邮件、Slack等)
- 定期清理:自动清理过期的连接记录
- 灰度测试:先在测试环境验证后再应用到生产环境
根据实际需求选择合适的方法,对于生产环境推荐使用SSH配置+PAM模块的组合方式,既稳定又高效。