Last-modified: 2013-02-11 (月) 13:44:09
自宅サーバー/さくらインターネットVPS試用

概要

ちょっとしたアイディアを思いついたのですが、今使っているグローバルIP以外で試したい内容です。
そこで以前から興味のあった、さくらインターネットのVPSサービスを使ってみることにしました。
初期費用0円、月額980円、年払だと10,780円で、グローバルIP1個とサーバーリソースを使えます。
2週間のお試し期間も用意されています。
さくらのVPS|VPS(仮想専用サーバ)はさくらインターネット


ここでは、契約してから最低限のサーバー設定を行うまでを見ていきます。
構築手順は、自宅サーバー/構築概要(CentOS)をベースに、色々省いて構築します。
サーバーのデフォルト設定はこちらをご覧ください。
SAKURA Internet // サポート - オンラインマニュアル - さくらのVPS OSセットアップ情報
なお、アイディアの実現が目的だったので、さくらVPSのパフォーマンス計測などは行っていません。

方法

  1. 契約し、会員メニューを開きます。
    契約直後から使えます。
  2. [サポート] - [VPSコントロールパネル]からログインすると、このような画面になります。
    ディストリはCentOS 5.5 x64で、ほぼ最小構成で入っている状態でした。
    [OS再インストール]にて、UbuntuやDebian、Fedora、FreeBSDのx86/x64に入れ直すこともできるようです。
    WS000000.PNG
  3. [仮想サーバ操作]からサーバーを起動し、[リモートコンソール]へ移動すると
    このような画面が表れ、一応ターミナルとして使えます。
    ですが、重くて使いものにならないので、SSHで繋ぎ直します。
    ちなみに、rootパスワードは、契約時に登録したメールアドレス宛に届いています。
    WS000001.PNG
  4. SSHでログインします。
    ユーザーは「root」、パスワードはメール記載のものを使います。
  5. 一般ユーザーの追加、昇格可能ユーザの制限、rootパスワードの変更を行ないます。
    LinuxTips/ユーザーを追加したい
    LinuxTips/昇格可能ユーザーを制限したい
  6. SSH可能なユーザを限定し、公開鍵暗号方式でのみログインできるようにします。
    また、ポート番号も変更します。
    SSH/CentOSへのインストール
    SSH/ポート番号を変更したい
  7. 日本語環境にするため、「/etc/sysconfig/i18n」を編集します。
    Everything is expanded.Everything is shortened.
      1
    
     
    
    LANG="ja_JP.UTF-8"
  8. お気に入りの.bashrcと.vimrcを別のサーバからコピペします。
    bash/My設定
    Vim/My設定
  9. vimが貧弱なので、高機能なvimを入れ直します。
    Vim/CentOSへのインストール
  10. iptablesの設定を行ないます。
    iptables/CentOSへのインストール
    今回はこのようにしました。
    Everything is expanded.Everything is shortened.
      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    
    -
    !
    -
    !
     
     
    -
    !
     
     
     
     
    -
    |
    !
     
    -
    !
    -
    !
     
     
     
     
    -
    |
    |
    |
    !
     
     
     
    -
    !
     
     
     
    -
    !
     
     
     
     
    -
    !
     
     
    -
    !
     
     
     
     
     
     
     
     
     
     
     
     
     
    -
    |
    |
    |
    !
    -
    !
    -
    !
    -
    !
    -
    !
    -
    !
    -
    |
    !
    -
    !
     
    -
    |
    |
    |
    |
    |
    |
    !
    -
    |
    |
    !
    -
    !
    -
    !
     
     
    -
    |
    |
    |
    !
    -
    !
    -
    !
    -
    !
    -
    |
    |
    |
    |
    !
    
    #!/bin/sh
     
    # ***変数***
    LOCAL_IF="eth0"
    LOCAL_IP="<グローバルIP>"
     
    # ***モジュール読み込み***
    modprobe ipt_state
    modprobe ipt_LOG
    modprobe ipt_REJECT
    modprobe ipt_limit
     
    # ***クリア***
    # 全ルール削除
    iptables -F
    iptables -X
    # パケットカウンタクリア
    iptables -Z
    # デフォルト拒否
    iptables -P INPUT DROP
    iptables -P OUTPUT DROP
    iptables -P FORWARD DROP
     
     
    # ************************************
    # チェイン作成
    # ************************************
    # ***フラグメント化されたパケット破棄用チェイン***
    iptables -N drop_fragment
    iptables -A drop_fragment -f -j LOG --log-prefix "FRAGMENT: " -m limit --limit 1/s --limit-burst 10
    iptables -A drop_fragment -f -j DROP
     
    # ***不正パケット破棄用チェイン***
    iptables -N drop_invalid_packet
    iptables -A drop_invalid_packet -j LOG --log-prefix "INVALID_PACKET: " -m limit --limit 1/s --limit-burst 10
    iptables -A drop_invalid_packet -j DROP
     
    # ***SYNFLOOD攻撃対策用チェイン***
    iptables -N drop_synflood
    iptables -A drop_synflood -m limit --limit 10/s --limit-burst 20 -j RETURN
    iptables -A drop_synflood -j LOG --log-prefix "SYNFLOOD: " -m limit --limit 1/s --limit-burst 10
    iptables -A drop_synflood -j DROP
     
    # ***不正な組み合わせのフラグ破棄用チェイン***
    iptables -N drop_invalid_flag
    iptables -A drop_invalid_flag -j LOG --log-prefix "INVALID_FLAS: " -m limit --limit 1/s --limit-burst 10
    iptables -A drop_invalid_flag -j DROP
    #
    iptables -N drop_flag
    iptables -A drop_flag -p tcp --tcp-flags ACK,FIN FIN -j drop_invalid_flag
    iptables -A drop_flag -p tcp --tcp-flags ACK,PSH PSH -j drop_invalid_flag
    iptables -A drop_flag -p tcp --tcp-flags ACK,URG URG -j drop_invalid_flag
    iptables -A drop_flag -p tcp --tcp-flags FIN,RST FIN,RST -j drop_invalid_flag
    iptables -A drop_flag -p tcp --tcp-flags SYN,FIN SYN,FIN -j drop_invalid_flag
    iptables -A drop_flag -p tcp --tcp-flags SYN,RST SYN,RST -j drop_invalid_flag
    iptables -A drop_flag -p tcp --tcp-flags ALL ALL -j drop_invalid_flag
    iptables -A drop_flag -p tcp --tcp-flags ALL NONE -j drop_invalid_flag
    iptables -A drop_flag -p tcp --tcp-flags ALL FIN,PSH,URG -j drop_invalid_flag
    iptables -A drop_flag -p tcp --tcp-flags ALL SYN,FIN,PSH,URG -j drop_invalid_flag
    iptables -A drop_flag -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j drop_invalid_flag
     
     
    # ************************************
    # INPUT
    # ************************************
    # ローカルホストからの入力は全て許可
    iptables -A INPUT -i lo -j ACCEPT
    # SYN FLOOD攻撃かをチェック
    iptables -A INPUT -p tcp --syn -j drop_synflood
    # 不正な組み合わせのフラグかをチェック
    iptables -A INPUT -p tcp -j drop_flag
    # フラグメント化されたパケットかをチェック
    iptables -A INPUT -j drop_fragment 
    # パケットの正当性をチェック
    iptables -A INPUT -m state --state INVALID -j drop_invalid_packet
    # 接続済みのものは全て許可
    iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
    # サービス
    #iptables -A INPUT -p tcp --dport 21 -m state --state NEW -j ACCEPT	# FTP
    iptables -A INPUT -p tcp --dport <変更後ポート番号> -m state --state NEW -j ACCEPT    # SSH
    #iptables -A INPUT -p tcp --dport 25 -m state --state NEW -j ACCEPT	# SMTP
    iptables -A INPUT -p tcp --dport 53 -m state --state NEW -j ACCEPT    # DOMAIN(DNS)
    iptables -A INPUT -p udp --dport 53 -m state --state NEW -j ACCEPT    # DOMAIN(DNS)
    #iptables -A INPUT -p tcp --dport 80 -m state --state NEW -j ACCEPT	# HTTP
    #iptables -A INPUT -p udp --dport 110 -m state --state NEW -j ACCEPT	# POP3
    #iptables -A INPUT -p tcp --dport 123 -m state --state NEW -j ACCEPT    # POPFile-POP3
    #iptables -A INPUT -p tcp --dport 443 -m state --state NEW -j ACCEPT	# HTTPS
    #iptables -A INPUT -p tcp --dport 465 -m state --state NEW -j ACCEPT	# SMTP-SSL
    #iptables -A INPUT -p tcp --dport 995 -m state --state NEW -j ACCEPT	# POP3-SSL
    # AUTHリクエストは、パフォーマンスを考慮しDROPではなくREJECT
    iptables -A INPUT -p tcp --dport 113 -j REJECT --reject-with tcp-reset
    # ICMP
    #iptables -A INPUT -p icmp --icmp-type time-exceeded -j ACCEPT
    # ブロードキャスト宛てパケットはログを出さずに破棄
    iptables -A INPUT -d 255.255.255.255 -j DROP 
    # DropboxのLanSyncパケットは破棄
    iptables -A INPUT -p udp --dport 17500 -j DROP
    # 上記以外は破棄
    iptables -A INPUT -j LOG --log-prefix "UNDEFIND_INPUT: " -m limit --limit 1/s --limit-burst 10
     
     
    # ************************************
    # OUTPUT
    # ************************************
    # ローカルホストからの出力は全て許可
    iptables -A OUTPUT -o lo -j ACCEPT
    # 不正な組み合わせのフラグかをチェック
    iptables -A OUTPUT -p tcp -j drop_flag
    # 新規接続を許可
    iptables -A OUTPUT -m state --state NEW -j ACCEPT
    # 接続済みのものは全て許可
    iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
    # ICMP
    #iptables -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT
    #iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT
    #iptables -A OUTPUT -p icmp --icmp-type time-exceeded -j ACCEPT
    # 上記以外は破棄
    iptables -A OUTPUT -j LOG --log-prefix "UNDEFIND_OUTPUT: " -m limit --limit 1/s --limit-burst 10
  11. SELinuxを有効にします。
    SELinux/SELinuxの有効無効を切り替えたい
  12. アップデートを行ないます。
    yum/アップデートしたい
  13. IPv6を無効にします。
    LinuxTips/IPv6を無効化したい
  14. 不要デーモンを停止します。
    ip6tablesとsendmailを止めました。
    LinuxTips/不要デーモンの自動起動を止めたい
  15. 以上で、アイディアを試す程度のサーバ構築は出来ました。

検証時の環境