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

来源:互联网 发布:中科汇联怎么样知乎 编辑:程序博客网 时间:2024/06/05 15:36

一、远程服务器默认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 root@xuegod2.cn
The key's randomart image is:
+--[ RSA 2048]----+
|     .o+.        |
|      o=+.       |
|    . .+Xo       |
|     BoB.o       |
|    = E.S        |
|   . . .         |
|                 |
|                 |
|                 |
+-----------------+
将生成的公钥分发到远程服务器:
[root@xuegod2 .ssh]# ssh-copy-id -i root@192.168.10.31
root@192.168.10.31's password: 
Now try logging into the machine, with "ssh 'root@192.168.10.31'", 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 root@xuegod2.cn
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 root@192.168.10.31"
root@192.168.10.31's password: 
Now try logging into the machine, with "ssh '-p2222 root@192.168.10.31'", 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 root@192.168.10.31
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 ~]# 

原创粉丝点击