画像処理(ImageMagick)

ブログなどでサムネイルを作成するにはImageMagickとPerlMagickをサポートしたサーバーが必要で、サポートしてないサーバーでは元画像が縮小表示で表示され重くなる。そこで自宅サーバでもImageMagickとPerlMagickをインストールして、サムネイルを作れるようにする。
■ ImageMagickのインストール(デフォルトでインストールされている。)

[root@server1 ~]# yum -y install ImageMagick

■ PerlMagickのインストール

[root@server1 ~]# yum -y install ImageMagick-perl

■Nucleusでサムネイルを作成するには

Nucleusでサムネイルを作成するのに必要なphp-gdをインストール。
[root@server1 ~]# yum -y install php-gd
httpd を再起動
[root@server1 ~]# service httpd restart
httpd を停止中: [ OK ]
httpd を起動中: [ OK ]

ファイルサーバ(samba)

Lan内であれば、誰でもユーザー認証なしで読み書きできるフルアクセスの共有フォルダを「samba」を作成。
■ sambaがインストールされてるか調べる

[root@server1 ~]# rpm -aq|grep samba
samba-common-3.5.4-68.el6_0.2.i686
samba-winbind-clients-3.5.4-68.el6_0.2.i686
samba-client-3.5.4-68.el6_0.2.i686

■ sambaをyumでインストール

[root@server1 ~]# yum install samba
Loaded plugins: fastestmirror, refresh-packagekit
Loading mirror speeds from cached hostfile
* base: mirror.khlug.org
* centosplus: mirror.khlug.org
* epel: ftp.jaist.ac.jp
* extras: mirror.khlug.org
* updates: ftp.jaist.ac.jp
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package samba.i686 0:3.5.4-68.el6_0.2 set to be updated
--> Finished Dependency Resolution
Dependencies Resolved
====================================================================================================
Package             Arch               Version                         Repository             Size
====================================================================================================
Installing:
samba               i686               3.5.4-68.el6_0.2                updates               5.0 M
Transaction Summary
====================================================================================================
Install       1 Package(s)
Upgrade       0 Package(s)
Total download size: 5.0 M
Installed size: 17 M
Is this ok [y/N]: y
Downloading Packages:
samba-3.5.4-68.el6_0.2.i686.rpm                                              | 5.0 MB     00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing     : samba-3.5.4-68.el6_0.2.i686                                                  1/1
Installed:
samba.i686 0:3.5.4-68.el6_0.2
Complete!

■ フルアクセスの共有フォルダ「share」の作成

[root@server1 ~]# mkdir /home/share
権限変更
[root@server1 ~]# chmod 777 /home/share
所有者変更
[root@server1 ~]# chown nobody:nobody /home/share
すでに作成してある共有フォルダ全てのフォルダ、ファイルの所有者を変更する必要がある場合
[root@server1 ~]# chown -R nobody /home/share

■ samba設定ファイル変更

[root@centos ~]# vi /etc/samba/smb.conf
[global]
日本語文字コード変更(追記)
unix charset = UTF-8
dos charset = CP932
ワークグループを環境に合わせて変更
workgroup = WORKGROUP
ネットワーク内のみからアクセス許可
;       hosts allow = 127. 192.168.1.
ユーザー認証なしの設定に変更
security = share
共有フォルダ(ゴミ箱機能付きの)「share」 セクションの作成(最終行に追加)
[share]
path = /home/share
writable = yes
guest ok = yes
guest only = yes
create mode = 0777
directory mode = 0777
share modes = yes
vfs objects = recycle
recycle:repository = .recycle
recycle:keeptree = no
recycle:versions = yes
recycle:touch = no
recycle:maxsize = 0
#Accessをファイルサーバで使うと、終了するたびに何故かごみ箱にファイルが作成される。
#なので、Accessのファイルはごみ箱にはファイルを作らないようにする。
recycle:exclude = *.tmp ~$* *.mdb *.ldb *.accdb *.laccdb

■ sambaの起動

[root@server1 ~]# /etc/rc.d/init.d/smb start
SMB サービスを起動中:                                      [  OK  ]
nmbの起動
[root@server1 ~]# /etc/rc.d/init.d/nmb start
自動起動設定
[root@server1 ~]# chkconfig smb on
[root@server1 ~]# chkconfig nmb on
[root@server1 ~]# chkconfig --list smb
smb             0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@server1 ~]# chkconfig --list nmb
nmb             0:off   1:off   2:on    3:on    4:on    5:on    6:off

■ Samba確認
Windowsクライアントで「スタート」→「ファイル名を指定して実行」→「名前」に”\\SERVER1\share”または”\\192.168.1.4\share”を入力して「OK」ボタンをクリック。

Webサーバ(Apache)

Apache はインターネットで最も普及しているオープンソースの Web サーバソフトウェアー。Apache は標準でインストールされているのでサーバーの設定に移る。
■Apache がインストールされてるかどうか確認する。

[root@server1 ~]# rpm -q httpd
httpd-2.2.3-11.el5_2.centos.4  ← パッケージ情報が表示された
パッケージ情報が表示されなければインストールする。
[root@server1 ~]# yum -y install httpd

■php、php-mbstringをインストール

[root@server1 ~]# yum -y install php php-mbstring

■Apacheの設定

Apacheの設定ファイルの編集
[root@server1 ~]# vi /etc/httpd/conf/httpd.conf
エラーページ等でOS名を表示しないようにする
ServerTokens OS
↓
ServerTokens Prod
サーバー名を指定
#ServerName www.example.com:80
↓
ServerName yokensaka.com:80 ← #をはずして独自ドメインに変更
# http://httpd.apache.org/docs-2.0/mod/core.html#options
# for more information.
#
CGIの許可、SSIの許可
Options Indexes FollowSymLinks
         ↓
Options Includes ExecCGI FollowSymLinks
.htaccessの許可
# It can be "All", "None", or any combination of the keywords:
#   Options FileInfo AuthConfig Limit
#
AllowOverride None
         ↓
AllowOverride All
長すぎるURI(414 Error)はログに記録しない
# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
#
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
         ↓
LogFormat "%h %l %u %t \"%!414r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
ログの記録内容変更
# logged therein and *not* in this file.
#
#CustomLog logs/access_log common
この下に以下を追加
SetEnvIf Request_URI "default\.ida" no_log
SetEnvIf Request_URI "cmd\.exe" no_log
SetEnvIf Request_URI "root\.exe" no_log
SetEnvIf Request_URI "Admin\.dll" no_log
SetEnvIf Request_URI "NULL\.IDA" no_log
SetEnvIf Remote_Addr 192.168.1 no_log
SetEnvIf Remote_Addr 127.0.0.1 no_log
CustomLog logs/access_log combined env=!no_log
エラーページ等でApacheのバージョンを表示しないようにする
ServerSignature On
↓
ServerSignature Off
デフォルトキャラセットを無効にする(文字化け対策)
AddDefaultCharset UTF-8
         ↓
#AddDefaultCharset UTF-8 ← #を付ける
CGIスクリプトを実行できる様にする
#AddHandler cgi-script .cgi
         ↓
AddHandler cgi-script .cgi .pl ← #をはずして.plを追加
iconsディレクトリのファイル一覧を表示しないようにする
<Directory "/var/www/icons">
Options Indexes MultiViews FollowSymLinks
↓
Options MultiViews FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>

■Perlのパスが/usr/local/bin/perlでもCGIを実行出来るようにする。

現在のPerlの位置
[root@server1 ~]# whereis perl
perl: /usr/bin/perl /usr/share/man/man1/perl.1.gz
シンボリックリンク設定(/usr/local/bin/perlから/usr/bin/perlへリンクをはる)
[root@server1 ~]# ln -s /usr/bin/perl /usr/local/bin/perl
変更後のPerlの位置
[root@server1 ~]# whereis perl
perl: /usr/bin/perl /usr/local/bin/perl /usr/share/man/man1/perl.1.gz

※/usr/local/bin/perlは存在するのにCGIが動かない等リンクがうまく貼れてない時の対処

[root@server1 ~]# whereis perl
perl: /usr/bin/perl /usr/share/man/man1/perl.1.gz
[root@server1 ~]# ln -s /usr/bin/perl /usr/local/bin/perl
ln: creating symbolic link `/usr/local/bin/perl': ファイルが存在します
[root@server1 ~]# rm /usr/local/bin/perl  ← 一旦削除
rm: remove 通常ファイル `/usr/local/bin/perl'? y
[root@server1 ~]# ln -s /usr/bin/perl /usr/local/bin/perl  ← リンクを貼り直す
[root@server1 ~]# whereis perl
perl: /usr/bin/perl /usr/local/bin/perl /usr/share/man/man1/perl.1.gz

■ドキュメントルートの所有者変更

[root@server1 ~]# chown higo:higo /var/www/html/ ← ドキュメントルート所有者変更
[root@server1 ~]# ll /var/www/ ← ドキュメントルート所有者変更確認
合計 32
drwxr-xr-x 2 root root 4096 2007-05-09 19:28 cgi-bin
drwxr-xr-x 3 root root 4096 2007-06-10 23:53 error
drwxr-xr-x 2 higo higo 4096 2007-05-09 19:28 html
drwxr-xr-x 3 root root 4096 2007-06-10 20:26 icons
※-Rオプションで、ある特定のディレクトリの階層化にある全てのフォルダ、ファイルの所有者を変更できる。
[root@server1 ~]# chown -R higo /var/www/html/abc・・・/

※但し、-Rオプションで /var/www/html/の全てのフォルダ、ファイルの所有者を変更するとPHPが自動生成するApacheユーザーのファイルなども変更され、うまく動かないものも出てくるので注意が必要。
■Apacheの起動

Apache(httpd)を起動する
[root@server1 ~]# /etc/rc.d/init.d/httpd start
httpd を起動中:                                            [  OK  ]

■Apacheの自動起動設定

[root@server1 ~]# chkconfig httpd on
[root@server1~]# chkconfig --list httpd
httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off
           ランレベル2~5がオンの状態であることを確認

■公開前の準備
ホームページを外部に公開するに当たって、ルータの設定でポート番号80番を開ける必要がある。
■ ポートチェック【ポート開放確認】
「管理しているサーバーが外部から接続アクセスできるか?」「ポートは開放されているか?」「portは閉じているか?」「ルータのポートは開放されているか」等の
ポートチェック・ポートの疎通確認テストはこちらで

NTPサーバ(ntp)

ここでは、自サーバをNTPサーバーとして立ち上げて、システム時刻を日本標準時間に合わせる。自サーバは、プロバイダーのNTPサーバより時刻を同期して、LAN内のクライアント、ルーターを自サーバの時刻と同期させるようにする。
※Centos7からはchronyがデフォルトとなってるのでこちらを参照
■NTP がインストールされてるかどうか確認。

[root@server1 ~]# rpm -q ntp
ntp-4.2.4p8-2.el6.i686  ← パッケージ情報が表示された
パッケージ情報が表示されなければNTPをインストールする。
[root@server1~]# yum -y install ntp

■NTPサーバーの設定

オリジナルの設定ファイル名を変更する。
[root@server1 ~]# mv /etc/ntp.conf /etc/ntp.conf.org
NTPサーバーの設定ファイルを作成
[root@server1 ~]# vi /etc/ntp.conf
driftfile /var/lib/ntp/drift
server ntp1.plala.or.jp  ← server には、プロバイダーのNTPサーバーを指定
server ntp2.plala.or.jp  ← セカンダリのNTPサーバーも指定しておく

時刻同期先の公開NTPサーバーを調べるには、NTP/プロバイダ・機関別一覧を参照
利用しているISPがここに無い時は、推奨公開サーバを使用することにする。
■NTPサーバーの起動

いったん、手動でNTPサーバの時刻と同期させる。
[root@server1 ~]# ntpdate ntp1.plala.or.jp
28 Dec 17:54:35 ntpdate[11526]: step time server 219.164.211.129 offset 37.009733 sec
NTPサーバーの起動
[root@server1 ~]# /etc/rc.d/init.d/ntpd start
ntpd を起動中:                                             [  OK  ]

■NTPサーバーの自動起動設定

[root@server1 ~]# chkconfig ntpd on
[root@server1 ~]# chkconfig --list ntpd
ntpd            0:off   1:off   2:on    3:on    4:on    5:on    6:off
           ランレベル2~5がオンの状態であることを確認

■NTPサーバーの動作確認

[root@server1 ~]# ntpq -p
remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
ntp1.plala.or.j 202.234.233.106  4 u   19   64    1    4.774    0.959   0.001
ntp2.plala.or.j 202.234.233.109  4 u   18   64    1    6.350    1.529   0.001
約15分後、再度NTPサーバーの動作確認。
[root@server1 ~]# ntpq -p
remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
+ntp1.plala.or.j 202.234.233.106  4 u   34   64  377    5.131  -21.802  14.379
*ntp2.plala.or.j 202.234.233.109  4 u   38   64  377    8.886  -19.952  13.052
指定したNTPサーバの前に*や+が表示された時は同期完了の状態。

■時刻同期設定
クライアントのPCで日付と時刻のプロパティ画面を表示し、「インターネット時刻」タブを表示。 サーバの入力欄に自サーバのIPアドレス「192.168.1.2」を入力し「今すぐ更新」ボタンを押す。 最後に「OK」ボタンを押すと、以降、自サーバより時刻を同期するようになる。
※サーバーでファイアーウォールを有効にしている場合は、サーバーの「ファイアーウォール設定」で「他のポート」に「123:udp」を追記しておく必要がある。そうしないとクライアント側で同期に失敗する。設定の方法はX Window Systemで起動し「アプリケーション・メニュー」から「システム設定」-「セキュリティレベル」を選択して「セキュリティレベルの設定ツール」を起動。「セキュリティレベル」で「ファイアウォールを有効にする」を選択し、「他のポート」に「123:udp」と入力。すでに他のポート設定が記述されてる場合は、カンマ記号(,)で区切って新しい設定を追記。
  
ルータの設定でNTPサーバーのIPアドレスを自サーバーのIPアドレス「192.168.1.2」に変更。  
これで、自サーバー、LAN内のWindowsマシン、ルーターの時刻が常に同期が取れてる状態で日本標準時間となるようになる。

FTPサーバ(vsFTPD)

FTPとは、ネットワーク経由でファイルを転送するためのプロトコル。インターネットにあるサーバから、ファイルをダウンロードしたりすることがあると思うが、その場合はFTPデーモンがサーバー上で起動している状態である。 FTPを使用する場合はLinuxに登録したユーザーでアクセスするのが基本だが、ユーザーに関係なく、アクセスする仕組みとしてAnonymous(匿名)FTPサービスというのも存在する。
■FTPサーバのインストール
FTPサーバを構築するには、vsftpd パッケージをインストールする。
PuTTYでSSH方式のリモート接続による操作で今後進める

Using username "higo".
Authenticating with public key "imported-openssh-key"
Last login: Mon Sep 19 06:10:52 2011 from 192.168.1.7
[higo@server1 ~]$
[higo@server1 ~]$ su -  ← スーパーユーザになる
パスワード(P): ← スーパーユーザのパスワードを入力
[root@server1 ~]# yum install vsftpd
Loaded plugins: fastestmirror, refresh-packagekit
Existing lock /var/run/yum.pid: another copy is running as pid 2557.
Another app is currently holding the yum lock; waiting for it to exit...
The other application is: PackageKit
Memory :  47 M RSS ( 95 MB VSZ)
Started: Mon Sep 19 08:36:00 2011 - 00:14 ago
State  : Sleeping, pid: 2557
Loading mirror speeds from cached hostfile
* base: ftp.jaist.ac.jp
* centosplus: ftp.jaist.ac.jp
* epel: ftp.jaist.ac.jp
* extras: ftp.jaist.ac.jp
* updates: mirror.khlug.org
Setting up Install Process
Resolving Dependencies
--> Running transactiyon check
---> Package vsftpd.i686 0:2.2.2-6.el6_0.1 set to be updated
--> Finished Dependency Resolution
Dependencies Resolved
====================================================================================================
Package              Arch               Version                        Repository             Size
====================================================================================================
Installing:
vsftpd               i686               2.2.2-6.el6_0.1                updates               155 k
Transaction Summary
====================================================================================================
Install       1 Package(s)
Upgrade       0 Package(s)
Total download size: 155 k
Installed size: 343 k
Is this ok [y/N]: y
Downloading Packages:
vsftpd-2.2.2-6.el6_0.1.i686.rpm                                              | 155 kB     00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing     : vsftpd-2.2.2-6.el6_0.1.i686                                                  1/1
Installed:
vsftpd.i686 0:2.2.2-6.el6_0.1
Complete!
FTPサーバを起動させる
[root@server1 ~]# /etc/rc.d/init.d/vsftpd start
vsftpd 用の vsftpd を起動中:                               [  OK  ]

■FTPサーバの設定ファイル変更

vsftpd.conf ファイルを編集
[root@server1 ~]# vi /etc/vsftpd/vsftpd.conf
# Example config file /etc/vsftpd/vsftpd.conf
#
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.
#
# READ THIS: This example file is NOT an exhaustive list of vsftpd options.
# Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's
# capabilities.
匿名ユーザ(anonymous)はログイン禁止(YES→NO)
anonymous_enable=NO
ユーザが打ち込んだ詳細なログを残す
xferlog_std_format=NO  (YES→NO)
log_ftp_protocol=YES   (次の行へ追加)
アイドル時のタイムアウトを設定(#を削除)
idle_session_timeout=600
接続時のタイムアウトを設定(#を削除、120→60)
data_connection_timeout=60
アスキーモードでのアップロードを許可する(#を削除)
ascii_upload_enable=YES
アスキーモードでのダウンロードを許可する(#を削除)
ascii_download_enable=YES
一般ユーザはホームディレクトリーより上へアクセスさせない(#を削除)
chroot_list_enable=YES
ホームディレクトリーより上へアクセスさせないユーザリスト(#を削除)
chroot_list_file=/etc/vsftpd/chroot_list
ディレクトリを削除できるようにする(#を削除)
ls_recurse_enable=YES
パッシブモードを有効にする(最下行へ追加)
pasv_promiscuous=YES
タイムスタンプ時間を日本時間に変更(最下行へ追加)
use_localtime=YES

■FTP接続ユーザ設定

ホームディレクトリーより上へアクセスさせないユーザリスト
[root@server1 ~]# vi /etc/vsftpd/chroot_list
例:ユーザ「centos」は、自ホームディレクトリーより上にアクセスさせない
centos
FTP接続させないユーザリスト
[root@server1 ~]# vi /etc/vsftpd/user_list
# vsftpd userlist
# If userlist_deny=NO, only allow users in this file
# If userlist_deny=YES (default), never allow users in this file, and
# do not even prompt for a password.
# Note that the default vsftpd pam config also checks /etc/vsftpd/ftpusers
# for users that are denied.
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody
centos  ←ユーザ「centos」をFTP接続させないようにリストに追加

■ FTPアクセス制限

サーバー自身、内部ネットワーク、特定の外部IP(xxx.xxx.xxx.xxx)からのアクセスを許可
[root@server1 ~]# echo "vsftpd : 127.0.0.1 192.168.1. xxx.xxx.xxx.xxx" >> /etc/hosts.allow 
上記以外の全てのアクセスを禁止
[root@server1 ~]# echo "vsftpd : ALL" >> /etc/hosts.deny

会社等からアクセスする場合、自分の会社の接続環境(IPアドレス)を調べ、/etc/hosts.allowへ登録する必要がある。そんな時は診断くんで確認できる。
■FTPサーバの起動

[root@server1 ~]# /etc/rc.d/init.d/vsftpd start
vsftpd用のvsftpdを起動中:                                  [  OK  ]

■FTPサーバの自動起動設定

[root@server1 ~]# chkconfig vsftpd on
[root@server1 ~]# chkconfig --list vsftpd
vsftpd          0:off   1:off   2:on    3:on    4:on    5:on    6:off
           ランレベル2~5がオンの状態であることを確認

■FTPサーバの動作確認

クライアント(IP:192.168.1.7)のコマンドプロンプトより行う。
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
ユーザ「centos」が拒否されるか?の確認
C:\Users\higo>ftp
ftp> op
宛先 192.168.1.4
192.168.1.4 に接続しました。
220 (vsFTPd 2.2.2)
ユーザー (192.168.1.4:(none)): centos
530 Permission denied.
ログインできませんでした。
ftp> bye ← FTP接続を終了
221 Goodbye.
ユーザ「higo」がログインできるか?の確認
C:\Users\higo>ftp
ftp> op
宛先 192.168.1.4
192.168.1.4 に接続しました。
220 (vsFTPd 2.2.2)
ユーザー (192.168.1.4:(none)): higo
331 Please specify the password.
パスワード:
230 Login successful.
ftp>
事前に作成しておいたtest.txtをサーバへアップロードする
ftp> put c:\test.txt
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 File receive OK.   アップロードできた
ftp: 19 bytes sent in 0.00Seconds 19000.00Kbytes/sec.
サーバよりファイルをダウンロードする
ftp> get test.txt
200 PORT command successful. Consider using PASV.
150 Opening BINARY mode data connection for test.txt (19 bytes).
226 File send OK.   ダウンロードできた
ftp: 19 bytes received in 0.00Seconds 19000.00Kbytes/sec.
FTP接続を終了
ftp> bye
221 Goodbye.

■外部からの接続
外部からの接続には、ルーターの設定で20番と21番のポートを開ける必要がある。
ドメイン名での接続は事前にドメイン名の取得を行っている必要がある。
■ ポートチェック【ポート開放確認】
「管理しているサーバーが外部から接続アクセスできるか?」「ポートは開放されているか?」「portは閉じているか?」「ルータのポートは開放されているか」等の
ポートチェック・ポートの疎通確認テストはこちらで
■FTPサーバの停止
ファイル転送はSSH (Secure Shell)を使用する場合は、FTPサーバは停止しておく。

[root@server1 ~]# /etc/rc.d/init.d/vsftpd stop
vsftpd を停止中:                                           [  OK  ]
起動時にFTPサーバを起動しない
[root@server1 ~]# chkconfig vsftpd off
[root@server1 ~]# chkconfig --list vsftpd
vsftpd          0:off  1:off  2:off  3:off  4:off  5:off  6:off
[root@linux ~]#

※vsftpd を停止したらルーターの設定で20番と21番のポートを閉じる。

ファイル転送(WinSCP)

WinSCPはSSH (Secure Shell)を使用してファイルを転送するプログラム。 Telnetによるリモートコンピュータへの接続ではパスワードが暗号化さないので、SSH(暗号化シェル)でのリモート接続を行う。 WinSCPを導入するに当って事前にSSHサーバの構築を行っている必要がある。
■秘密鍵のジェネレート
・秘密鍵をWinSCPで使用するには PuTTYgenによるジェネレート が必要。
■WinSCPをインストールする
WinSCP のダウンロードページからInstallation packageをダウンロードして、実行。
(最新版を確認してダウロード 11/09/19 時点ではWinSCP 4.3.5)
■WinSCPの設定

  1. winscp435setup.exeをダウンロードして、実行すると、WinSCP ログイン画面になる。
  2. Languagesボタンを押して[Japanese – 日本語]を選択
    ※[Japanese – 日本語]がない場合は[English]のままインストール、ここから[Japanese]をダウンロード、解凍してWinSCPディレクトリーの中に放り込む
  3. [セッション] ホスト名かサーバーのIPアドレスを入力(例 192.168.1.4 )
  4. [セッション] ユーザー名を入力(例 higo )
  5. [セッション] パスワードを入力
  6. [セッション] 秘密鍵の入力欄にある「 … 」ボタンを押して、保存した秘密鍵「 server.ppk 」ファイルを指定
  7. [セッション] ファイルプロトコル 「SFTP」を選択
    「SCP代替システムを設定」にチェックを入れる
  8. [環境] ファイル名をUTF-8でエンコード 「オン」を選択
    (サーバがEUC-JPの場合は「自動」を選択)
  9. [環境] [ディレクトリ] リモートディレクトリとローカルディレクトリを環境に合わせて設定
  10. [SSH] プロトコルの優先順位を[2]に選択
  11. 「保存」ボタンを押す
  12. セッションの保存名に、適当な名前を付けてOKボタンを押す(例 192.168.1.4 )

■接続前の準備
外部からの接続には、ルータの設定が必要なので、ルータの設定で22番のポートを開ける。
■WinSCPの起動

  1. WinSCPを起動し、保存したセッションを選択してログイン。
  2. パスワードの入力画面が出るので、パスワードを入力し、OKボタンを押す。

インストール後の設定 CentOS 6.0

インストール後の設定は、コマンドを一つ一つ入力して作業を進めていくのが本筋なのだが、初めてだと結構入力ミスなどあり思うようにうまくいかないことがある。そこでCentOSのデスクトップ画面の上のタスクバーにある ウェブ・ブラウザ をクリックして、このページを開きコピペで作業を進めるようにする。まずは、アプリケーション→システムツールから端末を起動してインストール後の設定を行う。
■ RAID動作確認
インストール時に設定したRAID1(ミラーリング)が正しく動作しているかどうかは/proc/mdstat を見ることで確認できる。以下のようにmd1はsda1とsdb1で構成され、md2はsda2とsdb2で構成され、それぞれ2つのパーティションできちんとミラーリングされていることが確認できる。

[higo@server1 ~]$ su - ← rootで作業は進める
パスワード: ← rootのパスワードを入力(画面上には何も表示されない)
[root@server1 ~]#
[root@server1 ~]# vi /proc/mdstat
Personalities : [raid1]
md1 : active raid1 sda1[0] sdb1[1]
307188 blocks super 1.0 [2/2] [UU]
md2 : active raid1 sdb2[1] sda2[0]
243889020 blocks super 1.1 [2/2] [UU]
bitmap: 2/2 pages [8KB], 65536KB chunk
unused devices: 

■ ブートローダを2台目のHDDにもインストールする
RAIDはすでに構築されているが、ブートローダは片方のHDDにしか入っていないので、もしブートローダが入ってたHDDが壊れてしまった場合には起動することができなくなる。そこで、2台目のほうにもブートローダをインストールしてもしもの時にもうひとつのHDDからでも起動できるようにしておく。

[root@server1 ~]# grub
Probing devices to guess BIOS drives. This may take a long time.
GNU GRUB  version 0.97  (640K lower / 3072K upper memory)
[ Minimal BASH-like line editing is supported.  For the first word, TAB
lists possible command completions.  Anywhere else TAB lists the possible
completions of a device/filename.]
grub> device (hd0) /dev/sdb ← ブートローダをインストールしたいHDDの指定
device (hd0) /dev/sdb
grub> root (hd0,0) ← インストールしたHDDの起動パーティションの指定
root (hd0,0)
Filesystem type is ext2fs, partition type 0xfd
grub> install /grub/stage1 (hd0) /grub/stage2 p /grub/grub.conf ← インストールコマンド
install /grub/stage1 (hd0) /grub/stage2 p /grub/grub.conf
grub> quit ← grubのを終了

■ rootになれるユーザを制限

[root@server1 ~]# usermod -G wheel higo ← rootになれるユーザを管理者(higo)のみにする
[root@server1 ~]# vi /etc/pam.d/su
#%PAM-1.0
auth            sufficient      pam_rootok.so
# Uncomment the following line to implicitly trust users in the "wheel" group.
#auth           sufficient      pam_wheel.so trust use_uid
# Uncomment the following line to require a user to be in the "wheel" group.
auth            required        pam_wheel.so use_uid ← #を外す
auth            include         system-auth
account         sufficient      pam_succeed_if.so uid = 0 use_uid quiet
account         include         system-auth
password        include         system-auth
session         include         system-auth
session         optional        pam_xauth.so
次に以下のように入力して/etc/login.defs の最終行に SU_WHEEL_ONLY yes を追加。
[root@server1 ~]# echo "SU_WHEEL_ONLY yes" >> /etc/login.defs

■ ファイアーウォール(iptables)の無効化

[root@server1 ~]# /etc/rc.d/init.d/iptables stop
iptables: ファイアウォールルールを消去中:                  [  OK  ]
iptables: チェインをポリシー ACCEPT へ設定中filter         [  OK  ]
iptables: モジュールを取り外し中:                          [  OK  ]
ファイアウォール自動起動解除
[root@server1 ~]# chkconfig iptables off
[root@server1 ~]# chkconfig --list iptables
iptables        0:off   1:off   2:off   3:off   4:off   5:off   6:off

■ SELinux の無効化

[root@server1 ~]# vi /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing
↓
SELINUX=disabled ← システム起動時にSELinuxを無効化
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
設定を有効にする為再起動。
[root@server1 ~]# reboot

■ logwatchをインストールしてサーバー監視

[root@server1 ~]# yum -y install logwatch
Loaded plugins: fastestmirror, refresh-packagekit
Loading mirror speeds from cached hostfile
epel/metalink                                                      | 3.4 kB     00:00
* base: rsync.atworks.co.jp
* centosplus: rsync.atworks.co.jp
* epel: ftp.jaist.ac.jp
* extras: ftp.tsukuba.wide.ad.jp
* updates: rsync.atworks.co.jp
base                                                               | 3.7 kB     00:00
centosplus                                                         | 3.5 kB     00:00
epel                                                               | 4.3 kB     00:00
extras                                                             | 3.0 kB     00:00
extras/primary_db                                                  | 1.9 kB     00:00
updates                                                            | 3.5 kB     00:00
Setting up Install Process
Resolving Dependencies
--> Running transaction check
-> Package logwatch.noarch 0:7.3.6-49.el6 set to be updated
--> Processing Dependency: perl(Date::Manip) for package: logwatch-7.3.6-49.el6.noarch
--> Running transaction check
-> Package perl-Date-Manip.noarch 0:5.54-4.el6 set to be updated
--> Finished Dependency Resolution
Dependencies Resolved
============================================================
Package Arch Version Repository Size
============================================================
Installing:
logwatch noarch 7.3.6-49.el6 sl-security 297 k
Installing for dependencies:
perl-Date-Manip noarch 5.54-4.el6 sl 176 k
Transaction Summary
============================================================
Install 2 Package(s)
Upgrade 0 Package(s)
Total download size: 472 k
Installed size: 1.8 M
Downloading Packages:
(1/2): logwatch-7.3.6-49.el6.noarch.rpm | 297 kB 00:01
(2/2): perl-Date-Manip-5.54-4.el6.noarch.rpm | 176 kB 00:00
--------------------
Total 203 kB/s | 472 kB 00:02
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : perl-Date-Manip-5.54-4.el6.noarch 1/2
Installing : logwatch-7.3.6-49.el6.noarch 2/2
Installed:
logwatch.noarch 0:7.3.6-49.el6
Dependency Installed:
perl-Date-Manip.noarch 0:5.54-4.el6
Complete!

■ ランレベル3(CUI)で起動
Linuxはサーバー用途で使用するので、本来なら X Window System は必要ない。常に X Window System を利用する必要が無いのであれば、以下のように /etc/inittab を編集。そうする事で次回起動時よりランレベル3(CUI)で起動できる。

inittabの編集
[root@server1 ~]# vi /etc/inittab
#
# inittab       This file describes how the INIT process should set up
#               the system in a certain run-level.
        ・
        ・
id:5:initdefault:
  ↓
id:3:initdefault:  ← ランレベル5をランレベル3に変更。

これで次回起動時よりランレベル3(CUI)で起動できる。
■ X Window System
ランレベル3で起動後、一時的にX Window System を使いたいのであれば、startx コマンドを実行。

[root@server1 ~]# startx

■ ログアウト(GUIでは単に端末が閉じるだけ)

[root@server1 ~]# exit

今後は、一般ユーザログオン後に su – コマンドでスーパーユーザになり、設定変更を行って行く。 Linuxへのログインはスーパーユーザのログインは止める!
■ ホスト名を変更したい場合
インストールのとき設定したホスト名を後で変更したいときは、以下の設定ファイルを変更する事によって、ホスト名を変更する事ができる。 ホスト名の設定ファイルを変更。

[root@server1 ~]# vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=localhost.localdomain
      ↓
HOSTNAME=server1.yokensaka.com ← 独自ドメイン名に変更
GATEWAY=192.168.1.1
OSを再起動します。
[root@server1~]# reboot
ホスト名の表示
[root@server1 ~]# hostname
新ホスト名になっている
localhost.localdomain
      ↓
server1.yokensaka.com ←独自ドメイン名になっている

各サーバを構築してからホスト名を変更すると、各サーバが正常に動作しなくなる。ホスト名を変更したら、各サーバの設定ファイル内のホスト名も新ホスト名に変更する。また、各サーバーの設定ファイルを変更したらそのサーバを再起動する必要がある。(ホスト名の変更→各サーバの設定ファイルの変更→OSの再起動→動作確認)

ウィルス対策(Clam Antivirus)

Clam AntivirusはGPLライセンスに従って利用することができるオープンソースのソフトウェアであり、LinuxやBSD、Mac OS Xなど各種UNIX系のシステムで動作するアンチウイルスソフト。しかしyumではなかなか最新版にアップデートされないのでwgetでソースからインストールすることにする。
■古いバージョンのclamavがある場合はアンインストールしておく

アンインストール用にバックアップしてあったモジュールを展開。
[root@server1 ~]# tar zxvf clamav-0.97.1_self.tar.gz
clamav-0.97.1のディレクトリへ移動
[root@server1 ~]# cd clamav-0.97.1
clamav-0.97.1のアンインストール
[root@server1 clamav-0.97.1]# make uninstall
clamav-0.97.1のファイルとディレクトリーを削除
[root@server1 clamav-0.97.1]# cd
[root@server1 ~]# rm -f clamav-0.97.1_self.tar.gz
[root@server1 ~]# rm -rf clamav-0.97.1
古いバージョンのディレクトリーの削除
[root@server1 ~]# rm -rf /usr/local/clamav

■ユーザとグループの作成

clamavはインストールする前に
「clamav」という名前のユーザとグループを作成しておく必要がある。
[root@server1 ~]# groupadd clamav
[root@server1 ~]# useradd -g clamav -s /bin/false clamav

■最新版のClamAVをダウンロード (最新版は公式サイトで確認できる

[root@server11 ~]# wget http://downloads.sourceforge.net/clamav/clamav-0.97.2.tar.gz
--2011-09-19 18:44:56--  http://downloads.sourceforge.net/clamav/clamav-0.97.2.tar.gz
downloads.sourceforge.net をDNSに問いあわせています... 216.34.181.59
downloads.sourceforge.net|216.34.181.59|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 301 Moved Permanently
場所: http://downloads.sourceforge.net/project/clamav/clamav/0.97.2/clamav-0.97.2.tar.gz [続く]
--2011-09-19 18:44:56--  http://downloads.sourceforge.net/project/clamav/clamav/0.97.2/clamav-0.97.2.tar.gz
downloads.sourceforge.net:80 への接続を再利用します。
HTTP による接続要求を送信しました、応答を待っています... 302 Found
場所: http://jaist.dl.sourceforge.net/project/clamav/clamav/0.97.2/clamav-0.97.2.tar.gz [続く]
--2011-09-19 18:44:57--  http://jaist.dl.sourceforge.net/project/clamav/clamav/0.97.2/clamav-0.97.2.tar.gz
jaist.dl.sourceforge.net をDNSに問いあわせています... 2001:200:141:feed::feed, 150.65.7.130
jaist.dl.sourceforge.net|2001:200:141:feed::feed|:80 に接続しています... 失敗しました: 接続を拒否されました.
jaist.dl.sourceforge.net|150.65.7.130|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 44703953 (43M) [application/x-gzip]
`clamav-0.97.2.tar.gz' に保存中
100%[=========================================================================>] 44,703,953  15.3M/s 時間 2.8s
2011-09-19 18:45:00 (15.3 MB/s) - `clamav-0.97.2.tar.gz' へ保存完了 [44703953/44703953]
clamav-0.97.2を展開
[root@server1 ~]# tar zxvf clamav-0.97.2.tar.gz
ダウンロードしたファイルを削除
[root@server1 ~]# rm -f clamav-0.97.2.tar.gz
clamav-0.97.2ディレクトリーへ移動
[root@server1 ~]# cd clamav-0.97.2
Makefileを自動作成するためのツール「configure」を実行。
[root@server1 clamav-0.97.2]# ./configure --prefix=/usr/local/clamav
途中、zlib-devel パッケージがインストールされてないようでエラーになった。
zlib-devel パッケージをインストール。
[root@server1 ~]# yum install zlib-devel
再度「configure」を実行。
[root@server1 clamav-0.97.2]# ./configure --prefix=/usr/local/clamav
makeを実行し、clamavをインストール
[root@server1 clamav-0.97.2]# make
[root@server1 clamav-0.97.2]# make install

■アンインストール用にバックアップしておく

[root@server1 clamav-0.97.2]# cd
[root@server1 ~]# tar cvf clamav-0.97.2_self.tar ./clamav-0.97.2
[root@server1 ~]# gzip clamav-0.97.2_self.tar

■設定ファイル変更
インストールが正常に完了したらまず、二つの設定ファイル
/usr/local/clamav/etc/freshclam.conf
/usr/local/clamav/etc/clamd.conf
をエディタで開きExampleと書かれた行をコメントアウトし、保存。

[root@server1 ~]# vi /usr/local/clamav/etc/freshclam.conf
# Comment or remove the line below.
Example
↓
#Example
[root@server1 ~]# vi /usr/local/clamav/etc/clamd.conf
# Comment or remove the line below.
Example
↓
#Example

■「freshclam」を使用してVirusDBをアップデート

[root@server1 ~]# /usr/local/clamav/bin/freshclam
ClamAV update process started at Mon Sep 19 19:24:31 2011
main.cvd is up to date (version: 53, sigs: 846214, f-level: 53, builder: sven)
WARNING: getfile: daily-13357.cdiff not found on remote server (IP: 219.106.242.51)
WARNING: getpatch: Can't download daily-13357.cdiff from database.clamav.net
WARNING: getfile: daily-13357.cdiff not found on remote server (IP: 203.212.42.128)
WARNING: getpatch: Can't download daily-13357.cdiff from database.clamav.net
WARNING: getfile: daily-13357.cdiff not found on remote server (IP: 27.96.54.66)
WARNING: getpatch: Can't download daily-13357.cdiff from database.clamav.net
WARNING: Incremental update failed, trying to download daily.cvd
Downloading daily.cvd [100%]
daily.cvd updated (version: 13636, sigs: 193983, f-level: 60, builder: arnaud)
Downloading bytecode.cvd [100%]
bytecode.cvd updated (version: 144, sigs: 41, f-level: 60, builder: edwin)
Database updated (1040238 signatures) from database.clamav.net (IP: 120.29.176.126)

■ウィルススキャン確認(/etc/passwdをスキャンしてみる)

[root@server1 ~]# /usr/local/clamav/bin/clamscan --infected --remove --recursive /etc/passwd
----------- SCAN SUMMARY -----------
Known viruses: 1038820
Engine version: 0.97.2
Scanned directories: 0
Scanned files: 1
Infected files: 0
Data scanned: 0.00 MB
Data read: 0.00 MB (ratio 0.00:1)
Time: 8.935 sec (0 m 8 s)
version: 0.97.2でスキャンされてることを確認

■Clam AntiVirusの定期自動実行設定

毎日自動的にウィルス定義ファイル最新化して、全てのファイルのウィルススキャンを行うスプリクトの作成
[root@server1 ~]# vi clamscan
#!/bin/bash
PATH=/usr/bin:/bin
# excludelist
excludelist=/root/clamscan.exclude
if [ -s $excludelist ]; then
for i in `cat $excludelist`
do
if [ $(echo "$i"|grep \/$) ]; then
i=`echo $i|sed -e 's/^\([^ ]*\)\/$/\1/p' -e d`
excludeopt="${excludeopt} --exclude-dir=$i"
else
excludeopt="${excludeopt} --exclude=$i"
fi
done
fi
CLAMSCANTMP=`mktemp`
/usr/local/clamav/bin/freshclam > /dev/null
/usr/local/clamav/bin/clamscan --recursive --remove ${excludeopt} / > $CLAMSCANTMP 2>&1
[ ! -z "$(grep FOUND$ $CLAMSCANTMP)" ] && \
grep FOUND$ $CLAMSCANTMP | mail -s "Virus Found in `hostname`" root
rm -f $CLAMSCANTMP

■Clam AntiVirus定期自動実行スクリプトに実行権限付加

[root@server1 ~]# chmod 700 clamscan

■cron編集

[root@server1 ~]# crontab -e
00 03 * * * /root/clamscan ← 追加(毎日3:00にClam AntiVirusの定期自動実行)

■スキャン除外設定

/backupディレクトリと/sysディレクトリをスキャン対象外にするように設定
[root@server1 ~]# echo "/backup/" >> clamscan.exclude
[root@server1 ~]# echo "/sys/" >> clamscan.exclude

サーバの自動起動&自動終了

■ 予備機の定期自動起動
予備機には毎日決まった時間に自動起動させ、メインサーバからwww/htmlとMySQLのデータのバックアップを取得し、ある時間に自動でシャットダウンさせるように設定する。
追記:
現在はメインサーバー、予備サーバーとも ProLiant MicroServer になり、24時間稼働しているため、この設定は行なっていない。
BIOS設定

Power Management Setup
Resume by Alarm  Enabledを選択した場合に自動起動できる日時を設定できる。
Date(of Month) Alarm  0に設定すると毎日実行される。
Time(hh:mm:ss) Alarm   07:0:0に設定 毎日7:00に自動起動するようにする

■ システムクロック(date)とハードウェアクロック(clock)の確認
ntpdコマンドでシステムクロックを正確に合わせていれば、CentOSはシステム終了時にハードウェアクロックの時刻をシステムクロックの時刻に合わせるようになっている。

[root@server1 ~]# date;clock
2011年  9月 23日 金曜日 09:15:52 JST
2011年09月23日 09時15分52秒  -0.000268 秒

■ 予備機の定期自動シャットダウン設定

シャットダウンスクリプトの作成
[root@server1 ~]# vi shutdown.sh
#!/bin/sh
/sbin/shutdown -h now
作成したリストアスクリプトへ実行権限付加
[root@server1 ~]# chmod 700 shutdown.sh
シャットダウンスクリプトを実行してみる
[root@server1 ~]# ./shutdown.sh

■ 各スクリプト及びシャットダウン定期自動実行設定
シャットダウンすることが確認できたら起動しなおしてシャットダウン定期自動実行の設定をする。
※毎日7時に自動起動、各スクリプトを実行して12時に自動でシャットダウンするようにする。

[root@server1 ~]# crontab -e
30 07 * * * /root/tripwire.sh
00 08 * * * /root/chkrootkit.sh
30 08 * * * /root/clamscan
00 09 * * * /root/yum_upgrade.sh
30 09 * * * /root/restore.sh
00 10 * * * /root/mysqldump.sh ; /root/backup.sh
00 12 * * * /root/shutdown.sh ← 毎日12:00に自動でシャットダウンするスクリプトを実行

■ メインサーバの自動起動
停電があったとき、電源が回復したらサーバの電源が自動でオンになるように設定する。
BIOS設定 

AC Back Function  AC電力が失われたときから電力を回復した後のシステムの状態を決定する。
Soft-Off  AC電力を回復した時点でも、システムはオフになっている。(既定値)
Full-On  AC電力を回復した時点で、システムはオンになる。(今回はこの設定にした)
Memory  AC電力が回復した時点で、システムは電力を失う直前の状態に戻る。

BIOS設定画面に入るための代表的なキー
[F1]、[F2]、[F8]、[F10]、[F12]、[Delete]、[Tab]

バッファ・オーパーフロー対策(Exec-Shield)

Exec-Shieldを有効にして、バッファ・オーパーフロー攻撃をブロックする。
■ Exec-Shieldの設定

現状確認
[root@server1 ~]# cat /proc/sys/kernel/exec-shield
1
Exec-Shieldを有効にする。
[root@server1 ~]# echo 2 > /proc/sys/kernel/exec-shield
再確認
[root@server1 ~]# cat /proc/sys/kernel/exec-shield
2
起動時に、有効にする。
[root@server1 ~]# vi /etc/rc.d/rc.local
echo 2 > /proc/sys/kernel/exec-shield    ← 追加

■ Exec-Shieldの動作確認

[root@server1 ~]# wget http://pubs.research.avayalabs.com/src/libsafe-2.0-16.i386.rpm
--2011-09-19 19:34:28--  http://pubs.research.avayalabs.com/src/libsafe-2.0-16.i386.rpm
pubs.research.avayalabs.com をDNSに問いあわせています... 198.152.240.29
pubs.research.avayalabs.com|198.152.240.29|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 374371 (366K) [text/plain]
`libsafe-2.0-16.i386.rpm' に保存中
100%[=========================================================================>] 374,371     81.8K/s 時間 4.8s
2011-09-19 19:34:33 (76.5 KB/s) - `libsafe-2.0-16.i386.rpm' へ保存完了 [374371/374371]
[root@server1 ~]# rpm -ivh libsafe-2.0-16.i386.rpm  ← libsafeのインストール
準備中...                   ########################################### [100%]
1:libsafe                ########################################### [100%]
Adding libsafe to ld.so.preload for system wide protection
[root@server1 ~]# cp /usr/doc/libsafe-2.0/exploits/t1 ./  ← 攻撃ツールをコピー
[root@server1 ~]# rpm -e libsafe  ← libsafeのアンインストール
Removing libsafe from /etc/ld.so.preload (if exists)
[root@server1 ~]# ./t1  ← 攻撃ツールの実行
This program tries to use strcpy() to overflow the buffer.
If you get a /bin/sh prompt, then the exploit has worked.
Press any key to continue...abc;  ← 適当に入力
セグメンテーション違反です (コアダンプ)  ← バッファオーバーフローがブロックされた
[root@server1 ~]# rm ./t1  ← 攻撃ツールを削除
rm: remove 通常ファイル `./t1'? y  ← yと入力して攻撃ツールを削除