ssh
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSSH 管理与安全
SSH 管理与安全
概述
概述
SSH 密钥管理、跳板机配置、端口转发、安全加固等技能。
SSH 密钥管理、跳板机配置、端口转发、安全加固等技能。
基础连接
基础连接
连接命令
连接命令
bash
undefinedbash
undefined基础连接
基础连接
ssh user@hostname
ssh -p 2222 user@hostname # 指定端口
ssh user@hostname
ssh -p 2222 user@hostname # 指定端口
执行远程命令
执行远程命令
ssh user@hostname "command"
ssh user@hostname 'ls -la && df -h'
ssh user@hostname "command"
ssh user@hostname 'ls -la && df -h'
详细输出(调试)
详细输出(调试)
ssh -v user@hostname
ssh -vvv user@hostname # 更详细
undefinedssh -v user@hostname
ssh -vvv user@hostname # 更详细
undefined配置文件
配置文件
bash
undefinedbash
undefined~/.ssh/config
~/.ssh/config
Host myserver
HostName 192.168.1.100
User admin
Port 22
IdentityFile ~/.ssh/id_rsa_myserver
Host dev-*
User developer
IdentityFile ~/.ssh/id_rsa_dev
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
AddKeysToAgent yes
Host myserver
HostName 192.168.1.100
User admin
Port 22
IdentityFile ~/.ssh/id_rsa_myserver
Host dev-*
User developer
IdentityFile ~/.ssh/id_rsa_dev
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
AddKeysToAgent yes
使用配置
使用配置
ssh myserver
undefinedssh myserver
undefined密钥管理
密钥管理
生成密钥
生成密钥
bash
undefinedbash
undefined生成 RSA 密钥(推荐 4096 位)
生成 RSA 密钥(推荐 4096 位)
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
生成 Ed25519 密钥(推荐)
生成 Ed25519 密钥(推荐)
ssh-keygen -t ed25519 -C "your_email@example.com"
ssh-keygen -t ed25519 -C "your_email@example.com"
指定文件名
指定文件名
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_work
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_work
修改密码
修改密码
ssh-keygen -p -f ~/.ssh/id_rsa
undefinedssh-keygen -p -f ~/.ssh/id_rsa
undefined部署公钥
部署公钥
bash
undefinedbash
undefined方式1:ssh-copy-id
方式1:ssh-copy-id
ssh-copy-id user@hostname
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@hostname
ssh-copy-id user@hostname
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@hostname
方式2:手动复制
方式2:手动复制
cat ~/.ssh/id_ed25519.pub | ssh user@hostname "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
cat ~/.ssh/id_ed25519.pub | ssh user@hostname "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
方式3:直接编辑
方式3:直接编辑
ssh user@hostname
echo "public_key_content" >> ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
undefinedssh user@hostname
echo "public_key_content" >> ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
undefinedSSH Agent
SSH Agent
bash
undefinedbash
undefined启动 agent
启动 agent
eval "$(ssh-agent -s)"
eval "$(ssh-agent -s)"
添加密钥
添加密钥
ssh-add ~/.ssh/id_rsa
ssh-add -l # 列出已添加的密钥
ssh-add ~/.ssh/id_rsa
ssh-add -l # 列出已添加的密钥
转发 agent(跳板机场景)
转发 agent(跳板机场景)
ssh -A user@jumphost
undefinedssh -A user@jumphost
undefined端口转发
端口转发
本地转发
本地转发
bash
undefinedbash
undefined将本地端口转发到远程
将本地端口转发到远程
ssh -L local_port:target_host:target_port user@ssh_server
ssh -L local_port:target_host:target_port user@ssh_server
示例:访问远程 MySQL
示例:访问远程 MySQL
ssh -L 3306:localhost:3306 user@dbserver
mysql -h 127.0.0.1 -P 3306
ssh -L 3306:localhost:3306 user@dbserver
mysql -h 127.0.0.1 -P 3306
示例:访问内网服务
示例:访问内网服务
ssh -L 8080:internal.server:80 user@jumphost
curl http://localhost:8080
undefinedssh -L 8080:internal.server:80 user@jumphost
curl http://localhost:8080
undefined远程转发
远程转发
bash
undefinedbash
undefined将远程端口转发到本地
将远程端口转发到本地
ssh -R remote_port:local_host:local_port user@ssh_server
ssh -R remote_port:local_host:local_port user@ssh_server
示例:暴露本地服务
示例:暴露本地服务
ssh -R 8080:localhost:3000 user@public_server
ssh -R 8080:localhost:3000 user@public_server
现在可以通过 public_server:8080 访问本地 3000 端口
现在可以通过 public_server:8080 访问本地 3000 端口
undefinedundefined动态转发(SOCKS 代理)
动态转发(SOCKS 代理)
bash
undefinedbash
undefined创建 SOCKS5 代理
创建 SOCKS5 代理
ssh -D 1080 user@ssh_server
ssh -D 1080 user@ssh_server
使用代理
使用代理
curl --socks5 localhost:1080 http://example.com
undefinedcurl --socks5 localhost:1080 http://example.com
undefined后台运行
后台运行
bash
undefinedbash
undefined后台运行隧道
后台运行隧道
ssh -fNL 3306:localhost:3306 user@server
ssh -fNL 3306:localhost:3306 user@server
-f 后台运行
-f 后台运行
-N 不执行远程命令
-N 不执行远程命令
-L 本地转发
-L 本地转发
undefinedundefined跳板机配置
跳板机配置
ProxyJump(推荐)
ProxyJump(推荐)
bash
undefinedbash
undefined命令行
命令行
ssh -J jumphost user@target
ssh -J jumphost user@target
配置文件
配置文件
Host target
HostName 192.168.1.100
User admin
ProxyJump jumphost
Host jumphost
HostName jump.example.com
User jumper
undefinedHost target
HostName 192.168.1.100
User admin
ProxyJump jumphost
Host jumphost
HostName jump.example.com
User jumper
undefinedProxyCommand
ProxyCommand
bash
undefinedbash
undefined配置文件
配置文件
Host target
HostName 192.168.1.100
User admin
ProxyCommand ssh -W %h:%p jumphost
undefinedHost target
HostName 192.168.1.100
User admin
ProxyCommand ssh -W %h:%p jumphost
undefined安全加固
安全加固
sshd_config 配置
sshd_config 配置
bash
undefinedbash
undefined/etc/ssh/sshd_config
/etc/ssh/sshd_config
禁用密码登录
禁用密码登录
PasswordAuthentication no
ChallengeResponseAuthentication no
PasswordAuthentication no
ChallengeResponseAuthentication no
禁用 root 登录
禁用 root 登录
PermitRootLogin no
PermitRootLogin no
限制用户
限制用户
AllowUsers admin developer
AllowGroups sshusers
AllowUsers admin developer
AllowGroups sshusers
修改端口
修改端口
Port 2222
Port 2222
限制登录尝试
限制登录尝试
MaxAuthTries 3
MaxSessions 5
MaxAuthTries 3
MaxSessions 5
空闲超时
空闲超时
ClientAliveInterval 300
ClientAliveCountMax 2
ClientAliveInterval 300
ClientAliveCountMax 2
禁用不安全选项
禁用不安全选项
X11Forwarding no
PermitEmptyPasswords no
undefinedX11Forwarding no
PermitEmptyPasswords no
undefined应用配置
应用配置
bash
undefinedbash
undefined测试配置
测试配置
sshd -t
sshd -t
重载配置
重载配置
systemctl reload sshd
undefinedsystemctl reload sshd
undefined常见场景
常见场景
场景 1:批量执行命令
场景 1:批量执行命令
bash
undefinedbash
undefined使用 for 循环
使用 for 循环
for host in server1 server2 server3; do
ssh $host "uptime"
done
for host in server1 server2 server3; do
ssh $host "uptime"
done
使用 parallel-ssh
使用 parallel-ssh
pssh -h hosts.txt -i "uptime"
undefinedpssh -h hosts.txt -i "uptime"
undefined场景 2:文件传输
场景 2:文件传输
bash
undefinedbash
undefinedscp
scp
scp file.txt user@host:/path/
scp -r dir/ user@host:/path/
scp user@host:/path/file.txt ./
scp file.txt user@host:/path/
scp -r dir/ user@host:/path/
scp user@host:/path/file.txt ./
rsync over SSH
rsync over SSH
rsync -avz -e ssh source/ user@host:/dest/
undefinedrsync -avz -e ssh source/ user@host:/dest/
undefined场景 3:保持连接
场景 3:保持连接
bash
undefinedbash
undefined~/.ssh/config
~/.ssh/config
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
TCPKeepAlive yes
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
TCPKeepAlive yes
使用 autossh
使用 autossh
autossh -M 0 -fN -L 3306:localhost:3306 user@server
undefinedautossh -M 0 -fN -L 3306:localhost:3306 user@server
undefined故障排查
故障排查
| 问题 | 排查方法 |
|---|---|
| 连接超时 | 检查网络、防火墙、端口 |
| 权限被拒绝 | 检查密钥权限 (600)、authorized_keys |
| Host key 变更 | |
| Agent 转发失败 | 检查 |
| 连接断开 | 配置 |
bash
undefined| 问题 | 排查方法 |
|---|---|
| 连接超时 | 检查网络、防火墙、端口 |
| 权限被拒绝 | 检查密钥权限 (600)、authorized_keys |
| Host key 变更 | |
| Agent 转发失败 | 检查 |
| 连接断开 | 配置 |
bash
undefined调试连接
调试连接
ssh -vvv user@hostname
ssh -vvv user@hostname
检查密钥权限
检查密钥权限
ls -la ~/.ssh/
ls -la ~/.ssh/
id_rsa: 600
id_rsa: 600
id_rsa.pub: 644
id_rsa.pub: 644
authorized_keys: 600
authorized_keys: 600
~/.ssh: 700
~/.ssh: 700
undefinedundefined