SSH是Linux服务器运维的核心通道,密钥认证、跳板机访问控制和批量操作自动化构成了服务器安全运维的基础设施。通过OpenSSH密钥对管理、跳板机ProxyJump转发和Ansible批量编排,可以实现数百台服务器的安全访问与高效运维。本文从密钥生成、跳板机配置到Ansible自动化部署,完整梳理服务器SSH管理的工程实践。
SSH密钥对生成与安全配置
密钥认证相比密码认证有两个核心优势:抗暴力破解和免交互登录。ED25519是目前推荐的密钥算法,相比RSA-2048密钥更短、签名更快、安全性更高。
# 生成ED25519密钥对
ssh-keygen -t ed25519 -C "admin@yunthe.com" -f ~/.ssh/id_ed25519_prod
# 生成带密码保护的密钥(推荐生产环境)
ssh-keygen -t ed25519 -a 100 -f ~/.ssh/id_ed25519_secure
# 查看密钥指纹
ssh-keygen -lf ~/.ssh/id_ed25519_prod.pub
公钥分发到目标服务器使用ssh-copy-id命令,该命令会自动处理权限设置和authorized_keys文件追加。
# 分发公钥到目标服务器
ssh-copy-id -i ~/.ssh/id_ed25519_prod.pub user@192.168.1.100
# 批量分发公钥(配合密码认证临时使用)
for host in 192.168.1.{101..120}; do
ssh-copy-id -i ~/.ssh/id_ed25519_prod.pub user@$host
done
服务端sshd_config的安全加固配置:
# /etc/ssh/sshd_config 关键参数
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
LoginGraceTime 30
AllowUsers admin deploy
Protocol 2
X11Forwarding no
MaxSessions 10
ClientAliveInterval 300
ClientAliveCountMax 2
# 重载配置
sudo systemctl reload sshd
跳板机ProxyJump配置与多级转发
生产环境中服务器通常不直接暴露公网SSH端口,通过跳板机集中管理访问入口。OpenSSH 7.3+原生支持ProxyJump参数,配置简洁且无需额外安装工具。
# ~/.ssh/config 跳板机配置
Host bastion
HostName bastion.yunthe.com
User admin
Port 2222
IdentityFile ~/.ssh/id_ed25519_prod
# 限制连接复用
ControlMaster auto
ControlPath ~/.ssh/cm-%r@%h:%p
ControlPersist 10m
Host web-*
ProxyJump bastion
User admin
IdentityFile ~/.ssh/id_ed25519_prod
Host web-01
HostName 10.0.1.11
Host web-02
HostName 10.0.1.12
Host db-01
HostName 10.0.2.21
# 直接通过跳板机连接
ssh web-01
# 等价于
ssh -J admin@bastion.yunthe.com:2222 admin@10.0.1.11
多级跳板场景下,ProxyJump支持链式配置:
# 多级跳板:外网跳板 -> 内网跳板 -> 目标服务器
Host inner-bastion
HostName 10.0.0.5
ProxyJump bastion
User admin
Host target-host
HostName 10.0.10.50
ProxyJump inner-bastion
User deploy
SSH连接复用与会话管理
连接复用通过ControlMaster机制保持TCP连接长连接,避免每次SSH操作都重新建立握手和认证流程。批量运维场景下可以将连接建立时间从200ms级别降低到接近零开销。
# ~/.ssh/config 连接复用全局配置
Host *
ControlMaster auto
ControlPath ~/.ssh/cm-%r@%h:%p
ControlPersist 30m
ServerAliveInterval 60
ServerAliveCountMax 3
Compression yes
使用ssh -O管理持久连接:
# 检查连接状态
ssh -O check web-01
# 主动关闭连接
ssh -O exit web-01
# 强制取消所有后台连接
ssh -O cancel web-01
Ansible批量运维自动化配置
Ansible基于SSH协议执行批量操作,无需在目标服务器安装agent。配合跳板机配置,可以实现跨网段服务器的统一管理。
# ansible.cfg 配置
[defaults]
inventory = ./inventory
remote_user = admin
private_key_file = ~/.ssh/id_ed25519_prod
host_key_checking = False
timeout = 30
forks = 20
# 跳板机配置
ssh_args = -o ControlMaster=auto -o ControlPersist=30m
# 事实缓存
gathering = smart
fact_caching = jsonfile
fact_caching_connection = ./fact_cache
# inventory.ini 主机清单
[webservers]
web-01 ansible_host=10.0.1.11
web-02 ansible_host=10.0.1.12
web-03 ansible_host=10.0.1.13
[dbservers]
db-01 ansible_host=10.0.2.21
db-02 ansible_host=10.0.2.22
[all:vars]
ansible_ssh_common_args='-o ProxyJump=admin@bastion.yunthe.com:2222'
批量执行命令和playbook示例:
# 批量执行ping测试
ansible all -m ping
# 批量执行shell命令
ansible webservers -m shell -a "df -h / && free -m"
# 批量部署Nginx配置
# playbook.yml
---
- name: 批量部署Nginx配置
hosts: webservers
become: yes
tasks:
- name: 安装Nginx
package:
name: nginx
state: present
- name: 同步配置文件
copy:
src: ./nginx.conf
dest: /etc/nginx/nginx.conf
backup: yes
notify: reload nginx
- name: 确保Nginx运行
service:
name: nginx
state: started
enabled: yes
handlers:
- name: reload nginx
service:
name: nginx
state: reloaded
# 执行playbook
ansible-playbook -i inventory.ini playbook.yml --limit web-01,web-02
SSH端口转发与安全隧道配置
SSH端口转发在不开放额外防火墙端口的前提下实现内网服务的安全访问。本地转发将远程端口映射到本地,远程转发则相反。
# 本地转发:访问数据库内网端口
ssh -L 3306:10.0.2.21:3306 web-01 -N -f
# 之后本地连接 localhost:3306 即可访问内网数据库
# 远程转发:将本地服务暴露到跳板机
ssh -R 8080:localhost:80 bastion -N -f
# 跳板机上访问 localhost:8080 即可访问本地服务
# 动态端口转发(SOCKS代理)
ssh -D 1080 bastion -N -f
# 配置浏览器使用 SOCKS5 代理 localhost:1080
# 在~/.ssh/config中持久化转发配置
Host db-tunnel
HostName web-01
LocalForward 3306 10.0.2.21:3306
User admin
ExitOnForwardFailure yes
SSH密钥管理、跳板机访问控制和批量运维自动化构成了服务器运维的安全基座。密钥认证消除密码泄露风险,ProxyJump简化跳板访问路径,Ansible提供声明式批量编排能力,端口转发实现零额外端口暴露的内网访问。这些工具的组合使用可以将服务器日常运维效率提升一个数量级,同时将安全风险控制在可接受范围内。
原创文章,作者:小编,如若转载,请注明出处:https://www.yunthe.com/linux-fu-wu-qi-ssh-mi-yao-ren-zheng-yu-tiao-ban-ji-pei-zhi/