LIVE_STREAM_CONNECTED
SSH 使用与配置指南
A
Mastermind
admin
Dimension
Linux
Timestamp
2025年12月21日
SSH(Secure Shell)是远程登录和管理服务器的安全协议,是运维和开发的必备技能。
基础连接
# 基本连接
ssh username@hostname
# 指定端口
ssh -p 2222 username@hostname
# 指定私钥
ssh -i ~/.ssh/id_rsa username@hostname
密钥管理
生成密钥
# 生成 ED25519 密钥(推荐)
ssh-keygen -t ed25519 -C "your@email.com"
# 生成 RSA 密钥
ssh-keygen -t rsa -b 4096 -C "your@email.com"
# 指定文件名
ssh-keygen -t ed25519 -f ~/.ssh/my_key -C "comment"
复制公钥到服务器
# 自动复制
ssh-copy-id username@hostname
ssh-copy-id -i ~/.ssh/my_key.pub username@hostname
# 手动复制
cat ~/.ssh/id_ed25519.pub | ssh user@host "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
密钥权限
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
chmod 600 ~/.ssh/authorized_keys
SSH 配置文件
创建 ~/.ssh/config 简化连接:
# 默认配置
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
# 服务器别名
Host myserver
HostName 192.168.1.100
User root
Port 22
IdentityFile ~/.ssh/id_ed25519
Host dev
HostName dev.example.com
User deploy
Port 2222
IdentityFile ~/.ssh/dev_key
Host prod
HostName prod.example.com
User deploy
IdentityFile ~/.ssh/prod_key
ProxyJump jump-server
# 跳板机
Host jump-server
HostName jump.example.com
User admin
使用别名连接:
ssh myserver
ssh dev
ssh prod
文件传输
SCP
# 上传文件
scp file.txt user@host:/path/to/dest/
# 下载文件
scp user@host:/path/to/file.txt ./
# 上传目录
scp -r ./folder user@host:/path/to/dest/
# 指定端口
scp -P 2222 file.txt user@host:/path/
SFTP
# 连接
sftp user@host
# 常用命令
ls # 列出远程文件
lls # 列出本地文件
cd /path # 切换远程目录
lcd /path # 切换本地目录
get file.txt # 下载
put file.txt # 上传
mkdir dir # 创建目录
rm file # 删除
exit # 退出
Rsync
# 同步目录
rsync -avz ./local/ user@host:/remote/
# 删除目标多余文件
rsync -avz --delete ./local/ user@host:/remote/
# 排除文件
rsync -avz --exclude 'node_modules' ./local/ user@host:/remote/
# 显示进度
rsync -avz --progress ./local/ user@host:/remote/
端口转发
本地转发
将远程端口映射到本地:
# 访问本地 8080 等于访问远程 80
ssh -L 8080:localhost:80 user@host
# 访问本地 3306 等于访问远程数据库
ssh -L 3306:db.internal:3306 user@host
# 后台运行
ssh -fNL 8080:localhost:80 user@host
远程转发
将本地端口暴露到远程:
# 远程 8080 转发到本地 3000
ssh -R 8080:localhost:3000 user@host
动态转发(SOCKS 代理)
ssh -D 1080 user@host
# 后台运行
ssh -fND 1080 user@host
跳板机
通过跳板机连接
# 方式一:ProxyJump
ssh -J jump@jump-host target@target-host
# 方式二:ProxyCommand
ssh -o ProxyCommand="ssh -W %h:%p jump@jump-host" target@target-host
配置文件方式
Host target
HostName target.internal
User deploy
ProxyJump jump-server
SSH Agent
# 启动 agent
eval $(ssh-agent)
# 添加密钥
ssh-add ~/.ssh/id_ed25519
ssh-add ~/.ssh/other_key
# 查看已添加的密钥
ssh-add -l
# 删除所有密钥
ssh-add -D
服务端配置
编辑 /etc/ssh/sshd_config:
# 端口
Port 22
# 禁止 root 登录
PermitRootLogin no
# 禁止密码登录
PasswordAuthentication no
# 允许公钥认证
PubkeyAuthentication yes
# 允许的用户
AllowUsers deploy admin
# 登录超时
LoginGraceTime 30
# 最大尝试次数
MaxAuthTries 3
重启服务:
sudo systemctl restart sshd
常用技巧
保持连接
# 客户端配置 ~/.ssh/config
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
复用连接
Host *
ControlMaster auto
ControlPath ~/.ssh/sockets/%r@%h-%p
ControlPersist 600
创建目录:mkdir -p ~/.ssh/sockets
远程执行命令
# 执行单个命令
ssh user@host "ls -la"
# 执行多个命令
ssh user@host "cd /app && git pull && pm2 restart all"
# 执行本地脚本
ssh user@host 'bash -s' < local_script.sh
后台运行
# 后台执行命令
ssh user@host "nohup ./script.sh > output.log 2>&1 &"
故障排查
# 详细输出
ssh -v user@host
ssh -vvv user@host # 更详细
# 测试连接
ssh -T git@github.com
# 检查密钥
ssh-keygen -l -f ~/.ssh/id_ed25519.pub
常见问题
Permission denied
- 检查密钥权限
- 确认公钥已添加到服务器
- 检查 sshd_config 配置
Connection refused
- 检查 SSH 服务是否运行
- 检查防火墙端口
- 确认 IP 和端口正确
Host key verification failed
# 删除旧的 host key
ssh-keygen -R hostname
总结
SSH 核心技能:密钥认证、配置文件管理、端口转发、文件传输。合理配置可大幅提升远程管理效率和安全性。