Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux SSH 遠程管理實戰全書:從安裝配置、金鑰認證到堡壘機審計體系構建

編輯:Linux 教學

SSH (Secure Shell) 以加密傳輸徹底取代明文 Telnet,成為 Linux 遠程管理標準。本文涵蓋 OpenSSH 服務端/客戶端安裝、金鑰對生成與分發、配置加固、多因子認證、跳板機/堡壘機架構、SCP/SFTP 文件傳輸、隧道轉發、自動化運維的完整實戰指南。

一、OpenSSH 服務端部署與加固


# 安裝 (發行版包管理器)
apt install openssh-server        # Debian/Ubuntu
dnf install openssh-server        # RHEL/Fedora
pacman -S openssh                 # Arch

# 核心配置 /etc/ssh/sshd_config
Port 22222                        # 非標準端口
PermitRootLogin no                # 禁用 Root 登入
PubkeyAuthentication yes          # 僅允許公鑰認證
PasswordAuthentication no         # 禁用密碼認證
PermitEmptyPasswords no
ChallengeResponseAuthentication no
UsePAM yes                        # 支援 MFA
AuthenticationMethods publickey   # 強制公鑰
MaxAuthTries 3
LoginGraceTime 30
ClientAliveInterval 300
ClientAliveCountMax 2
AllowUsers opsuser@10.0.0.0/8     # 限制用戶+來源 IP
Banner /etc/ssh/banner.txt        # 法律警告

# 重載生效
systemctl reload sshd

二、金鑰對生成、分發與管理

1. 客戶端生成 Ed25519 金鑰 (現代推薦,比 RSA 2048 更強更快)


ssh-keygen -t ed25519 -a 100 -C "user@host-$(date +%F)"
# -a 100 迭代輪數增加暴力破解成本
# 生成:~/.ssh/id_ed25519 (私鑰)、~/.ssh/id_ed25519.pub (公鑰)

2. 分發公鑰到服務端


# 方法一:ssh-copy-id (需臨時啟用密碼認證)
ssh-copy-id -i ~/.ssh/id_ed25519.pub -p 22222 user@server

# 方法二:手動追加 (服務端)
mkdir -p ~/.ssh
chmod 700 ~/.ssh
cat >> ~/.ssh/authorized_keys <<'EOF'
ssh-ed25519 AAAAC3... user@host-2024-01-15
EOF
chmod 600 ~/.ssh/authorized_keys
chown -R user:user ~/.ssh

3. 多金鑰管理:config 別名與 IdentityFile


# ~/.ssh/config
Host prod-web
    HostName 10.1.1.10
    User opsuser
    Port 22222
    IdentityFile ~/.ssh/id_ed25519_prod
    IdentitiesOnly yes

Host github
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_github
    IdentitiesOnly yes

三、多因子認證 (MFA) :Google Authenticator / TOTP


# 服務端安裝
apt install libpam-google-authenticator

# 用戶初始化
google-authenticator
# 掃描二維碼至 Authenticator App,記錄應急刮刮卡代碼

# PAM 配置 /etc/pam.d/sshd
auth required pam_google_authenticator.so nullok
# nullok 允許未設置 MFA 的用戶暫時用密鑰登入,全員遷移後移除

# sshd_config 確認
ChallengeResponseAuthentication yes
AuthenticationMethods publickey,keyboard-interactive

四、跳板機/堡壘機架構


# SSH 配置透明代理 (ProxyJump)
Host *.internal
    ProxyJump bastion.example.com
    User opsuser
    IdentityFile ~/.ssh/id_ed25519

# 單次使用
ssh -J bastion.example.com internal-server

# 專業堡壘機方案
# JumpServer (開源、Web UI、資產授權、會話回放、命令過濾)
# Apache Guacamole (無客戶端、RDP/SSH/VNC 統一入口)
# Teleport (證書基礎、Kubernetes 整合、審計日誌)

五、SCP/SFTP 文件傳輸與 rsync 高效同步

六、SSH 隧道與端口轉發


# 本地轉發:本地 8080 -> 遠程服務器的 localhost:80
ssh -L 8080:localhost:80 -p 22222 user@server

# 遠程轉發:遠程服務器 8080 -> 本地 localhost:3000
ssh -R 8080:localhost:3000 -p 22222 user@server

# 動態 SOCKS5 代理:本地 1080 -> 遠程網絡
ssh -D 1080 -p 22222 user@server
# 瀏覽器/代理工具設置 SOCKS5 127.0.0.1:1080

七、自動化運維:Ansible + SSH 原生整合


# inventory/hosts.yml
all:
  vars:
    ansible_user: opsuser
    ansible_port: 22222
    ansible_ssh_private_key_file: ~/.ssh/id_ed25519
  hosts:
    web1:
      ansible_host: 10.1.1.10
    web2:
      ansible_host: 10.1.1.11

# 執行
ansible all -m ping
ansible-playbook deploy.yml

八、審計與合規:會話記錄與命令過濾

  • OpenSSH 原生:LogLevel VERBOSE 記錄登入/登出、ForceCommand 限制命令
  • auditd:監控 /usr/sbin/sshd、/bin/bash 關鍵調用
  • 專業堡壘機:JumpServer/Teleport 完整會話回放 (類終端錄像)、關鍵命令攔截、實時告警

九、常見故障排查速查

工具典型命令優勢
scpscp -P 22222 file.tar.gz user@host:/path/簡單、無需額外服務
sftpsftp -P 22222 user@host 交互式目錄操作、斷點續傳
rsyncrsync -avz -e 'ssh -p 22222' /src/ user@host:/dst/增量、壓縮、保留屬性、帶寬限制
sshfssshfs -p 22222 user@host:/remote /mnt/local掛載遠程目錄如本地

最佳實踐清單:Ed25519 金鑰、禁用密碼/Root、MFA 全覆蓋、非標端口、堡壘機統一入口、rsync 替代 scp、配置納入 Git、定期輪換金鑰、審計回放就緒。SSH 不僅是遠程登入工具,更是現代基礎設施自動化的神經中樞。

Copyright © Windows教程網 All Rights Reserved
現象排查命令常見原因
Permission denied (publickey)ssh -vvv 客戶端、journalctl -u sshd -f 服務端公鑰未授權、權限 700/600 錯誤、SELinux 標籤、Home 目錄加密
Connection refusedss -ntlp | grep sshd服務未啟動、端口不匹配、防火牆阻擋
Timeoutping/traceroute網絡不通、安全組/防火牆、IP 變更
Host key verification failedssh-keygen -R hostname服務端重裝/金鑰輪換、中間人攻擊
Broken pipe / Write failed調整 ClientAliveInterval長連接空閒被防火牆切斷