如何在 Linux 上设置 SSH 免密码登录?

如何在 Linux 上设置 SSH 免密码登录?

我的 Raspberry Pi 运行默认的 Debian 映像并启用了 SSH。我按如下方式登录我的 Pi:

ssh pi@<IP-address-of-my-Pi>

我每次都必须输入密码。我可以以某种方式让我的 Pi 接受来自本地计算机的登录吗?我正在运行 Fedora Linux。

答案1

是的,您可以使用 SSH 对 Raspberry Pi 自动进行身份验证。

作为先决条件,需要满足以下条件:

Linux

在 Linux 下,您可以通过包管理器安装 SSH,使用 生成密钥对ssh-keygen,使用 复制密钥到 Pissh-copy-id并使用 进行测试ssh

在 Linux 上安装 SSH

大多数 Linux 发行版都预装了 SSH 客户端。如果由于某种原因您没有,请使用包管理器安装它:

对于基于 RPM 的 Linux 发行版(例如 Fedora 和 Suse):

sudo yum install ssh

对于基于 DEB 的 Linux 发行版(例如 Debian 和 Ubuntu):

sudo apt-get install ssh

在 Linux 上生成密钥对

首先,您需要一个公钥/私钥对。因此,如果您没有,请运行以下命令以使用默认设置生成密钥对。

$ ssh-keygen
generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/id_rsa.pub.
The key fingerprint is:
24:55:ee:67:83:72:82:55:5f:b9:b4:09:2a:fa:56:a1 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|                 |
|         +    .  |
|        S    E   |
|         .  + +  |
|          .o . o.|
|         o.oo. oo|
|          ==o.BO+|
+-----------------+

将公钥复制到 Linux 上的 Pi

其次,您需要将生成的公钥复制到您想要无密码登录的计算机。因此,在计算机上运行以下命令,并且作为用户,您希望能够访问 Pi:

$ ssh-copy-id pi@<IP-address-of-your-Pi>
Password:
Now try logging into the machine, with "ssh 'pi@<IP-address-of-your-Pi>'", and check in:
  .ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.

在 Linux 上通过 SSH 连接到 Pi

最后,使用 SSH 登录,以验证是否不需要密码:

$ ssh pi@<IP-address-of-your-Pi>

答案2

检查您尝试复制的文件的权限,确保您通过 ssh 登录的用户有权读取这些文件。

相关内容