1. SSH 免密登录

SSH免密登录是SSH认证的两个方式之一,主要过程是将SSH_Client端公钥传送到SSH_Server服务器上并追加到/root/.ssh/authorized_keys文件中间(root或其他用户)。

2. 生成秘钥对

如果系统中没有ssh的认证秘钥,需要手动创建一个秘钥对。

ssh-keygen -t rsa -P ''
  • -t : 指定非对称秘钥算法
  • -P : 指定秘钥的加密密码,查看秘钥需要输入密码,确保在私钥丢失时也无法被窃取
  • 路径 : 秘钥保存在指定的目录中,默认即可

    2.1.1. 例如:

    [root@izj6cdhdoq5a5zsf7bf7a3z ~]# ssh-keygen -t rsa -P ''
    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa):
    Your identification has been saved in /root/.ssh/id_rsa.
    Your public key has been saved in /root/.ssh/id_rsa.pub.
    The key fingerprint is:
    SHA256:w8cWuOINoTGQaP0+HbACje2/spFzAIMTd5zAHElzoDc root@izj6cdhdoq5a5zsf7bf7a3z
    The key's randomart image is:
    +---[RSA 2048]----+
    |.=&Bo. |
    |oB=Oo. . |
    |=oE + + . . |
    | oo+ * + o . |
    | .= + S + |
    | o= = + |
    | + .+ . |
    | .+. |
    | .o |
    +----[SHA256]-----+

    3. 使用ssh-copy-id方式

    ssh-copy-id root@172.31.1.2

    使用默认的配置复制秘钥

    3.1.1. 例如:

[root@host1 /root]
$ssh-copy-id root@172.31.1.2
root@172.31.1.2's password:
Now try logging into the machine, with "ssh 'root@172.31.1.2'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[root@host1 /root]
$ssh root@172.31.1.2
Last login: Mon Aug 6 17:20:38 2018 from 172.31.56.126
[root@host2 ~]$

4. 直接导入方式

直接导入方式是指将host1的公钥(例如:/root/.ssh/id_rsa.pub),通过复制的粘贴的方式,追加到host2主机的/root/.ssh/authorized_keys文件中,如果不存在就创建该文件。 注意:

  • /root/.ssh/authorized_keys文件不能有非法字符,一行一个配置
  • /root/.ssh/authorized_keys文件权限是600

在一些业务场景下需要快速无密码登录远程服务器,SSH提供无密码认证登录方式。

4.1. 创建秘钥对

创建秘钥对,即公钥和私钥。公钥进行加密,需要发送给远端服务器。而私钥用于解密,需要安全保存。

创建公私钥对可以使用ssh-keygen命令,一步步的输入。也可以一次性指定所需要的参数直接生成。如下示例就是直接生成

ssh-keygen  -t rsa -P '' -f /root/.ssh/id_rsa
[root@VM_11_7_centos ~]# ssh-keygen  -t rsa -P '' -f /root/.ssh/id_rsa
Generating public/private rsa key pair.
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
ac:ef:85:ca:c7:41:ea:47:98:06:03:56:94:69:f4:37 root@VM_11_7_centos
The key\'s randomart image is:
+--[ RSA 2048]----+
|   ++o           |
|  o +.           |
| . o  . E        |
|    o  o..       |
|     o =S        |
|      =.o.       |
|     o.o...      |
|     ..o+.       |
|      o+o        |
+-----------------+
  • -t : 加密类型;SSHv1可以使用rsa1,对于SSHv2可以使用 dsaecdsaed25519 或者 rsa
  • -P : 指定密码;无密码登录这里不能输入内容。
  • -f : 秘钥文件路径;默认是/root/.ssh/id_rsa

4.2. 发送公钥

发送公钥可以有很多种方式,就是复制/root/.ssh/id_rsa.pub文件到目标主机上,可以通过scp、ftp、http、nft、U盘等方法。同时ssh也提供一种便捷的方式ssh-copy-id

使用ssh-copy-id可以直接将公钥复制到远端服务器上,并自动配置到/root/.ssh/authorized_keys文件。如果使用其他方式复制需要进行下一步配置任务。

ssh-copy-id -i /root/.ssh/id_rsa  root@192.168.100.100
  • -i : identity_file 指定认证文件。默认认证文件是/root/.ssh/id_rsa
  • root@ : 默认是用户是当前用户

待验证方法

cat ~/.ssh/id_rsa.pub | ssh -p 22 user@host ‘cat >> ~/.ssh/authorized_keys’

4.3. 配置认证

如果使用scp等复制文件的方式将id_rsa.pub复制到远程服务器上,还需要后续的配置。 将公钥追加到目标的机器authorized_keys文件中

cat /tmp/192-168-100-99-id_rsa.pub >> /root/.ssh/authorized_keys

4.4. 权限配置

在配置使用SSH的时候需要注意一些权限的配置,合理配置当前用户的权限。

例如root用户(需要验证)

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod 600 ~/.ssh/id_rsa.pub
chmod 600 /etc/rsyncd.secrets 

4.5. 其他

虽然配置了密钥认证,但是第一次登录还是需要输入密码的

Copyright © 温玉 2021 | 浙ICP备2020032454号 all right reserved,powered by Gitbook该文件修订时间: 2022-01-02 10:01:32

results matching ""

    No results matching ""