2023年11月25日土曜日

OpenLDAPをソースからインストールする

OpenLDAPはRHEL 7までは、リポジトリからyumコマンドを使ってインストールすることができたが、RHEL 8になってからOpenLDAPは含まれないようになってしまい、RHELのサポート範囲外のソフトウェアとなってしまった。

RHEL 8以降は、OpenLDAPからRed Hat Directory Server (RHDS) を利用することができる。しかし、RHDSは389 Directory Serverがベースとなっており、OpenLDAPや構築方法や運用方法が異なってしまう。

以上より、RHEL 8以降においてもOpenLDAPが利用するため、本記事では、RHEL 9にOpenLDAPをソースからインストールする手順を記載する。

環境

今回は以下のOSに対してインストールを行った。OpenLDAPは本記事作成時のLTSリリースの最新版である2.5.16をダウンロードした。

  • OS : RHEL 9
  • OpenLDAP : 2.5.16

OpenLDAPのドメイン等の情報は以下とする。

設定項目 設定値
ドメイン dc=example,dc=com
RootDN cn=Manager,dc=example,dc=com
OU ou=group,dc=example,dc=com
グループ cn=ldapgrp,ou=group,dc=example,dc=com
ユーザ1 uid=user01,ou=group,dc=example,dc=com
ユーザ2 uid=user01,ou=group,dc=example,dc=com

OpenLDAPインストール

1. OpenLDAPのソースを入手

OpenLDAPのソースは以下からダウンロードできる。今回はLTSリリースである2.5.16をダウンロードした。

ファイル名はopenldap-2.5.16.tgzとなる。以下の通り解凍して、解凍後のディレクトリ内に移動しておく。

# tar zxf openldap-2.5.16.tgz
# cd openldap-2.5.16

2. 前提パッケージインストール

ビルド時にエラーや警告が表示されないように、以下前提パッケージのインストールを行う。

# dnf install gcc make cyrus-sasl-devel openssl-devel systemd-devel -y

3. Makefile作成

./configureを実行してMakefileを作成する。オプションは以下の通り設定する。なお、設定可能なオプションは他にも多数あり、./configure --helpにて確認することができる。

オプション 説明
--with-tls SSL/TLSを有効化。
--with-cyrus-sasl SASLによる認証を有効化。
--enable-crypt パスワードの暗号化を有効化。
--prefix=/usr インストールディレクトリを変更。デフォルトは/usr/local/となる。
--sysconfdir=/etc 設定ファイル配置ディレクトリを変更。デフォルトは/usr/local/etcとなる。

./configureの実行結果を以下に記載する。

# ./configure --with-tls --with-cyrus-sasl --enable-crypt --with-systemd --prefix=/usr --sysconfdir=/etc
Configuring OpenLDAP 2.5.16-Release ...
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking configure arguments... done
checking for cc... cc
checking for ar... ar
checking for strip... strip
checking whether make sets $(MAKE)... yes

~(中略)~

config.status: executing libtool commands
config.status: executing default commands
Making servers/slapd/backends.c
    Add config ...
    Add ldif ...
    Add monitor ...
    Add mdb ...
    Add relay ...
Making servers/slapd/overlays/statover.c
    Add syncprov ...
Please run "make depend" to build dependencies

Makefileが作成されていることを確認しておく。

# ls -l Makefile
-rw-r--r--. 1 root root 9256 10月 14 07:16 Makefile

4. 依存関係チェック

作成したMakfefileで依存関係をチェックする。特にエラーがなければ問題ない。

# make depend
Making depend in /root/openldap-2.5.16
  Entering subdirectory include
make[1]: ディレクトリ '/root/openldap-2.5.16/include' に入ります
Making ldap_config.h
make[1]: ディレクトリ '/root/openldap-2.5.16/include' から出ます

~(中略)~

  Entering subdirectory man8
make[3]: ディレクトリ '/root/openldap-2.5.16/doc/man/man8' に入ります
make[3]: 'depend' に対して行うべき事はありません.
make[3]: ディレクトリ '/root/openldap-2.5.16/doc/man/man8' から出ます

make[2]: ディレクトリ '/root/openldap-2.5.16/doc/man' から出ます

make[1]: ディレクトリ '/root/openldap-2.5.16/doc' から出ます

5. ビルド実行

ビルド実行する。1~2分程度で完了する。

#make
Making all in /root/openldap-2.5.16
  Entering subdirectory include
make[1]: ディレクトリ '/root/openldap-2.5.16/include' に入ります
make[1]: 'all' に対して行うべき事はありません.
make[1]: ディレクトリ '/root/openldap-2.5.16/include' から出ます

~(中略)~

make[3]: ディレクトリ '/root/openldap-2.5.16/doc/man/man8' から出ます

make[2]: ディレクトリ '/root/openldap-2.5.16/doc/man' から出ます

make[1]: ディレクトリ '/root/openldap-2.5.16/doc' から出ます

6. インストール

ビルド後、インストールを実行する。なお、インストール前にmake testコマンドでインストール前のテストを行うことができるので、安全にインストールを進めたい場合は実行すること。make testは数分~数十分時間を要する。

# make install
Making all in /root/openldap-2.5.16
  Entering subdirectory include
make[1]: ディレクトリ '/root/openldap-2.5.16/include' に入ります
make[1]: 'all' に対して行うべき事はありません.
make[1]: ディレクトリ '/root/openldap-2.5.16/include' から出ます

~(中略)~

installing slappasswd.8 in /usr/share/man/man8
installing slapschema.8 in /usr/share/man/man8
installing slaptest.8 in /usr/share/man/man8
make[3]: ディレクトリ '/root/openldap-2.5.16/doc/man/man8' から出ます

make[2]: ディレクトリ '/root/openldap-2.5.16/doc/man' から出ます

make[1]: ディレクトリ '/root/openldap-2.5.16/doc' から出ます

以上で、OpenLDAPのソースからのインストールは完了となる。続けて初期設定を行う。

OpenLDAP初期設定

1. 初期設定

設定ファイルをバックアップしておく。

# cd ~
# cp -R /etc/openldap ~/openldap.org
# ls -ld  ~/openldap.org
drwxr-xr-x. 4 root root 163 10月 14 07:37 /root/openldap.org

OpenLDAPの管理用ユーザーのパスワードをslappasswdを使って生成する。

# slappasswd
New password:
Re-enter new password:
{SSHA}7B1EpVxClcKpBwnlqG73YtB7IrUXhTLM

設定ファイルを以下の通り追記・変更を行う。includeではOpenLDAPで必要となるスキーマを追加している。olcSuffixolcRootDNはOpenLDAPのドメインに変更を行う。olcRootPWは先ほど生成したパスワードを指定する。

# vi /etc/openldap/slapd.ldif

~(中略)~

include: file:///etc/openldap/schema/core.ldif
include: file:///etc/openldap/schema/cosine.ldif  #<-★追記
include: file:///etc/openldap/schema/inetorgperson.ldif  #<-★追記
include: file:///etc/openldap/schema/nis.ldif  #<-★追記

~(中略)~

dn: olcDatabase=mdb,cn=config
objectClass: olcDatabaseConfig
objectClass: olcMdbConfig
olcDatabase: mdb
olcDbMaxSize: 1073741824
olcSuffix: dc=example,dc=com  #<-★変更
olcRootDN: cn=Manager,dc=example,dc=com  #<-★変更
# Cleartext passwords, especially for the rootdn, should
# be avoided.  See slappasswd(8) and slapd-config(5) for details.
# Use of strong authentication encouraged.
olcRootPW: {SSHA}7B1EpVxClcKpBwnlqG73YtB7IrUXhTLM  #<-★変更

~(以下略)~

設定ファイルの前後でdiffした結果は以下の通り。

# diff /etc/openldap/slapd.ldif{,.default}
44,46d43
< include: file:///etc/openldap/schema/cosine.ldif
< include: file:///etc/openldap/schema/inetorgperson.ldif
< include: file:///etc/openldap/schema/nis.ldif
87,88c84,85
< olcSuffix: dc=example,dc=com
< olcRootDN: cn=Manager,dc=example,dc=com
---
> olcSuffix: dc=my-domain,dc=com
> olcRootDN: cn=Manager,dc=my-domain,dc=com
92c89
< olcRootPW: {SSHA}7B1EpVxClcKpBwnlqG73YtB7IrUXhTLM
---
> olcRootPW: secret

OpenLDAPのデータ格納に必要となるディレクトリを作成する。

# mkdir /usr/var/openldap-data
# mkdir /etc/openldap/slapd.d

slapaddコマンドで設定反映を行う。

# /usr/sbin/slapadd -v -n 0 -F /etc/openldap/slapd.d -l /etc/openldap/slapd.ldif
added: "cn=config" (00000001)
added: "cn=schema,cn=config" (00000001)
added: "cn={0}core,cn=schema,cn=config" (00000001)
added: "cn={1}cosine,cn=schema,cn=config" (00000001)
added: "cn={2}inetorgperson,cn=schema,cn=config" (00000001)
added: "cn={3}nis,cn=schema,cn=config" (00000001)
added: "olcDatabase={-1}frontend,cn=config" (00000001)
added: "olcDatabase={1}mdb,cn=config" (00000001)
added: "olcDatabase={2}monitor,cn=config" (00000001)
Closing DB...

2. OpenLDAP起動確認

まずは手動でOpenLDAPを起動し、正常に起動することを確認する。

# /usr/libexec/slapd -F /etc/openldap/slapd.d -h 'ldapi:/// ldap:/// ldaps:///'

起動後に、ldapsearchで問題なく情報の参照ができるか確認する。

# ldapsearch -x -b '' -s base '(objectclass=*)' namingContexts

# extended LDIF
#
# LDAPv3
# base <> with scope baseObject
# filter: (objectclass=*)
# requesting: namingContexts
#

#
dn:
namingContexts: dc=example,dc=com ←★設定したドメインが表示されることを確認

# search result
search: 2
result: 0 Success

# numResponses: 2
# numEntries: 1

確認後、一度OpenLDAPを停止しておく。

# ps -ef | grep slapd | grep -v grep
root       69944       1  0 08:29 ?        00:00:00 /usr/libexec/slapd -F /etc/openldap/slapd.d -h ldapi:/// ldap:/// ldaps:///
# pkill slapd
# ps -ef | grep slapd | grep -v grep
#

3. systemdへ登録

RHELの場合、systemdでサービスの起動・停止を制御できた方が便利となる。以下の通り、systemdにOpenLDAPのサービスを登録するための設定ファイルを作成する。

# cat << 'EOF' > /etc/systemd/system/slapd.service
[Unit]
Description=OpenLDAP server
After=syslog.target network.target

[Service]
Type=forking
User=root
Group=root
PIDFile=/usr/var/run/slapd.pid
ExecStart=/usr/libexec/slapd -F /etc/openldap/slapd.d -h 'ldapi:/// ldap:/// ldaps:///'
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill -TERM $MAINPID

[Install]
WantedBy=multi-user.target
EOF

上記ファイルを作成後、systemctlコマンドで起動停止が問題なくできることを確認しておく。

# systemctl daemon-reload
# systemctl start slapd
# systemctl enable slapd
# systemctl status slapd
● slapd.service - OpenLDAP server
     Loaded: loaded (/etc/systemd/system/slapd.service; enabled; preset: disabled)
     Active: active (running) since Sat 2023-10-14 08:43:52 JST; 8s ago
   Main PID: 70011 (slapd)
      Tasks: 2 (limit: 23166)
     Memory: 5.0M
        CPU: 8ms
     CGroup: /system.slice/slapd.service
             mq70011 /usr/libexec/slapd -F /etc/openldap/slapd.d -h "ldapi:/// ldap:/// ldaps:///"

10月 14 08:43:52 t1191rhel systemd[1]: Starting OpenLDAP server...
10月 14 08:43:52 t1191rhel slapd[70010]: @(#) $OpenLDAP: slapd 2.5.16 (Oct 14 2023 07:29:29) $
                                                  root@t1191rhel:/root/openldap-2.5.16/servers/slapd
10月 14 08:43:52 t1191rhel slapd[70011]: slapd starting
10月 14 08:43:52 t1191rhel systemd[1]: Started OpenLDAP server.

# systemctl stop slapd
# systemctl status slapd
○ slapd.service - OpenLDAP server
     Loaded: loaded (/etc/systemd/system/slapd.service; enabled; preset: disabled)
     Active: inactive (dead) since Sat 2023-10-14 18:28:23 JST; 1s ago
   Duration: 9h 44min 31.510s
    Process: 70408 ExecStop=/bin/kill -TERM $MAINPID (code=exited, status=0/SUCCESS)
   Main PID: 70011 (code=exited, status=0/SUCCESS)
        CPU: 14ms

10月 14 08:44:24 t1191rhel slapd[70011]: conn=1001 op=3 ADD dn="cn=ldapgrp,ou=group,dc=example,dc=com"
10月 14 08:44:24 t1191rhel slapd[70011]: conn=1001 op=3 RESULT tag=105 err=0 qtime=0.000010 etime=0.000472 text=
10月 14 08:44:24 t1191rhel slapd[70011]: conn=1001 op=4 UNBIND
10月 14 08:44:24 t1191rhel slapd[70011]: conn=1001 fd=13 closed
10月 14 18:28:23 t1191rhel systemd[1]: Stopping OpenLDAP server...
10月 14 18:28:23 t1191rhel slapd[70011]: daemon: shutdown requested and initiated.
10月 14 18:28:23 t1191rhel slapd[70011]: slapd shutdown: waiting for 0 operations/tasks to finish
10月 14 18:28:23 t1191rhel slapd[70011]: slapd stopped.
10月 14 18:28:23 t1191rhel systemd[1]: slapd.service: Deactivated successfully.
10月 14 18:28:23 t1191rhel systemd[1]: Stopped OpenLDAP server.

起動・停止が問題なくできることが確認できたら、OpenLDAPは起動させておこう。

# systemctl start slapd

4. ユーザー登録確認

ここからは実際にOpenLDAPにグループやユーザーの登録を行ってみよう。

まずは、ドメイン、OU、グループの登録を行うためのLDIFファイルを作成する。

# cat << EOF > base.ldif
dn: dc=example,dc=com
objectClass: top
objectClass: dcObject
objectClass: organization
dc: example
o: Example Inc.

dn: ou=group,dc=example,dc=com
objectClass: organizationalUnit
ou: Group

dn: cn=ldapgrp,ou=group,dc=example,dc=com
objectClass: posixGroup
gidNumber: 10000
cn: ldapgrp
EOF

ldapaddコマンドで登録を行う。パスワードは初期設定時に生成したパスワードとなる。

# ldapadd -x -D "cn=Manager,dc=example,dc=com" -W -f base.ldif
Enter LDAP Password:
adding new entry "dc=example,dc=com"

adding new entry "ou=group,dc=example,dc=com"

adding new entry "cn=ldapgrp,ou=group,dc=example,dc=com"

次にユーザー登録を行う。以下の通りLDIFファイルを作成する。

# cat << EOF > user.ldif
dn: uid=user01,ou=group,dc=example,dc=com
uid: user01
cn: Hoge Hoge
sn: user01
objectClass: posixAccount
objectClass: inetOrgPerson
userPassword: {SSHA}7B1EpVxClcKpBwnlqG73YtB7IrUXhTLM
loginShell: /bin/bash
uidNumber: 10001
gidNumber: 10000
homeDirectory: /home/user01

dn: uid=user02,ou=group,dc=example,dc=com
uid: user02
cn: Hoge Hoge
sn: user02
objectClass: posixAccount
objectClass: inetOrgPerson
userPassword: {SSHA}7B1EpVxClcKpBwnlqG73YtB7IrUXhTLM
loginShell: /bin/bash
uidNumber: 10002
gidNumber: 10000
homeDirectory: /home/user02
EOF

ldapaddコマンドで登録を行う。

# ldapadd -x -D "cn=Manager,dc=example,dc=com" -W -f user.ldif
Enter LDAP Password:
adding new entry "uid=user01,ou=group,dc=example,dc=com"

adding new entry "uid=user02,ou=group,dc=example,dc=com"

最後に、ldapsearchコマンドで登録状況を確認して完了となる。

# ldapsearch -LLL -Y EXTERNAL -H ldapi:/// -b 'dc=example,dc=com'
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
dn: dc=example,dc=com
objectClass: top
objectClass: dcObject
objectClass: organization
dc: example
o: Example Inc.

dn: ou=group,dc=example,dc=com
objectClass: organizationalUnit
ou: Group

dn: cn=ldapgrp,ou=group,dc=example,dc=com
objectClass: posixGroup
gidNumber: 10000
cn: ldapgrp

dn: uid=user01,ou=group,dc=example,dc=com
uid: user01
cn: Hoge Hoge
sn: user01
objectClass: posixAccount
objectClass: inetOrgPerson
userPassword:: e1NTSEF9WFhY772eKOecgeeVpSnvvZ5YWFg=
loginShell: /bin/bash
uidNumber: 10001
gidNumber: 10000
homeDirectory: /home/user01

dn: uid=user02,ou=group,dc=example,dc=com
uid: user02
cn: Hoge Hoge
sn: user02
objectClass: posixAccount
objectClass: inetOrgPerson
userPassword:: e1NTSEF9WFhY772eKOecgeeVpSnvvZ5YWFg=
loginShell: /bin/bash
uidNumber: 10002
gidNumber: 10000
homeDirectory: /home/user02

以上で、RHEL 9にOpenLDAPをソースからインストールする手順は完了となる。

2023年11月18日土曜日

Grafanaインストール&Zabbixの監視データを可視化する手順

Grafanaは、ログやデータなどをグラフなどに可視化する、データ可視化OSSとなる。Grafanaが可視化するデータを「データソース」と呼ぶ。データソースは、各種クラウド上に保存されている情報やDBなどに対応しており、それ以外にもプラグインを追加することでデータソースを追加することが可能となる。

本記事ではGrafanaの入門編として、GrafanaをインストールしてZabbixプラグインを追加し、Zabbixの監視データを可視化する手順を記載する。

環境

Grafanaをインストールした環境は以下の通り。

  • OS : AlmaLinux 9.2
  • Grafana : v10.0.3
  • Zabbix : 6.0.17

Grafanaインストール手順

1, Grafanaのパッケージのリポジトリ設定

Grafanaはdnfを使ってインストールすることができる。

まずは、GrafanaのリポジトリのGPGキーをダウンロードしインポートする。wgetは環境によってはインストールされていないことから、curlでダウンロードした。

# curl -LO https://rpm.grafana.com/gpg.key
# rpm --import gpg.key

以下の通りリポジトリの設定ファイルを作成する。

# cat << EOF > /etc/yum.repos.d/grafana.repo
[grafana]
name=grafana
baseurl=https://rpm.grafana.com
repo_gpgcheck=1
enabled=1
exclude=*beta*
gpgcheck=1
gpgkey=https://rpm.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
EOF

2. Grafanaインストール

dnfを使ってGrafanaをインストールする。

# dnf install grafana
AlmaLinux 9 - AppStream                                                                                             1.4 MB/s | 8.8 MB     00:06
AlmaLinux 9 - BaseOS                                                                                                2.4 MB/s | 2.9 MB     00:01
AlmaLinux 9 - Extras                                                                                                 19 kB/s |  17 kB     00:00
grafana                                                                                                              60 kB/s | 2.4 kB     00:00
GPG 鍵 0x2CF3C0C6 をインポート中:
 Userid     : "Grafana Labs <engineering@grafana.com>"
 Fingerprint: 0E22 EB88 E39E 1227 7A77 60AE 9E43 9B10 2CF3 C0C6
 From       : https://rpm.grafana.com/gpg.key
これでよろしいですか? [y/N]: y ★yを入力してエンター
grafana                                                                                                              10 MB/s |  37 MB     00:03
依存関係が解決しました。
====================================================================================================================================================
 パッケージ                                            アーキテクチャー        バージョン                          リポジトリー               サイズ
====================================================================================================================================================
インストール:
 grafana                                               x86_64                  10.0.3-1                            grafana                     79 M
依存関係のインストール:
 dejavu-sans-fonts                                     noarch                  2.37-18.el9                         baseos                     1.3 M
 fontconfig                                            x86_64                  2.14.0-2.el9_1                      appstream                  274 k
 fonts-filesystem                                      noarch                  1:2.0.5-7.el9.1                     baseos                     9.0 k
 freetype                                              x86_64                  2.10.4-9.el9                        baseos                     387 k
 graphite2                                             x86_64                  1.3.14-9.el9                        baseos                      94 k
 harfbuzz                                              x86_64                  2.7.4-8.el9                         baseos                     624 k
 langpacks-core-font-en                                noarch                  3.0-16.el9                          appstream                  9.4 k
 libpng                                                x86_64                  2:1.6.37-12.el9                     baseos                     116 k
 urw-base35-bookman-fonts                              noarch                  20200910-6.el9                      appstream                  846 k
 urw-base35-c059-fonts                                 noarch                  20200910-6.el9                      appstream                  873 k
 urw-base35-d050000l-fonts                             noarch                  20200910-6.el9                      appstream                   76 k
 urw-base35-fonts                                      noarch                  20200910-6.el9                      appstream                  9.8 k
 urw-base35-fonts-common                               noarch                  20200910-6.el9                      appstream                   21 k
 urw-base35-gothic-fonts                               noarch                  20200910-6.el9                      appstream                  642 k
 urw-base35-nimbus-mono-ps-fonts                       noarch                  20200910-6.el9                      appstream                  795 k
 urw-base35-nimbus-roman-fonts                         noarch                  20200910-6.el9                      appstream                  855 k
 urw-base35-nimbus-sans-fonts                          noarch                  20200910-6.el9                      appstream                  1.3 M
 urw-base35-p052-fonts                                 noarch                  20200910-6.el9                      appstream                  973 k
 urw-base35-standard-symbols-ps-fonts                  noarch                  20200910-6.el9                      appstream                   41 k
 urw-base35-z003-fonts                                 noarch                  20200910-6.el9                      appstream                  275 k
 xml-common                                            noarch                  0.6.3-58.el9                        appstream                   31 k

トランザクションの概要
====================================================================================================================================================
インストール  22 パッケージ

ダウンロードサイズの合計: 89 M
インストール後のサイズ: 312 M
これでよろしいですか? [y/N]: y ★yを入力してエンター

~(中略)~

インストール済み:
  dejavu-sans-fonts-2.37-18.el9.noarch                                  fontconfig-2.14.0-2.el9_1.x86_64
  fonts-filesystem-1:2.0.5-7.el9.1.noarch                               freetype-2.10.4-9.el9.x86_64
  grafana-10.0.3-1.x86_64                                               graphite2-1.3.14-9.el9.x86_64
  harfbuzz-2.7.4-8.el9.x86_64                                           langpacks-core-font-en-3.0-16.el9.noarch
  libpng-2:1.6.37-12.el9.x86_64                                         urw-base35-bookman-fonts-20200910-6.el9.noarch
  urw-base35-c059-fonts-20200910-6.el9.noarch                           urw-base35-d050000l-fonts-20200910-6.el9.noarch
  urw-base35-fonts-20200910-6.el9.noarch                                urw-base35-fonts-common-20200910-6.el9.noarch
  urw-base35-gothic-fonts-20200910-6.el9.noarch                         urw-base35-nimbus-mono-ps-fonts-20200910-6.el9.noarch
  urw-base35-nimbus-roman-fonts-20200910-6.el9.noarch                   urw-base35-nimbus-sans-fonts-20200910-6.el9.noarch
  urw-base35-p052-fonts-20200910-6.el9.noarch                           urw-base35-standard-symbols-ps-fonts-20200910-6.el9.noarch
  urw-base35-z003-fonts-20200910-6.el9.noarch                           xml-common-0.6.3-58.el9.noarch

完了しました!

3. Grafana起動

インポートしそのままGrafanaは起動することができるが、もしプロキシを経由してインターネット接続を行う環境の場合は、以下の通り/etc/sysconfig/grafana-serverにプロキシ関連の環境変数を記載しておく。

# vi /etc/sysconfig/grafana-server

# ↓最下行に以下を追加(記載例)
HTTP_PROXY=http://192.168.33.23:8080
HTTPS_PROXY=http://192.168.33.23:8080
NO_PROXY=localhost,127.0.0.1,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16

Grafanaを起動する。

# systemctl start grafana-server
# systemctl enable grafana-server

4. Grafanaにログイン

Grafanaは、デフォルトではHTTPの3000番ポートで待ち受ける。以下のURLをブラウザに入力するとログイン画面が表示されるはずだ。

http://[IPアドレス]:3000

ログインユーザー名、パスワードは以下を入力する。初回ログイン時はパスワード変更を求められるので、任意のパスワードに変更する。

  • ユーザー名 : admin
  • パスワード : admin

Zabbixのデータを可視化する

1. Zabbixプラグインをインストール

Zabbixの情報を可視化する場合、GrafanaにZabbixのプラグインをインストールする必要がある。プラグインのインストールはgrafana-cliコマンドで行う。

# grafana-cli plugins install alexanderzobnin-zabbix-app
? Downloaded and extracted alexanderzobnin-zabbix-app v4.3.1 zip successfully to /var/lib/grafana/plugins/alexanderzobnin-zabbix-app

Please restart Grafana after installing or removing plugins. Refer to Grafana documentation for instructions if necessary.

プラグインインストール後、Grafanaを一度再起動しておく。

# systemctl restart grafana-server

2. Zabbixプラグインを有効化

GrafanaのGUIにて[Administration]>[Plugins]に遷移し、[zabbix]で検索するとプラグインが表示されるので、表示されたZabbixのプラグインを選択する。

右上の[Enable]ボタンを選択する。

4. データソースの設定

GrafanaのGUIにて[Administration]>[Data sources]に遷移し、[Add data source]ボタンを選択する。

データソース選択画面で[zabbix]で検索すると、1件表示されるはずなので、それを選択する。

データソース設定画面では、最低限以下を設定する。

設定項目 設定値
Name Zabbix
URL http://[ZabbixサーバのIPアドレス]/zabbix/api_jsonrpc.php
Username Zabbixサーバのアクセス権限があるユーザー名
Password 上記ユーザー名のパスワード

ページ下部にある[Save & test]ボタンを選択し、エラー表示されずに接続先のZabbixのバージョンが表示されれば成功となる。

5. ダッシュボードを追加

[Dashboards]画面にてダッシュボードを作成する。

例えば以下はZabbixサーバの/領域の使用量をグラフに表示する設定となる。

設定項目 設定値
Data source Zabbix
Query type Metrics
Group Zabbix servers
Host Zabbix server
Item tag filesystem: /
Item /: Used space

以上で、GrafanaをインストールしてZabbixの監視データを可視化する手順は完了となる。

2023年11月11日土曜日

Roundcubeを使ってWebメール環境を構築する

先日、Postfix+DovecotでバーチャルドメインのIMAPメールサーバーを構築する手順を記事にした。

通常メールを確認する際は、Thunderbirdなどのメールクライアントの導入が必要となるが、メールクライアントをダウンロードしてPCにインストールしアカウントの設定を行うといった作業は、何度も実施すると結構な負荷となる。

そこで今回は、Webサーバ上で動作するメールクライアント「Roundcube」を使って、Webメール環境を構築してみることにした。

環境

Roundcubeの必要とするDBやPHPのシステム要件は、以下を参照すること。

上記を踏まえ、今回Roundcubeをインストールした環境は以下の通りとした。

  • OS : AlmaLinux 8.8
  • DB : MariaDB 10.3.35
  • Roundcube : 1.6.2
  • PHP : 7.4
  • Apache HTTP Server : 2.4.37
  • Postfix : 3.5.8
  • Dovecot : 2.3.16

以下記事の手順にて、Postfix+DovecotによるSMTP/IMAPサーバが構築済みであることが前提となる。

Roundcubeインストール手順

1. 必要パッケージのインストール

Roundcubeは必要となるパッケージが多岐にわたる。順にインストールしていこう。

まずはDBとしてMariaDBをインストールする。

dnf install httpd mariadb-server -y

次にPHPをインストールする。PHPのバージョンは7.3以上であることがシステム要件となることから、今回はPHP 7.4を選定した。また、Roundcubeは公式サイトのファイルではなく、Remiリポジトリに存在するRPMパッケージにてインストールを行う。

dnf module enable php:7.4 -y
dnf install http://rpms.remirepo.net/enterprise/remi-release-8.rpm -y
dnf --enablerepo=remi install roundcubemail -y

PHPにてMariaDBを利用するためのライブラリであるphp-mysqlnd (MySQL Native Driver) をインストールする。

dnf install php-mysqlnd -y

ImageMagicをインストールするために必要なパッケージがインストールする。gccmakeはこの後実行するpeclコマンドで必要となる。

dnf install php-devel php-pear gcc make ImageMagick ImageMagick-devel -y

単純にRPMパッケージをインストールするだけではRoundcubeからImageMagicを認識しないため、さらにpeclコマンドを実行し、ImageMagicのインストールを行う。

# pecl install imagick
WARNING: channel "pecl.php.net" has updated its protocols, use "pecl channel-update pecl.php.net" to update
downloading imagick-3.7.0.tgz ...
Starting to download imagick-3.7.0.tgz (360,138 bytes)
.........................................................................done: 360,138 bytes
33 source files, building
running: phpize
Configuring for:
PHP Api Version:         20190902
Zend Module Api No:      20190902
Zend Extension Api No:   320190902
Please provide the prefix of ImageMagick installation [autodetect] :
 ↑★そのままエンター

~(中略)~

Build process completed successfully
Installing '/usr/lib64/php/modules/imagick.so'
Installing '/usr/include/php/ext/imagick/php_imagick_shared.h'
install ok: channel://pecl.php.net/imagick-3.7.0
configuration option "php_ini" is not set to php.ini location
You should add "extension=imagick.so" to php.ini

ImageMagicのライブラリのパスを記載したPHPの設定ファイルを作成する。

# cat << EOF > /etc/php.d/20-imagick.ini
extension=/usr/lib64/php/modules/imagick.so
EOF

2. MariaDB設定

ここで一度MariaDBを起動する。

systemctl start mariadb
systemctl enable mariadb

MariaDBを起動したのちに、Roundcubeで用いるDB、ユーザ、パスワードを設定する。今回は以下の通り設定した。

  • DB : roundcubemail
  • ユーザ : roundcube
  • パスワード : password
# mysql -uroot
MariaDB [(none)]> CREATE DATABASE roundcubemail CHARACTER SET utf8 COLLATE utf8_general_ci;
MariaDB [(none)]> GRANT ALL PRIVILEGES ON roundcubemail.* TO roundcube@localhost IDENTIFIED BY 'password';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> quit

DBとDBユーザ作成後、初期設定用のSQLを流し込む。

# mysql -u roundcube roundcubemail -p < /usr/share/roundcubemail/SQL/mysql.initial.sql
Enter password: ←★先ほど作成したroundcubeユーザのパスワードを入力
# ←★エラーなくプロンプトが表示されればOK

3. Apache/PHP設定

PHPの設定ファイルであるphp.iniは最低限タイムゾーンの設定を変更しておく。

sed -i "s#;date.timezone =#date.timezone = 'Asia/Tokyo'#g" /etc/php.ini

Apacheの設定ファイルとして/etc/httpd/conf.d/roundcubemail.confがインストール時に作成されるが、そのままではローカルからのアクセスのみ許可されていることから、外部からのアクセスができるよう以下コマンドを行い設定を置換する。

sed -i 's/Require local/Require all granted/g' /etc/httpd/conf.d/roundcubemail.conf

最後にApacheを起動する。

systemctl start httpd
systemctl enable httpdd

4. インストールウィザードにアクセス

問題なくApacheが起動すれば、Roundcubeのインストールウィザード画面にアクセスできるはずだ。ブラウザから以下URLにアクセスしてみよう。

  • http://[Roundcubeのホスト名/IPアドレス]/roundcubemail/installer/

インストールウィザードでは、各種PHPのライブラリのインストール状況やDBの状況が表示される。ここでは原則すべての項目が「OK」になっていることを確認しよう(DBのみMySQLが「OK」となっていれば問題ない)。


5. config作成

インストールウィザードではRoundcubeの各種設定を行うことができる。多数の設定項目が存在するが、最低限以下表の通り設定を行えば問題ない。

設定項目 説明
Database setup > Database password (omit for sqlite) DBのroundcubeユーザのパスワードを入力する。
IMAP Settings > username_domain ここにドメインを記載すると、ログイン時にドメインを省略できる。
SMTP Settings > smtp_host 今回は同じサーバでSMTPサーバが動作していることから、localhost:25を指定する。
Display settings & user prefs > language 日本語環境にするため、ja_JPを指定する。

設定後、画面下部の「CREATE CONFIG」ボタンを選択する。

6. configをアップロード

設定した内容でconfig.inc.phpの設定ファイルが作成され表示される。

表示されたconfigの内容をブラウザ上でコピーし、/etc/roundcubemail/config.inc.phpのファイルを作成して貼り付ける。

# vi /etc/roundcubemail/config.inc.php
<?php

/* Local configuration for Roundcube Webmail */

~(中略)~

// You can connect to any other googie-compliant service by setting 'spellcheck_uri' accordingly.
$config['spellcheck_engine'] = 'enchant';

Roundcubeの設定反映のためApacheを再起動する。

systemctl restart httpd

7. ログイン確認

これでRoundcubeにログインできるはずだ。以下URLにアクセスしログイン画面が表示されることを確認しよう。

DovecotのIMAP認証に用いるものと同じユーザ (ローカルパートのみでOK) とパスワードを用いてログインできれば成功となる。

以上で、Roundcubeを使ってWebメール環境を構築する手順は完了となる。

2023年11月4日土曜日

Postfix+DovecotでバーチャルドメインのIMAPメールサーバーを構築する手順

先日、メールサーバー構築をする際の定番であるPostfixとDovecotを使ってIMAPのメールサーバーを構築した。

本記事ではその際に得られたノウハウをもとに、Postfix+DovecotでバーチャルドメインのIMAPメールサーバーを構築する手順を記載する。

環境

今回Postfix及びDovecotをインストールした環境は以下の通り。

  • OS : AlmaLinux 8.8
  • Postfix : 3.5.8
  • Dovecot : 2.3.16

共通

1. Postfix及びDovecotをインストール

Postfix及びDovecotのパッケージがインストールされていない場合は、dnfでインストールする。

dnf install postfix dovecot -y

2. バーチャルドメイン用のOSユーザ・グループの作成

バーチャルドメインの場合は、OSユーザではなくDovecot独自のユーザにてメールボックスのユーザが管理される。ただし、メールボックスへのメール配送等はOSユーザを用いる必要があることから、vmailという名前のユーザ・グループを作成する。

groupadd -g 10000 vmail
useradd -u 10000 -g vmail vmail

3. メールボックス作成

メールボックスを/var/spool/virtualというディレクトリで作成する。

mkdir /var/spool/virtual
chown -R vmail:vmail /var/spool/virtual/

上記ディレクトリの配下に、各ユーザのメールボックスが以下のディレクトリ構成で作成される。

/var/spool/virtual/[ドメイン名]/[ユーザ名]/Maildir

Postfix構築手順

1. 設定ファイルバックアップ

設定変更の前に設定ファイルをディレクトリ丸ごとバックアップする。

cp -rp /etc/postfix /root/postfix.org

2. main.cf設定

Postfixの主要な設定は/etc/postfix/main.cfに記載する。以下に主要な設定内容を記載する。

設定項目 説明
myhostname メールサーバのホスト名をFQDNで記載する。
mydomain メールサーバのドメインを記載する。今回はバーチャルドメイン環境であるため、管理対象のメールアドレスのドメインと一致しなくても問題ない。
inet_interfaces 外部からSMTPでアクセスできるようにallを指定する。
inet_protocols 今回はIPv4を指定するが、IPv4にだけでなくIPv6も使用する場合はallを指定する。
mynetworks 外部から接続可能なIPアドレスやネットワークを指定する。ここで設定したネットワークは後述するSASL認証も不要でメール送信が可能となるため、必必要以上に設定しないよう注意する。
relayhost 自ドメイン以外のメールをリレーするサーバを指定する。
home_mailbox メールボックス形式をMailbox形式(ユーザ単位で1ファイルでメールを管理)またはMaildir形式(1メールに対して1ファイルで管理)で指定する。通常はMaildir形式を指定すれば問題ない。なお、Maildir形式に場合は最後に/を付け、Maildir/で指定すること。
smtpd_banner メールヘッダー等に表示するメールサーバ情報を指定する。セキュリティの観点から、使用しているバージョン等は記載しない方針とし、ESMTPのみ設定する。
virtual_mailbox_domains バーチャルドメインで管理するドメインを記載したファイルのパスを指定する。今回は、/etc/postfix/vdomainsというファイルで管理する。
virtual_mailbox_base バーチャルドメインで管理するメールボックスのディレクトリを指定する。今回は/var/spool/virtualを指定する。
virtual_mailbox_maps バーチャルドメインで管理するユーザと、ユーザのメールボックスを紐づけするファイルのパスを指定する。今回は、/etc/postfix/vmailboxというファイルで管理する。なお、大規模環境においてはユーザを大量に管理する必要があることから、処理速度を考慮しハッシュ化したものを指定する(ハッシュ化はpostmapコマンドで行う)。
virtual_uid_maps バーチャルドメインでメール配送等の処理に使用するOSユーザを指定する。前述したvmailユーザのUIDを指定する。
virtual_gid_maps バーチャルドメインでメール配送等の処理に使用するOSグループを指定する。前述したvmailグループのGIDを指定する。
smtpd_sasl_auth_enable SASL認証を有効にする。
smtpd_sasl_type SASL認証で使用する認証先を設定する。今回はDovecotで設定するユーザ・パスワードを用いることからDovecotを指定する。なお、SASLの認証設定は、後程Dovecot側でも実施する。
smtpd_sasl_path PostfixとDovecotが同一環境で動作する環境であれば、private/authを指定すれば問題ない。これが、通信に使用するUNIXソケットファイルのパスになる。
smtpd_sasl_security_options SASL認証時のオプションを指定する。noanonymousにて匿名での認証を拒否し、noplaintextにて平文での認証を拒否する。
smtpd_recipient_restrictions メールを配送する際の制限を行う。permit_sasl_authenticatedにてSASL認証済みであれば許可し、permit_mynetworksにてmynetworksで設定したネットワークからの接続であれば許可する。最後に記載されているrejectにて、それ以外のメール配送を拒否する。
disable_vrfy_command セキュリティの観点からVRFYコマンドを禁止するため、yesで設定する。
smtpd_helo_required セキュリティの観点からSMTPコマンド接続時のHELOコマンドを必須にするため、yesで設定する。

実際のmain.cfの記載例を以下に記載する。

/etc/postfix/main.cf変更箇所抜粋

myhostname = mx01.example.com
mydomain = example.com
inet_interfaces = all
inet_protocols = ipv4
mynetworks = 10.0.0.1
relayhost = [10.0.0.1]:25
home_mailbox = Maildir/
smtpd_banner = ESMTP
virtual_mailbox_domains = /etc/postfix/vdomains
virtual_mailbox_base = /var/spool/virtual
virtual_mailbox_maps = hash:/etc/postfix/vmailbox
virtual_uid_maps = static:10000
virtual_gid_maps = static:10000
smtpd_sasl_auth_enable = yes
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_security_options = noanonymous, noplaintext
smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject
disable_vrfy_command = yes
smtpd_helo_required = yes

3. バーチャルドメインの設定ファイルの作成

バーチャルドメインの設定として、以下2つのファイルを作成する。

ファイル 内容
/etc/postfix/vdomains バーチャルドメインで管理するドメインを記載したファイル。メールサーバーで管理するドメインを一覧として記載する。
/etc/postfix/vmailbox バーチャルドメインで管理するユーザと、ユーザのメールボックスを紐づけするファイル。メールアドレスとメール保存先のディレクトリを一覧として記載する。ディレクトリは、[ドメイン名]/[メールアドレスのローカルパート]/Maildir/の形式で記載する。

それぞれのファイルの記載例を以下に記載する。

/etc/postfix/vdomains

example.com
example1.com

/etc/postfix/vmailbox

ex01@example.com      example.com/ex01/Maildir/
ex02@example.com      example.com/ex02/Maildir/
ex11@example1.com     example1.com/ex11/Maildir/

/etc/postfix/vmailboxpostmapコマンドを用いてハッシュ化しておく。

postmap /etc/postfix/vmailbox

4. Postfix設定反映

設定反映前に設定ファイルのチェックを行う。

# postfix check
#  ←★エラーがなければ何も表示しない

問題がなければ、Postfixに設定を反映するため再起動する。

systemctl restart postfix
systemctl enable postfix

Dovecot構築手順

1. 設定ファイルバックアップ

設定変更の前に設定ファイルをディレクトリ丸ごとバックアップする。

cp -rp /etc/dovecot /root/dovecot.org

2. 各種設定を実施

DovecotはPostfix以上に設定ファイルが細分化されている。今回の修正対象ファイルを以下に記載する。

ファイル 内容
/etc/dovecot/dovecot.conf メインの設定ファイル。プロトコルとしてIMAPを指定する(デフォルトではPOP3、IMAPの両方が動作する)。
/etc/dovecot/conf.d/10-auth.conf 認証方式を記載する。今回は平文による認証を無効化したうえで、CRAM-MD5による認証方式を用いる。
/etc/dovecot/conf.d/auth-passwdfile.conf.ext 認証する際のパスワードファイルの指定を行う。パスワード認証情報(passdb)及びユーザ認証情報(userdb)は、後程作成する/etc/dovecot/usersファイルを参照するよう設定する。
/etc/dovecot/conf.d/10-master.conf PostfixがSASL認証で使用する際のUNIXソケットファイルのパスを指定する。

それぞれのファイルの記載例を以下に記載する。

/etc/dovecot/dovecot.conf変更箇所抜粋

protocols = imap lmtp

/etc/dovecot/conf.d/10-auth.conf変更箇所抜粋

disable_plaintext_auth = yes
auth_mechanisms = cram-md5
#!include auth-system.conf.ext
!include auth-passwdfile.conf.ext

/etc/dovecot/conf.d/auth-passwdfile.conf.ext変更箇所抜粋

passdb {
  driver = passwd-file
  args = scheme=CRAM-MD5 username_format=%u /etc/dovecot/users
}
userdb {
  driver = passwd-file
  args = username_format=%u /etc/dovecot/users
  default_fields = uid=vmail gid=vmail home=/var/spool/virtual/%d/%n/Maildir
}

/etc/dovecot/conf.d/10-master.conf変更箇所抜粋

service auth {
  unix_listener /var/spool/postfix/private/auth {
    mode = 0666
    user = postfix
    group = postfix
  }
  ~(略)~
}

3. ユーザ・パスワードファイルを作成

Dovecotのユーザ・パスワードファイルを作成する。パスワード情報は平文ではなくCRAM-MD5で暗号化されたものを記載する。CRAM-MD5の暗号化文字列の作成は以下コマンドで行う。

doveadm pw -s CRAM-MD5 -p [パスワード文字列]

上記で作成したパスワード文字列を用いて、以下書式でユーザ・パスワードファイルを作成する。行末のコロン2つ(::)は必要となるので省略しないこと。

[ユーザ名]:[暗号化パスワード文字列]::

ユーザ・パスワードファイルの記載例を以下に記載する。

/etc/dovecot/users

test01@example.com:{CRAM-MD5}dcbe8064d829ee98ad16817611150a123455fe1c9dfd79f5395be892f162bfd3::
test02@example.com:{CRAM-MD5}0a9a6905dfdefaf79e95b5a63274464123456166a2f27e593150e37ef2db952::
test11@example1.com:{CRAM-MD5}b71384fd047350e89123456a80ee9959dec89ff9766d75d45ab923792e44ea16::

4. 設定を反映

設定反映前に設定ファイルのチェックを行う。以下コマンドをすると各種設定値が羅列されるが、その際にエラーが表示されていなければ、設定値としては問題ない。

doveconf -n

Dovecotに設定を反映するため再起動する。

systemctl restart dovecot
systemctl enable dovecot

動作確認

メールクライアントを使って確認することが手っ取り早い。

Windows環境であれば、Thunderbird Portable Editionがインストール不要で使え、複数アカウント登録も簡単にできるのでお勧めとなる(ただし、容量は120MB程度あるので注意)。

今回はThunderbird Portable Editionを用いた確認手順を記載する。

1. Thunderbirdのアカウントの設定

Thunderbirdを開き、「設定」→「アカウント設定」を開き、「アカウント操作」から「メールアカウントを追加」を選択する。

でアカウント追加を行う。その際に受信サーバー(IMAP)と送信サーバー(SMTP)の設定を以下の通り行う。

受信サーバー

設定項目 設定値 説明
プロトコル IMAP -
ホスト名 構築したサーバのIPアドレス -
ポート番号 143 STARTTLSであれば143、SSL/TLSであれば993を選択する。
接続の保護 STARTTLS Dovecotの/etc/dovecot/conf.d/10-ssl.confにてssl = requiredの設定がされていることから、STARTTLSまたはSSL/TLSのどちらかを選択する。ここまでの設定で明示的に設定はしていないが、DovecotはデフォルトでIMAPSの通信が可能な構成となっている。
認証方式 暗号化されたパスワード認証 CRAM-MD5で設定されているため、「暗号化されたパスワード認証」を指定する。
ユーザー名 Dovecotで設定したユーザ名を指定 /etc/dovecot/usersに記載したユーザ名を指定する。

送信サーバー

設定項目 設定値 説明
ホスト名 構築したサーバのIPアドレス -
ポート番号 25 -
接続の保護 なし SSL/TLSの設定はしていないため、「なし」を指定する。
認証方式 暗号化されたパスワード認証 CRAM-MD5で設定されているため、「暗号化されたパスワード認証」を指定する。
ユーザー名 Dovecotで設定したユーザ名を指定 /etc/dovecot/usersに記載したユーザ名を指定する。

図13

2. 設定時の警告を承認

アカウントの作成指示に以下のセキュリティ警告が表示される。いずれも、「確認」と「セキュリティ例外を承認」を選択する。


4. アカウント作成

設定が問題なければ「アカウント作成が完了しました」の画面が表示される。

3. メール送受信テスト

同様の手順で複数アカウントを作成し、お互いでメールを送受信できることを確認できれば動作確認完了となる。

以上で、Postfix+DovecotでバーチャルドメインのIMAPメールサーバーを構築する手順は完了となる。

人気の投稿