环境:
客户端: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