码迷,mamicode.com
首页 > 系统相关 > 详细

Linux-ssh的rsa认证登录配置

时间:2016-05-13 10:45:24      阅读:614      评论:0      收藏:0      [点我收藏+]

标签:

首先看一下实验环境:

[root@localhost ~]# cat /proc/version #ip 192.168.254.130
Linux version 2.6.32-431.el6.x86_64 (mockbuild@c6b8.bsys.dev.centos.org) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) ) #1 SMP Fri Nov 22 03:15:09 UTC 2013
[root@wulaoer ~]# cat /proc/version   #ip 192.168.254.131
Linux version 2.6.32-431.el6.x86_64 (mockbuild@c6b8.bsys.dev.centos.org) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) ) #1 SMP Fri Nov 22 03:15:09 UTC 2013
wlof@wlof-virtual-machine:~$ sudo cat /proc/version #192.168.254.129
[sudo] wlof 的密码: 
Linux version 4.2.0-16-generic (buildd@lcy01-07) (gcc version 5.2.1 20151003 (Ubuntu 5.2.1-21ubuntu2) ) #19-Ubuntu SMP Thu Oct 8 15:35:06 UTC 2015

两台centos和一台ubuntu,两台centos版本相同,为了区分我把每台的用户用红色进行区分。如果你的是刚刚安装的系统那就需要你安装ssh,centos直接yum -y install ssh 如果是ubuntu的话就用sudo apt-get install openssh-server很简单的,一条命令执行就可以了。

第一:密钥生成  

[root@wulaoer ~]# 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:
8e:5f:78:ca:fc:79:cc:e0:c7:18:2a:31:2f:0a:8d:40 root@wulaoer
The key‘s randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
| E               |
|.                |
|.       S        |
| . o  oo .o      |
|  o . .+oooB     |
|   .  o+o+o.*    |
|    .. o=.oo     |
+-----------------+
[root@wulaoer ~]# cd /root/.ssh/ #如果是ubuntu的话是家目录下的用户名下的.ssh目录下。
[root@wulaoer .ssh]# ls
id_rsa  id_rsa.pub

第二:可以直接在本地修改公钥文件名也可以在传送的过程修改,这个根据自己习惯。

[root@wulaoer .ssh]# ll
总用量 12
-rw-------. 1 root root 1675 5月  12 23:10 id_rsa
-rw-r--r--. 1 root root  394 5月  12 23:10 id_rsa.pub
-rw-r--r--. 1 root root  397 5月  12 20:13 known_hosts
[root@wulaoer .ssh]# mv id_rsa.pub authorized_keys   #或者
[root@wulaoer .ssh]# scp -r authorized_keys 192.168.254.130:/root/.ssh/
root@192.168.254.130‘s password: 
authorized_keys 100% 394 0.4KB/s 00:00

第三:测试直接登录看一下需不需要使用密码。

[root@wulaoer .ssh]# ssh 192.168.254.130
Last login: Fri May 13 00:28:12 2016 from 192.168.254.131
[root@localhost ~]# 

这里需要注意下,主机名已经发送变化,说明已经登录成功。  

一台机器名wulaoe和一台ubuntu同时免密码登录localhost,wulaoer已经设置好了,下面是设置ubuntu。

wlof@wlof-virtual-machine:~$ cd /home/wlof/.ssh/  #这里的wolf是我的用户名下的家目录
wlof@wlof-virtual-machine:~/.ssh$ ls
known_hosts
wlof@wlof-virtual-machine:~/.ssh$ ssh-keygen -t rsa #如果这里要是加sudo的话认证的用户就是root了,要看一下认证文件的路径
Generating public/private rsa key pair.
Enter file in which to save the key (/home/wlof/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/wlof/.ssh/id_rsa.   #私钥文件路径
Your public key has been saved in /home/wlof/.ssh/id_rsa.pub.  #公钥文件路径
The key fingerprint is:
SHA256:fE+oB2MQ4GFwWa4Misx8BsTTPRBtcztHWgkoUA/G78A wlof@wlof-virtual-machine
The key‘s randomart image is:
+---[RSA 2048]----+
|.o*BX++.. . |
|.o.B+X o + |
| .+ =.* = |
|=..E o * . . |
|o+ o= S o . |
| o . . = o |
| . . . |
| . |
| |
+----[SHA256]-----+
wlof@wlof-virtual-machine:~/.ssh$ ls
id_rsa id_rsa.pub known_hosts

ssh验证测试

wlof@wlof-virtual-machine:~/.ssh$ scp -r id_rsa.pub root@192.168.254.130:/root/.ssh/authorized_keys  #在这里要注意必须要在连接的IP前加root@,要不系统默认会用当前用户名,在这里已经要用追加的方式加进去        
root@192.168.254.130‘s password: 
id_rsa.pub                            100%  407     0.4KB/s   00:00    
wlof@wlof-virtual-machine:~/.ssh$ ssh 192.168.254.130
wlof@192.168.254.130‘s password: 

wlof@wlof-virtual-machine:~/.ssh$ ssh root@192.168.254.130
Last login: Fri May 13 00:28:44 2016 from 192.168.254.131
[root@localhost ~]#     #主机名已经切换过来,说明登录成功

一台登录多台设置  

  

  

  

Linux-ssh的rsa认证登录配置

标签:

原文地址:http://www.cnblogs.com/wulaoer/p/5486579.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!