1. 需求

内网中有个非常小的独立环境需要有堡垒机实现登录工作.
客户机通过防火墙规则拒绝其他服务器的连接请求.部署一个堡垒机会显得稍微臃肿一点.正好看到了阿里云的Linux堡垒机界面,那么就用shell搞个差不多的功能,在一台管理机上实现.
回放功能用cockpit来实现

相关服务:日本云服务器租用

2. 配置文件

2.1 创建堡垒机账号

创建堡垒机账号并设置密码

useradd bastion
echo "June#20250616shanghai| passwd --stdin bastion"

2.2 创建inventory文件

这个就参考ansible inventory
按 名称|IP:PORT|协议|用户名|用户组|备注 的格式注册服务器清单

[root@localhost ~]# cat hosts.conf 
#格式: 名称|IP:PORT|协议|用户名|用户组|备注
Webserver-1|192.168.1.1|22|ssh|root|web|
Webserver-2|192.168.1.2|22|ssh|root|web|
Webserver-3|192.168.1.3|22|ssh|root|web|
Webserver-4|192.168.1.4|22|ssh|root|web|
Webserver-5|192.168.1.5|22|ssh|root|web|
Webserver-6|192.168.1.6|22|ssh|root|web|
Webserver-11|192.168.1.11|22|ssh|root|web|
Webserver-12|192.168.1.12|22|ssh|root|web|
Webserver-13|192.168.1.13|22|ssh|root|web|
Webserver-14|192.168.1.14|22|ssh|root|web|
Webserver-15|192.168.1.15|22|ssh|root|web|
Webserver-16|192.168.1.16|22|ssh|root|web|
DBserver-1|192.168.1.101|3322|ssh|root|db|
DBserver-2|192.168.1.102|3322|ssh|root|db|
DBserver-3|192.168.1.103|3322|ssh|root|db|
DBserver-4|192.168.1.104|3322|ssh|root|db|
DBserver-5|192.168.1.105|3322|ssh|root|db|
DBserver-6|192.168.1.106|3322|ssh|root|db|
Rocky-8.10|192.168.2.80|22|ssh|root||
Centos7.9|192.168.2.7|22|ssh|root||
Centos7-1|192.168.2.17|22|ssh|root|web|
DC-k8s-master01|192.168.3.11|6022|ssh|root|k8s|
DC-k8s-master02|192.168.3.12|6022|ssh|root|k8s|
DC-k8s-master03|192.168.3.13|6022|ssh|root|k8s|
DC-k8s-etcd01|192.168.3.21|6022|ssh|root|k8s|
DC-k8s-etcd02|192.168.3.22|6022|ssh|root|k8s|
DC-k8s-etcd03|192.168.3.23|6022|ssh|root|k8s|
DC-k8s-node01|192.168.3.101|6022|ssh|root|k8s|
DC-k8s-node02|192.168.3.102|6022|ssh|root|k8s|
DC-k8s-node03|192.168.3.103|6022|ssh|root|k8s|
DC-k8s-node04|192.168.3.104|6022|ssh|root|k8s|
DC-k8s-node05|192.168.3.105|6022|ssh|root|k8s|
DC-k8s-node06|192.168.3.106|6022|ssh|root|k8s|
DC-k8s-node07|192.168.3.107|6022|ssh|root|k8s|
Bastion-Host01|192.168.8.101|2222|ssh|root|infra|
Bastion-Host02|192.168.8.102|2222|ssh|root|infra|
Bastion-Host03|192.168.8.103|2222|ssh|root|infra|
Ansible-automation-platform|192.168.2.80|22|ssh|root|ansible|
Webserver-1|192.168.1.1|22|ssh|root|web|
Webserver-2|192.168.1.2|22|ssh|root|web|
Webserver-3|192.168.1.3|22|ssh|root|web|
Webserver-4|192.168.1.4|22|ssh|root|web|
Webserver-5|192.168.1.5|22|ssh|root|web|
Webserver-6|192.168.1.6|22|ssh|root|web|
Webserver-11|192.168.1.11|22|ssh|root|web|
Webserver-12|192.168.1.12|22|ssh|root|web|
Webserver-13|192.168.1.13|22|ssh|root|web|
Webserver-14|192.168.1.14|22|ssh|root|web|
Webserver-15|192.168.1.15|22|ssh|root|web|
Webserver-16|192.168.1.16|22|ssh|root|web|
DBserver-1|192.168.1.101|3322|ssh|root|db|
DBserver-2|192.168.1.102|3322|ssh|root|db|
DBserver-3|192.168.1.103|3322|ssh|root|db|
DBserver-4|192.168.1.104|3322|ssh|root|db|
DBserver-5|192.168.1.105|3322|ssh|root|db|
DBserver-6|192.168.1.106|3322|ssh|root|db|
Rocky-8.10|192.168.2.80|22|ssh|root|infra|
Centos7.9|192.168.2.7|22|ssh|root|infra|

2.3 主脚本

/root/bastion.sh

#!/bin/bash

# =============================================
# 主机管理脚本 - Web3 主机连接工具
# 功能:通过光标上下选择主机,并通过SSH连接
# =============================================

CONFIG_FILE="hosts.conf"
PAGE_SIZE=35  # 每页显示的主机数量

# 颜色定义
GREEN='\033[0;32m'
NC='\033[0m'          # 恢复默认颜色
REVERSE='\033[7m'     # 反色(用于高亮当前选中项)

# =============================================
# 函数:读取配置文件并加载主机信息
# =============================================
load_hosts() {
    declare -a hosts=()
    while IFS= read -r line; do
        # 跳过注释行和空行
        [[ $line =~ ^# ]] && continue
        [[ -z $line ]] && continue
        
        # 解析字段(名称|IP|端口|协议|用户|组|备注)
        IFS='|' read -r name ip port proto user group comment <<< "$line"
        
        # 设置默认值
        : ${port:=22}
        : ${proto:=ssh}
        : ${group:=default}
        
        # 组合IP和端口
        ip_port="$ip:$port"
        hosts+=("$name|$ip_port|$proto|$user|$group|$comment")
    done < "$CONFIG_FILE"
    
    echo "${hosts[@]}"
}

# =============================================
# 函数:显示表头
# =============================================
show_header() {
    echo -e "${GREEN}NUM    NAME 		                   IP:PORT               PROTO   USER  GROUP      COMMENT${NC}"
    echo -e "${GREEN}----------------------------------------------------------------------------------------------------------------${NC}"
}

# =============================================
# 函数:显示当前页的主机列表
# =============================================
show_hosts() {
    local i
    for ((i=page_start; i<page_start+PAGE_SIZE && i<total; i++)); do
        if [[ $i -eq $current_selection ]]; then
            # 当前选中的主机用反色显示
            echo -ne "\033[7m"  # 反色
        fi
        
        IFS='|' read -r name ip_port proto user group comment <<< "${hosts[i]}"
        
        # 提取IP和端口
        ip="${ip_port%:*}"
        port="${ip_port#*:}"
        
        # 格式化输出,确保对齐
        printf "%-6s %-35s %-24s %-6s %-5s %-10s %-20s\n" \
               "$((i+1))" "$name" "$ip_port" "$proto" "$user" "$group" "$comment"
        
        if [[ $i -eq $current_selection ]]; then
            echo -ne "\033[0m"  # 恢复默认颜色
        fi
    done
}

# =============================================
# 主程序
# =============================================

# 读取主机配置
hosts=($(load_hosts))
total=${#hosts[@]}
current_selection=0  # 当前选中的主机索引
page_start=0         # 当前页起始索引

# =============================================
# 主循环
# =============================================
while true; do
    clear
    
    show_header
    show_hosts
    
    echo -e "${GREEN}----------------------------------------------------------------------------------------------------------------${NC}"
    echo -e "${GREEN}当前页: $(( (page_start / PAGE_SIZE) + 1 ))/$(( (total + PAGE_SIZE - 1) / PAGE_SIZE ))${NC}"
    echo -e "${GREEN}当前选中: $((current_selection + 1))/${total}${NC}"
    echo -e "${GREEN}操作: ↑↓ 选择主机, Enter 确认连接, q 退出${NC}"
    
    # 读取单个按键输入
    read -rsn1 key
    
    case $key in
        $'\x1b')  # 处理方向键(ESC序列)
            read -rsn2 -t 0.1 rest  # 读取剩余的ESC序列
            case "$rest" in
                "[A")  # 上箭头
                    if [[ $current_selection -gt 0 ]]; then
                        ((current_selection--))
                        # 如果滚动到上一页
                        if [[ $current_selection -lt $page_start ]]; then
                            page_start=$((current_selection))
                        fi
                    fi
                    ;;
                "[B")  # 下箭头
                    if [[ $current_selection -lt $((total - 1)) ]]; then
                        ((current_selection++))
                        # 如果滚动到下一页
                        if [[ $current_selection -ge $((page_start + PAGE_SIZE)) ]]; then
                            page_start=$((current_selection - PAGE_SIZE + 1))
                        fi
                    fi
                    ;;
            esac
            ;;
        "")  # Enter键
            if [[ $total -gt 0 ]]; then
                IFS='|' read -r name ip_port proto user group comment <<< "${hosts[current_selection]}"
                ip="${ip_port%:*}"
                port="${ip_port#*:}"
                
                echo -e "${NC}"
                echo "正在连接 $user@$ip (端口: $port) ..."
                
                # 调试信息 - 显示实际执行的命令
                echo "调试: 执行命令: ssh -p $port $user@$ip"
                
                # 实际执行SSH连接
                ssh -p "$port" "$user@$ip"
                
                # 连接完成后返回脚本
                echo -e "${GREEN}连接已关闭,返回主机选择界面...${NC}"
            fi
            ;;
        "q")  # 退出
            exit 0
            ;;
        *)  # 其他按键
            echo "错误: 无效输入!"
            read -p "按回车继续..."
            ;;
    esac
done

2.4 禁止bastion用户执行shell命令

将bastion用户的shell锁定在堡垒机登录界面,它只能登录授权的服务器,但无法在本机上执行任何shell

usermod -s  /home/bastion/bastion.sh bastion

3. 安装配置cockpit

3.1 安装Cockpit

由于堡垒机需要到一点审计的功能,我们使用Cockpit来实现

yum install -y cockpit-composer cockpit-machines cockpit-pcp cockpit-podman cockpit-session-recording tlog
systemctl restart pmlogger
systemctl enable --now cockpit.socket

在这里插入图片描述

3.2 配置Cockpit

用服务器ip:9090打开cockpit管理页面,并使用root用户登录
在这里插入图片描述

单击主菜单中的 Session Recording ,然后单击齿轮图标。
在这里插入图片描述

导航到 SSSD Config 并选择 Scope →Some,bastion,点击Save
在这里插入图片描述

4.测试

先对清单中的服务器进行免密授权

# 生成秘钥(只要运行1次,有了就不需要再次运行了)
ssh-keygen
# 进行免密
ssh-copy-id $ip

测试运行

chmod +x /root/bastion.sh
/root/bastion.sh

在这里插入图片描述
按键盘上 下键选择服务器,到了35以后会更新出后续的清单
在这里插入图片描述
如果要返回就一直按上,它又能显示出之前的服务器清单
选择到要进入的服务器按回车自动连接
在这里插入图片描述
确认ip地址
在这里插入图片描述
按ctrl+d返回清单
在这里插入图片描述
我们再回到cockpit上看下录屏审计功能是否完成
在这里插入图片描述
看下我们具体做了什么
我们可以按播放键或者拖动进度条查看具体做了哪些事情,甚至于执行的命令在红框内也做了展示
在这里插入图片描述
好了.堡垒机的大部分功能我们都实现了