Ubuntu 14.04 Server (Trusty Tahr)のインストールと初期設定・環境構築について。
- このメモの目的と内容
- kvm仮想マシンを各種サーバとして使うための自分用メモ
- インストールにはubuntu-14.04.1-server-i386.isoを使用
- インストールするマシンとして、256MBのメモリと10GBのストレージ(virtio)2台が接続されている仮想マシンを使用
仮想マシンの作成
仮想マシンマネージャーで仮想マシンを作成する。 作成後、すぐにはインストールを開始せず、VNCサーバーのキーマップをjaにしておく。
インストール手順
ブート画面
- F2 Language: [English]を選択
- F3 Keymap: [Japan]を選択
- F4 Modes: [Install a minimal virtual machine]を選択
- [Install Ubuntu Server]を選択
Select a language
- Language: [English - English]を選択
Select your location
- Country, territory or area: [other] → [Asia] → [Japan]の順に選択
Configure locales
- Country to base default locale settings on: [United States - en_US.UTF-8]を選択
Loading additional components
- (インストールが終わるまで待つ)
Configure the network
(DHCPを無効にしている場合)
- Network configuration method: [Configure network manually]を選択
- (以下ダイアログに従ってIPアドレス等を入力)
Set up users and passwords
- (ダイアログに従ってユーザ名・パスワード等を入力)
- Encrypt your home directory?: <No>を選択
Configure the clock
- Is this time zone correct?: 適切なタイムゾーンが選択されていることを確認して<Yes>を選択
Partition disks
- Partitioning method: [Manual]を選択
- 以下のように設定
Virtual disk1 (vda) 10GB #1 primary 512M ext4 /boot Mount option: defaults #5 logical (max) ext4 /usr Mount option: defaults Virtual disk2 (vdb) 10GB #1 primary (max) ext4 / Mount option: defaults #5 logical 512M swap - - [Finish partitioning and write changes to disk]を選択
- Write the changes to disks?: パーティションのフォーマットを確認して<Yes>を選択
Installing the base system
- (インストールが終わるまで待つ)
Configure the package manager
- HTTP proxy information: 空欄のままもしくは必要に応じて適切なURLを入力
Configuring tasksel
- How do you want to manage upgrades on this system? [No automatic updates]を選択
Software selection
- (サーバの構成に従って適当なものを選択)
- [Mail server]の設定例
- Postfix Configuration
- General type of mail configuration: [Satellite system]を選択
- System mail name: 適当な名前を入力
- SMTP relay host: メール配送に用いるSMTPサーバのホスト名を入力
- Postfix Configuration
Install the GRUB boot loader on a hard disk
- Install the GRUB boot loader to the master boot record?: <Yes>を選択
Finish the installation
- Installation complete: ディスクを取り出して<Continue>を選択
- 再起動
インストール後の設定
デフォルトのエディタ変更
好みのエディタに変更する。
$ sudo update-alternatives --config editor There are 4 choices for the alternative editor (providing /usr/bin/editor). Selection Path Priority Status ------------------------------------------------------------ * 0 /bin/nano 40 auto mode 1 /bin/ed -100 manual mode 2 /bin/nano 40 manual mode 3 /usr/bin/vim.basic 30 manual mode 4 /usr/bin/vim.tiny 10 manual mode Press enter to keep the current choice[*], or type selection number: 3 update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in manual mode.
~/.vimrc
以下の内容で作成。
set encoding=utf-8
set fileencoding=utf-8
set fileencodings=utf-8,utf-16,japan
set backspace=2
set tabstop=2
set shiftwidth=4
set expandtab
highlight tabs ctermbg=green guibg=green
set list
set number
set ruler
set smartindent
~/.bashrc
以下の内容を追記。
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
PS1='\!> \[\033[0;33m\]\u\[\033[m\]@\[\033[0;36m\]\h\[\033[m\] [\[\033[0;32m\]\t\[\033[m\] \w]\$ '
case $TERM in
kterm|xterm|cygwin)
TITLEBAR="\u@\h:\w"
PS1="\[\033]0;${TITLEBAR}\007\]${PS1}"
;;
esac
/etc/aliases
root宛のメール送信先を変更する。 /etc/aliases
を作成して以下の内容を記入。
root: root@example.com
変更を反映する。
sudo newaliases
/etc/hosts
必要に応じてホストを追記。
グループ・ユーザー
必要に応じて追加。
groupadd -g 999 xxxxxx
adduser --uid 999 --gid 999 --disabled-login --disabled-password --no-create-home xxxxxx
IPv6の無効化
以下の内容を追記。
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
sudo sysctl -p
で変更を反映する。
シリアルコンソール
virsh console
で接続できるようにするための設定。 /etc/init/ttyS0.conf
を作成して以下の内容を書き込む。
start on stopped rc RUNLEVEL=[2345]
stop on runlevel [!2345]
respawn
exec /sbin/getty -L 115200 ttyS0 xterm
この設定は再起動後に有効になる。
仮想マシンの設定にデバイスの種類がpty
のシリアルデバイスが存在していることを確認し、virsh console 仮想マシン名
で接続する。
$ virsh console server1 ドメイン server1 に接続しました エスケープ文字は ^] です Ubuntu 14.04.1 LTS server1 ttyS0 server1 login:
GRUB 2
設定を書き換え、起動時にGRUB 2のメニュー画面が表示されるようにする。 GRUB_HIDDEN_TIMEOUT
をコメントアウトし、GRUB_TIMEOUT
にタイムアウト秒数を指定する。
#GRUB_HIDDEN_TIMEOUT=0
GRUB_TIMEOUT=5
virsh console
でシリアルコンソールに接続できるようにしたい場合は、以下の内容も追記する。
GRUB_CMDLINE_LINUX="console=tty0 console=ttyS0,115200n8"
GRUB_TERMINAL=serial
GRUB_SERIAL_COMMAND="serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"
変更内容を反映する。
$ sudo update-grub2 Generating grub.cfg ... Found linux image: /boot/vmlinuz-2.6.32-21-generic-pae Found initrd image: /boot/initrd.img-2.6.32-21-generic-pae Found memtest86+ image: /memtest86+.bin done
パッケージのインストールと設定
パッケージを最新の状態に更新
sudo apt-get update
sudo apt-get dist-upgrade
不要なパッケージの削除
sudo apt-get purge \
acpid \
apport \
at \
bc \
byobu \
landscape-common \
nano \
ntfs-3g libntfs-3g.* libntfs.* \
ppp pppconfig pppoeconf \
screen \
tmux \
wpasupplicant \
wireless-tools wireless-regdb \
whoopsie
NFS
sudo apt-get install nfs-common
マウントポイントを作成。
sudo mkdir -p /srv/nfs
/etc/fstabを編集後、マウントする。
sudo vi /etc/fstab
sudo mount -a
マウントされたかどうか確認する。
cat /etc/mtab
ls -l /srv/nfs
NTP
sudo apt-get install ntp
適当なNTPサーバを設定し、再起動。
sudo vi /etc/ntp.conf
sudo service ntp restart
Logwatch
sudo apt-get install logwatch
テストした結果をroot宛にメールで送信する。
sudo logwatch --mailto root
Munin
sudo apt-get install munin-node
/etc/munin/pluginsから必要なプラグインだけを残す。
sudo unlink xxx
sudo ln -s /usr/share/munin/plugins/xxx
データを収集するマシンのみからのアクセスを許可するように変更する。
allow ^192\.168\.0\.10$
munin-nodeを再起動。
sudo service munin-node restart
データを収集するマシンからtelnetで接続できるか試す。
$ telnet server1 4949 Trying 192.168.0.20... Connected to server1. Escape character is '^]'. # munin node at server1.example.com nodes server1.example.com . quit Connection closed by foreign host.
lighttpd plugin
lighttpdのステータスを収集するプラグイン(rtucker/munin-lighttpd)を導入する。
まずlighttpdの設定ファイルを編集し、mod_statusによるステータス参照が行えるようにする。
server.modules = ("mod_status", ...)
$HTTP["remoteip"] == "192.168.0.0/24" {
status.status-url = "/status"
}
lighttpdを再起動したのち、http://hostname/status?auto
にアクセスしてステータスが次のような平文で表示されればOK。
Total Accesses: xxxxx Total kBytes: xxxxx Uptime: xxxxx BusyServers: 1 IdleServers: 127 Scoreboard: h_______________________________________________
次に、プラグインのスクリプトをダウンロードして/usr/share/munin/plugins/
に配置する。
cd /usr/share/munin/plugins/
wget https://raw.githubusercontent.com/rtucker/munin-lighttpd/master/lighttpd_
chmod a+x ./lighttpd_
プラグインのディレクトリにリンクを作成する。 (収集する必要のある値のみで可)
ln -s /usr/share/munin/plugins/lighttpd_ /etc/munin/plugins/lighttpd_accesses
ln -s /usr/share/munin/plugins/lighttpd_ /etc/munin/plugins/lighttpd_busyservers
ln -s /usr/share/munin/plugins/lighttpd_ /etc/munin/plugins/lighttpd_idleservers
ln -s /usr/share/munin/plugins/lighttpd_ /etc/munin/plugins/lighttpd_kbytes
ln -s /usr/share/munin/plugins/lighttpd_ /etc/munin/plugins/lighttpd_uptime
プラグインの設定ファイルを開き、先に設定したlighttpdのステータスページのURLを記述する。
[lighttpd_*]
env.statusurlenv.statusurl http://hostname/status?auto
再起動して設定を反映する。
service munin-node restart
収集側のサーバーからmunin-nodeにログインし、データが収集できるか確認する。 コマンドfetch lighttpd_uptime
を入力してlighttpdのuptimeが返されればOK。
$ telnet hostname 4949
Trying 192.168.0.999...
Connected to hostname.
Escape character is '^]'.
# munin node at hostname
fetch lighttpd_uptime
data.value xxxxxx
.
quit
Connection closed by foreign host.
AWStats
sudo apt-get install awstats
/etc/awstats
以下の設定ファイルを編集後、ログが読み込まれデータが正しく収集されるかテストする。
sudo -u www-data /usr/share/doc/awstats/examples/awstats_updateall.pl now -awstatsprog=/usr/lib/cgi-bin/awstats.pl -confdir="/etc/awstats/"
ls /var/lib/awstats
必要に応じてcron(/etc/cron.d/awstats
)の設定およびHTTPサーバの設定を更新したのち、http://サーバ名/awstats/awstats.pl?config=設定ファイル名
を開いて収集されたデータが表示されるか確かめる。
Postfix+Dovecot(IMAP)+maildrop
sudo apt-get install postfix dovecot-core dovecot-imapd maildrop
メールの格納先をMaildirにする
home_mailbox = Maildir/
mail_location = maildir:~/Maildir
maildropでフィルタ・振り分ける
まずホームディレクトリに.forward
を作成し、ユーザー宛のすべてのメールをmaildropコマンドに転送させる。
"|/usr/bin/maildrop"
次に.mailfilter
を作成し、フィルタ・振り分けの設定を記述する。
# maildrop.logに動作ログを出力する
logfile "$HOME/maildrop.log"
# Subjectヘッダに'Cron'という文字列が含まれている場合は
# メールボックス'INBOX/Cron'に振り分ける
if (/^Subject:.*Cron/:h)
{
to "$HOME/Maildir/.INBOX.Cron/"
}
# それ以外のメールは'INBOX'に振り分ける