system / ssh
===
###### tags: `OS / Ubuntu / cmd / system`
###### tags: `OS`, `Ubuntu`, `linux`, `command`, `system`, `ssh`, `ssh-keygen`, `rsa`, `ed25519`, `ecdsa`
<br>
[TOC]
<br>
## 安裝 SSH Server
> for Ubuntu(22.04 / 24.04), by gpt-5 (2025/11/03)
### 一鍵安裝(含服務啟動、基本防火牆)
```bash
sudo apt update
sudo apt install -y openssh-server
sudo systemctl enable --now ssh
sudo systemctl status ssh --no-pager
# 若你有啟用 UFW(預設多半關閉)
# sudo ufw allow OpenSSH # 等同於 allow 22/tcp
# 若剛好沒啟用 UFW 想開啟(可略過)
# sudo ufw enable
```
---
### 本機快速測試(同一台機器)
```bash
# 先看 SSH 伺服器是否在聽 22 埠
ss -tlnp | grep ':22'
# 嘗試從本機連回本機
ssh "$USER"@localhost
# 或顯示詳細除錯訊息
ssh -vv "$USER"@127.0.0.1
```
---
### 推薦的最小安全化設定(使用 drop-in 檔)
Ubuntu 預設會 `Include /etc/ssh/sshd_config.d/*.conf`,建議新增一個本地檔案,不改動原始設定。
```bash
sudoedit /etc/ssh/sshd_config.d/99-local.conf
```
內容(可直接複製):
```conf=
# 變更埠號可減少掃描噪音(選擇性)
# Port 2222
Protocol 2
PubkeyAuthentication yes
PasswordAuthentication no # 建議關閉密碼登入,改用金鑰
PermitRootLogin prohibit-password # 禁止 root 用密碼登入
PermitEmptyPasswords no
# 視需要限制可登入的帳號/群組(選擇性)
# AllowUsers youruser
# AllowGroups sshusers
```
儲存後檢查語法並套用:
```bash
sudo sshd -t # 無輸出代表語法 OK
sudo systemctl reload ssh
```
> 如果你改了埠號,記得放行新的埠:
> `sudo ufw allow 2222/tcp`
---
### 金鑰登入(強烈建議)
在「用戶端」產生金鑰,並安裝到 Ubuntu 伺服器(若用戶端就是同一台,也一樣):
```bash
ssh-keygen -t ed25519 -C "your-note"
ssh-copy-id youruser@<伺服器IP或主機名>
# 若改了埠: ssh-copy-id -p 2222 youruser@host
```
沒有 `ssh-copy-id` 時,手動把用戶端 `~/.ssh/id_ed25519.pub` 內容附加到伺服器 `~/.ssh/authorized_keys`。
---
### 常見除錯指令
```bash
# 服務狀態與日誌
systemctl status ssh --no-pager
journalctl -u ssh -e
# 監聽埠檢查
ss -tlnp | grep ssh
# 防火牆(UFW)規則檢查
sudo ufw status numbered
```
<br>
---
<br>
## 安裝 SSH Client
### Ubuntu / Debian
```bash
sudo apt update
sudo apt install openssh-client
```
<br>
## ssh-keygen
> by gpt-4o (2025/06/02)
### demo
```bash=
# install tool: ssh-keygen
# sudo apt update
# sudo apt install openssh-client
# 產生 RSA 主機金鑰(4096 bits,不要 passphrase)
ssh-keygen -t rsa -b 4096 -f id_rsa_key -N '' -C ''
# 產生 ED25519 主機金鑰(不要 passphrase)
ssh-keygen -t ed25519 -f id_ed25519_key -N '' -C ''
```
---
### ✅ `-t <key_type>`
> - `-t dsa | ecdsa | ecdsa-sk | ed25519 | ed25519-sk | rsa`
> - Specifies the type of key to create. The possible values are “dsa”, “ecdsa”, “ecdsa-sk”, “ed25519”, “ed25519-sk”, or “rsa”.
> - This flag may also be used to specify the desired signature type when signing certificates using an RSA CA key. The available RSA signature variants are “ssh-rsa” (SHA1 signatures, not recommended), “rsa-sha2-256”, and “rsa-sha2-512” (the default).
指定金鑰的演算法類型(key type)
- ### `-t <key_type>` 的常見選項如下
| 演算法 | 說明 |
|--------|------|
| `rsa` | 傳統 RSA 金鑰(較舊,建議至少 2048-bit) |
| `ecdsa` | 基於橢圓曲線的金鑰,適合特定用途但相容性不如 ed25519 |
| `ed25519` | **較新、速度快、安全性強**,預設使用 256-bit,推薦用法 |
| `dsa` | 已過時,**不建議使用**(OpenSSH 預設已不支援) |
- ### 🛡️ 為什麼推薦使用 `ed25519`?
- 安全性比 RSA 高(在相同金鑰長度下)
- 金鑰長度固定為 256-bit(不需指定長度)
- 效率高,適合現代伺服器與用戶端
- 預設在 OpenSSH 7.0 以後皆支援
- ### 推薦選項
如果你沒有特別需求,`ed25519` 是產生 SSH 金鑰最推薦的選項。
---
### ✅ `-f <filename>`
- `-f filename`
Specifies the filename of the key file.
(指定金鑰檔案的檔名。)
---
### ✅ `-N <new_passphrase>`
> - `-N new_passphrase` : Provides the new passphrase.
> - passphrase
> a string of words that must be used to gain access to a computer system or service
> (一串必須用來存取電腦系統或服務的字詞。)
設定新金鑰的密碼(passphrase)
- 你這裡使用 `-N ''` 表示**不設定密碼**,即金鑰無 passphrase。
- 無密碼的金鑰可以自動化使用,但安全性較低。
---
### ✅ `-C <comment>`
> - `-C comment` : Provides a new comment.
設定公鑰中的註解(comment)
- 這個註解會被寫入到 `.pub` 公鑰檔案中,通常用來辨識金鑰用途或擁有者。
- 例如:`-C 'web-server-01'`,可以看到像這樣的公鑰內容:
```
ssh-ed25519 AAAAC3... user@hostname
```
- 你這裡使用 `-C ''` 表示**不添加註解**。
---
### 參數總結
| 參數 | 用途 | 範例值 | 說明 |
|------|------|--------|------|
| `-f` | 儲存金鑰的檔案路徑&名稱 | `my_id_ed25519` | 在當前目錄下產生<br> - `my_id_ed25519`<br> - `my_id_ed25519.pub`<br><br>若沒指定,預設為:<br>`~/.ssh/id_ed25519` |
| `-N` | 設定密碼 | `''` | 不設定 passphrase |
| `-C` | 設定註解 | `'user@hostname'` | 公鑰註解,用於識別金鑰用途 |
<br>
## 討論
### SSH 的遠端主機金鑰發生變化
- ### Question
```
$ ssh root@127.0.0.1 -p 32222
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:iSSZFAFnfD/w8qQ7YRXR6VkHIO90oQyUvEFZUDzddZ4.
Please contact your system administrator.
Add correct host key in /home/root/.ssh/known_hosts to get rid of this message.
Offending RSA key in /home/root/.ssh/known_hosts:68
Host key for [127.0.0.1]:32222 has changed and you have requested strict checking.
Host key verification failed.
```
- ### gpt-4.1 (2025/07/17)
你遇到的問題是 SSH 的遠端主機金鑰發生變化,通常發生在:
* 伺服器重裝或更換了 SSH 金鑰。
* IP 或 Port 被重新分配給其他主機。
* 你主動調整了 SSH 配置。
---
### ⚠️ 解決步驟
#### 方法一:刪除舊金鑰(推薦)
透過以下命令刪除有問題的 host key:
```bash
ssh-keygen -R "[127.0.0.1]:32222"
```
- `ssh-keygen -R` 的功能是 移除指定主機的 SSH 金鑰(從 `known_hosts` 檔案中刪除對應的 host key)。
- `-R` 指的是「Remove」。
- 後面接的參數是主機的 IP 或域名及 Port(若使用非預設的 22 Port)。
然後再次 SSH 連線:
```bash
ssh root@127.0.0.1 -p 32222
```
當詢問是否信任新的 host key 時,輸入 `yes`。
---
#### 方法二:手動編輯 known\_hosts(適合進階用戶)
手動編輯檔案 `/home/root/.ssh/known_hosts`:
```bash
vim /home/root/.ssh/known_hosts
```
* 移至第 **68** 行,刪除該行,並保存退出。
接著再重新連線:
```bash
ssh root@127.0.0.1 -p 32222
```
---
**完成上述步驟後,即可成功登入。**
<br>
{%hackmd vaaMgNRPS4KGJDSFG0ZE0w %}