第 174 / 187 篇

17.3. 远程访问

说明

本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/Appendix2/ssh_scp.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。

17.3. Remote Access

用户常常想远程登录到 Unix 机器,或者把文件从一台机器复制到另一台机器。安全外壳(Secure Shell,SSH)可以用来从另一台运行 Unix、或运行 Windows 和 MacOS 等其他操作系统的机器远程登录到一台 Unix 机器。安全复制(Secure Copy,SCP)可以用来把文件从一个系统复制到另一个系统。

Often times users want to remotely log into to Unix machines or copy files from one machine to another. Secure Shell (SSH) is useful for remotely logging into a Unix machine from another machine that running Unix or running another operating systems like Windows and MacOS. And Secure Copy (SCP) is useful for copying files from one system to another.

17.3.1. 远程登录

17.3.1. Remote Login

可以用 ssh 命令登录到远程 Unix 机器。ssh 在你运行 ssh 的主机与你连接到的远程机器之间提供加密的通信。

You can log into a remote Unix machine using the ssh command. ssh provides encrypted communication between the host machine that you run ssh from and the remote machine that you connected to.

要通过 SSH 登录到远程机器,写出远程机器上的用户名,后跟这台机器的互联网主机名。例如,如果 Sarita 想从她的笔记本电脑远程登录到 cs.college.edu 网络上的机器 cs87 中她的 sarita 账户,她会输入下面的命令(为了明确每条命令在哪台机器上执行,laptop$ 提示符是 Sarita 笔记本电脑上的 shell 提示符,cs87$ 提示符是远程机器上的 shell 提示符)。

To SSH into a remote machine, list the username on the remote machine followed by the machine’s Internet hostname. For example, if Sarita wanted to remotely log into her sarita account on the machine cs87 on cs.college.edu network from her laptop, she would enter the following command (to clarify which machine is executing each of these commands, the laptop$ prompt is the shell prompt on Sarita’s laptop, and the cs87$ prompt is the shell prompt on the remote machine).

1
laptop$ ssh sarita@cs87.cs.college.edu

如果连接成功,远程机器上运行的 ssh 服务器会要求用户输入其在那台机器上的密码。一旦成功远程登录,ssh 就会在远程机器上启动一个 Unix shell,用户可以通过它访问远程系统上的文件并运行应用程序。

If successfully connected, the ssh server running on the remote machine will ask the user to enter their password on that machine. Once successfully remotely logged in, ssh starts a unix shell on the remote machine through which the user can access files and run applications on the remote system.

1
2
3
laptop$ ssh sarita@cs87.cs.college.edu
sarita@cs.college.edu's password:
cs87$

从非 Unix 操作系统用 SSH 远程连接

Remotely connecting with SSH from non-Unix OSs

要从非 Unix 系统 ssh 到 Unix 系统,你可能需要先在自己的机器上安装一个 SSH 客户端,不过它很可能已经装好了。然后你需要一个终端窗口来运行 ssh。在 MacOS 上,打开并使用终端(Terminal)应用程序。在 Windows 机器上,可以使用 PowerShell,或者安装并使用 Putty。

To ssh into a Unix system from a non-Unix system you may need to first install an SSH client on your machine, however it is likely already installed. You then need a terminal window to run ssh On MacOS, open and use the terminal application. On Windows machines, you can either use PowerShell or install and use Putty.

17.3.2. 远程文件复制

17.3.2. Remote File Copy

如果你想在远程系统的机器之间传输文件,可以使用 scp(secure copy,安全复制)。scp 命令看起来与 cp 命令很像,只是源或目标之一被指定为远程机器上的路径名。与 ssh 一样,scp 要求用户输入远程机器上该用户的密码,以便访问远程系统上被复制或要复制到的文件。如果 scp 成功,它会显示关于所复制文件的一些统计信息,包括文件的字节数、平均传输速率和总传输时间。

If you want to transfer files to or from a machine on a remote system you can use scp (secure copy). The scp command looks much like the cp command except that one of either the source or the destination is specified as a pathname on a remote machine. Like, ssh, scp requires the user to type the password of the user on the remote machine to allow access to the files being copied or from on the remote system. If scp is successful, it displays some statistics about the file being copied including the size of the file in bytes, the average transfer rate, and the total transfer time.

例如,要把名为 prog.c 的文件从你家里的机器复制到 cs87.cs.college.edu 机器上的 sam 用户那里,你会输入:

For example, to copy the file named prog.c from your home machine to the sam user on the cs87.cs.college.edu machine, you would type:

1
2
3
4
5
6
7
8
9
# scp prog.c into home directory on cs87.cs.college.edu
laptop$ scp prog.c sam@cs87.cs.college.edu:.
sam@cs.college.edu's password:
prog.c 100% 282 56.4KB/s 00:00

# scp prog.c into CS31 subdirectory on cs87.cs.college.edu
laptop$ scp prog.c you@cslab.cs.college.edu:./CS31/.
sam@cs.college.edu's password:
prog.c 100% 282 56.4KB/s 00:00

你也可以把远程路径名作为 scp 的源命令行参数,把文件从远程机器复制到本地机器。例如,要把远程系统上 sarita 用户文件中的远程文件 unix_notes/basics 复制到你家里机器上的当前目录,这样做:

You can also copy files from a remote machine to a local machine using the remote path name as the source command line argument to scp. For example, to copy a remote file unix_notes/basics from the sarita user’s files on the remote system to the current directory on your home machine, do:

1
2
3
laptop$ scp sarita@cs87.cs.college.edu:./unix_notes/basics ./
sarita@cs.college.edu's password:
basics 100% 78 86.3KB/s 00:00

如果你想传输许多文件,先把它们打包成单个归档文件会很有用。然后复制这个归档文件,再解包得到那些文件。此外,在传输之前压缩文件会减少总的传输时间。tar 工具是打包和解包文件的一种方式,gzip 和 bzip2 是文件压缩工具的两个例子。关于使用 tar 和文件压缩的更多信息,见归档与压缩一节。

If you want to transfer many files, it is useful to package them up into a single archive file first. Then copy over the archive file, and unpack it to get the set of files. In addition, compressing the file before transfer will decrease the total transfer time. The tar utility is one way to pack and unpack files, and gzip and bzip2 are two examples of file compression utilities. See [targzbz] for more information on using tar and file compression.

原书旁注 · 生成 SSH 密钥(Generating SSH Keys)

远程连接 Linux 机器时,你不需要生成 ssh 密钥;默认情况下,ssh 用你在所连接的远程系统上的用户名和密码进行身份认证。然而,对于某些网站(例如 GitHub),你可能需要把你的 ssh 公钥上传到它的 Web 服务器上,才能访问它的某些资源。ssh 使用公钥-私钥加密来加密要通过网络连接安全发送的数据。ssh 使用的这类加密称为非对称加密:你主机(客户端)上的私钥用来加密通过网络发送给服务器的外发数据,服务器用你的公钥来解密你的消息。服务器在把响应通过网络发回给你之前,用你的公钥加密它,ssh 客户端收到消息后用你的私钥解密。为了让这一切能够工作,你首先需要创建一对 ssh 公钥-私钥,然后把公钥上传到服务器(并把私钥私密地保存在你的客户端机器上)。

要生成 ssh 密钥,运行 ssh-keygen -t rsa。它会提示你输入密钥的存放位置(默认在你的 ~/.ssh 目录中),并要求你为正在生成的 ssh 密钥输入一个口令(passphrase):

1
2
3
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
...

如果成功,它会在你的 ~/.ssh 目录中创建两个新文件:

  • id_rsa:你的私钥(不要分享这个)

  • id_rsa.pub:你的公钥(可以上传到 Web 服务器的那个)

然后你可以把 id_rsa.pub 文件的内容复制粘贴到需要它的 Web 服务器上。

你也可以用这种方法把公钥上传到远程主机上,以便用密钥口令而不是密码来连接。

For remotely connecting to a Linux machine, you don’t need to generate ssh keys; by default ssh uses your username and password on the remote system you are connecting to for authentication. However, for some websites, like GitHub, you may need to upload your public ssh key to its web server to access some of its resources. ssh uses public-private key encryption to encrypting data to be sent securely over a network connection. The type of encryption ssh uses is known as asymmetric encryption, where your private key on your host machine (the client) is used to encrypt outgoing data that is sent over the network to a server, and the server uses your public key to decrypt your message. The server encrypts its response using your public key before sending it back to you on the network, and when the ssh client receives the message it is decrypted using your private key. In order to get this to work, you first need to create an ssh public-private key pair, and then upload your public key to the server (and keep your private key private on your client machine).

To generate an ssh key, run ssh-keygen -t rsa. It will prompt you for a key-location, which by default is in your ~/.ssh directory, and ask you to enter your passphrase for the ssh key you are generating:

1
2
3
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
...

If successful, this creates two new files in your ~/.ssh directory:

  • id_rsa: your private key (don’t share this one)

  • id_rsa.pub: your public key (the one you can upload to web servers)

You can then copy and paste the contents of your id_rsa.pub file to the web server that requires it.

You can also use this method to upload your public key to remote hosts to connect using your key passphrase rather than your password.

译注 · SSH 实际上怎样加密数据

上面对加密过程的描述并不准确。按 SSH 传输层协议规范 RFC 4253,连接建立时双方通过密钥交换(例如 Diffie-Hellman)生成一次性的会话密钥,之后的数据用协商好的对称加密算法加密,并用由共享密钥算出的 MAC 保护完整性。你的公钥-私钥对是在这条加密通道建立之后,用于身份认证的(让服务器确认你持有与已登记公钥相配的私钥),并不是用来加密通信内容的,服务器也不会用你的公钥加密响应。

现代更新(截至 2026-11)· SSH 密钥的新默认值

  • Ed25519 成为默认:OpenSSH 9.5 起,ssh-keygen 默认生成 Ed25519 密钥(不再需要也不必写 -t rsa)。发布说明指出,Ed25519 公钥体积小、用起来很方便,OpenSSH 自 6.5 版(2014 年 1 月)起就已支持。生成的文件相应地叫 id_ed25519 和 id_ed25519.pub。OpenSSH 9.5 发布说明
  • RSA/SHA-1 签名默认停用:OpenSSH 8.8 起默认禁用使用 SHA-1 哈希的 RSA 签名,因为 SHA-1 已被攻破。不过发布说明也指出,对大多数用户这一变化是无感的,不需要替换现有的 ssh-rsa 密钥,它们会自动改用 RSA/SHA-256/512 签名。OpenSSH 8.8 发布说明

17.3.3. 参考资料

17.3.3. References

更多信息参见:

For more information see:

  • 手册页(例如 man ssh)
  • The man pages (e.g., man ssh)