Centos主机SSH免密登录操作
1、使用ssh-keygen产生公钥与私钥对
命令如下:
ssh-keygen
例子:
[root@server-nzhc0fqh .ssh]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
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:eeEesTLdIuocKOdn8RL3c7Xv2pC4O870GLa4R6PlO/I root@server-nzhc0fqh
The key's randomart image is:
+---[RSA 2048]----+
| |
| |
| o |
| + = |
| S B . |
| .o..* o+... |
| . o o= ..==oo. |
| + o+.. ==**.o |
| .oo. oOE=o++|
+----[SHA256]-----+
参数 -t rsa 表示使用rsa算法进行加密,不输入参数时默认使用rsa算法加密,执行后,会在/home/当前用户/.ssh目录下找到id_rsa(私钥)和id_rsa.pub(公钥)
[root@server-nzhc0fqh .ssh]# ll ~/.ssh/
total 12
-rw------- 1 root root 0 Jul 4 19:26 authorized_keys
-rw------- 1 root root 1766 Jul 5 14:18 id_rsa #私钥
-rw-r--r-- 1 root root 402 Jul 5 14:18 id_rsa.pub #公钥
-rw-r--r-- 1 root root 175 Jul 4 19:54 known_hosts
2、用ssh-copy-id将公钥复制到远程机器中
命令如下:
ssh-copy-id -i ~/.ssh/id_rsa.pub 用户名字@192.168.x.xxx
例子:
[root@server-nzhc0fqh .ssh]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.110.30.197
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '10.110.30.197 (10.110.30.197)' can't be established.
ECDSA key fingerprint is SHA256:2OFWiy6fVGxkMofhKBUjJVLEmw+t/iBO4mmqoFq424s.
ECDSA key fingerprint is MD5:ae:30:91:94:bf:d5:fc:45:80:fd:43:ec:8d:ed:66:ae.
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
------------- Welcome to IOP Cloud Server ---------------
root@10.110.30.197's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@10.110.30.197'"
and check to make sure that only the key(s) you wanted were added.
3、登录到远程机器不用输入密码
命令如下:
ssh 用户名字@192.168.x.xxx
例子:
[root@server-nzhc0fqh /]# ssh root@10.110.30.197
------------- Welcome to IOP Cloud Server ---------------
Last login: Fri Jul 5 14:52:25 2019 from 10.110.30.156
[root@localhost ~]#