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 服務實戰:告別 Telnet 明文傳輸,構建加密遠程運維通道

編輯:Linux 教學

為什麼必須拋棄 Telnet?

Telnet 採用明文傳輸:用戶名、密碼、命令、輸出全部在網絡裸奔。任何掌握抓包工具 (Wireshark、tcpdump) 的攻擊者,均可輕鬆竊取憑證。更危險的是中間人攻擊 (MITM):攻擊者攔截連接,偽裝成 Server 接收客戶端數據,再偽裝成客戶端轉發給真 Server,雙方毫無察覺,數據被竊取、篡改、注入惡意代碼。

SSH 協議的安全優勢

  • 強加密傳輸:對稱加密 (AES/ChaCha20) 保護數據機密性;非對稱加密 (RSA/Ed25519/ECDSA) 實現密鑰交換與主機認證。
  • 完整性校驗:HMAC 防篡改。
  • 多重認證:密碼、公鑰、鍵盤互動、GSSAPI、多因子 (MFA) 靈活組合。
  • 通道轉發:X11 轉發、端口轉發、SOCKS 代理,一條連接滿足多樣需求。

進階加固:SSH + TCP Wrappers 雙重防護

TCP Wrappers (/etc/hosts.allow, /etc/hosts.deny) 基於主機名/IP 訪問控制列表 (ACL),在服務進程接受連接前攔截:

  • 防止主機名/IP 欺騙 (雙向 DNS 驗證、拒絕源路由)。
  • 不提供加密,但與 SSH 結合:TCP Wrappers 守門 (拒非法 IP),SSH 加密通道 (防竊聽/篡改),纵深防禦。

實戰部署指南

1. 安裝 OpenSSH (主流發行版預裝,若缺失)

# RHEL/CentOS/Fedora
dnf install -y openssh-server openssh-clients

# Debian/Ubuntu
apt update && apt install -y openssh-server openssh-client

# 源碼編譯 (如需特定版本/特性)
wget https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-9.8p1.tar.gz
tar xzf openssh-*.tar.gz && cd openssh-*
./configure --prefix=/usr --sysconfdir=/etc/ssh --with-pam --with-kerberos5
make && make install

編譯安裝審計技巧:安裝前 find / > /tmp/before,安裝後 find / > /tmp/after,diff /tmp/before /tmp/after > /tmp/changes,永久記錄文件變更,利於後續審計與回滾。

2. 核心配置加固 (/etc/ssh/sshd_config)

Port 2222                    # 修改默認端口,減少自動掃描噪音
Protocol 2                   # 僅啟用 SSH-2 協議
PermitRootLogin no           # 禁止 root 直接登入 (核心原則)
PubkeyAuthentication yes     # 啟用公鑰認證
PasswordAuthentication no    # 禁用密碼認證 (強制公鑰)
PermitEmptyPasswords no      # 禁止空密碼
MaxAuthTries 3               # 限制嘗試次數
ClientAliveInterval 300      # 保活間隔 5 分鐘
ClientAliveCountMax 2        # 2 次無響應斷開
AllowUsers admin ops         # 白名單限制允許登入用戶
# 或 AllowGroups sshusers    # 基於組控制
Banner /etc/ssh/banner       # 登入警告横幅

3. TCP Wrappers 配置

# /etc/hosts.deny
sshd: ALL                    # 默認拒絕所有

# /etc/hosts.allow
sshd: 192.168.1.0/255.255.255.0 10.0.0.5  # 僅允許內網管理段及跳板機
sshd: .example.com           # 允許特定域名 (需 DNS 正反解一致)

4. 金鑰對生成與分發 (Ed25519 推薦,比 RSA 2048 更短更強)

# 客戶端生成
ssh-keygen -t ed25519 -a 100 -C "admin@workstation-2024"

# 部署公鑰到服務端
ssh-copy-id -i ~/.ssh/id_ed25519.pub -p 2222 admin@server_ip
# 或手動: cat ~/.ssh/id_ed25519.pub | ssh -p 2222 admin@server_ip "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

5. 啟動與防火牆放行

systemctl enable --now sshd
firewall-cmd --permanent --add-port=2222/tcp && firewall-cmd --reload
# 或 iptables -A INPUT -p tcp --dport 2222 -j ACCEPT

最佳實踐總結

  1. 禁用 root 登入 + 禁用密碼認證 + 強制公鑰 是鐵律。
  2. 修改默認端口、配置 TCP Wrappers、啟用 Fail2ban 防暴力破解。
  3. 定期審計 /var/log/secure (RHEL) 或 /var/log/auth.log (Debian)。
  4. 跳板機/堡壘機統一出口,禁用 SSH Agent Forwarding 除非必須。

SSH 是 Linux 遠程運維的生命線,配置得當,它是堅不可摧的加密隧道;配置失誤,它是敵人通往核心的高速公路。

Copyright © Windows教程網 All Rights Reserved