Skip to content

SSH 基础

连接到 remote 服务器 securely 使用 SSH, 键, and 配置 file for 简单 access.

你将学到

  • 什么 SSH 是 and 为什么它 是更多 secure than 密码
  • Generating and using SSH 键 pairs
  • 连接到 remote 服务器 and 复制 files
  • 配置 SSH 配置 file for shortcuts

概念讲解

什么是 SSH?

SSH (Secure Shell) is a cryptographic network protocol for accessing remote computers securely over an unsecured network. It encrypts all traffic between your machine and the server, preventing eavesdropping and man-in-the-middle attacks. SSH is the primary way DevOps engineers connect to Linux servers, deploy code, and administer remote systems. It replaces older, insecure protocols like Telnet and FTP.

SSH Key Pairs

SSH uses public-key cryptography. You generate a key pair: a private key (which you keep secret) and a public key (which you share). You copy the public key to the server's ~/.ssh/authorized_keys file. When you connect, the server verifies you possess the matching private key without ever transmitting it. This is far more secure than passwords — keys cannot be brute-forced and are never sent over the network.

Connecting and Copying Files

To connect, use ssh 用户@hostname. To copy files, use scp for single files or rsync for directories with efficient delta transfers. The ssh-keygen command generates keys, and ssh-复制-id installs your public key on a server. Once set up, you can connect and transfer files without typing a password each time.

The SSH Config File

The ~/.ssh/配置 file lets you define shortcuts for your servers. Instead of typing ssh -i ~/.ssh/mykey.pem [email protected] -p 2222, you can create a config entry named prod and simply type ssh prod. This file also lets you set default keys, ports, and other options per host, making it much easier to manage multiple servers.

SSH Agent and Forwarding

The SSH agent holds your decrypted private keys in memory so you don't type the passphrase every time. Start it with eval $(ssh-agent) and add keys with ssh-添加. Agent forwarding (- flag) lets you jump from one server to another without copying your private key — the server forwards authentication requests back to your local agent. This is useful for deploying from a CI server to production. However, use forwarding carefully on untrusted servers, as a compromised server could hijack your agent connection.

示例代码

              # Generate an SSH key pair (ED25519 is recommended)
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/id_ed25519

# Copy your public key to a remote server
ssh-copy-id -i ~/.ssh/id_ed25519.pub [email protected]

# Connect to a remote server
ssh [email protected]

# Copy files securely
scp ./app.tar.gz user@server:/tmp/
rsync -avz ./dist/ user@server:/var/www/html/

# SSH config shortcut (~/.ssh/config)
# Host prod
#   HostName 10.0.0.5
#   User admin
#   IdentityFile ~/.ssh/id_ed25519
#   Port 2222
            

ssh-keygen 创建键 pair, ssh-复制-id 安装公共键 on 服务器, and scp/rsync transfer files. SSH 配置 file 定义连接 shortcuts.

动手试一试

常见错误

错误写法

Using RSA 键使用小 位大小

正确做法

Use ED25519 键 (ssh-keygen -t ed25519) or RSA 使用 at 最少 4096 位. Older 1024-位 RSA 键是 considered insecure and 能是 cracked.

错误写法

Leaving 私有键 permissions 也打开

正确做法

设置 chmod 600 ~/.ssh/id_ed25519. SSH 将 refuse 到 use 私有键 那个是 readable 通过 other 用户, as 它是 security risk.

练习 SSH 基础

1 个练习

练习 SSH 基础

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误