パーミッションを変えてしまって ssh でログインできなくなった時


root で

chmod -R 777 /

とかやってしまうと、秘密鍵が他のユーザーでも読めてしまうため ssh でログインできなくなってしまいます。

以下のように -vvv をつけると、 ssh のログが表示できます。パーミッションの不備の場合は、 key_load_public: No such file or directory と表示されたりします。

ssh -vvv -l username example.com

OpenSSH_7.1p2, OpenSSL 1.0.2d 9 Jul 2015                                        
debug1: Reading configuration data /home/user/.ssh/config           
debug1: Reading configuration data /etc/ssh/ssh_config                          
debug1: Connecting to example.com [xxx.xxx.xxx.xxx] port 22.               
debug1: Connection established.                                                 
debug1: key_load_public: No such file or directory                              
debug1: identity file /home/user/.ssh/id_rsa type -1                                                                      
debug1: key_load_public: No such file or directory                              
debug1: identity file /home/user/.ssh/id_rsa type -1                                                                 
debug1: Enabling compatibility mode for protocol 2.0                            
debug1: Local version string SSH-2.0-OpenSSH_7.1                                
ssh_exchange_identification: Connection closed by remote host            

こうなったら、ひとつずつパーミッションを修正していくしかありません。

まず、ホームディレクトリ。 ~/.ssh は 700 中のファイルは 600 が良いでしょう

chmod 700 ~/
chmod 700 ~/.ssh
chmod 600 ~/.ssh/*

次に /etc/ssh 以下のファイル。ディレクトリは 755、 *_key.pub ファイル は 644、 *_key ファイルは 600 です。ここは root 権限で行います。

chmod 755 /etc/ssh
chmod 644 *_key.pub
chmod 600 *_key

最後に特権分離用のディレクトリです。ここは world-writable でなければ良いので、 711 などで大丈夫です。 ここも root 権限で行ないます。

chmod 711 /var/empty/sshd

ちなみに、ここのパーミッションが不備だと、 /var/empty/sshd must be owned by root and not group or world- writable とか言われて sshd が起動しません。

ここまでやれば、 ssh でログインできるはずです。