dshimizu/blog

アルファ版

さくらのVPSにインストールしたFreeBSD 9.1-RELEASEの初期設定

はじめに

以前本ブログで記載した"こちら"の記事の内容を元に、さくらのVPSのISOイメージインストールを利用して、FreeBSD 9.1-RELEASEのISOイメージからFreeBSD 9.1でのZFS FileSystem環境を構築したので、その際に行ったOSの初期設定についてまとめました。

設定項目

初期設定として、以下を実施しています。

  1. 日本語キーボードの設定
  2. rootパスワードの設定
  3. ホスト名、ネットワークの設定
  4. ユーザ/グループの作成
  5. SSHの設定
  6. FW(ipfw)の設定
  7. DNSゾルバの設定
  8. NTPの設定
  9. メール(Sendmail)の設定
  10. Ports Collectionの設定

環境

構築環境は以下のとおりです。

プラットフォーム さくらのVPS 2G
OS FreeBSD 9.1-RELEASE amd64

FreeBSD 9.1-RELEASEのOS初期設定

最初はIPアドレスも設定されていないので、さくらのVPSコントロールパネルからVNCコンソールを開き、作業を行います。

日本語キーボードの設定

keymapがデフォルト(american phonetic layout)になっているようなので、利用しているキーボードに合わせて設定します。
ここでは日本語キーボードレイアウトのものに変更します。


# kbdcontrol -l jp.106.kbd < /dev/console

システム起動時にも有効となるように/etc/rc.confへ記載します。


# vi /etc/rc.conf
### KeyBoard Setting ###
keymap="jp.106"
rootパスワード設定

初期状態ではrootのパスワードが設定されていないので、パスワードを設定します。


# passwd
Changing local password for root
New Password:
Retype New Password:
ホスト名、ネットワークの設定

/etc/rc.confにOSの基本情報を追記します。 環境に合わせてホスト名、ネットワークといった各種情報を入力します。


# vi /etc/rc.conf
### Hostname Setting ###
hostname="xxxxxxxxx.sakura.ne.jp"

### Network Setting ###
ifconfig_em0="inet xxx.xxx.xxx.xxx netmask xxx.xxx.xxx.xxx"
defaultrouter="xxx.xxx.xxx.xxx"
NICバイスIPアドレスを割り当てたので、ネットワークを起動します。

# /etc/netstart
ユーザ/グループ作成

まず、"pw groupadd"で必要なグループを作成します。 ここでは例としてhogeというグループをグループID:10001として作成しています。


# pw groupadd hoge -g 10001

続いてadduserコマンドでインタラクティブ(対話的)に新規ユーザを作成します。
ここでは例としてhogeというユーザを作成し、hogeグループとwheelグループに所属させます。
特に記載しない場合はデフォルト値([]内の値)が設定されます。


# adduser
### 作成ユーザの設定 ###
### ユーザ名入力
Username: hoge
### フルネーム入力
Full name: hoge
### ユーザIDを指定
Uid (Leave empty for default): 10001
### ログイングループを指定
Login group [hoge]:
### 他のログイングループを指定(ここでwheelグループを指定)
Login group is users. Invite j into other groups? []: wheel
### ログインクラスを指定(デフォルトのままでよいのでそのままEnterキー押下)
Login class [default]:
### 利用したいシェルを指定
Shell (sh csh tcsh bash rbash nologin) [sh]: tcsh
### ホームディレクトリの指定
Home directory [/home/hoge]:
### パスワード認証の可否の設定
Use password-based authentication? [yes]:
### 空パスワードの設定の可否の設定
Use an empty password? (yes/no) [no]:
### ランダムパスワードでの設定の可否の設定
Use a random password? (yes/no) [no]:
### パスワードの設定
Enter password:
Enter password again:
### アカウントをロックさせるかどうかの設定
Lock out the account after creation? [no]:
### 設定内容の確認
Username : hoge
Password : *****
Full Name : hoge
Uid : 10001
Class :
Groups : hoge wheel
Home : /home/hoge
Shell : /bin/tcsh
Locked : no
### 上記内容で作成してよいかの確認(問題なければyes)
OK? (yes/no): yes
adduser: INFO: Successfully added (hoge) to the user database.
### 別のユーザを作成するかの確認
Add another user? (yes/no): no
Goodbye!
SSHの設定

SSHの設定を行います。
※セキュリティ的な問題は多少ありますが、キー認証のみ許可する設定にするのでteratermなどでログインして作業するなどが良いかも知れません。
/etc/ssh/sshd_configの設定を編集します。 sshのkey認証のみの接続を許可するように設定しています。


# vi /etc/ssh/sshd_config
### sshd_configの設定 ###
### SSHのポート番号変更(ここでは例として10022と設定)
Port 10022

### Rootログインの禁止
PermitRootLogin No

### 公開鍵認証を許可
RSAAuthentication yes
PubkeyAuthentication yes
### 鍵ファイルのパス指定
AuthorizedKeysFile .ssh/authorized_keys

### パスワードログインを禁止
PasswordAuthentication no
### 空のパスワード利用を禁止
PermitEmptyPasswords no
### チャレンジレスポンス認証を禁止
ChallengeResponseAuthentication no

### sshでのログインを許可するユーザを指定
AllowUsers hoge

先ほど作成したユーザにスイッチし、SSH公開鍵を作成します。


# su - hoge

RSA暗号方式での鍵ペアを作成します。


# ssh-keygen -t rsa
### RSA暗号鍵の生成 ###
Generating public/private rsa key pair.
### 必要であれば、キーファイルの設置先とファイル名を指定します。
Enter file in which to save the key (/home/hoge/.ssh/id_rsa):
### パスフレーズを2度入力します。
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/hoge/.ssh/id_rsa.
Your public key has been saved in /home/hoge/.ssh/id_rsa.
The key fingerprint is:
**:**:**:**:**:**:**:**:**:**:**:**:**:**:**:** hoge@xxxxxxxx.sakura.ne.jp
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| |
| |
| |
| |
| |
| |
| |
+-----------------+

$HOME/.ssh/ディレクトリに秘密鍵(id_rsa)と公開鍵(id_rsa.pub)が作成されます。 /etc/ssh/sshd_configの設定により、公開鍵はユーザのホームディレクトリ配下の「.ssh/authorized_keys」が参照されますので、公開鍵のファイル名を変更します。


$ cd .ssh
$ cat id_rsa.pub >> $HOME/.ssh/authorized_keys
$ chmod 600 authorized_keys

また、パーミッションもそのユーザのみしか読み書きできないようにしておく必要があります。


$ chmod 600 authorized_keys

秘密鍵については、SSHクライアントを利用する端末にコピーするなどして取得しておきます。
rc.confにSSH起動の設定を追記します。


# vi /etc/rc.conf
### SSH Setting ###
sshd_enable="YES"

sshdを起動します。


# service sshd start
Starting sshd
FW(ipfw)の設定

/etc/rc.firewallという名前のファイルがデフォルトで存在しますが、新規にipfw用のコンフィグファイルを作成します。ここではipfw.confというファイル名で作成していますが、名前は何でも構いません。
一番最後のルール999だと/var/log/secureに多量のログが出力されますので、それが困る場合は削除しても構いません。ルール65535に"deny ip from any to any"が自動で設定されるためです。


# vi /etc/ipfw.conf
### ipfw.confの設定 ###
#! /bin/sh

FW="ipfw -q"
RULE="ipfw -q add"


### Delete Rule
$FW -f flush

# Permit loopback address
$RULE 10 allow all from any to any via lo0

# Drop Needless Packet
$RULE 20 deny all from any to 127.0.0.0/8
$RULE 21 deny all from 127.0.0.0/8 to any
$RULE 22 deny all from any to 172.16.0.0/12
$RULE 23 deny all from 172.16.0.0/12 to any
$RULE 24 deny all from any to 192.168.0.0/16
$RULE 25 deny all from 192.168.0.0/16 to any

$RULE 30 deny tcp from any to any frag

# statefull
$RULE 50 check-state
$RULE 60 allow tcp from any to any established
$RULE 70 allow all from any to any out keep-state
$RULE 80 deny icmp from any to any

# Permit DNS
$RULE 201 allow udp from me to any 53 out keep-state

# Permit NTP
$RULE 202 allow udp from me to 210.188.224.14 123 in keep-state
$RULE 203 allow udp from me to any 123 out keep-state

# Permit SSH(prot 10022)
$RULE 301 allow tcp from any to me 10022 in
$RULE 302 allow tcp from me to any 22 out

# Permit Mail
$RULE 303 allow tcp from me to any 25 in
$RULE 304 allow tcp from me to any 25 out

# Permit http/https
$RULE 305 allow tcp from any to me 80 in
$RULE 306 allow tcp from me to any 80 out
$RULE 307 allow tcp from any to me 443 in
$RULE 308 allow tcp from me to any 443 out

# deny and log everything
$RULE 999 deny log all from any to any

rc.confへipfw起動の設定を追記します。


# vi /etc/rc.conf
### Firewall Setting ###
firewall_enable="YES"
firewall_logging="YES"
firewall_script="/etc/ipfw.conf"

ipfwを起動します。


# /etc/rc.d/ipfw start
Starting ipfw.
DNSゾルバの設定

ゾルバの設定を行います。 ※利用しているリージョン等によって内容が異なると思いますので、環境に合わせて設定します。


# vi /etc/resolv.conf
### resolv.confの設定 ###
nameserver xxx.xxx.xxx.xxx
nameserver xxx.xxx.xxx.xxx
serach sakura.ne.jp
NTPの設定

まず、タイムゾーンの設定を行います。


# tzsetup

NTPの設定を行います。
参照先NTPサーバはさくらインターネット社のNTPサーバとし、外部からこのVPSサーバのntpdサービスの参照を無効化します。


# vi /etc/ntp.conf
### ntp.confの設定 ###
restrict default ignore
restrict -6 default ignore
restrict 127.0.0.1
restrict ntp1.sakura.ad.jp kod nomodify notrap nopeer noquery
server ntp1.sakura.ad.jp

rc.confへNTP起動の設定を導入します。


# vi /etc/rc.conf
### ntpd Setting ###
ntpd_enable="YES"
ntpd_config="/etc/ntp.conf"

最初は狂いが大きいと思いますので、ntpd起動前に、まず、カーネルクロックを強制的に修正します。


# ntpdate -b -v ntp1.sakura.ad.jp

続いて、ハードウェアクロックをカーネルクロックに合わせます。


# hwclock --systohc

そして、ntpdを起動します。


# /etc/rc.d/ntpd start
Starting ntpd.

しばらくすると時刻同期が開始されます。


# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*ntp1.sakura.ad. 133.243.238.163 2 u 12 64 377 8.565 0.058 0.114
メール(Sendmail)の設定

最近ではPostfixが人気なのでそちらを使っても良いですが、デフォルトでインストールされているSendmailを利用しました。 細かいオプションはいくつかありますが、ここでは外部からのSMTP接続は全て拒否し、内部からのSMTP接続、メール送信を可能となるよう、下記を/etc/rc.confへ記載します。


# vi /etc/rc.conf
### Sendmail Setting ###
sendmail_enable="NO"
sendmail_submit_enable="YES"
sendmail_outbound_enable="YES"
sendmail_msp_queue_enable="YES"

Sendmailを起動させます。


# /etc/rc.d/sendmail onestart
Starting sendmail.
Ports Collectionの取得

最新のPorts Collectionを取得します。


# cd /tmp
# fetch ftp://ftp.jp.freebsd.org/pub/FreeBSD/releases/amd64/9.1-RELEASE/ports.txz

/usr配下へ展開します。


# tar xzpf ./ports.txz -C /usr/

これでportsが利用可能になりましたので、必要なアプリケーションをインストールしてカスタマイズしてきます。
なお、portsnapによるportsを自動アップデートするような設定は特に必要なかったのでやっていません。

おわりに

以上で、さくらのVPS上でのFreeBSD 9.1-RELEASEの初期設定は完了です。
ipfwなどもう少し見直すべき点もありそうですが、これからいろいろ調整していければと思います。