2013年6月12日水曜日

CentOSの設定2(Serversman@VPS)

前回に続いて基本的なセキュリティの設定をします。
前回sshのrootログインの禁止を設定しましたが、
今回はよりセキュアな公開鍵認証でのログイン方法に変更します。
まずはじめにローカルPCで鍵生成を行います。
私はLinuxを使っているのでssh-keygenで作成しました。


$ ssh-keygen -t rsa

ここでパスフレーズが聞かれますが、ログインする際に必要なので
覚えておきましょう。空のままだとパスフレーズは聞かれずログイン出来ます。
$ ls ~/.ssh
id_rsa id_rsa.pub

id_rsa(秘密鍵)とid_rsa.pub(公開鍵)が作成されました。

続いて公開鍵をサーバーに転送します。
$ scp -P 3843 ~/.ssh/id_rsa.pub ユーザー名@接続先IP:

公開鍵を認証できるようにしましょう。
$ ssh -p 3843 ユーザー名@接続先 ← サーバーにログイン

ログインしたら、次のようにファイル作成を行う。
$ mkdir .ssh ← .sshを作成
$ chmod 700 .ssh ←パーミッション変更
$ cd .ssh
$ touch authorized_keys
$ chmod 600 authorized_keys ←パーミッション変更
$ cat ~/id_rsa.pub >> authorized_keys
$ rm ~/id_rsa.pub

続いて、公開鍵認証をする為にsshd_configの設定を行う。
# vim /etc/ssh/sshd_config

#PubkeyAuthentication yes
#AuthorizedKeysFile .ssh/authorized_keys
↓ コメントを外す
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

ついでにパスワードでのログインを禁止しましょう。
PasswordAuthentication yes
    ↓
PasswordAuthentication no

sshdの再起動
# service sshd restart



続いて標準ではファイアウォールの設定がされてませんので、
iptablesの設定をします。
現在のiptablesの状況
# iptables -L

Chain INPUT (policy ACCEPT)
target prot opt source destination

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

INPUT、FORWARD、OUTPUT全て許可になっています。

まずポリシーを決めます。
# iptables -P INPUT DROP
# iptables -P OUTPUT ACCEPT
# iptables -P FORWARD DROP

これだとパケットは何一つ受けとりません。こうしておいて通すパケットを
明示的に指定していきます。通すパケットを最小限にするのが目的です。

私の場合webサーバーとして使うのでport80,443とsshのport3843のみ通すようにしました。
Serversman@VPSではsshのポートが標準で3843に変更されてます。

パケットを指定するルールを設定します。まず、ping(icmp)とlo interfaceを通るパケットは通します。
# iptables -A INPUT -i lo -j ACCEPT
# iptables -A INPUT -p icmp -j ACCEPT


続いて既にconnectionが張られている通信を許可します。
# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT


http,https,sshを許可します。
# iptables -A INPUT -p tcp --dport 80 -j ACCEPT ←http
# iptables -A INPUT -p tcp --dport 443 -j ACCEPT ←https
# iptables -A INPUT -p tcp --dport 3843 -j ACCEPT ← ssh


設定を保存。保存しないと再起動時に設定が消えます。
# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]


iptablesの再起動
# service iptables restart
iptables: Flushing firewall rules: [ OK ]
iptables: Setting chains to policy ACCEPT:filter [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: [ OK ]

以上でiptablesの設定は完了。

sshのポートですが、もしsshd_configでポート変更した場合、
ポート番号を合わせないとログイン出来なくなってしまうので注意。

0 件のコメント:

コメントを投稿