ssh使用默认及非默认22端口密钥登录

一、远程服务器默认ssh端口为22

生成公钥,路径保持默认即可:

[root@xuegod2 .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:
49:3a:5c:3c:50:d8:b3:a4:13:f4:6c:a9:b0:96:53:cb [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|     .o+.        |
|      o=+.       |
|    . .+Xo       |
|     BoB.o       |
|    = E.S        |
|   . . .         |
|                 |
|                 |
|                 |
+-----------------+
将生成的公钥分发到远程服务器:
[root@xuegod2 .ssh]# ssh-copy-id -i [email protected]
[email protected]'s password: 
Now try logging into the machine, with "ssh '[email protected]'", and check in:


  .ssh/authorized_keys


to make sure we haven't added extra keys that you weren't expecting.

登录远程服务器:
[root@xuegod2 .ssh]# ssh 192.168.10.31
Last login: Wed May 10 23:55:37 2017 from 192.168.10.32
Warning ! From now on, all of your operation has been record!
[root@xuegod1 ~]# 


二、远程服务器默认ssh端口为非默认端口

这里将远程服务器ssh端口修改为2222

在客户端服务器上生成公钥:

[root@xuegod2 .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:
08:83:aa:ea:55:22:b6:aa:e8:f9:c8:1e:3b:b4:bd:03 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|   .             |
|  . o            |
| .   o .         |
|.o . .. S        |
|oEo o            |
|oo+.             |
|=o*o             |
|XOooo            |
+-----------------+

分发生成的公钥到远程服务器:

[root@xuegod2 .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub  "-p2222 [email protected]"
[email protected]'s password: 
Now try logging into the machine, with "ssh '-p2222 [email protected]'", and check in:


  .ssh/authorized_keys


to make sure we haven't added extra keys that you weren't expecting.


登录远程服务器:
[root@xuegod2 .ssh]# ssh -p 2222 [email protected]
Last login: Wed May 10 23:39:10 2017 from 192.168.10.32
Warning ! From now on, all of your operation has been record!
[root@xuegod1 ~]# 

你可能感兴趣的:(Linux,网路安全)