ssh 免密钥

环境:

客户端:192.168.1.171 centos7
服务端:192.168.1.172 centos7

一、在客户端使用ssh-keygen生成私钥和公钥

全部回车即可

[root@3c5dc52d25fe ~]# ssh-keygen 
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:
SHA256:WPhN2NvmxdEQL/cHjIlu+yFNdDR5/KV9w4L+o5BNlPQ root@3c5dc52d25fe
The key's randomart image is:
+---[RSA 2048]----+
|           .  =+ |
|       . o..o=.=+|
|      . o +o=EB+*|
|       + +.= +.B=|
|      . S *.+ + =|
|         .+B .  .|
|         oo.=    |
|          .o +   |
|           .o .  |
+----[SHA256]-----+

二、把公钥发给服务端机器,通过 ssh-copy-id -i命令

ssh-copy-id -i /root/.ssh/id_rsa.pub 192.168.1.172

[root@3c5dc52d25fe ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub 192.168.1.172 
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.1.172 (192.168.1.172)' can't be established.
ECDSA key fingerprint is SHA256:3ry14vwCl4DStjRm3u6EqkuOven2EQpPLNjPkpgXks8.
ECDSA key fingerprint is MD5:2d:ca:30:a3:82:08:c4:49:04:0a:23:fc:a0:c2:5a:8d.
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@192.168.1.172's password: 

Number of key(s) added: 1

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

三、ssh测试

[root@3c5dc52d25fe ~]# ssh -l root 192.168.1.172 "ifconfig eth1"
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.172  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::ec96:f1ff:fe53:f405  prefixlen 64  scopeid 0x20<link>
        ether ee:96:f1:53:f4:05  txqueuelen 1000  (Ethernet)
        RX packets 3562292  bytes 402869036 (384.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4063865  bytes 6553103066 (6.1 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

可以直接使用远程命令了

---------------------end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值