Linux 远程登录

SSH 登录、密钥认证、免密与端口转发、scp/rsync 传文件。

Beginner
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@host

config 别名

Text
# ~/.ssh/config
Host api
    HostName 203.0.113.10
    User deploy
    Port 2222
    IdentityFile ~/.ssh/id_ed25519
    ServerAliveInterval 60
Bash
ssh api          # 之后只需这一句

传文件

Bash
scp app.tar api:/opt/app/
rsync -avz --progress ./dist/ api:/var/www/app/
ToolStrengthUse
scpSimple, always availableOne-off copies
rsyncDelta transfer, resume, excludeDeploy directories
sftpInteractiveBrowsing

端口转发

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 3
ssh -L is the fastest way to reach an internal service — much faster than asking ops to open a security group. Close it when done.

Friends & Partners