Hexo

看。 那海中的星辰

0%

SSH 配置

本文主要讲述如何进行 SSH 的客户端和服务端的配置,便于工作中的使用。


Overview

  • public key 用于放置在 target os
  • private key 放在 local os 上,注意安全保存。

当使用 SSH client 连接 target os 时,target 会确定 client 有匹配的 private key,才会授权 client 访问。

SSH key 使用方式

  1. 本地生成 key
1
ssh-keygen -t rsa -b 4096 -C "xx@example.com" -f .ssh/vm/ms_xzard -N {password}

生成对应的 key pair 到 .ssh/vm/ms_xzard folder

  1. 配置 target
1
ssh-copy-id -i ~/.ssh/vm/ms_xzard.pub azureuser@myserver

public key copy 到 target 的 ~/.ssh/authorized_keys 文件中,SSH 连接时 target 会通过 authorized_keys 和 client 匹配对应的私钥。

  1. 配置 client
1
ssh-add -K .ssh/vm/ms_xzard

在 Mac OS 中,可以通过上述 -K 参数将 private key 添加到 Keychain 中。

  1. 登录 target
1
ssh vm_name@ip

检查服务端配置

path /etc/ssh/sshd_config
修改配置,禁止密码登陆,强制rsa登陆

1
2
3
4
PubkeyAuthentication yes
PasswordAuthentication no
# Authentication 仅允许指定用户登陆
AllowUsers {{user}}

service sshd restart

tip

每次登录都要繁琐的输入 name 和 ip,我们可以通过配置 client 来简化这个操作

1
2
3
4
5
6
7
8
9
touch ~/.ssh/config
vim ~/.ssh/config
# edit text
Host myvm
UseKeychain yes
AddKeysToAgent yes
Hostname ip
User user
IdentityFile ~/.ssh/vm/pi_raspberry

通过以上配置,下次登录时候就可以直接键入 name 登录

1
ssh myvm

参考文档

SSH official