远程连接 #

SSH 基础 #

ssh - 安全远程登录 #

bash
# 连接远程服务器
$ ssh user@hostname
$ ssh user@192.168.1.100

# 指定端口
$ ssh -p 2222 user@hostname

# 指定私钥文件
$ ssh -i ~/.ssh/id_rsa user@hostname

# 执行远程命令
$ ssh user@hostname 'command'
$ ssh user@hostname 'ls -la'

# 启用 X11 转发
$ ssh -X user@hostname

# 启用端口转发
$ ssh -L 8080:localhost:80 user@hostname

# 后台运行
$ ssh -f user@hostname 'command'

SSH 配置 #

bash
# 客户端配置文件 ~/.ssh/config
Host myserver
    HostName 192.168.1.100
    User username
    Port 22
    IdentityFile ~/.ssh/id_rsa

Host *
    ServerAliveInterval 60
    ServerAliveCountMax 3

# 使用配置连接
$ ssh myserver

SSH 密钥认证 #

bash
# 生成密钥对
$ ssh-keygen -t rsa -b 4096
$ ssh-keygen -t ed25519

# 指定文件名
$ ssh-keygen -f ~/.ssh/mykey

# 添加密码
$ ssh-keygen -t rsa -b 4096 -C "comment"

# 复制公钥到服务器
$ ssh-copy-id user@hostname
$ ssh-copy-id -i ~/.ssh/id_rsa.pub user@hostname

# 手动复制公钥
$ cat ~/.ssh/id_rsa.pub | ssh user@hostname 'cat >> ~/.ssh/authorized_keys'

# 测试密钥登录
$ ssh -i ~/.ssh/id_rsa user@hostname

SSH 代理 #

bash
# 启动 SSH 代理
$ eval $(ssh-agent)

# 添加密钥
$ ssh-add ~/.ssh/id_rsa

# 列出已添加的密钥
$ ssh-add -l

# 删除所有密钥
$ ssh-add -D

# 删除指定密钥
$ ssh-add -d ~/.ssh/id_rsa

# 锁定代理
$ ssh-add -x

# 解锁代理
$ ssh-add -X

SSH 端口转发 #

bash
# 本地端口转发
# 将本地 8080 转发到远程 80
$ ssh -L 8080:localhost:80 user@hostname

# 将本地 3306 转发到远程 MySQL
$ ssh -L 3306:localhost:3306 user@hostname

# 远程端口转发
# 将远程 8080 转发到本地 80
$ ssh -R 8080:localhost:80 user@hostname

# 动态端口转发(SOCKS 代理)
$ ssh -D 1080 user@hostname

# 后台运行
$ ssh -fNL 8080:localhost:80 user@hostname

SSH 跳板机 #

bash
# 通过跳板机连接
$ ssh -J jumpuser@jumphost user@targethost

# 配置跳板机
# ~/.ssh/config
Host target
    HostName target.example.com
    User user
    ProxyJump jumpuser@jumphost.example.com

# 使用配置连接
$ ssh target

SCP - 安全复制 #

基本用法 #

bash
# 复制本地文件到远程
$ scp file.txt user@hostname:/path/to/destination/

# 复制远程文件到本地
$ scp user@hostname:/path/to/file.txt ./

# 复制目录
$ scp -r directory/ user@hostname:/path/to/destination/

# 指定端口
$ scp -P 2222 file.txt user@hostname:/path/

# 指定私钥
$ scp -i ~/.ssh/id_rsa file.txt user@hostname:/path/

# 显示进度
$ scp -v file.txt user@hostname:/path/

# 保留文件属性
$ scp -p file.txt user@hostname:/path/

# 压缩传输
$ scp -C file.txt user@hostname:/path/

# 限制带宽
$ scp -l 1000 file.txt user@hostname:/path/  # 1000 Kbit/s

远程到远程 #

bash
# 从远程服务器复制到另一个远程服务器
$ scp user1@host1:/path/to/file user2@host2:/path/to/destination/

# 通过本地转发
$ scp -3 user1@host1:/path/to/file user2@host2:/path/to/destination/

rsync - 同步工具 #

基本用法 #

bash
# 同步目录
$ rsync -av source/ destination/

# 同步到远程
$ rsync -av source/ user@hostname:/path/to/destination/

# 从远程同步
$ rsync -av user@hostname:/path/to/source/ destination/

# 显示进度
$ rsync -av --progress source/ destination/

# 显示统计
$ rsync -av --stats source/ destination/

常用选项 #

bash
# -a 归档模式(保留权限、时间戳等)
$ rsync -a source/ destination/

# -v 显示详细信息
$ rsync -av source/ destination/

# -z 压缩传输
$ rsync -avz source/ user@hostname:/path/

# -h 人类可读格式
$ rsync -avh source/ destination/

# -P 显示进度和断点续传
$ rsync -avP source/ destination/

# --delete 删除目标多余文件
$ rsync -av --delete source/ destination/

# --exclude 排除文件
$ rsync -av --exclude='*.log' source/ destination/
$ rsync -av --exclude={'*.log','*.tmp'} source/ destination/

# --include 包含文件
$ rsync -av --include='*.txt' --exclude='*' source/ destination/

# --bwlimit 限制带宽
$ rsync -av --bwlimit=1000 source/ destination/

# -n 干运行(只显示会做什么)
$ rsync -avn source/ destination/

目录末尾斜杠 #

bash
# 有斜杠:复制目录内容
$ rsync -av source/ destination/
# 结果:destination/ 包含 source/ 的内容

# 无斜杠:复制目录本身
$ rsync -av source destination/
# 结果:destination/source/ 包含内容

实用示例 #

bash
# 备份目录
$ rsync -avz --delete /home/user/ /backup/user/

# 同步网站
$ rsync -avz --delete --exclude='.git' ./site/ user@server:/var/www/html/

# 增量备份
$ rsync -avz --backup --backup-dir=/backup/incremental/ source/ destination/

# 大文件断点续传
$ rsync -avP --partial large_file user@server:/path/

# 同步排除特定目录
$ rsync -avz --exclude={'node_modules','.git','__pycache__'} project/ user@server:/path/

# 镜像同步
$ rsync -avz --delete --checksum source/ destination/

SFTP - 安全文件传输 #

基本用法 #

bash
# 连接 SFTP
$ sftp user@hostname
$ sftp -P 2222 user@hostname

# 批量执行命令
$ sftp -b commands.txt user@hostname

SFTP 命令 #

text
┌─────────────────────────────────────────────────────────────┐
│                    SFTP 命令                                │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   本地命令(前缀 l)                                         │
│   lcd        切换本地目录                                    │
│   lls        列出本地目录                                    │
│   lmkdir     创建本地目录                                    │
│   lpwd       显示本地目录                                    │
│                                                             │
│   远程命令                                                   │
│   cd         切换远程目录                                    │
│   ls         列出远程目录                                    │
│   mkdir      创建远程目录                                    │
│   pwd        显示远程目录                                    │
│   rm         删除远程文件                                    │
│   rmdir      删除远程目录                                    │
│                                                             │
│   传输命令                                                   │
│   get        下载文件                                        │
│   put        上传文件                                        │
│   mget       批量下载                                        │
│   mput       批量上传                                        │
│                                                             │
│   其他                                                       │
│   help       显示帮助                                        │
│   exit/quit  退出                                           │
│                                                             │
└─────────────────────────────────────────────────────────────┘

SFTP 示例 #

bash
sftp> ls
file1.txt  file2.txt  directory/

sftp> cd directory
sftp> pwd
Remote working directory: /home/user/directory

sftp> get file.txt
Fetching /home/user/file.txt to file.txt

sftp> get file.txt local_name.txt

sftp> put local_file.txt
Uploading local_file.txt to /home/user/local_file.txt

sftp> mget *.txt

sftp> mput *.txt

sftp> exit

SSH 隧道高级用法 #

本地端口转发详解 #

bash
# 访问远程内网服务
# 本地 8080 -> 远程服务器 -> 内网 Web 服务器
$ ssh -L 8080:192.168.1.200:80 user@jumpserver

# 访问远程数据库
$ ssh -L 3306:localhost:3306 user@dbserver

# 多个端口转发
$ ssh -L 8080:localhost:80 -L 3306:localhost:3306 user@server

远程端口转发详解 #

bash
# 让远程服务器访问本地服务
$ ssh -R 8080:localhost:80 user@remote

# 让远程服务器访问本地数据库
$ ssh -R 3306:localhost:3306 user@remote

# 允许远程服务器其他主机访问
# 需要在远程服务器配置 GatewayPorts yes
$ ssh -R 0.0.0.0:8080:localhost:80 user@remote

动态端口转发 #

bash
# 创建 SOCKS 代理
$ ssh -D 1080 user@server

# 配置浏览器使用 SOCKS 代理
# 代理地址:127.0.0.1:1080

# 使用代理
$ curl --socks5 127.0.0.1:1080 https://example.com

小结 #

通过本节学习,你应该掌握:

  1. SSH:远程登录、密钥认证、端口转发
  2. SCP:安全文件复制
  3. rsync:高效文件同步
  4. SFTP:交互式文件传输
  5. SSH 隧道:端口转发高级用法

下一步,我们将学习 Shell 脚本基础。

最后更新:2026-04-11