MacのVirtualBoxに作ったゲストOSにRSA鍵の認証で接続できるように設定する

MacVirtualBoxに作ったCentOSを少し便利にできたので、いよいよ鍵認証のログイン許可を設定します。
ponsuke-tarou.hatenablog.com

  • 目的 : Linux環境でSSHの鍵作成から設定まで勉強したい!
  • 環境

今回使うのは、公開鍵暗号方式です。

www.atmarkit.co.jp

設定ファイルを使って設定します。

sshd_configのバックアップを取ります。

設定に失敗したり元の設定に戻したくなった時のためにバックアップを取ります。

# rootとしてログインします。
$ ssh root@192.168.0.6
root@192.168.0.6s password: 
Last login: Fri Oct 20 15:35:12 2017 from 192.168.56.1

# sshd_configの存在を確認します。対象の設定ファイルは、ssh_configではなくsshd_configです。
$ ls -la /etc/ssh/*config*
-rw-r--r--. 1 root root 2276 1020 06:52 /etc/ssh/ssh_config
-rw-------. 1 root root 4358 1018 23:50 /etc/ssh/sshd_config
-rw-------. 1 root root 3907 1020 06:52 /etc/ssh/sshd_config.rpmnew

# ファイルをコピーしてバックアップを取ります。
$ cp /etc/ssh/ssh_config /etc/ssh/ssh_config.20170731bak

# ファイルを確認します。
$ ls -la /etc/ssh/*config*
-rw-r--r--. 1 root root 2276 1020 06:52 /etc/ssh/ssh_config
-rw-------. 1 root root 4358 1018 23:50 /etc/ssh/sshd_config
-rw-------. 1 root root 4361  81 00:12 /etc/ssh/sshd_config.20170731bak
-rw-------. 1 root root 3907 1020 06:52 /etc/ssh/sshd_config.rpmnew

sshd_configを編集します。

# viエディタで開きます。
$ vi /etc/ssh/ssh_config
#       $OpenBSD: sshd_config,v 1.93 2014/01/10 05:59:19 djm Exp $
<省略>
# Authentication:

#LoginGraceTime 2m
#PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

RSAAuthentication yes #<------------------------ コメントアウトを外します(RAS認証を許可する)。
PubkeyAuthentication yes #<------------------------ コメントアウトを外します(公開鍵認証を許可する)。

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile      .ssh/authorized_keys #<------------------------ コメントアウトを外します(公開鍵登録ファイルのパスと名前)。

#AuthorizedPrincipalsFile none

#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
RhostsRSAAuthentication no #<------------------------ コメントアウトを外します(RHosts認証を許可しない)。
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication yes
<省略>
# 保存して閉じます。
:wq

# sshdサービスを再起動します。
$ systemctl restart sshd