ssh 免密登录和别名登录

免密登录

1.首先创建本机的公钥和私钥,使用命令ssh-keygen。
默认生成的公钥名为id_rsa.pub ,私钥名为id_rsa。当然也可以通过参数 -t 来指定名称,如:ssh-keygen -t rsa


 kobe@kobedeMacBook-Pro  ~  ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/kobe/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/kobe/.ssh/id_rsa.
Your public key has been saved in /Users/kobe/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:iaU1FUOUMKgOfdF3PAJcSEXW74FRJS3GUCiLu4CiATg kobe@kobedeMacBook-Pro.local
The key's randomart image is:
+---[RSA 2048]----+
|       ==X@=.*+o.|
|      o ++=.B +..|
|.  . . .+o = * . |
|E . o .=.o. . o  |
|.. o oo S.   . . |
|. . o . .     .  |
| o .   . .       |
|.       .        |
|                 |
+----[SHA256]-----+

2.使用ssh-copy-id <用户名@主机地址>来将公钥添加到目的主机,这里可以使用-i <公钥地址> 来指定使用本机的那个公钥,如:-i ~/.ssh/id_rsa.pub

✘ kobe@kobedeMacBook-Pro  ~  ssh-copy-id -i /Users/kobe/.ssh/id_rsa.pub kobe@192.168.107.2
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/kobe/.ssh/id_rsa.pub"
/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
kobe@192.168.107.2's password:

Number of key(s) added:        1

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

注意:
如果生成的公钥和私钥不是默认名id_rsa.pub和id_rsa,那么在使用ssh登录时,也需要指定私钥文件路径。例如:生成的文件为~/.ssh/github.rsa,那么登录时需要使用ssh -i ~/.ssh/github.rsa 用户名@主机地址

别名登录

使用vim ~/.ssh/config命令编辑文件,添加如下内容:

Host kobe_service
HostName 192.168.107.2
Port 22
User kobe
IdentityFile ~/.ssh/id_rsa.pub
IdentitiesOnly yes

使用ssh <Host>登录目的主机,这里使用ssh kobe_service就相当于执行ssh root@192.168.107.2。
注释:
HostName 指定登录的主机名或IP地址
Port 指定登录的端口号
User 登录用户名
IdentityFile 登录的公钥文件
IdentitiesOnly 只接受SSH key 登录

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Secure Shell Secure Shell(安全外壳协议,简称SSH)是一种加密的网络传输协议,目的是在不...
    cuteximi_1995阅读 7,112评论 0 0
  • SSH是安全的远程登录,使用SSH v2的协议版本,基于DH算法做密钥交换,基于RSA或DSA实现身份认证。SSH...
    Yi_Feng阅读 4,913评论 0 1
  • SSH 为建立在应用层和传输层基础上的安全协议。SSH 是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协...
    妄语莫言阅读 3,134评论 0 0
  • SSH 为 Secure Shell 的缩写,由 IETF 的网络小组(Network Working Group...
    shuff1e阅读 5,757评论 1 11
  • 登录流程 密钥登录比密码登录安全,主要是因为他使用了非对称加密,登录过程中需要用到密钥对。整个登录流程如下: 远程...
    流浪java阅读 5,385评论 0 0

友情链接更多精彩内容