Linux SSH 连接配置教程
目录
一、SSH 连接基础
1.1 SSH 工作流程
sequenceDiagram
participant Client as SSH客户端
participant Server as SSH服务器
participant Auth as 认证服务
Client->>Server: 1. TCP三次握手
Server->>Client: 2. 建立连接
Client->>Server: 3. 协议版本协商
Server->>Client: 4. 密钥交换
Client->>Server: 5. 用户认证
Server->>Auth: 6. 验证用户
Auth->>Server: 7. 认证结果
Server->>Client: 8. 认证成功
Client->>Server: 9. 打开Shell会话
1.2 常用 SSH 客户端
| 客户端 | 平台 | 特点 |
|---|---|---|
| OpenSSH | Linux/macOS/Windows | 命令行,默认安装 |
| PuTTY | Windows | 图形界面,免费 |
| Termius | 全平台 | 跨平台,现代化界面 |
| Windows Terminal | Windows | Win10/11 自带 |
| MobaXterm | Windows | 功能丰富,绿色版 |
1.3 连接基本语法
# 基本连接
ssh username@hostname_or_ip
# 指定端口
ssh -p 2222 username@hostname
# 指定密钥文件
ssh -i ~/.ssh/my_key username@hostname
# 完整选项
ssh -p 22 -i ~/.ssh/id_rsa -o PreferredAuthentications=publickey username@hostname
二、Ubuntu/Debian 系列
2.1 安装 OpenSSH 服务器
flowchart TD
A[Ubuntu/Debian] --> B[安装openssh-server]
B --> C[检查服务状态]
C --> D[配置防火墙]
D --> E[修改配置文件]
E --> F[重启服务]
# 1. 安装 OpenSSH 服务器
sudo apt update
sudo apt install -y openssh-server
# 2. 检查服务状态
sudo systemctl status ssh
# 或
sudo systemctl status sshd
2.2 服务管理命令
# 启动服务
sudo systemctl start ssh
# 停止服务
sudo systemctl stop ssh
# 重启服务
sudo systemctl restart ssh
# 设置开机自启
sudo systemctl enable ssh
# 禁用开机自启
sudo systemctl disable ssh
2.3 防火墙配置
# Ubuntu 默认使用 UFW
# 允许 SSH 连接
sudo ufw allow ssh
# 或指定端口
sudo ufw allow 22/tcp
# 查看防火墙状态
sudo ufw status
# 启用防火墙(如未启用)
sudo ufw enable
2.4 SSH 配置示例
# 编辑 SSH 配置文件
sudo vim /etc/ssh/sshd_config
# 常用配置项
Port 22 # 监听端口
PermitRootLogin yes # 允许root登录(生产环境建议no)
PubkeyAuthentication yes # 启用公钥认证
PasswordAuthentication yes # 启用密码认证
MaxAuthTries 3 # 最大认证尝试次数
ClientAliveInterval 300 # 客户端存活检测(秒)
ClientAliveCountMax 2 # 断开前的存活检测次数
# 重启服务使配置生效
sudo systemctl restart ssh
三、CentOS/RHEL 系列
3.1 CentOS 7 / RHEL 7
flowchart TD
A[CentOS 7 / RHEL 7] --> B[安装openssh-server]
A --> C[firewalld防火墙]
A --> D[SELinux配置]
# 1. 安装 OpenSSH 服务器
sudo yum install -y openssh-server openssh-clients
# 2. 启动服务
sudo systemctl start sshd
# 3. 设置开机自启
sudo systemctl enable sshd
# 4. 检查状态
sudo systemctl status sshd
3.2 CentOS Stream / RHEL 8+
# 使用 dnf(RHEL 8+)
sudo dnf install -y openssh-server
# 启动和管理
sudo systemctl start sshd
sudo systemctl enable sshd
sudo systemctl status sshd
3.3 firewalld 防火墙配置
# CentOS 7 使用 firewalld
# 允许 SSH
sudo firewall-cmd --permanent --add-service=ssh
# 或指定端口
sudo firewall-cmd --zone=public --add-port=22/tcp --permanent
# 重载防火墙
sudo firewall-cmd --reload
# 查看已开放的服务
sudo firewall-cmd --list-all
# 常用命令
sudo firewall-cmd --state # 查看状态
sudo firewall-cmd --permanent --add-service=ssh # 永久允许SSH
sudo firewall-cmd --reload # 重载配置
3.4 SELinux 配置
# 检查 SELinux 状态
getenforce
# 临时关闭(不推荐用于生产环境)
sudo setenforce 0
# 永久配置 SSH 端口(SELinux 启用时)
sudo semanage port -l | grep ssh # 查看当前 SSH 端口
sudo semanage port -a -t ssh_port_t -p tcp 2222 # 添加新端口
# 安装 semanage(如未安装)
sudo yum install -y policycoreutils-python
3.5 CentOS 完整配置示例
# 完整安装脚本
#!/bin/bash
set -e
echo "=== CentOS SSH 安装配置 ==="
# 安装
sudo yum install -y openssh-server openssh-clients
# 配置
sudo sed -i 's/#Port 22/Port 22/' /etc/ssh/sshd_config
sudo sed -i 's/#PermitRootLogin yes/PermitRootLogin yes/' /etc/ssh/sshd_config
sudo sed -i 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/' /etc/ssh/sshd_config
# 防火墙
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
# 启动服务
sudo systemctl enable sshd
sudo systemctl restart sshd
echo "=== 完成 ==="
四、Fedora 系列
4.1 安装与配置
# Fedora 使用 dnf
sudo dnf install -y openssh-server
# 启动服务
sudo systemctl enable --now sshd
# 防火墙(Fedora 使用 firewalld)
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
# SELinux(如使用)
sudo setsebool -P ssh_sysadm_login on
4.2 配置注意
Fedora 相比 CentOS/RHEL:
- 默认使用
sshd而不是ssh作为服务名 - 防火墙配置相同
- SELinux 配置相同
五、Arch Linux 系列
5.1 安装与启动
flowchart TD
A[Arch Linux] --> B[安装openssh]
B --> C[启用sshd服务]
C --> D[配置防火墙]
D --> E[创建SSH密钥]
# 1. 安装
sudo pacman -S openssh
# 2. 启用服务
sudo systemctl enable sshd
sudo systemctl start sshd
# 3. 检查状态
sudo systemctl status sshd
5.2 iptables/nftables 防火墙
# 如果使用 iptables
sudo pacman -S iptables
sudo systemctl enable iptables
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables-save > /etc/iptables/iptables.rules
# 如果使用 nftables
sudo pacman -S nftables
sudo systemctl enable nftables
5.3 用户权限配置
# 设置 SSH 目录权限
mkdir -p ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
# 添加管理员到 SSH 组(可选)
sudo usermod -aG wheel username
六、通用配置与优化
6.1 sshd_config 完整配置
# 编辑配置文件
sudo vim /etc/ssh/sshd_config
# ==================== 常用配置 ====================
# --- 基础配置 ---
Port 22 # SSH 端口
ListenAddress 0.0.0.0 # 监听地址
Protocol 2 # SSH 协议版本
# --- 认证配置 ---
PermitRootLogin no # 禁止root登录(生产环境)
PubkeyAuthentication yes # 公钥认证
PasswordAuthentication no # 禁止密码认证(生产环境)
PermitEmptyPasswords no # 禁止空密码
MaxAuthTries 3 # 最大尝试次数
MaxSessions 10 # 最大会话数
# --- 超时配置 ---
ClientAliveInterval 300 # 客户端存活检测(秒)
ClientAliveCountMax 2 # 断开前检测次数
LoginGraceTime 60 # 登录宽限时间(秒)
# --- 安全配置 ---
X11Forwarding no # 禁用X11转发(除非需要)
PrintMotd no # 不打印 MOTD
TCPKeepAlive yes # TCP保活
UseDNS no # 不进行DNS反向解析(加速连接)
StrictModes yes # 严格模式
MaxStartups 10:30:100 # 未登录连接数限制
# --- 日志配置 ---
SyslogFacility AUTH
LogLevel INFO
# --- 子系统 ---
Subsystem sftp /usr/lib/openssh/sftp-server
6.2 安全加固检查清单
flowchart TD
A[SSH安全加固] --> B[认证方式]
A --> C[访问控制]
A --> D[加密算法]
A --> E[日志监控]
B --> B1[密钥认证优先]
B --> B2[禁用root登录]
B --> B3[限制密码强度]
C --> C1[ AllowUsers]
C --> C2[ AllowGroups]
C --> C3[防火墙端口]
D --> D1[禁用旧算法]
D --> D2[使用强加密]
E --> E1[记录登录尝试]
E --> E2[监控异常IP]
6.3 强化配置文件示例
# /etc/ssh/sshd_config 强化版
Port 2222
ListenAddress 0.0.0.0
# 认证
PermitRootLogin no
PubkeyAuthentication yes
PasswordAuthentication no
PermitEmptyPasswords no
MaxAuthTries 3
MaxSessions 5
# 超时
ClientAliveInterval 300
ClientAliveCountMax 2
LoginGraceTime 30
# 安全
X11Forwarding no
AllowTcpForwarding no
AllowStreamLocalForwarding no
PermitUserEnvironment no
PrintMotd no
UseDNS no
StrictModes yes
# 用户限制
AllowUsers admin deploy@192.168.1.0/24
AllowGroups sshusers
# 日志
SyslogFacility AUTH
LogLevel VERBOSE
# 加密(可选,谨慎使用)
# Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com
# KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org
# MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com
七、SSH 密钥配置
7.1 密钥生成
flowchart LR
A[生成密钥对] --> B[公钥上传]
B --> C[测试连接]
C --> D[禁用密码]
A --> A1[Ed25519 - 推荐]
A --> A2[RSA - 兼容性]
A --> A3[ECDSA - 中等]
# 生成 Ed25519 密钥(推荐)
ssh-keygen -t ed25519 -C "your_email@example.com"
# 生成 RSA 密钥(兼容旧系统)
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# 生成 ECDSA 密钥
ssh-keygen -t ecdsa -b 521 -C "your_email@example.com"
# 指定密钥文件位置
ssh-keygen -t ed25519 -f ~/.ssh/my_server_key -C "my_server"
# 设置密钥密码(可选)
# 会提示输入 passphrase
7.2 公钥上传方法
方法1:ssh-copy-id(推荐)
# 基本用法
ssh-copy-id username@hostname
# 指定端口
ssh-copy-id -p 2222 username@hostname
# 指定密钥
ssh-copy-id -i ~/.ssh/my_key.pub username@hostname
方法2:手动复制
# 1. 在客户端查看公钥
cat ~/.ssh/id_ed25519.pub
# 2. 在服务器上创建目录并添加公钥
ssh username@hostname
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "ssh-ed25519 AAAA..." >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
方法3:使用 scp
# 先复制公钥到服务器
scp ~/.ssh/id_ed25519.pub username@hostname:/tmp/
# 然后在服务器上添加
ssh username@hostname
cat /tmp/id_ed25519.pub >> ~/.ssh/authorized_keys
rm /tmp/id_ed25519.pub
7.3 SSH Config 配置
# 编辑 SSH 配置文件
vim ~/.ssh/config
# ==================== 配置示例 ====================
# 默认配置(适用于所有主机)
Host *
ServerAliveInterval 300
ServerAliveCountMax 2
Compression yes
AddKeysToAgent yes
# 服务器1 - 生产环境
Host prod-server
HostName 192.168.1.100
User admin
Port 2222
IdentityFile ~/.ssh/prod_key
ForwardAgent yes
AddKeysToAgent yes
# 服务器2 - 开发环境
Host dev-server
HostName dev.example.com
User developer
Port 22
IdentityFile ~/.ssh/dev_key
PreferredAuthentications publickey
# GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/github_key
IdentitiesOnly yes
# 跳板机
Host jump-server
HostName jumphost.example.com
User admin
ProxyJump bastion@bastion.example.com
# 通过跳板机连接内网机器
Host internal-server
HostName 192.168.1.50
User admin
ProxyJump jump-server
7.4 Windows Terminal SSH 配置
{
"profiles": {
"defaults": {},
"list": [
{
"guid": "{...}",
"name": "Ubuntu Server",
"commandline": "ssh -i ~/.ssh/my_key admin@192.168.1.100",
"startingDirectory": "C:\\Users\\Username"
}
]
}
}
八、故障排查
8.1 常见问题速查
flowchart TD
A[SSH连接问题] --> B[连接被拒绝]
A --> C[认证失败]
A --> D[连接超时]
A --> E[密钥被拒绝]
B --> B1[服务未启动]
B --> B2[端口不对]
B --> B3[防火墙阻止]
C --> C1[密码错误]
C --> C2[密钥权限]
C --> C3[authorized_keys]
D --> D1[网络不通]
D --> D2[防火墙]
D --> D3[服务器负载]
E --> E1[权限问题]
E --> E2[密钥格式]
E --> E3[selinux上下文]
8.2 连接被拒绝
# 1. 检查服务是否运行
sudo systemctl status sshd
# 2. 检查端口是否监听
sudo ss -tlnp | grep :22
# 或
sudo netstat -tlnp | grep :22
# 3. 检查防火墙
# Ubuntu/Debian
sudo ufw status
# CentOS/RHEL
sudo firewall-cmd --list-all
# 4. 检查 hosts.deny
cat /etc/hosts.deny
8.3 密钥认证失败
# 1. 检查文件权限
ls -la ~/.ssh/
# 正确权限:
# .ssh/ -> 700 (drwx------)
# id_rsa -> 600 (-rw-------)
# id_rsa.pub -> 644 (-rw-r--r--)
# authorized_keys -> 600 (-rw-------)
# 2. 设置正确权限
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
chmod 600 ~/.ssh/authorized_keys
# 3. 检查 SSH 配置
grep -E "PubkeyAuthentication|AuthorizedKeysFile" /etc/ssh/sshd_config
# 4. 查看服务器日志
# Ubuntu/Debian
sudo journalctl -u ssh -f
# CentOS/RHEL
sudo tail -f /var/log/secure
8.4 SELinux 问题(CentOS/RHEL)
# 检查 SELinux 上下文
ls -Z ~/.ssh/
# 恢复 SELinux 上下文
sudo restorecon -R ~/.ssh
# 手动设置上下文
sudo semanage fcontext -a -t ssh_home_t ~/.ssh/authorized_keys
sudo restorecon ~/.ssh/authorized_keys
# 临时关闭 SELinux(调试用)
sudo setenforce 0
# 永久关闭(编辑文件)
sudo vim /etc/selinux/config
# SELINUX=disabled
8.5 调试连接
# 1. 查看详细连接信息
ssh -v username@hostname
# 2. 更详细的调试
ssh -vvv username@hostname
# 3. 测试端口连通性
telnet hostname 22
nc -zv hostname 22
curl -v telnet://hostname:22
# 4. 客户端配置测试
ssh -G username@hostname # 查看解析后的配置
8.6 发行版快速对比
flowchart TB
A[发行版对比] --> B[Ubuntu]
A --> C[Debian]
A --> D[CentOS]
A --> E[Fedora]
A --> F[Arch]
B --> B1[apt安装]
B1 --> B2[ufw防火墙]
C --> C1[apt安装]
C1 --> C2[ufw防火墙]
D --> D1[yum安装]
D1 --> D2[firewalld]
D1 --> D3[SELinux]
E --> E1[dnf安装]
E1 --> E2[firewalld]
E1 --> E3[SELinux]
F --> F1[pacman安装]
F1 --> F2[iptables/nftables]
| 发行版 | 包管理器 | 防火墙 | 特殊配置 |
|---|---|---|---|
| Ubuntu | apt | ufw | - |
| Debian | apt | ufw/iptables | - |
| CentOS 7 | yum | firewalld | SELinux |
| CentOS Stream | dnf | firewalld | SELinux |
| Fedora | dnf | firewalld | SELinux |
| Arch | pacman | iptables/nftables | - |
| Rocky/AlmaLinux | dnf | firewalld | SELinux |
附录:常用发行版一键配置脚本
Ubuntu/Debian
#!/bin/bash
# ubuntu-ssh-setup.sh
set -e
echo "=== Ubuntu/Debian SSH 配置 ==="
# 安装
sudo apt update && sudo apt install -y openssh-server
# 配置
sudo sed -i 's/#Port 22/Port 22/' /etc/ssh/sshd_config
sudo sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
sudo sed -i 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/' /etc/ssh/sshd_config
# 防火墙
sudo ufw allow ssh
# 启动
sudo systemctl enable ssh
sudo systemctl restart ssh
echo "=== 完成 ==="
CentOS 7
#!/bin/bash
# centos7-ssh-setup.sh
set -e
echo "=== CentOS 7 SSH 配置 ==="
# 安装
sudo yum install -y openssh-server openssh-clients
# 配置
sudo sed -i 's/#Port 22/Port 22/' /etc/ssh/sshd_config
sudo sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
sudo sed -i 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/' /etc/ssh/sshd_config
# 防火墙
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
# SELinux(可选,注释掉如不需要)
# sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
# 启动
sudo systemctl enable sshd
sudo systemctl restart sshd
echo "=== 完成 ==="
最后更新:2026年4月

浙公网安备 33010602011771号