SSH 免秘钥登录

  1.什么是SSH

  • SSH(远程连接工具)连接原理:ssh服务是一个守护进程(demon),系统后台监听客户端的连接,ssh服务端的进程名为sshd,负责实时监听客户端的请求(IP 22端口),包括公共秘钥等交换等信息。
  • ssh服务端由2部分组成: openssh(提供ssh服务)    openssl(提供加密的程序)
  • ssh的客户端可以用 XSHELL,Securecrt, Mobaxterm等工具进行连接

 2.免秘钥登录远程服务器

客户端IP:192.168.139.136      服务端IP:192.168.139.135

 2.1.首先我们在客户端生成一个公钥和一个私钥

  •  [root@localhost ~]# ssh-keygen -t dsa
  •  Generating public/private dsa key pair.
  • Enter file in which to save the key (/root/.ssh/id_dsa): 
  •  Enter passphrase (empty for no passphrase): 
  •   Enter same passphrase again: 
  •  Your identification has been saved in /root/.ssh/id_dsa.
  •  Your public key has been saved in /root/.ssh/id_dsa.pub.
  •   The key fingerprint is:
  •  SHA256:uIywlzFlO4k3uo/OgpSKHvX+HWP/KKQtkl9ufwynJsk [email protected]
  •  The key's randomart image is:
  •  +---[DSA 1024]----+
  •  |                 |
  •  |                 |
  •  |      o          |
  • |     + +         |
  • |  o.+ B S        |
  • | o.o.O + . . .   |
  • |+o. =.+ ==. =    |
  • |+ oo.= o=E+o.o   |
  • |.. o=o=ooo=+o.   |
  • +----[SHA256]-----+

2.2.我们将公钥拷贝到服务端

  • 利用远程机的用户登录
  • ssh-copy-id -i id_dsa.pub [email protected]
  • [root@localhost .ssh]# ssh-copy-id -i id_dsa.pub [email protected]
  • /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_dsa.pub"
  • /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
  • [email protected]'s password: 
  • Number of key(s) added: 1
  • Now try logging into the machine, with:   "ssh '[email protected]'"
  • and check to make sure that only the key(s) you wanted were added. 

2.3.远程连接服务端

  • [root@localhost .ssh]# ssh [email protected]
  • Last failed login: Thu May 23 16:18:25 CST 2019 from 192.168.139.136 on ssh:notty
  • There were 2 failed login attempts since the last successful login.
  • Last login: Thu May 23 16:14:24 2019 from knight-pc.airdream
  • [root@localhost ~]# ls
  •  anaconda-ks.cfg
  •  [root@localhost ~]# cd /
  •  [root@localhost /]# ls
  •  bin  boot  data  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  swap  sys  tmp  usr  var  web
  •   [root@localhost /]# cd home
  •   [root@localhost home]# ls
  •    git
  •   [root@localhost home]# exit
  •    logout
  •  Connection to 192.168.139.135 closed

3. 总结一下钥匙和锁的关系

  3.1.多个钥匙开一把锁

      把id_dsa.pub 复制给各个服务器

   3.2.一个钥匙开duobasuo

              把id_dsa 传给各个服务器

              把id_dsa 传给自己 

你可能感兴趣的:(SSH 免秘钥登录)