﻿# CentOS 6.4 安裝後以金鑰登入出現 Server refused our key

前幾天裝了一台 CentOS 6.4 作業系統，結果第一個發生的問題就是遠端 SSH 連線時，無法透過金鑰登入，出現的問題正是 “Server refused our key” 錯誤，經過進一步查詢與了解後，原來又是 SELinux 機制搞的鬼，當然，SELinux 是個可以讓你的 Linux 非常安全的防護機制，如果可以的話，建議不要關閉這個功能。本篇文章將說明如何正確設定 SELinux 標籤，讓你可以正確的使用金鑰登入主機。

下圖是登入失敗的樣子：

![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/0c882bb6a223c9357946-image_2017.png)

先來看看我通常建立 **.ssh/authorized\_keys** 的過程 ( 或是使用 [ssh-copy-id](http://linux.die.net/man/1/ssh-copy-id) 來把公鑰複製到遠端電腦 )

[![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/43ddbf74f3be9a4da6ad-image_thumb_165.png)](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/349a3fd4a59ae993bfe2-image_2018.png)

從這個過程可以看出，建立完成之後的 **.ssh/authorized\_keys** 檔案標籤(Label)為 **user\_home\_t**，這是使用者目錄的預設值，任何使用者建立的檔案，預設都會標上這個標籤。

不過，由於 **.ssh** 資料夾與 **.ssh/authorized\_keys** 檔案會被 **sshd** 讀取，若要讓目錄與檔案擁有正確擁有讀取權限的話，該 **.ssh** 目錄必須被標示為 **user\_home\_dir\_t** 標籤，而 **.ssh/authorized\_keys** 檔案則必須被標示為 **ssh\_home\_t** 標籤才行。

要解決這個問題，你只要登入帳號後，輸入以下指令，將該目錄與其子目錄與檔案全部都還原成 SELinux 安全強化標籤的預設值，如此一來就能立即解決此問題：

```
restorecon -R -v .ssh/
```

如果想要將主機上所有目錄與使用者 (含 root 帳號) 都一併重設的話，也可以考慮以下指令：

```
restorecon -R -v /home/
```

```
restorecon -R -v /root/
```

另一個解決方法，就是透過 [ssh-keygen](http://linux.die.net/man/1/ssh-keygen) 工具產生 **.ssh** 目錄，如此一來之後建立的檔案也會套用正確的 SELinux 標籤，如下圖示：

[![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/edc4028abcb0a5aa9e0e-image_thumb_166.png)](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/a8d1f26e34caa4c0b27a-image_2019.png)

最後，分享關閉 SELinux 的方法，或許有人有需要：

設定暫時關閉 SELinux (執行完立即生效)

```
echo 0 > /selinux/enforce
```

若要永久關閉 SELinux 的限制，需修改 **/etc/selinux/config** 設定檔，將 **enforcing** 改成 **disabled** 即可

```
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
```

**相關連結**

-   [DarkTraining.com| linux information | CentOS public key 'Server refused our key'"](http://darktraining.com/linux/87/)
-   [Bug 499343 - SELinux prevents sshd from reading authorized\_keys file and allowing private key authentication](https://bugzilla.redhat.com/show_bug.cgi?id=499343)
-   [SELinux 簡介](https://web.archive.org/web/20160421044831/http://www.suse.url.tw:80/redhat/SELinux.htm)
-   [鳥哥的 Linux 私房菜 -- 程序管理與 SELinux 初探](https://linux.vbird.org/linux_basic/centos7/0440processcontrol.php#selinux)
-   [Practical SELinux for the beginner: Contexts and labels](http://www.techrepublic.com/blog/opensource/practical-selinux-for-the-beginner-contexts-and-labels/2458)
-   [Security-Enhanced Linux - Wikipedia, the free encyclopedia](http://en.wikipedia.org/wiki/Security-Enhanced_Linux) / [安全增強式Linux](http://zh.wikipedia.org/wiki/%E5%AE%89%E5%85%A8%E5%A2%9E%E5%BC%BA%E5%BC%8FLinux)
-   [How to Disable SELinux](http://www.crypt.gen.nz/selinux/disable_selinux.html)
