2.2. 第一步:在客户端创建自己的公钥-私钥对

下面的命令的演示是在 Unix 系统中实现的,但是并不是说 Windows 不能用,参见后面的“吉祥三宝——Cygwin版”,看看 Windows 中是如何使用的。

SSH 有版本1 和版本2 的区别,为了能兼容低版本的 SSHD(D: 即 Daemon,服务/服务器是也),下面示例中,对两种版本的协议都支持。

  1. 创建 ssh 1 兼容的公钥私钥对

    localshell$ cd $HOME
    localshell$ ssh-keygen -t rsa1
    Generating public/private rsa1 key pair.
    Enter file in which to save the key (/home/localshell/.ssh/identity
    ):
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /localshell/.ssh/identity.
    Your public key has been saved in /home/localshell/.ssh/identity.pub.
    
  2. 创建 ssh 2 兼容的公钥私钥对

    分别演示用两种算法,生成两套公钥-私钥对。

    localshell$ cd $HOME
    localshell$ ssh-keygen -t dsa
    Generating public/private dsa key pair.
    Enter file in which to save the key (/home/localshell/.ssh/id_dsa
    ):
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /localshell/.ssh/id_dsa.
    Your public key has been saved in /home/localshell/.ssh/id_dsa.pub.
    localshell$ ssh-keygen -t rsa
    ... ...
    
[注意]

在创建公钥-私钥对的时候,系统询问对私钥加密的密码:Enter passphrase (empty for no passphrase)。为了实现无口令SSH认证,不要将私钥加密,因此在询问此口令时,直接按回车。