zoukankan      html  css  js  c++  java
  • Linux SSH无密码登录

    Linux服务器常见的登录方式有两种:密码登录、秘钥登录。工作中我们最常使用的是用秘钥登录的方法,因为使用秘钥登录更高效、更安全。

    如何实现SSH无密码登录:

    原理:无密码ssh登录的主要操作为将本机中的ssh密钥对中的公钥如id_rsa.pub拷贝到目标机器的ssh验证文件authorized_keys中。

    下面我将在docker中实现SSH无密码登录的操作

    1.首先运行一个centos容器作为跳板机使用

    sudo docker run -it centos /bin/bash

    2.安装openssh及其相关软件包,使用命令获取公钥与私钥

    yum -y install openssh*

    [root@75940a29d36a ~]# ssh-keygen -t rsa

    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa):
    Created directory '/root/.ssh'.
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    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:
    8a:13:2b:96:45:78:fc:61:b6:c8:f5:67:50:3f:4a:19 root@75940a29d36a
    The key's randomart image is:
    +--[ RSA 2048]----+
    | E |
    | o . + |
    | . + = . o o |
    | + * + o . . |
    | = o S + |
    | o + . o |
    | + + . |
    | . . . |
    | |
    +-----------------+

    从输出信息中我们可以看到,我们是以root身份进行登录,并在/root下创建了.ssh/目录用于存放公钥和私钥,当然authorized_keys也要放到.ssh/目录下

    3.使用docker打开第二个centos容器作为线上服务器使用

    sudo docker run -it centos /bin/bash

    4.同样我们安装openssh及其相关的软件包,并获取一个密钥对,但是需要注意的是我们的密钥对并没有实际的作用,我们获取密钥对主要是因为它可以在用户的家目录下自动创建.ssh/目录,当然我们也可以手动创建。

    yum -y install openssh*

    [root@710a756dfa5b ~]# ssh-keygen -t rsa
    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa):
    Created directory '/root/.ssh'.
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    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:
    20:07:a2:d9:33:bd:51:9f:87:f8:b8:e1:72:c8:6a:b2 root@710a756dfa5b
    The key's randomart image is:
    +--[ RSA 2048]----+
    | . . . |
    | + o o o o |
    |o + + + + . |
    | o = + . |
    | . o S |
    | . o o |
    | + + |
    |. .. o |
    |E+. |
    +-----------------+

    5.回到我们的跳板机上将我们的公钥传送到线上服务器的用户家目录的.ssh/目录下

    [root@75940a29d36a ~]# ssh-copy-id -i .ssh/id_rsa.pub root@172.18.0.3
    /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

    /usr/bin/ssh-copy-id: ERROR: ssh: connect to host 172.18.0.3 port 22: Connection refused

    [root@75940a29d36a ~]# ssh-copy-id -i .ssh/id_rsa.pub root@172.18.0.3
    The authenticity of host '172.18.0.3 (172.18.0.3)' can't be established.
    ECDSA key fingerprint is 76:f8:d5:28:0c:f6:cc:16:1f:58:be:26:23:3f:95:e6.
    Are you sure you want to continue connecting (yes/no)? yes
    /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
    /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
    root@172.18.0.3's password:

    Number of key(s) added: 1

    Now try logging into the machine, with: "ssh 'root@172.18.0.3'"
    and check to make sure that only the key(s) you wanted were added.

    现在我们已经将公钥传送至线上服务器的对应的用户家目录下的.ssh/目录下,通过输出信息我们也可以看到,现在让我们尝试使用ssh root@172.18.0.3测试是否可以连接到线上服务器

    [root@75940a29d36a ~]# ssh 'root@172.18.0.3'
    [root@710a756dfa5b ~]#

    注意:使用ssh-copy-id命令等同与将本地的公钥粘贴到远程服务器的.ssh/目录下的authorized_keys文件中,只不过使用这种方式更快捷,降低了错误率。

    我们现在发现连接没有问题,也就是我们成功的设置了SSH无密码登录!但是还有一个问题就是,ssh默认的端口号为22,但是实际工作中我们很少使用默认的端口号,所以我们就出现了新的问题,那就是如何ssh-copy-id非22端口,与直接使用ssh命令或scp命令指定端口有些不同:

    方法一:执行命令时就直接指定

    ssh-copy-id -i ~/.ssh/id_rsa.pub "-p 1111 root@172.18.0.3"

    方法二:通过修改配置文件定义端口

    #vim ~/.ssh/config
    加上内容:
    Host server
    Hostname ip
    Port 1111

    方法三:修改全局配置文件实现

    vim /etc/ssh/ssh_config

    Port 1111

    失败时需要注意的项:

    1.实际工作中我们可能同一密钥对多台服务器使用,这样就需要我们通过拷贝密钥对来实现,但是这样就可能会出现新的问题,那就是权限问题。

    [root@710a756dfa5b .ssh]# ll -ah
    total 24K
    drwx------ 2 root root 4.0K May 26 03:28 .
    dr-xr-x--- 3 root root 4.0K May 26 03:01 ..
    -rw------- 1 root root 399 May 26 03:11 authorized_keys
    -rw------- 1 root root 1.7K May 26 03:01 id_rsa
    -rw-r--r-- 1 root root 399 May 26 03:01 id_rsa.pub
    -rw-r--r-- 1 root root 172 May 26 03:28 known_hosts

    我们需要注意的是公钥的权限为644,私钥的权限为600还需要注意对应的公钥私钥对应的属主属组。

    2.当我们操作没问题却连接失败时,很有可能是用户名关系对应的问题。

    例如:我们实际工作中线上服务器很少使用root用户,我们的跳板机是test用户,但是我们新上线了一批服务器,最开始的操作需要使用root用户的权限,要求我们在跳板机上使用test用户通过ssh连接新上线的服务器可以直接转换为root用户,这种情况我们就需要注意用户名对应的问题,我们的公钥与私钥一定是在/home/test/.ssh/目录下,远程服务器则是在/root/.ssh/下有写有我们的公钥的authorized_keys,并且连接时注意指定要登录的用户名。

    3.现在使用ansible自动化工具的用户,一定要设置SSH秘钥登录,并且最好使用相互对应的用户名,会减少很多不必要的麻烦。

    4.出现错误时不要慌张,本能的第一反应是查看输出内容,理清排错思路,查看服务进程是否启用,端口号是否已经占用。

    结束语:本人新手,对技术时刻保持着敬畏之心,如有错误望有志之士告知,不胜感激!!!

     
    作者:Federico Sun
    博客地址:https://www.cnblogs.com/Cherry-Linux
    座右铭:人总是这么痛苦吗?还是只有小时候这样?总是如此。
  • 相关阅读:
    docker的核心概念、docker的安装与卸载
    centos 7 配置yum源
    杂记
    linux命令之rpm(软件的安装卸载)
    chrony服务及cobbler+pxe实现自动化装机
    sshd登录控制脚及本sudo权限设置
    selinux控制脚本、AWK应用、监控访问脚本实例
    排除GC引起的CPU飙高
    POI报表导入导出
    逆向学习之环境准备
  • 原文地址:https://www.cnblogs.com/Cherry-Linux/p/6907769.html
Copyright © 2011-2022 走看看