ssh配置

一、安装sshd服务

1.Ubuntu服务器安装

1
2
# Ubuntu软件源安装
sudo apt-get install openssh-server

2.Windows 10下安装

​ 如果想在windows平台上使用ssh服务,需要安装相关的ssh服务,这里以PowerShell安装OpenSSH为例,参考:安装 OpenSSH | Microsoft Docs

Step1

1
2
3
4
5
6
7
8
9
# 检查本机ssh安装情况
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
# 返回结果如下:表面我的ssh客户端与服务端都已安装完毕

Name : OpenSSH.Client~~~~0.0.1.0
State : Installed

Name : OpenSSH.Server~~~~0.0.1.0
State : Installed

Step2

1
2
3
4
5
# 根据需要安装客户端/服务端
# Install the OpenSSH Client
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
# Install the OpenSSH Server
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

二、配置服务端信息

sshd参考:sshd(8) - OpenBSD manual pages

1.Ubuntu系统

(1) 写入公钥

​ 在~/.ssh/authorized_keys文件中写入公钥数据(相关目录与文件可以自行创建)

注:如果是自行创建的文件,记得修改目录与文件的权限:其中.ssh 的权限应该是 700;公钥文件authorized_keys的权限应该是 644;私钥文件id_rsa的权限可以使用600

1
2
3
4
5
6
7
8
9
# 修改 .ssh 的权限为 700
cd ~
chmod 700 .ssh
# 修改 authorized_keys 的权限为 644
cd .ssh
chmod 644 authorized_keys
# 如果本机需要使用私钥
# 修改 id_rsa 的权限为 600
chmod 600 id_rsa

(2) 服务端配置文件 /etc/ssh/sshd_config

详情:(28条消息) Linux中ssh配置详解_穆瑾轩的博客-CSDN博客_linux ssh配置

配置文件路径 /etc/ssh/sshd_config, 常用配置项如下:

1
2
3
4
#PermitRootLogin no # 可以禁止root登录
RSAAuthentication yes # 开启RSA认证
PubkeyAuthentication yes # 开启公钥认证
#PasswordAuthentication no # 可以设置禁用密码登录

2.Windows 10系统

​ 启动sshd服务端:

1
2
3
4
# Set the sshd service to be started automatically
Get-Service -Name sshd | Set-Service -StartupType Automatic
# Now start the sshd service
Start-Service sshd

​ 启动ssh客户端连接目标机器:

1
2
3
ssh <登录使用的用户名>@目标机器IP地址
# 调试模式使用ssh————会显示调试日志信息
ssh -v <登录使用的用户名>@目标机器IP地址

三、配置客户端信息

1.Ubuntu 客户端配置

详情:(28条消息) Linux中ssh配置详解_穆瑾轩的博客-CSDN博客_linux ssh配置

​ 配置文件路径 /etc/ssh/ssh_config, 常用配置如下

1
2
3
4
5
# 可以自定义私钥文件路径
# IdentityFile ~/.ssh/id_rsa
# IdentityFile ~/.ssh/id_dsa
# IdentityFile ~/.ssh/id_ecdsa
# IdentityFile ~/.ssh/id_ed25519

2.Windows 10系统

​ 管理密钥

1
2
3
4
5
6
7
8
9
10
### 密钥管理 ###
# 使用ssh时,会自动在.ssh目录下寻找各协议默认对应的私钥文件名作为私钥用于向服务器验证身份
# 因此,需要使用ssh-agent与ssh-add命令配合才可以使用自定义名的私钥文件去验证
### ####### ###
# 设置ssh-agent服务自动启动
Get-Service ssh-agent | Set-Service -StartupType Automatic
# 手动启动ssh-agent服务
Start-Service ssh-agent
# 添加自定义私钥
ssh-add 私钥路径

四、密钥对的生成

原理:使用SSH—最新加密算法ecsda密钥认证登陆_轻描淡写的岁月的技术博客_51CTO博客

1.密钥生成: ssh-keygen指令

参考:SSH-keygen用法 - 杨浪 - 博客园 (cnblogs.com)

1
2
3
4
5
6
7
8
9
10
11
# 密钥生成:ssh-keygen
# 常用指令:
# -t 后接加密方式
# -C 后接注释(一般是邮箱 或者 用户名@机器名)
# -f 后接秘钥对的名字
# -b:指定密钥长度
### 示例 ###
ssh-keygen -b 4096 -t rsa -f rsa_demo -C wilson@wilsonPC
# 生成结果:
# 私钥文件 ./rsa_demo
# 公钥文件 ./rsa_demo.pub

2.密钥对内容解读

1
2
3
4
5
6
7
8
9
10
### 私钥 ###
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXkt...AAB93aWxzb25wY0BXSUxTT04tREVTS1RPUC12ZXIyMDE2AQI=
-----END OPENSSH PRIVATE KEY-----
### 公钥 ###
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAA.../U= wilsonpc@WILSON-DESKTOP-ver2016
# ssh-rsa 表示协议使用了rsa协议
# AAA...x/U= 公钥内容
# wilsonpc@WILSON-DESKTOP-ver2016 指令中-C wilson@wilsonPC所添加的注释内容,如果没有指定则默认为 当前用户名@当前机器名

3.公钥的使用

​ 想要使上一步中的秘钥对生效,则还需要将公钥写入服务器的authorized_keys中来使其生效

1
2
# 例如可以使用cat命令在Ubuntu服务器上将.pub内文件写入authorized_keys
cat rsa_demo.pub >> authorized_keys