8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

セキュアなSSHサーバの作成

Last updated at Posted at 2016-02-22

AWSでのセキュアなSSHサーバの作成

前回、AWSでEC2を作成したが、SSHまわりをデフォルトのまま使用するのは危ない気がするので若干の修正を加える。

ssh-2 rsaの鍵を作成

putty key generatorを使用してkeyを作成します。

ssh-2rsaを選択しbit数を「4096」にします。
「generate」ボタンを押下してkeyの四角の囲みの中でマウスをランダムに動かします。
それによりランダムなキーが作成されます。

image

生成が終了すると下記のようにpublic keyができるのでこれを全選択してコピーしておきます。
また、秘密鍵を保存しておきます。

image

ec2に接続し下記ファイルを編集します。

$ vi .ssh/authorized_keys

既に1行キーが記載されているのでその次の行に先ほどコピーした公開鍵を貼り付け保存します。

puttyで接続します。

先ほど保存した秘密鍵を使用して接続できることを確認しておきます。

port変更

デフォルトのSSHポート22番だと攻撃が沢山くるのでとりあえず変更しておきましょう。
前回作成したEC2が紐づいているセキュリティグループを開き、インバウンドタブで編集を押下します。
すると下記のようなウインドウが出るので「ルールの追加」を押下し
適当なポート(1024番以降)を登録しておきます。

image

sshサーバの設定変更

sshサーバの設定を変更する
protocol 1も可能になっているのでprotocol 2だけに。

# cp /etc/ssh/sshd_config /etc/ssh/sshd_config.org

# vi /etc/ssh/sshd_config
下記のように編集

sshd_config
# Port 22
Port 21202

# Protocol 2
Protocol 2

# LoginGraceTime 2m
LoginGraceTime 1m

# StrictModes yes
StrictModes yes

# MaxAuthTries 6
MaxAuthTries 3

# PermitEmptyPasswords no
PermitEmptyPasswords no

# PasswordAuthentication yes
PasswordAuthentication no

# UseDNS yes
UseDNS no

# /etc/init.d/sshd restart

UsePAM noにしたかったがnoにするとamazon linuxではエラーになってしまうのでyesのままにしておく。

ポートを「21202」、秘密鍵を先ほど作成した物にして接続できるかをテストする。
接続できたら

$ vi .ssh/authorized_keys

で一行目のデフォルトの物を削除しておく。

セキュリティグループにて22番ポートを閉じる

22番ポートを削除しておく。
image

以上。

8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?