﻿# Ubuntu / Debian Linux 重新產生 OpenSSH Host Keys 的方法

上週用 [Remastersys](http://www.geekconnection.org/remastersys/) 安裝壹台預先設定好的主機，但安裝好之後卻發現 OpenSSH Server 完全連不上，一連上就會掛掉 ( 顯示 **Connection closed by ip.add.ress** 錯誤訊息 )，後來發現原來是安裝在不同主機後的 OpenSSH Host Keys 已經無法使用，所以必須重新產生主機金鑰才能讓遠端透過 OpenSSH 登入。

**第一步：刪除舊的 SSH Host Keys**

```
/bin/rm /etc/ssh/ssh_host_*
```

**第二步：重新設定 OpenSSH Server**

```
dpkg-reconfigure openssh-server
```

大功告成！

設定好之後如果你使用 PieTTY 登入會看到一段警告的訊息，確認無誤的話按下 Yes 即可：

![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/980c4fdf2ce3def50406-SNAGHTML17d72e2e.png)

不過如果你在 Linux 環境下，就會導致該主機無法再連線，這時會出現以下錯誤訊息：

```
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@    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 the RSA host key has just been changed.The fingerprint for the RSA key sent by the remote host ise8:ea:91:57:4e:fe:1e:3e:5a:4e:f9:7f:0e:bb:ea:de.Please contact your system administrator.Add correct host key in /root/.ssh/known_hosts to get rid of this message.Offending key in /root/.ssh/known_hosts:9RSA host key for 10.31.1.13 has changed and you have requested strict checking.Host key verification failed.
```

這時建議直接直接刪除上述紅字部分所指示的第 9 行，刪掉之後就可以重新連線了：

**相關連結**

-   [Ubuntu / Debian Linux Regenerate OpenSSH Host Keys](http://www.cyberciti.biz/faq/howto-regenerate-openssh-host-keys/)
-   [Warning: Remote Host Identification Has Changed error and solution](http://www.cyberciti.biz/faq/warning-remote-host-identification-has-changed-error-and-solution/)
