什么是SSH

SSH (Secure Shell) 一种安全的加密登录,好吧我们先并不关心他是不是安全,只知道这是一种可以通过远程登录计算机(终端)的一个工具。

SSH基本使用

SSH,就像所有的网络应用一样,包括clientserver两个部分。

  • client:客户端,指的是用户在操作的计算机,大概率是笔记本
  • server:服务端,指的是用户要操作的计算机,通常会是一些服务器

SSH 安装

据我所知,Linux Mac大抵都自带了ssh工具,这里只是说一下Windows的安装(包括client和server)

Windows SSH Server

我们可以使用OpenSSH作为ssh工具,参照微软文档进行安装

安装 OpenSSH | Microsoft Learn

这里简单的说明步骤:

  1. 查询ssh相关的包(其实就是client和server)

    1
    Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
  2. 返回的结果如下 - 这个例子表明安装了server而没有安装client

    1
    2
    3
    4
    5
    Name  : OpenSSH.Client~~~~0.0.1.0
    State : NotPresent

    Name : OpenSSH.Server~~~~0.0.1.0
    State : Installed
  3. 根据需要安装服务器或客户端组件

    1
    2
    # Install the OpenSSH Server
    Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
  4. 启动(配置自启动和防火墙-port 22

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    # Start the sshd service
    Start-Service sshd

    # OPTIONAL but recommended:
    Set-Service -Name sshd -StartupType 'Automatic'

    # Confirm the Firewall rule is configured. It should be created automatically by setup. Run the following to verify
    if (!(Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue | Select-Object Name, Enabled)) {
    Write-Output "Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, creating it..."
    New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
    } else {
    Write-Output "Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists."
    }

Windows 配置密钥登陆
C:\ProgramData\ssh\administrators_authorized_keys 保存管理员的公钥, C:\Users\username\.ssh\authorized_keys 保存用户的公钥
微软文档中提到: 如果用户属于管理员组,则改为使用 %programdata%/ssh/administrators_authorized_keys
https://learn.microsoft.com/zh-cn/windows-server/administration/openssh/openssh_server_configuration#authorizedkeysfile

Windows SSH Client

这里理论上可以向server一样安装openssh的客户端,但考虑到需要使用ssh工具的人大抵还是会使用git的,这里直接安装git后再git bash中使用ssh即可

当然,安装openssh的客户端方法如下:

1
2
# Install the OpenSSH Client
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0

SSH连接(使用密码)

安装好后,打开terminalgit bash)输入

1
ssh -h

打开ssh命令说明,剩下的参照说明操作,本文结束(bushi)

这里给出一个最基本的连接格式:

1
2
3
ssh username@host
#-----example-----#
ssh qianmo@10.129.45.47
  • username : 这个用户名指的是要登陆的计算机(服务器)中你希望使用的用户名

    当然,(特别是Windows用户)如果你不清楚用户名是什么,大可打开用户这个文件夹,看看里面哪个名字熟悉一些(haha)

  • host : 一般来讲这个参水会填一个本地ip 例如192.168.x.x10.21.x.x 当然在某些特定的条件下也可以使用域名 [www.xxx.xxx](http://www.xxx.xxx) 甚至设备名 DESKTOP-XXXXXX 这些暂时不表

第一次连接后可能会弹一大段表示本地没有记录过这个server,这时候直接输入yes 回车就好了

如果有密码这时候会要求输入,输入过程不显示,输入后回车确认,如果看到用户名切换到服务器的用户就说明成功了

1
2
3
4
5
6
7
8
9
10
11
$ ssh qianmo@10.129.45.47
Linux raspberrypi 6.1.21-v8+ #1642 SMP PREEMPT -- aarch64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login:
qianmo@raspberrypi:~ $

至此,你可以连接你希望连接的所有服务器了


SSH连接(使用密钥)

先说为什么需要密钥:我们上传密钥后就不再需要每次连接都输入密码了

生成密钥

这里会涉及RSA加密和公钥私钥,只需要记住,私钥一定要自己保存,不可以公开,自然也就不可以上传到服务器上。

首先要在本地生成一对密钥,打开terminal(Windows 用 git bash

1
ssh-keygen -t rsa

之后输入三个回车 这三个回车分别表示:存储到默认位置,不设置密码,确认不设置密码。如果需要也可以自行设置

这对key保存的位置总的说是 User/name/ssh/… Linux可能是 ~/.ssh/…

上传公钥

生成密钥后,我们要把公钥上传到服务器

最简单的方法:打开terminal(Windows 用 git bash

1
2
3
ssh-copy-id username@host
#-----example-----#
ssh-copy-id qianmo@10.129.45.47

这样会自动上传到服务器中。这个位置大概在User/name/ssh/authorized_keys (Windows) ~/.ssh/authorized_keys(Linux)

如果没能自动上传,可以自己创建这个文件,再把公钥拷贝进去

1
nano authorized_keys
1
2
3
4
GNU nano 5.4          authorized_keys    

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCy9bA...
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCqPa9...

同样的,需要授权多个客户端也可以在这里添加


SSH别名(配置config)

显然让人同时记住用户名 主机地址 密码是痛苦的,我们通过密钥解决了密码的问题,那么是不是也有方法来让我们不需要记住ip地址呢?

那肯定有。

买个域名(bushi)

客户端修改config文件(没有就新建一个)

进入上面一直在说的ssh文件夹中:User/name/ssh/authorized_keys (Windows) ~/.ssh/authorized_keys (Linux)

编辑config文件

1
nano config
1
2
3
4
5
GNU nano 7.2           config
# Read more about SSH config files: https://linux.die.net/man/5/ssh_config
Host pi
HostName 10.129.45.47
User qianmo

分别填写三个字段:

  • Host : 自己起的一个好记的名字
  • HostName : 主机名称或地址,一般是ip地址
  • User : 用户名

保存退出,至此只需要 ssh Host 就可以连接了

1
2
3
4
5
6
7
8
9
10
11
$ ssh pi
Linux raspberrypi 6.1.21-v8+ #1642 SMP PREEMPT -- aarch64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login:
qianmo@raspberrypi:~ $