Linux 远程登录
SSH 登录、密钥认证、免密与端口转发、scp/rsync 传文件。
Key PointsSSH 是加密的远程 shell;配好密钥免密后,用 config 别名一条 ssh 就进;传文件用 rsync 比 scp 更好用。
登录
Bash
ssh user@host
ssh -p 2222 user@host
ssh -i ~/.ssh/id_ed25519 user@host免密登录
Bash
ssh-keygen -t ed25519
ssh-copy-id user@hostconfig 别名
Text
# ~/.ssh/config
Host api
HostName 203.0.113.10
User deploy
Port 2222
IdentityFile ~/.ssh/id_ed25519
ServerAliveInterval 60Bash
ssh api # 之后只需这一句传文件
Bash
scp app.tar api:/opt/app/
rsync -avz --progress ./dist/ api:/var/www/app/| Tool | Strength | Use |
|---|---|---|
scp | Simple, always available | One-off copies |
rsync | Delta transfer, resume, exclude | Deploy directories |
sftp | Interactive | Browsing |
端口转发
Bash
# 把远端 3306 转到本地 13306(连内网数据库)
ssh -L 13306:127.0.0.1:3306 api
# 把本地 8080 暴露到远端(临时给同事看)
ssh -R 8080:127.0.0.1:8080 api保持连接
Bash
# 服务端 /etc/ssh/sshd_config
ClientAliveInterval 60
ClientAliveCountMax 3ssh -L is the fastest way to reach an internal service — much faster than asking ops to open a security group. Close it when done.登录
Bash
ssh user@host
ssh -p 2222 user@host
ssh -i ~/.ssh/id_ed25519 user@host免密登录
Bash
ssh-keygen -t ed25519
ssh-copy-id user@hostconfig 别名
Text
# ~/.ssh/config
Host api
HostName 203.0.113.10
User deploy
Port 2222
IdentityFile ~/.ssh/id_ed25519
ServerAliveInterval 60Bash
ssh api # 之后只需这一句传文件
Bash
scp app.tar api:/opt/app/
rsync -avz --progress ./dist/ api:/var/www/app/| 工具 | 优势 | 场景 |
|---|---|---|
scp | 简单,到处都有 | 临时拷文件 |
rsync | 增量、断点续传、排除规则 | 部署目录 |
sftp | 交互式 | 浏览文件 |
端口转发
Bash
# 把远端 3306 转到本地 13306(连内网数据库)
ssh -L 13306:127.0.0.1:3306 api
# 把本地 8080 暴露到远端(临时给同事看)
ssh -R 8080:127.0.0.1:8080 api保持连接
Bash
# 服务端 /etc/ssh/sshd_config
ClientAliveInterval 60
ClientAliveCountMax 3ssh -L 是排查内网服务最快的办法,比让运维开安全组快得多。记得用完就断。