初めてのシステムと日記

システムも日記も初めてです。

さくらVPSの初期設定とチューニング

最近いろいろ試したいことがあったので、さくらのVPSを契約してみました。
レンタルサーバーだとサーバー屋さんがサーバー設定をしてくれますが、
VPSの場合、自分で全て設定しなければなりません。
そして仕事で使っていたので知っていたのですが、本当に必要最低限のものしか入っていない。。


ここでは、自分が必要と考えた初期設定とチューニングを記述します。

■ユーザー追加

root権限で作業するのは危ないので別ユーザーを作成します。
ここではuserという名前で作成してます。

$ adduser user
$ passwd user
# パスワードを2回聞かれるので入力
Changing password for user user.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.

■鍵交換

自分のPCでssh-keygenを実行します。

$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/user/.ssh/id_rsa):  // 鍵の保存先
Enter passphrase (empty for no passphrase): // パスフレーズ入力 
Enter same passphrase again:  // もう一度パスフレーズを入力


~/.sshディレクトリに鍵が作成されます。

$ ls -la ~/.ssh/ 
total 24
drwx------   5 user  staff   170  1 29 11:19 .
drwxr-xr-x+ 29 user  staff   986  1 26 00:45 ..
-rw-------   1 user  staff  1675  1 29 11:19 id_rsa
-rw-r--r--   1 user  staff   404  1 29 11:19 id_rsa.pub
-rw-r--r--   1 user  staff   802  1 29 11:05 known_hosts


id_rsa.pubをVPS側に~/.ssh/authorized_keysという名前で保存します。
~/.sshディレクトリがなければ作成します。

$ mkdir ~/.ssh
$ vi ~/.ssh/authorized_keys // id_rsa.pubの中身を記述


記述が終わったらVPS側のパーミッションを変更します。

$ chmod 700 ~/.ssh
$ chmod 600 ~/.ssh/authorized_keys


これで自分のPCから公開鍵を使ったSSH接続ができます。

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

iptablesがインストールされているか確認します。

$ yum list | grep iptables
// インストールされてる
iptables.x86_64                            1.3.5-5.3.el5_4.1           installed
iptables-ipv6.x86_64                       1.3.5-5.3.el5_4.1           installed


もしインストールされていなかったらyumでインストールします。

$ yum -y install iptables


自動起動するようになっているか確認します。

$ chkconfig --list | grep iptables
iptables       	0:off	1:off	2:on	3:on	4:on	5:on	6:off


もし自動起動するようになっていなければ設定します。

$ chkconfig iptables on 


現在の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

何も設定されていない。。


iptablesの設定ファイルを新規に作成します。
解放するポートはSSH、HTTP、FTPで、ポート番号はデフォルトにしています。
(念のためSSHのポート番号はふせておきます)

$ vi /etc/sysconfig/iptables

// 以下設定ファイルの中身
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [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 -p icmp --icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p 50 -j ACCEPT
-A RH-Firewall-1-INPUT -p 51 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# SSH, HTTP, FTP1, FTP2
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport #### -j ACCEPT
-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 20 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
COMMIT


設定が終わったらiptablesを再起動します。

$ /etc/rc.d/init.d/iptables restart
Flushing firewall rules:                                   [  OK  ]
Setting chains to policy ACCEPT: filter                    [  OK  ]
Unloading iptables modules:                                [  OK  ]
Applying iptables firewall rules:                          [  OK  ]


最後に設定が反映されているか確認します。

$ iptables -L    
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
RH-Firewall-1-INPUT  all  --  anywhere             anywhere            

Chain FORWARD (policy ACCEPT)
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     icmp --  anywhere             anywhere            icmp any 
ACCEPT     esp  --  anywhere             anywhere            
ACCEPT     ah   --  anywhere             anywhere            
ACCEPT     udp  --  anywhere             224.0.0.251         udp dpt:mdns 
ACCEPT     udp  --  anywhere             anywhere            udp dpt:ipp 
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ipp 
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED 
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh 
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:http 
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ftp-data 
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ftp 
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:https 
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited 

バッチリです。


■利用していないデーモンを終了

これはチューニングになります。
CentOSは多くのサービスがデフォルトで動いているため、必要ないサービスをOffにします。

$ chkconfig auditd off
$ chkconfig autofs off
$ chkconfig avahi-daemon off
$ chkconfig bluetooth off
$ chkconfig cups off
$ chkconfig firstboot off
$ chkconfig gpm off
$ chkconfig haldaemon off
$ chkconfig hidd off
$ chkconfig isdn off
$ chkconfig kudzu off
$ chkconfig lvm2-monitor off
$ chkconfig mcstrans off
$ chkconfig mdmonitor off
$ chkconfig messagebus off
$ chkconfig netfs off
$ chkconfig nfslock off
$ chkconfig pcscd off
$ chkconfig portmap off
$ chkconfig rawdevices off
$ chkconfig restorecond off
$ chkconfig rpcgssd off
$ chkconfig rpcidmapd off
$ chkconfig smartd off
$ chkconfig xfs off
$ chkconfig yum-updatesd off


サービスに関してはこちらのブログを参考にしました。

CentOSをサーバーとして活用するための基本的な設定 - さくらインターネット創業日記


■サーバー再起動

全ての設定が終わったらサーバーを再起動します。

$ reboot

これで設定が反映されます。


以下、設定前と設定後のメモリ使用量です。

設定前
$ free
             total       used       free     shared    buffers     cached
Mem:        510540     431436      79104          0      47652     307488
-/+ buffers/cache:      76296     434244
Swap:      2048276        336    2047940
設定後
$ free
             total       used       free     shared    buffers     cached
Mem:        509760     168016     341744          0      16048     119724
-/+ buffers/cache:      32244     477516
Swap:      2048276          0    2048276

約270MBぐらいメモリ使用量を減らすことが出来ました。


これで必要最低限のサーバー設定とチューニングが完了となります。