AlmaLinux8でのFreeIPAサーバーの設定

前提条件

・ AlmaLinuxのバージョン: 8.5
・ ipaサーバーのバージョン: 4.9.6
・ プライマリのIPアドレスは192.168.0.10
・ セカンダリのIPアドレスは192.168.0.11

プライマリサーバーの設定

FQDNでホスト名を設定する。
※FQDNが192.168.0.10に名前解決できるようにしておく

# hostnamectl set-hostname ipa1.local.example.com

モジュールを検索して必要そうなものを特定する。

$ dnf module list

idmのDL1にServerと書いてあったので、それをインストールする。

# dnf install @idm:DL1

モジュールのインストールが完了したら、必要なパッケージをインストールする。

# dnf install ipa-server ipa-server-dns

対話型のインストーラーを起動する。
※同時にbindも設定してもらう
※DNSフォワーダーも設定する
※そのほかは大体Enterで続行した

# ipa-server-install

ファイアウォールの例外を登録するよう促されるので登録する。
※サービス名で設定する場合は「ldap」ではなく「freeipa-ldap」なので注意
※freeipa-replicationはセカンダリを構築しない場合はいらない

# firewall-cmd --permanent --add-service={freeipa-ldap,freeipa-ldaps,freeipa-replication,dns,ntp,http,https}
# firewall-cmd --reload

参照するDNSサーバーを自身にする。

# nmcli connection modify コネクション名 ipv4.dns 192.168.0.10
# nmcli connection up コネクション名

chronyのserver/clientの設定をする。
※変更箇所のみ記載

# Allow NTP client access from local network.
#allow 192.168.0.0/16
allow 192.168.0.0/24

# Select which information is logged.
#log measurements statistics tracking
server ntp.nict.jp iburst

chronydを再起動する。

# systemctl restart chronyd

初回ログイン時にホームディレクトリを自動生成するよう設定する。

# authconfig --enablemkhomedir --update

セカンダリサーバーの設定

プライマリのIdMにWEBブラウザでアクセスして、セカンダリのhostを追加する。

セカンダリのホスト名をFQDNに変更する。

# hostnamectl set-hostname ipa2.local.example.com

参照するDNSサーバーをプライマリIPAサーバーにする。

# nmcli connection modify コネクション名 ipv4.dns 192.168.0.10
# nmcli connection up コネクション名

必要なパッケージをインストールする。

# dnf install @idm:DL1
# dnf install ipa-server ipa-server-dns

IPAクライアントとして設定する。

# ipa-client-install

chronyのserver/clientの設定をする。
※変更箇所のみ記載

# Allow NTP client access from local network.
#allow 192.168.0.0/16
allow 192.168.0.0/24

# Select which information is logged.
#log measurements statistics tracking
server ntp.nict.jp iburst

chronydを再起動する。

# systemctl restart chronyd

初回ログイン時にホームディレクトリを自動生成するよう設定する。

# authconfig --enablemkhomedir --update

ファイアウォールの例外を登録する。

# firewall-cmd --permanent --add-service={freeipa-ldap,freeipa-ldaps,freeipa-replication,dns,ntp,http,https}
# firewall-cmd --reload

レプリカインストールを実行する。なぜか「–setup-ca」オプションをつけると必ず失敗する。ほかのディストリビューション(Rocky Linux、Oracle Linux)で試してみたり、色々な小細工してみたけど解決できなかったので、今回はCA設定をプライマリだけで運用することにした。
※DNSとしても動作させるため「–setup-dns」オプション指定

# ipa-replica-install --setup-dns --forwarder <forwarderのIPアドレス1> --forwarder <forwarderのIPアドレス2>

参照するDNSサーバーを自身にする。

# nmcli connection modify コネクション名 ipv4.dns 192.168.0.11
# nmcli connection up コネクション名

補足

プライマリのCA設定がレプリケーションできていないため定期的にバックアップを取得する必要がある。下記のコマンドでバックアップを作成できるが、作成場所は指定できず/var/lib/ipa/backup/に「ipa-full-2022-04-09-14-28-16」という形でディレクトリが作成される。cronで実行する場合は適宜スクリプトを書いた方が良い。

# ipa-backup

対してリストアはバックアップファイルへのパスを指定して実行する。

# ipa-restore /var/lib/ipa/backup/ipa-full-2022-04-09-14-28-16/

AlmaLinux8でsudoのパスワード入力をなしにする

今回はwheelグループを対象に設定する。下記コマンドを実行して/etc/sudoersを編集する。

# visudo

ファイル終盤ぐらいにある下記の部分、上の方をコメントブロックし、下の方をコメント解除する。

## Allows people in group wheel to run all commands
# %wheel        ALL=(ALL)       ALL

## Same thing without a password
%wheel  ALL=(ALL)       NOPASSWD: ALL

AlmaLinux8でdhcpとbindを連携させ家庭内DDNS

インストール

今回はDNSサーバーもDHCPサーバーも1台で兼任させる。
まずインストールする。
※Version bind9.11.26 dhcp4.3.6

# dnf install bind dhcp-server

DNSサーバーの設定

bindから設定する。
下記の箇所の内容を変更または追記する。

options {
    // zoneファイルの保存場所
    directory "/var/named/";
    // ほかのdnsサーバーはいないのでno
    notify no;
    // LAN内からの問い合わせを許可
    allow-query {
        localhost;
        192.168.1.0/24;
    };
    // 自分で名前解決できないときの丸投げ先
    forwarders {
        192.168.1.254;
        8.8.8.8;
    };
};

zone "example.jp." {
    type master;
    file "example.jp.zone";
    // 自身からの変更を許可しておく
    allow-update {
        localhost;
    };
};

さっき指定したzoneファイルを作成する。
※admin.example.jp.の部分はadmin@example.jpという管理者のE-mailという意味

$TTL 86400
@    IN    SOA    dns.example.jp.    admin.example.jp. (
           2022032319
           10800
           36400
           604800
           86400
)

       IN    NS   dns.example.jp.
dns    IN    A    192.168.1.11

作成したらファイルの所有者をnamedに変更する。
※/var/named/にスティッキービットが設定されているため

# chown named:named /var/named/example.jp.zone

DHCPサーバーの設定

設定ファイルを下記のように編集する。

# DNSサーバーに更新要求を送信する設定
ddns-update-style interim;
# 普通のDHCPの設定
subnet 192.168.1.0 netmask 255.255.255.0 {
    range 192.168.1.101 192.168.1.120;
    option routers 192.168.1.254;
    option domain-name "example.jp";
    option domain-name-servers 192.168.1.11;
    # 更新を通知するDNSサーバーを指定
    zone example.jp. {
        primary localhost;
    }
}

その他の必要な設定

SELinuxとFirewallに例外を追加する。

# setsebool -P named_write_master_zones true
# firewall-cmd --add-service=dns --permanent
# firewall-cmd --add-service=dhcp --permanent
# firewall-cmd --reload

サービスを有効にして起動。

# systemctl enable named
# systemctl start named
# systemctl enable dhcpd
# systemctl start dhcpd

補足

nsupdate

nsupdateを使用してDNSサーバーを更新をテストするには bind-utils をインストールし、nsupdateコマンドを実行する。実行したら対話型コンソールになるので下記のような感じで入力する。何もエラーが出なければOK。

>server 127.0.0.1
>update add test.example.jp. 3600 A 192.168.1.222
>send
>server 127.0.0.1
>update delete test.example.jp.
>send
rndc

rndcでdumpして現在のDNSレコードを確認する。

# rndc dumpdb -zones
# cat /var/named/named_dump.db | grep dns.example.jp.

AlmaLinux8のApacheにZeroSSLで手動発行した証明書をインストールする

AlmaLinux release 8.5 (Arctic Sphynx)
Apache 2.4.37
OpenSSL 1.1.1k

SSL関連のパッケージをインストールする。

# dnf install openssl
# dnf install mod_ssl

ZeroSSLにログインして発行した証明書のzipファイルをWebサーバーにアップロードして解凍する。

# unzip test.example.com.zip -d /etc/ssl/test.example.com/

SELinuxのラベル付けを行うためのユーティリティをインストールする。

# dnf install policycoreutils-python-utils

httpdからファイルを読み込めるようにSELinuxのラベルを追加する。

# semanage fcontext -a -t httpd_sys_content_t "/etc/ssl/test.example.com(/.*)?"
# restorecon -RF /etc/ssl/test.example.com/

Apacheの設定ファイルの最下部にSSL証明書の設定を追記する。

<VirtualHost *:443>
    ServerName test.example.com
    DocumentRoot "/var/www/html/test/"
    <Directory "/var/www/html/test/">
        AllowOverride All
    </Directory>
    SSLEngine on
    SSLProtocol -All +TLSv1.2
    SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5:!SEED:!IDEA:!3DES:!RC4:!DH
    SSLHonorCipherOrder On
    SSLCertificateFile /etc/ssl/test.example.com/certificate.crt
    SSLCertificateKeyFile /etc/ssl/test.example.com/private.key
    SSLCertificateChainFile /etc/ssl/test.example.com/ca_bundle.crt
</VirtualHost>

Apacheの設定を再読み込みする。

# systemctl reload httpd

AlmaLinux8にMariaDBをインストールする

AlmaLinux release 8.5 (Arctic Sphynx)
MariaDB 10.3.28

MariaDBをインストールする。

# dnf install mariadb-server

設定ファイルの[mysqld]の下に文字コードの指定を追記する。

[mysqld]
character-set-server=utf8mb4

MariaDBの初期設定を行う。

# mysql_secure_installation
---------------------------------
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

AlmaLinux8のApacheにLet’sEncryptの証明書をインストールする

AlmaLinux release 8.5 (Arctic Sphynx)
Apache 2.4.37
OpenSSL 1.1.1k

SSL関連のパッケージをインストールする。

# dnf install openssl
# dnf install mod_ssl

Let’sEncryptの証明書を発行するためのcertbotをインストールする。

# dnf install epel-release
# dnf install certbot

SSL証明書を発行する。何度も失敗したりしても制限がかかるので、–dry-runオプションを指定してテストする。

# certbot certonly --webroot -w /var/www/html/test/ -d test.example.com
-------------------------------------------------------------
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel): admin@test.example.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: N
Account registered.
Requesting a certificate for test.example.com

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/test.example.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/test.example.com/privkey.pem
This certificate expires on 2022-06-01.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
 * Donating to EFF:                    https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

発行が成功すると下記の場所に証明書が作られる。ほかのサーバーに移行する場合は/etc/letsencrypt/ごとコピーすればOKだった。

/etc/letsencrypt/live/test.example.com/cert.pem
/etc/letsencrypt/live/test.example.com/privkey.pem
/etc/letsencrypt/live/test.example.com/chain.pem

cronで毎朝4時にSSL証明書を更新するジョブを追加する。更新されるかは自動で決定され、更新された場合のみ–deploy-hookオプションで指定されたコマンドが実行される。

0 4 * * * certbot renew --deploy-hook "systemctl reload httpd"

Apacheの設定ファイルの最下部にSSL証明書の設定を追記する。

<VirtualHost *:443>
    ServerName test.example.com
    DocumentRoot "/var/www/html/test/"
    <Directory "/var/www/html/test/">
        AllowOverride All
    </Directory>
    SSLEngine on
    SSLProtocol -All +TLSv1.2
    SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5:!SEED:!IDEA:!3DES:!RC4:!DH
    SSLHonorCipherOrder On
    SSLCertificateFile /etc/letsencrypt/live/test.example.com/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/test.example.com/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/test.example.com/chain.pem
</VirtualHost>

Apacheの設定を再読み込みする。

# systemctl reload httpd

AlmaLinux8にApache+PHPをインストールする

AlmaLinux release 8.5 (Arctic Sphynx)
Apache 2.4.37
PHP 7.2.24

Apacheをインストールする。

# dnf install httpd

Apacheの設定ファイル内の「Indexes」を削除して、index.phpが存在しない場合のディレクトリ内の一覧表示機能を無効にする。

<Directory "/var/www/html">
    #Options Indexes FollowSymLinks
    Options FollowSymLinks

続けてApacheの設定ファイルにドメインごとにディレクトリ指定を行う設定を追記する。

#
# test.example.com Subdomain configuration
#
<VirtualHost *:80>
        ServerName test.example.com
        DocumentRoot /var/www/html/test/
        <Directory /var/www/html/test/>
                AllowOverride All
        </Directory>
</VirtualHost>

SELinuxのラベル付けを行うためのユーティリティをインストールする。

# dnf install policycoreutils-python-utils

/var/www/html/test/以下のファイルをhttpdから変更できるようSELinuxのラベルを付与する。

# semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html/test(/.*)?"
# restorecon -RF /var/www/html/test/

httpdがネットワークに接続することを許可する。

# setsebool -P httpd_can_network_connect true

PHPと使用する拡張をインストールする。

# dnf install php
# dnf install php-mbstring
# dnf install php-json
# dnf install php-mysqlnd
# dnf install php-xml
# dnf install php-gd

8MB程度のファイルをアップロードするため、下記の箇所を設定変更する。

post_max_size = 10M
upload_max_filesize = 10M

WEBサイトからE-mailを送信するため、sendmailをインストールして有効化する。

# dnf install sendmail
# dnf install postfix
# systemctl enable --now postfix

httpdからE-mailを送信できるようSELinuxの設定を変更する。

# setsebool -P httpd_can_sendmail true

Apacheを起動して有効化する。

# systemctl enable --now httpd

AlmaLinux8にsftp接続して特定ディレクトリのみ表示する

AlmaLinux release 8.5 (Arctic Sphynx)

専用のユーザーを追加してパスワードを設定する。

# useradd sftp
# passwd sftp

専用のディレクトリを作成する。chrootに指定するディレクトリの所有者はrootでパーミッションは755である必要がある。

# mkdir /data/
# chmod 755 /data/
# mkdir /data/sftp/
# chown sftp:sftp /data/sftp/

設定ファイルを変更してサブシステムをinternal-sftpに変更する。

#Subsystem      sftp    /usr/libexec/openssh/sftp-server
Subsystem       sftp    internal-sftp

再度、設定ファイルを変更、「Match User」の箇所の例をコピーして下記のように追記する。

Match User sftp
        X11Forwarding no
        AllowTcpForwarding no
        PermitTTY no
        ForceCommand internal-sftp
        ChrootDirectory /data/