欢迎来到之路教程(on itroad-com)
如果 ssh-copy-id 命令不可用的替代方法
如果服务器没有安装 openssh-clients 软件包,我们可以使用另一种方法。
我们可以使用 scp 命令直接复制公钥。
$ cat id_rsa.pub | ssh user@lab02 "cat >> ~/.ssh/authorized_keys"
本文列出了在 Linux 中设置 ssh 密钥以配置无密码 ssh 的步骤。
ssh-keygen 是用于生成公钥和私钥的命令(如果我们还没有这样做的话)。
使用 ssh-copy-id 命令,我们可以将密钥复制到我们想要进行无密码 ssh 设置的目标服务器。
以我们要设置 ssh 密钥的用户身份登录。
在这种情况下,我们使用用户 sandy。为用户sandy 创建私钥和公钥。
当要求输入密码时按两次 Enter 键,因为我们将保留密码为空。
$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/Users/sandy/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /Users/sandy/.ssh/id_rsa. Your public key has been saved in /Users/sandy/.ssh/id_rsa.pub. The key fingerprint is: SHA256:Y2lfHXY1+i5Pok1NuhsfZ16JsIrdUzY1699SwlvOHOY sandy@oranges-MacBook-Air.local The key's randomart image is: +---[RSA 2048]----+ | ..| | . o| | .o .| | . o.= | | S ...ooo| | o o .o+O++| | ..oB=#=| | o o.+ @EO| | . o o.+.+=| +----[SHA256]-----+
注意:我们可以在 ssh-keygen 上指定一个选项,例如 size 和 type 。
我们可以在 ssh-keygen 命令的手册页上找到更多信息
从 ssh-keygen 的手册页:
-b bits Specifies the number of bits in the key to create. For RSA keys, the minimum size is 768 bits and the default is 2048 bits. Generally, 2048 bits is considered sufficient. DSA keys must be exactly 1024 bits as specified by FIPS186-2. -t type Specifies the type of key to create. The possible values are “rsa1” for protocol version 1 and “dsa”, “ecdsa” or “rsa” for protocol version 2.
- 新密钥将位于 /home/test1/.ssh。
移动到 .ssh 目录,如果密钥已创建,请检查公共
$ cd .ssh $ $ ls -lrt id* -rw-r--r-- 1 sandy staff 414 Oct 20 20:35 id_rsa.pub -rw------- 1 sandy staff 1675 Oct 20 20:35 id_rsa
- 将公钥复制到目标服务器。
$ ssh-copy-id -i id_rsa.pub test1@lab02 The authenticity of host 'lab02 (192.168.219.149)' can't be established. RSA key fingerprint is dd:0c:77:26:da:f4:ed:30:64:26:96:29:b3:38:cc:9c. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'lab02,192.168.219.149' (RSA) to the list of known hosts. test1@lab02's password:
检查确认
现在尝试使用“ssh 'test1@lab02'”登录机器,并检查文件 ~/.ssh/authorized_keys 以确保我们没有添加我们不期望的另外密钥。
测试密钥,我们应该直接登录目标服务器,而不需要输入密码。
$ ssh test1@lab02 [test1@lab02 ~]$
日期:2020-09-17 00:13:54 来源:oir作者:oir
