Ubuntu Server 15.04メモ(1)

そろそろ16.04LTSが出てくるころ合いなので15に触れて慣れておくためのメモ。

■systemd関連
サービス状態の一覧表示
# systemctl list-unit-files -t service

サービス設定変更の有効化
# systemctl daemon-reload

■Journalログの配置ディレクトリ作成
# mkdir /var/log/journal
# reboot
⇒パーミッションは再起動したら/var/run/

■rsyslogのSevierity表示
・/etc/rsyslog.confに以下を追加

$template precise,”%timegenerated%,%HOSTNAME%,%syslogpriority-text%,%syslogfacility-text%,%syslogtag%,%msg%\n”

・/etc/rsyslog.d/50-default.confに以下を追加
*.*;auth,authpriv.none -/var/log/messages;precise

⇒差分を見るために/var/log/messagesに出力させるようにした。
(Defaultの出力は/var/log/syslogに出力)

# systemctl restart rsyslog.service
⇒rsyslogを再起動して設定反映

以下のようになった。
Jan 27 21:45:39,ubuntu15,info,daemon,systemd[491]:, Reached target Timers.
Jan 27 21:45:40,ubuntu15,info,daemon,systemd[491]:, Starting Timers.
Jan 27 21:45:40,ubuntu15,info,daemon,systemd[491]:, Reached target Paths.
Jan 27 21:45:40,ubuntu15,info,daemon,systemd[491]:, Starting Paths.
Jan 27 21:45:40,ubuntu15,info,daemon,systemd[491]:, Reached target Sockets.
Jan 27 21:45:40,ubuntu15,info,daemon,systemd[491]:, Starting Sockets.
Jan 27 21:45:40,ubuntu15,info,daemon,systemd[491]:, Reached target Basic System.

■IPv6無効
# vi /etc/sysctl.conf
最終行に追記

net.ipv6.conf.all.disable_ipv6 = 1

# sysctl -p
net.ipv6.conf.all.disable_ipv6 = 1
上記出力を確認

# ip a
⇒v6出力が消えたことを確認

■ホスト名の確認
# hostnamectl status
Static hostname: ubuntu15
Icon name: computer-vm
Chassis: vm
Machine ID: 0ff620127564dda29f0f975c56a1f0e7
Boot ID: 0aceb06669814ecd8f03e3b5dc412f08
Virtualization: microsoft
Operating System: Ubuntu 15.04
Kernel: Linux 3.19.0-15-generic
Architecture: x86-64
root@ubuntu15:~#

念のため、他の方法でもやってみたが出力は同じ。
# uname -n
ubuntu15

# hostname
ubuntu15

■ホスト名の変更
# hostnamectl set-hostname ubuntu5

# hostname
ubuntu5

# more /etc/hostname
ubuntu5

# hostnamectl
Static hostname: ubuntu5
Icon name: computer-vm
Chassis: vm
Machine ID: 0ff620127564dda29f0f975c56a1f0e7
Boot ID: 0aceb06669814ecd8f03e3b5dc412f08
Virtualization: microsoft
Operating System: Ubuntu 15.04
Kernel: Linux 3.19.0-15-generic
Architecture: x86-64

設定ファイルをいじるよりはコマンドで変更するほうがよい思われる。

■ルーティングテーブル確認
# ip route list
default via 192.168.1.1 dev eth0
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.22

■ルーティングテーブル追加

①レガシーな書き方
/etc/network/interfacesへ以下のように追記

# add static route
post-up ip route add CIDR via GATEWAY

例としては以下のようなイメージ
post-up ip route add 192.168.1.21/32 via 192.168.1.21

②systemdでやる方法
# systemctl enable systemd-networkd.service
# systemctl enable systemd-resolved.service

/etc/systemd/network以下に設定ファイルを作成
以下は例

[Match]
Name=eth0

[Network]
Address=192.168.1.30/24
Gateway=192.168.1.1
DNS=192.168.1.1

[Route]
Gateway=192.168.1.21
Destination=192.168.1.21/32

※/etc/network/interfacesを記載しているとそちらが優先されるみたいなのでその場合はファイルをリネームするなどしておく。

# systemctl restart systemd-networkd.service
# systemctl restart systemd-resolved.service
上記で設定の有効化(またはreboot)

DNSが動かなくなるので以下の対応を行う

/etc/systemd/resolved.confの編集
# more /etc/systemd/resolved.conf
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See resolved.conf(5) for details

[Resolve]
DNS=192.168.1.1
FallbackDNS=8.8.8.8
#LLMNR=yes

DNSと必要に応じてFallbackDNSを設定する。
ただそのままだと名前が引けない。straceかけたら/etc/resolv.confをOpenしているようだが、設定はなにも書かれていない。
そのまま/etc/resolv.confを編集すればよいみたいに見えるがファイルに

# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND — YOUR CHANGES WILL BE OVERWRITTEN

と書かれているため、このままではNG。
resolv.confはシンボリックリンクになっており、実体は/run/resolvconf/resolv.confのようだ。
ただしresolved.confに書いた設定は/run/systemd/resolve/resolv.confには反映されている。
とりあえずなぜ?というのは分からないがあれこれ検索した結果

# ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf

で解決した。
■NTP設定
# systemctl stop systemd-timesyncd.service
⇒sytemdのtimesyncdの動作を停止

# systemctl status systemd-timesyncd.service
⇒停止を確認

# systemctl disable systemd-timesyncd.service
Removed symlink /etc/systemd/system/sysinit.target.wants/systemd-timesyncd.service
⇒サービスの自動起動を停止

# systemctl enable ntp
Synchronizing state for ntp.service with sysvinit using update-rc.d…
Executing /usr/sbin/update-rc.d ntp defaults
Executing /usr/sbin/update-rc.d ntp enable

ここまでしたが、ntpqで反応しない。syslogをチェックしたら

Jan 27 16:43:25 ubuntu15 kernel: [ 825.481566] audit: type=1400 audit(1453880605.985:20): apparmor=”DENIED” operation=”open” profile=”/usr/sbin/ntpd” name=”/run/systemd/resolve/resolv.conf” pid=682 comm=”ntpd” requested_mask=”r” denied_mask=”r” fsuid=0 ouid=102
Jan 27 16:43:27 ubuntu15 kernel: [ 827.483745] audit: type=1400 audit(1453880607.989:21): apparmor=”DENIED” operation=”open” profile=”/usr/sbin/ntpd” name=”/run/systemd/resolve/resolv.conf” pid=684 comm=”ntpd” requested_mask=”r” denied_mask=”r” fsuid=0 ouid=102
Jan 27 16:43:45 ubuntu15 ntpd_intres[646]: parent died before we finished, exiting
Jan 27 16:44:30 ubuntu15 kernel: [ 889.516262] audit: type=1400 audit(1453880670.021:22): apparmor=”DENIED” operation=”open” profile=”/usr/sbin/ntpd” name=”/run/systemd/resolve/resolv.conf” pid=684 comm=”ntpd” requested_mask=”r” denied_mask=”r” fsuid=0 ouid=102
Jan 27 16:44:57 ubuntu15 systemd[1]: Starting Cleanup of Temporary Directories…
Jan 27 16:44:57 ubuntu15 systemd-tmpfiles[689]: [/usr/lib/tmpfiles.d/var.conf:14] Duplicate line for path “/var/log”, ignoring.
Jan 27 16:44:57 ubuntu15 systemd[1]: Started Cleanup of Temporary Directories.
Jan 27 16:45:18 ubuntu15 systemd[1]: Started Session 2 of user jun.
Jan 27 16:45:18 ubuntu15 systemd[1]: Starting Session 2 of user jun.
Jan 27 16:46:32 ubuntu15 kernel: [ 1011.580735] audit: type=1400 audit(1453880792.085:23): apparmor=”DENIED” operation=”open” profile=”/usr/sbin/ntpd” name=”/run/systemd/resolve/resolv.conf” pid=684 comm=”ntpd” requested_mask=”r” denied_mask=”r” fsuid=0 ouid=102
root@ubuntu15:/var/log#

こんな感じでapparmorが邪魔をしているっぽい。

下記サイトを参考にして対応した。
http://ubulog.blogspot.jp/2009/06/ubuntu-apparmor.html

# apt-get install apparmor-profiles apparmor-utils
⇒上記でツール類をインストールする。

# aa-complain /usr/sbin/ntpd
Setting /usr/sbin/ntpd to complain mode.
⇒上記でComplain modeにする。

# apparmor_status
⇒ntpdがcomplain modeであることを確認

# systemctl restart ntp.service

# ntpq -p
設定したserverが表示されていることを確認

■SNMP設定
# apt install snmpd

# vi /etc/snmp/snmpd.conf
⇒設定は14LTSと同じ

# systemctl restart snmpd.service
⇒設定の反映を確認する
■参考サイト
・Linux女子部 systemd徹底入門
http://www.slideshare.net/enakai/linux-27872553
・ubuntuで AppArmor を使ってセキュアな環境を構築する
http://ubulog.blogspot.jp/2009/06/ubuntu-apparmor.html
・rsyslogの基本設定とログのプライオリティ情報の出力
http://kanjuku-tomato.blogspot.jp/2015/05/rsyslog.html