CentOS7 配置SSH无密码登陆

CentOS7 配置SSH无密码登陆

配置SSH无密码登陆一共大概需要四步:

第一:确认是否安装SSH,并且启动

请看代码


[root@MyCentOS /]# rpm -qa|grep ssh
openssh-clients-6.6.1p1-25.el7_2.x86_64
openssh-server-6.6.1p1-25.el7_2.x86_64
libssh2-1.4.3-10.el7_2.1.x86_64
openssh-6.6.1p1-25.el7_2.x86_64
[root@MyCentOS /]# chkconfig --list sshd

注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。 
      如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。
      欲查看对特定 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

服务 sshd 信息读取出错:没有那个文件或目录
这表示已经安装,但是没有开机启动


请看如下代码设置开机启动,并查看断口是否已经被开启


[root@MyCentOS /]# chkconfig --level 2345 sshd on
注意:正在将请求转发到“systemctl enable sshd.service”。
[root@MyCentOS /]# service sshd restart
Redirecting to /bin/systemctl restart  sshd.service
[root@MyCentOS /]# netstat -antp |grep sshd
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      7350/sshd           
tcp6       0      0 :::22                   :::*                    LISTEN      7350/sshd    

如果没有安装,就请执行

yum install openssh-server


第二步:生成公钥和私钥



ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa

-t 密钥类型 -P 密语 the passphrase -f 密钥文件

如果您不知道参数的意思,或最后设置出错等,您也可以直接输入以下命令


ssh-keygen -t rsa


[likilone@MyCentOS ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/likilone/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/likilone/.ssh/id_rsa.
Your public key has been saved in /home/likilone/.ssh/id_rsa.pub.
The key fingerprint is:
24:6d:f2:8b:3f:f8:f6:4a:a4:5b:e3:70:bb:2a:4d:44 likilone@MyCentOS
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|      E.         |
|     .o +        |
|      .*         |
|     .  S        |
|      .+ .       |
|     o+.*        |
|    . oOoo       |
|     .o+B=.      |
+-----------------+

一直回车即可,注意 passphrase 就是 密语,这里直接回车,否则您启动SSH时会提示您

Enter passphrase for key 输入密语,这样就不是免密码登录了。

id_rsa 就是您的私匙

id_rsa.pub 就是您的公匙


第三步:导入公匙到认证文件,并且更改权限


[likilone@MyCentOS ~]$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
[likilone@MyCentOS ~]$  chmod 600 ~/.ssh/authorized_keys
[likilone@MyCentOS ~]$ chmod 700 ~/.ssh
注意:权限如果不对,是无法登陆的哦


第四步:测试

输入命令 ssh localhost


[likilone@MyCentOS .ssh]$ ssh localhost
Last login: Thu May 12 07:45:08 2016 from localhost



爆款云服务器s6 2核4G 低至0.46/天,具体规则查看活动详情Blog Img