---
title: "使用 PuTTYgen 在 Windows XP 上生成密钥对以及 SSH 的远程复制文件功能——中"
author: "Lewis·Lee"
date: "2010-07-07T10:48:13Z"
tags: []
categories: ["操作系统"]
slug: "puttygen-windows-xp-ssh"
draft: false
lastmod: "2010-07-30T21:49:00Z"
id: 707181
url: "https://douglee.iteye.com/blog/707181"
---

注意 ：代码中的 # 只表示注释作用，该内容不能输入到命令行窗口中

 

**使用 PuTTYgen 在 Windows 系统上生成密钥对**

- Locate and run the PuTTYgen.exe（ PuTTY Key Generator），select SSH-2 RSA key type, then press the Generate button and move the mouse over the blank area (as prompted) to generate some randomess on the keys, as the following graph 20.6：

![](http://dl.iteye.com/upload/attachment/274119/bc8c6ae0-0308-3117-b9a7-92fe86f23c96.png)

图 20.6 在 PuTTYgen 中生成公钥对

- When the key generation completed, copy all the text of public key in the box and append it to lewis's $home/.ssh/authorized\_keys file, as the following graph 20.7：

![](http://dl.iteye.com/upload/attachment/274121/03a8ecdb-820a-38d5-b233-64013e05230c.png)

图 20.7 复制公钥内容到 authorized\_keys 文件中

- Click “Save private key” button to save the PuTTY-generated private key that pair the public key appended to $HOME/.ssh/authorized\_keys file:

![](http://dl.iteye.com/upload/attachment/274123/a5315acb-eb0f-3a68-80e0-0d6e8b7c3e5c.png)

图 20.8 PuTTYgen 已生成公钥

- Run the Putty client (putty.exe) to specify the saved private key and auto-login username before initiating connection to lewis.

      第四点的内容和 [在 Windows XP 上使用 OpenSSH 和 PuTTY 的公钥认证远程管理 Unubtu Server 10.04 LTS——上](/admin/blogs/707136) 对应内容一模一样，这里不再累赘。

      我们可以按“Save private key”，将私钥保存到本地磁盘上。由于我们要使用公钥认证登录到服务器上，所以必须将私钥保存起来，这样 PuTTY 就可以使用该私钥来登录到服务器上。那我们为什么不保存公钥呢？由于使用 PuTTYgen 保存的公钥格式和 Ubuntu Server 的公钥格式不一样（到底那些格式不一样，请参考 [How To Fix “Server Refused Our Key” Error That Caused By Putty Generated RSA Public Key?](http://www.walkernews.net/2009/03/22/how-to-fix-server-refused-our-key-error-that-caused-by-putty-generated-rsa-public-key/) ），所以我们没有必要保存它。那登录服务器时要用到公钥的，那正确的公钥在哪里呢？下面，我就给你解答这个问题。

      我使用 Linux 也不到两个星期，为了解决这个问题，就找我的挚友——[Google](http://www.google.com/) 帮忙了。首先，当然是查看 [PuTTY User Manual](http://tartarus.org/~simon/putty-snapshots/htmldoc/) 喽接着查找关于，接着找到 [Chapter 8: Using public keys for SSH authentication](http://tartarus.org/%7Esimon/putty-snapshots/htmldoc/Chapter8.html#pubkey) （该文档对使用 SSH 权限的公钥认证描述的非常清晰，关于这方面的问题，直接找该文档即可） ，然后就找到了 [8.2.10 ‘Public key for pasting into authorized\_keys file’](http://the.earth.li/%7Esgtatham/putty/0.60/htmldoc/Chapter8.html#puttygen-pastekey) （我们 下载 PuTTY 的 zip 包，直接解压即可运行，里面已包含用户手册，同时也已将它上传到附件中） ，我已将该文档转化为 PDF 格式（只要安装了 Adobe Acrobat，就可以使用 IE 浏览器将 HTML 文件转化为 PDF），详见附件。

1. 该文档提到：OpenSSH 服务器要求 SSH-2 的公钥格式也是一行格式（one-line format），“Public key for pasting into OpenSSH authorized\_keys file”的文本框中给出的是正确的且是一行格式的公钥数据。一般的，我们会使用鼠标来选择这个文本框中的所有内容，接着按“Ctrl+C” 将其内容复制到剪贴板，然后将公钥数据黏贴到已建立连接到服务器的 PuTTY 会话中 ，相关命令行详见下文。本人已测试过，该方法可行。
2. copy all the text of public-key in the box and save it to the text file that name is id\_rsa.pub, then use PuTTY's PSCP tool to transfer the id\_rsa.pub file to lewis's (the Ubuntu Server) $HOME/.ssh category, next append it to the same category of authorized\_keys file. If the authorized\_keys file is not exsit, then you must create it, and grant it to the -wr------ access right. We will use the following command-line to fullfil this task. 我自己的方法本质上就是官方提供的方法，只是用文本文件作为传递的桥梁而已。所以，建议直接使用第一种方法。

```
# 第一种方案的命令行数据
$ cd .ssh
$ sudo nano authorized_keys
# 然后将光标定位到文件的末尾空白行，按“Ctrl+C”，就可以将公钥数据追加到authorized_keys 文件中了，接着按“Ctrl+O”保存文件，再按“Ctrl+X”退出文件编辑状态。
# 文件保存后，重新启动 SSH 服务
$ sudo /etc/init.d/ssh restart
```

```
# 第二种方案的命令行数据
# 打开 Windows 命令行窗口，输入下面命令代码
# pscp -i E:\key\id_rsa1.ppk -P 2323 -scp E:\key\id_rsa1.pub lewis@192.168.1.199:/home/lewis/id_rsa1.pub
$ cd .ssh
# 将 id_rsa1.pub 文件的内容追加到 authorized_keys 文件的末尾
$ cat id_rsa1.pub >> authorized_keys

# If $HOME/.ssh/authorized_keys does not exist then
$ cd .ssh
$ cp id_rsa1.pub authorized_keys
# 给该用户赋予操作该文件的相关权限
$ chmod 600 authorized_keys
# alternative
$ cd $HOME/.ssh
$ touch authorized_keys
$ chmod 600 authorized_keys
```

      PSCP 的参数选项请参考官方文档 [PSCP Usage](http://the.earth.li/~sgtatham/putty/0.60/htmldoc/Chapter5.html#pscp-usage) ，对每个参数的含义都解释的非常清晰明了。For troubleshooting, you can refer to Putty Event Log in windows, i.e. right-click the Putty window title bar and click on the **Event Log** menu:

![](http://dl.iteye.com/upload/attachment/274127/b9587c39-0a59-3fe4-b078-69d1b1cfd6c1.png)

      刚开始，我也出现“Server Refused Our Key”错误，就是因为直接将 PuTTYgen 生成的公钥保存了，然后就 append it to the  authorized\_keys，并且那时的 .ssh 目录下还没有包含 authorized\_keys 文件。然后就到 Google 搜索相关资料，参考了以下几篇文章，并将它们总结成上面这些文字。

      怎么界面都是命令行的，没有图形界面的？在下一篇中，我将给你解答。

 

**参考资料**

1. [Chapter 8: Using public keys for SSH authentication](http://the.earth.li/%7Esgtatham/putty/0.60/htmldoc/Chapter8.html#pubkey) 下的 [8.2.10 ‘Public key for pasting into authorized\_keys file’](http://the.earth.li/%7Esgtatham/putty/0.60/htmldoc/Chapter8.html#puttygen-pastekey)
2. [Configure Putty To Support Password-less SSH Login](http://www.walkernews.net/2008/06/05/configure-putty-to-support-password-less-ssh-login/)
3. [How To Fix “Server Refused Our Key” Error That Caused By Putty Generated RSA Public Key?](http://www.walkernews.net/2009/03/22/how-to-fix-server-refused-our-key-error-that-caused-by-putty-generated-rsa-public-key/)

**SSH 小技巧**

**用 scp 远程复制文件**

通过 scp 命令，可以将本地文件复制到远程 SSH 服务器，也可以将远程 SSH 服务器上的文件复制到本地，还可以从一台 SSH 服务器将文件复制到另一个 SSH 服务器。如果在 Windows 平台上，则可以使用 PuTTY 的姊妹程序 PSCP。

1. 将本地文件复制到 SSH 服务器
2. 将 SSH 服务器文件复制到本地
3. 两台 SSH 服务器之间复制文件
4. 复制某目录下的所有文件
5. 使用公钥认证

```
# 注意：将 PuTTY 的根目录添加到系统环境变量中
# 使用 SSH 的公钥认证方式
# 将本地文件复制到 SSH 服务器
pscp -i E:\key\id_rsa1.ppk -P 2323 -pw ****** -scp E:\key\id_rsa1.pub lewis@192.168.1.199:/home/lewis/
id_rsa1.pub               | 0 kB |   0.2 kB/s | ETA: 00:00:00 | 100%

# 将 SSH 服务器的私钥文件文件复制到本地
pscp -i E:\key\id_rsa1.ppk -P 2323 -pw ****** -scp lewis@192.168.1.199:/home/lewis/.ssh/id_rsa E:\key\id_rsa_server.ppk
id_rsa.ppk                | 1 kB |   1.7 kB/s | ETA: 00:00:00 | 100%

# 在两台 SSH 服务器直接复制文件，只能使用公钥认证方式登录
scp -P 2323 -i /home/lewis/.ssh/id_rsa id_rsa.pub lewis@192.168.1.198:/home/lewis/
```

     scp、PSCP命令的具体参数详见以下官方文档：[scp(1)](http://www.openbsd.org/cgi-bin/man.cgi?query=scp&sektion=1) 、[PSCP Usage](http://the.earth.li/%7Esgtatham/putty/0.60/htmldoc/Chapter5.html#pscp)

**在客户端上指定命令**

      使用 ssh，可以在命令行上指定要执行的命令。

**在服务器上限制所执行的命令**

      在服务器上做限制，使 SSH 连接进来后，只能够执行指定的命令。为此，我们需要修改~/.ssh/authorized\_keys 文件，在相应的 key 面前，用 command 关键字加上要执行的命令，格式为：

```
command="/path/to/some/command arge " ssh-rsa ......(key)......
```

注意 ：所有内容必须在同一行内。如果你使用 nano 编辑器，则需要加上 -w 选项，禁用自动换行功能。

       SSH 连接后，会从服务器的 ~/.ssh/authorized\_keys读取该 command 参数，并执行其中指定的命令，然后退出，直接回到客户端。你可以在服务器上看看命令是否执行成功：

```
$ cat files.list
```

       SSH 的这种功能，在很多时候可以派上用场。

**修改密钥口令**

      你可以随时修改密钥的口令，使用 ssh-keygen 的 -p 选项即可。

**将密钥放入内存**

      如果你需要频繁地使用密钥进行远程连接，则可以用 ssh-agent 代理创建一个会话，然后用 ssh-add 将私钥加载到内存中：

```
$ ssh-agent $SHELL
$ ssh-add
```

      现在，在此会话中，所以的远程 ssh 命令都不需要再调用密钥了，当然也就不需要再频繁地输入口令了。

      使用结束后，记得从内存中删除密钥：

```
$ ssh-add -D
```

      然后，退出 ssh-agent 会话：

```
$ exit
```

     在 Windows 下，可以使用 Pageant 实现相同的功能。

![](http://dl.iteye.com/upload/attachment/274133/98115a19-c4a2-366c-b704-e1a1b69345a7.png)

 

![](http://dl.iteye.com/upload/attachment/274135/85269fa9-229a-3465-bddb-439deb982f3c.png)

 

上一篇：[在 Windows XP 上使用 OpenSSH 和 PuTTY 的公钥认证远程管理 Unubtu Server 10.04 LTS——上](/admin/blogs/707136)

下一篇：[在 Windows XP 上使用 FileZilla 来图形化界面管理 Linux Server——下](/admin/blogs/707294)