鍵認証によるssh接続

sshのデフォルトの設定はパスワード認証による接続になるが成り済ましによってパスワードが分かってしまうと他人でも接続できてしまうため問題がある。

そこでパスワード認証よりももっと安全な接続方法である公開鍵・秘密鍵を使ったssh接続に関する説明をする。

鍵認証による接続に必要なもの

  • 公開鍵
  • 秘密鍵
  • パスフレーズ

鍵の作成

$ ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_dsa.
Your public key has been saved in /home/user/.ssh/id_dsa.pub.
The key fingerprint is:
77:05:88:82:12:ac:1c:56:f4:55:f0:7c:f8:ab:63:a0 USERname@hostname

この時パスフレーズを聞かれるので設定してください

ssh-keygenを実行すると ~/.ssh/ 内に2つの鍵が作成されます

id_dsa      <=秘密鍵
id_dsa.pub <=公開鍵

※秘密鍵は権限に注意して厳重に保管する事 chmod 0600 id_dsa

公開鍵の内容をリモートサーバにあるログイン先 ~/.ssh/authorized_keys に追加します

ssh-rsa AAAAB3NzaC1yc2EKOREWAD~XE9E= rsa-key-20081027
ssh-dss AAAAB3NzaC1kc3KOTTIMODAMIID~G2s/w= USER@hostname

※一つの鍵は1行で書かれておりこの例では2つの秘密鍵が設定されております

ログイン実行

ssh -l username 192.168.0.10
The authenticity of host '192.168.0.10 (192.168.0.10)' can't be established.
DSA key fingerprint is 57:02:38:1a:3a:f1:b6:90:6d:82:9a:37:47:27:73:b1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.10' (DSA) to the list of known hosts.
Last login: Mon Oct 27 15:31:33 2008 from 192.168.0.100

※今回の例ではパスフレーズを設定しなかった(空)のため入力を求められません

ログインdebug

ログインが上手く行かない場合サーバーとクライアント間の接続のやりとりが分かります

※vオプションを付けてログインすると処理内容が見ることができる

$ ssh -v -l username 192.168.0.10
OpenSSH_3.8.1p1 Debian-8.sarge.4, OpenSSL 0.9.7e 25 Oct 2004
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to 192.168.0.10 [192.168.0.10] port 22.
debug1: Connection established.
debug1: identity file /home/username/.ssh/identity type -1
debug1: identity file /home/username/.ssh/id_rsa type -1
debug1: identity file /home/username/.ssh/id_dsa type 2
debug1: Remote protocol version 2.0, remote software version OpenSSH_4.5p1 FreeBSD-20061110
debug1: match: OpenSSH_4.5p1 FreeBSD-20061110 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_3.8.1p1 Debian-8.sarge.4
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-cbc hmac-md5 none
debug1: kex: client->server aes128-cbc hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Host '192.168.0.10' is known and matches the DSA host key.
debug1: Found key in /home/username/.ssh/known_hosts:1
debug1: ssh_dss_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Trying private key: /home/username/.ssh/identity
debug1: Trying private key: /home/username/.ssh/id_rsa
debug1: Offering public key: /home/username/.ssh/id_dsa
debug1: Server accepts key: pkalg ssh-dss blen 434
debug1: read PEM private key done: type DSA
debug1: Authentication succeeded (publickey).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
Last login: Mon Oct 27 15:33:29 2008 from 192.168.0.100
Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994
        The Regents of the University of California.  All rights reserved.

FreeBSD 7.1-PRERELEASE (MYKERNEL) #1: Sun Sep  7 13:28:57 UTC 2008

パスワード認証を止める

鍵認証の設定が完了し接続が確認したらパスワード認証は止めてしまいましょう

sshdが動いているサーバーの /etc/ssh/sshd_config に以下を追加

PasswordAuthentication no
UsePAM no

上記を設定した上でsshdを再起動して他の端末からパスワード認証で接続が拒否されればOKです

created:

Back to top