Linux下实现免密码登陆

1.Linux下生成密钥bash

  ssh-keygen的命令手册,经过”man ssh-keygen“命令:服务器

  经过命令”ssh-keygen -t rsa“ssh

  生成以后会在用户的根目录生成一个 “.ssh”的文件夹spa

  进入“.ssh”会生成如下几个文件.net

  authorized_keys:存放远程免密登陆的公钥,主要经过这个文件记录多台机器的公钥
code

  id_rsa : 生成的私钥文件
ip

  id_rsa.pub : 生成的公钥文件
ci

  know_hosts : 已知的主机公钥清单rem

    若是但愿ssh公钥生效需知足至少下面两个条件:get

      1) .ssh目录的权限必须是700

     2) .ssh/authorized_keys文件权限必须是600

2.远程免密登陆

  原理图:

  经常使用如下几种方法:

    2.1 经过ssh-copy-id的方式

    命令: ssh-copy-id -i ~/.ssh/id_rsa.pub <romte_ip>

    举例:      

1
2
3
4
5
6
7
8
9
10
11
[root@ test . ssh ] # ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.91.135
root@192.168.91.135's password:
Now try logging into the machine, with "ssh '192.168.91.135'" , and check in :
 
. ssh /authorized_keys
 
to make sure we haven 't added extra keys that you weren' t expecting.
 
[root@ test . ssh ] # ssh root@192.168.91.135
Last login: Mon Oct 10 01:25:49 2016 from 192.168.91.133
[root@localhost ~] #

    常见错误:

      [root@test ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.91.135

      -bash: ssh-copy-id: command not found //提示命令不存在

      解决办法:yum -y install openssh-clients

   2.2 经过scp将内容写到对方的文件中

      命令:scp -p ~/.ssh/id_rsa.pub root@<remote_ip>:/root/.ssh/authorized_keys

      举例:

1
2
3
4
5
6
7
8
9
[root@ test . ssh ] # scp -p ~/.ssh/id_rsa.pub root@192.168.91.135:/root/.ssh/authorized_keys
root@192.168.91.135's password:
id_rsa.pub 100% 408 0.4KB /s 00:00
[root@ test . ssh ] #
[root@ test . ssh ] #
[root@ test . ssh ] #
[root@ test . ssh ] # ssh root@192.168.91.135
Last login: Mon Oct 10 01:27:02 2016 from 192.168.91.133
[root@localhost ~] #

      也能够分为两步操做:

      $ scp ~/.ssh/id_rsa.pub root@<remote_ip>:pub_key //将文件拷贝至远程服务器       $ cat ~/pub_key >>~/.ssh/authorized_keys //将内容追加到authorized_keys文件中, 不过要登陆远程服务器来执行这条命令

相关文章
相关标签/搜索