LoginSignup
3
3

More than 5 years have passed since last update.

さくらのVPS 標準OSの環境構築(2) ファイアウォールとシステムの設定

Last updated at Posted at 2016-04-23

前回

さくらのVPS 標準OSの環境構築(1) VPS導入とログイン方法の変更

ファイアーウォールの設定

ファイアーウォールを設定し、不要なポートへのアクセスを禁止します。
基本的に全てを拒否し、必要に応じてそのポート番号だけを解放するように設定します。

80番(HTTP)/443番(HTTPS)/SSH_PORT番(SSH)のポートを解放します。
今回はSSHのSFTPのみを利用し通常のFTPは使わないため、20番/21番ポートは解放は行いません。

IPv4用のiptables、IPv6用のip6tablesの両方を設定します。
大体の設定は同じですが、若干表記が違うので注意しましょう。

iptablesの設定

iptablesを編集します。
ファイルが存在しない場合は新規作成します。

vi /etc/sysconfig/iptables

/etc/sysconfig/iptables
+ *filter
+ :INPUT   DROP [0:0]
+ :FORWARD DROP [0:0]
+ :OUTPUT  ACCEPT [0:0]
+ :RH-Firewall-1-INPUT - [0:0]
+ 
+ -A INPUT -j RH-Firewall-1-INPUT
+ -A FORWARD -j RH-Firewall-1-INPUT
+ -A RH-Firewall-1-INPUT -i lo -j ACCEPT
+ -A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
+ 
+ # HTTP, HTTPS, SSH
+ -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80    -j ACCEPT
+ -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 443    -j ACCEPT
+ -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport SSH_PORT    -j ACCEPT
+ 
+ -A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
+ 
+ COMMIT

設定を保存したら、iptablesを再起動します。

service iptables restart

実行結果
iptables: チェインをポリシー ACCEPT へ設定中filter         [  OK  ]
iptables: ファイアウォールルールを消去中:                  [  OK  ]
iptables: モジュールを取り外し中:                          [  OK  ]
iptables: ファイアウォールルールを適用中:                  [  OK  ]

iptablesが正しく設定されているか確認します。

sudo iptables -L

実行結果
Chain INPUT (policy DROP)
target     prot opt source               destination         
RH-Firewall-1-INPUT  all  --  anywhere             anywhere            

Chain FORWARD (policy DROP)
target     prot opt source               destination         
RH-Firewall-1-INPUT  all  --  anywhere             anywhere            

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain RH-Firewall-1-INPUT (2 references)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED 
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:http 
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:https 
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:SSH_PORT 
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited 

ip6tablesの設定

ip6tablesを編集します。
ファイルが存在しない場合は新規作成します。

vi /etc/sysconfig/ip6tables

下記の設定を、そのまま貼り付けます。

/etc/sysconfig/ip6tables
+ *filter
+ :INPUT   DROP [0:0]
+ :FORWARD DROP [0:0]
+ :OUTPUT  ACCEPT [0:0]
+ :RH-Firewall-1-INPUT - [0:0]
+ 
+ -A INPUT -j RH-Firewall-1-INPUT
+ -A FORWARD -j RH-Firewall-1-INPUT
+ -A RH-Firewall-1-INPUT -i lo -j ACCEPT
+ -A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
+ 
+ # HTTP, HTTPS, SSH
+ -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80    -j ACCEPT
+ -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 443    -j ACCEPT
+ -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport SSH_PORT    -j ACCEPT
+ 
+ -A RH-Firewall-1-INPUT -j REJECT --reject-with icmp6-adm-prohibited
+ 
+ COMMIT

設定を保存したら、ip6tablesを再起動します。

service ip6tables restart

実行結果
ip6tables: チェインをポリシー ACCEPT に設定中: filter      [  OK  ]
ip6tables: ファイアウォールルールを消去中:                 [  OK  ]
ip6tables: モジュールを取り外し中:                         [  OK  ]
ip6tables:  ファイアウォールルールを適用中:                [  OK  ]

ip6tablesが正しく設定されているか確認します。

sudo ip6tables -L

実行結果
Chain INPUT (policy DROP)
target     prot opt source               destination         
RH-Firewall-1-INPUT  all      anywhere             anywhere            

Chain FORWARD (policy DROP)
target     prot opt source               destination         
RH-Firewall-1-INPUT  all      anywhere             anywhere            

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain RH-Firewall-1-INPUT (2 references)
target     prot opt source               destination         
ACCEPT     all      anywhere             anywhere            
ACCEPT     all      anywhere             anywhere            state RELATED,ESTABLISHED 
ACCEPT     tcp      anywhere             anywhere            state NEW tcp dpt:http 
ACCEPT     tcp      anywhere             anywhere            state NEW tcp dpt:https 
ACCEPT     tcp      anywhere             anywhere            state NEW tcp dpt:63306 
REJECT     all      anywhere             anywhere            reject-with icmp6-adm-prohibited 

メールサーバーの設定

メールサーバーpostfixの設定を行います。
メールに関しては別のサービスを利用しているので、今回しっかりとした設定はしません。
最低限サーバーからメール送信が行えれば良いところまで。

ホスト名とドメイン名の設定を行います。
ホスト名は完全修飾ドメイン名を、ドメイン名はホスト名から最初の要素を引いたものを設定します。

vi /etc/postfix/main.cf

/etc/postfix/main.cf
+ myhostname = mail.example.com
+ mydomain = example.com

設定を保存したら、postfixを再起動します。

service postfix restart

root宛のメールアドレス設定

管理ユーザーrootのメールアドレスを設定します。
test@example.comの部分を任意のメールアドレスに差し替えます。

vi /etc/aliases

/etc/aliases
  # Person who should get root's mail
- #root: marc
+ root: test@example.com

システムの自動アップデートの設定

自動アップデートのためのyum-cronをインストールします。

yum install yum-cron

アップデート不要なパッケージがあればyum.confに設定します。
[main]セクションに後述の行を追加します。

vi /etc/yum.conf

/etc/yum.conf
  [main]
  :
+ exclude=gcc* kde* openssh* kernel* php* mysql*
  :

yum-cronの設定ファイルを編集します。

vi /etc/sysconfig/yum-cron

yumの出力情報をまとめたメールの送信先に、rootユーザーを指定します。

/etc/sysconfig/yum-cron
+ MAILTO=root

PreLinkを無効にする

バイナリを変更して高速化を図るprelinkパッケージですが、バイナリ変更によってRubyなどうまく動作しなくなるものがあるため無効化します。

vi /etc/sysconfig/prelink

/etc/sysconfig/prelink
- PRELINKING=yes
+ PRELINKING=no

また、prelinkが変更したバイナリを元に戻す必要もあります。

prelink -ua

DenyHostsのインストール

DenyHostsは、定期的にログ監視を行い接続失敗しているIPアドレスをhosts.denyに登録してくれるパッケージです。
SSHのブルートフォースアタック対策になります。

yumコマンドでインストールします。

yum install denyhosts

インストール後、denuhostsの起動と自動起動の設定を行います。

service denyhosts start && \
chkconfig denyhosts on

注意点として、自分でアクセス失敗した場合に自分が登録される場合があります。
ですので、例外的に必ずアクセスを許可したいIPアドレスが設定できるようになっています。
固定IPを契約されている場合は登録しておきましょう。

allowed-hostsを編集し、アクセスを許可したいIPアドレスを追記します。

vi /var/lib/denyhosts/allowed-hosts

/var/lib/denyhosts/allowed-hosts
  # We mustn't block localhost
  127.0.0.1
+ xxx.xxx.xxx.xxx # <- 自分の固定IP

設定の変更後、denyhostsを再起動してください。

service denyhosts restart

ClamAntiVirusのインストール

Linux用のフリーで開発・運用されているウィルス対策ソフトClamAntiVirusをインストールします。

yum install clamd

インストール後、ClamAntiVirusの起動と自動起動の設定を行います。

service clamd start && \
chkconfig clamd on

ウイルス定義ファイルの更新を行います。

freshclam

スケジュール設定ファイルを作成し、後述の内容を記述します。

vi /etc/cron.daily/virusscan

/etc/cron.daily/virusscan
+ #!/bin/bash
+ 
+ PATH=/usr/bin:/bin
+ 
+ # clamd update
+ yum  --enablerepo=epel -y update clamd > /dev/null 2>&1
+ 
+ # virus scan
+ CLAMSCANTMP=`mktemp`
+ clamscan --recursive --remove  / > $CLAMSCANTMP 2>&1
+ [ ! -z "$(grep FOUND$ $CLAMSCANTMP)" ] && \
+ 
+ # when detect virus, send mail
+ grep FOUND$ $CLAMSCANTMP | mail -s "Virus Found in `hostname`" root
+ rm -f $CLAMSCANTMP

作成したスケジュール設定ファイルに実行権限を与えます。

chmod +x /etc/cron.daily/virusscan

これで定期的に定義ファイルの更新とウイルススキャンを自動で実行するようになります。

LogWatchのインストール

毎日1回、各種ログをメール1通にまとめて送信してくれるLogWatchをインストールします。
こちらはインストールと同時にcronにも登録され自動実行の設定がされます。

yum install logwatch

インストール後、ログの詳細レベルDetailHighに設定します。

vi /etc/logwatch/conf/logwatch.conf

/etc/logwatch/conf/logwatch.conf
+ Detail=High

次回

さくらのVPS 標準OSの環境構築(3) Webサーバーの設定

3
3
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
3
3