コンテンツにスキップ

サーバのセットアップ

ディスクの追加

パーティションサイズを取得

$ sudo parted -l /dev/sdx

partedで対象デバイスを選択

$ sudo parted /dev/sdx

gptでセットアップする

(parted) mklabel gpt

HDDは2TBを超えるとfdiskではなくpartedを使えと怒られる。あと、GPTしか2TBを超えると使えない。

パーティションを作成する

(parted) mkpart
パーティションの名前?  []? backupDisk
ファイルシステムの種類?  [ext2]? ext4
開始? 0
終了? 3001GB
警告: The resulting partition is not properly aligned for best performance.
無視(I)/Ignore/取消(C)/Cancel? i

パーティションをフォーマットする

$ mkfs.ext4 /dev/sdx1

memo

/etc/fstabを編集

fstabの末尾についている数字の意味。

$ man 5 fstab

The fifth field (fs_freq).
    This  field is used for these filesystems by the dump(8) command to determine which filesystems need to be dumped.  If the
    fifth field is not present, a value of zero is returned and dump will assume that the  filesystem  does  not  need  to  be
    dumped.

The sixth field (fs_passno).
    This  field is used by the fsck(8) program to determine the order in which filesystem checks are done at reboot time.  The
    root filesystem should be specified with a fs_passno of 1, and other filesystems should have a fs_passno of  2.   Filesys‐
    tems  within a drive will be checked sequentially, but filesystems on different drives will be checked at the same time to
    utilize parallelism available in the hardware.  If the sixth field is not present or zero, a value of zero is returned and
    fsck will assume that the filesystem does not need to be checked.

UUIDの確認方法

$ blkid /dev/sdx
/dev/sda3: LABEL="root" UUID="179f9487-5148-4f2b-a684-cd98fd459546" TYPE="xfs" 
/dev/sda1: LABEL="boot" UUID="161a786a-50d3-40e8-a3b3-82ee1b254f59" TYPE="xfs" 
/dev/sda2: LABEL="swap" UUID="3765bd90-b1a8-4622-8b8a-b4ff488036ef" TYPE="swap" 
/dev/sdb: UUID="29677d4a-e6d7-4847-b75e-cc7172e8bf16" TYPE="ext4" 
/dev/sdc1: UUID="b420aeb5-4e86-4ea7-89d9-6159cebd96d7" TYPE="ext4" PARTLABEL="backupDisk" PARTUUID="23d9c2ca-c6d8-41eb-884a-a30c821dbc2c"

IPアドレスの固定化

NetworkManagerでやるならnmcliで、従来からのやり方であれば/etc/sysconfig/network-scripts/ifcfg-enp2s0を書き換える。

CentOS7でIPを固定する - Qiita

SSHサーバの設定変更

// todo: 鍵認証に変更する

/etc/ssh/sshd_configでポート番号を22022へ変更する。

バナーの設定

[root@hotate ssh]# cat banner 
+++++++++++++++++++++++++++++++++++++++++++++
+       __  __      __        __     
+      / / / /___  / /_____ _/ /____ 
+     / /_/ / __ \/ __/ __ `/ __/ _ \
+    / __  / /_/ / /_/ /_/ / /_/  __/
+   /_/ /_/\____/\__/\__,_/\__/\___/ 
+
+++++++++++++++++++++++++++++++++++++++++++++
Font: Slant, Genarator: TAAG(patorjk.com)

SELinuxの設定

semanageが含まれているパッケージを調査する。

$ yum provides *bin/semanage
policycoreutils-python-2.5-17.1.el7.x86_64 : SELinux policy core python utilities
リポジトリー        : sl
一致          :
ファイル名    : /usr/sbin/semanage

パッケージpolicycoreutils-pythonに含まれていることが分かるのでインストール。

$ yum install policycoreutils-python

SELinuxでSSHが開放するポートを変更。

$ semanage port -a -t ssh_port_t -p tcp 22022

firewalldの設定

サービスごとの設定一覧は/usr/lib/firewalld/services/に配置されている。設定を書き換える場合は、これらを/etc/firewalld/services/へコピーしてから書き換える。

$ firewall-cmd --list-all  # firewallの設定確認
$ cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/
$ vim /etc/firewalld/services/ssh.xml
<?xml version="1.0" encoding="utf-8"?>
<service>
    <short>SSH</short>
    <description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>
    <port protocol="tcp" port="2222"/>
</service>
$ firewall-cmd --reload

参考: CentOS7のfirewalldでsshのポート番号を変更する方法 - Qiita

nginxのインストール

リポジトリの追加

参考: nginx: Linux packages

ファイル/etc/yum.repos.d/nginx.repoに以下の記述を追加する。

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

アップデートを行ってリポジトリ情報の更新を行う。

$ yum update

インストールの実行

$ yum install nginx

自動起動の有効化と起動

$ systemctl enable nginx
$ systemctl start nginx

firewalldで80/tcpを開く

$ firewall-cmd --add-service=http --permanent
$ firewall-cmd --reload

一時的な場合は--permanentをつけない。ただし設定は保存されない。

firewalldで80/tcpを閉じる

$ firewall-cmd --remove-service=http --permanent
$ firewall-cmd --reload

一時的な場合は--permanentをつけない。ただし設定は保存されない。

リバースプロキシの設定

サーバの/a/に来たリクエストを:81へフォワードする。proxy_set_header$hostにすることでリクエストにHostが含まれていなかった場合にserver_name`を設定してリクエストを送信する。

However, if this field is not present in a client request header then nothing will be passed. In such a case it is better to use the $host variable - its value equals the server name in the “Host” request header field or the primary server name if this field is not present:

参考: Module ngx_http_proxy_module

location /a/ {
    rewrite          /a/(.*)  /$1  break;
    proxy_pass       http://localhost:81;
    proxy_set_header Host      $host;
    proxy_set_header X-Real-IP $remote_addr;
}

モジュールhttp_sub_moduleでHTMLを変更

参考: Module ngx_http_sub_module

kea(DHCP)のインストール

公式サイトからソースを落とす。

Index of /isc/kea

$ wget wget http://ftp.isc.org/isc/kea/1.3.0/kea-1.3.0.tar.gz
$ tar xvfz kea-1.3.0.tar.gz
$ cd kea-1.3.0
$ ./configure --with-openssl --with-dhcp-mysql --prefix=/opt/kea
checking for OpenSSL library... configure: error: OpenSSL auto detection failed

インストール方法はhttp://kea.isc.org/docs/kea-guide.htmlに書かれている。

OpenSSLライブラリのインストール

OpenSSLのライブラリが見つからないと怒られたのでインストールする。

$ yum install openssl-devel

今度はヘッダファイル群が見つからないと怒られた。

$ ./configure --with-openssl --with-dhcp-mysql --prefix=/opt/kea
...
checking sys/filio.h usability... no
checking sys/filio.h presence... no
checking for sys/filio.h... no
checking log4cplus/logger.h usability... no
checking log4cplus/logger.h presence... no
checking for log4cplus/logger.h... no
configure: error: Missing required header files.

調べたところsys/filio.hはBSDで必要なよう。Linuxではlog4cplusが必要になる。EPELリポジトリからインストールすることが出来るのでEPELリポジトリを追加する。

EPELリポジトリの追加

rpmファイルをダウンロードする。

$ wget http://ftp.riken.jp/Linux/fedora/epel/epel-release-latest-7.noarch.rpm
$ rpm -ivh epel-release-latest-7.noarch.rpm

設定を確認してみる。enabled=1になっている箇所をenabled=0へ変更する。

$ vim /etc/yum.repos.d/epel.repo
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
...

log4plusのインストール

パッケージlog4plusをインストールする。

$ yum install log4cplus log4cplus-devel --enablerepo=epel
$ ./configure --with-openssl --with-dhcp-mysql --prefix=/opt/kea
...
checking boost/shared_ptr.hpp usability... no
checking boost/shared_ptr.hpp presence... no
checking for boost/shared_ptr.hpp... no
configure: error: Missing required header files.

boostのインストール

ライブラリboostをインストールする。

$ yum install boost boost-devel
$ ./configure --with-openssl --with-dhcp-mysql --prefix=/opt/kea
    ...
    Kea source configure results:
    -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Package:
Name:            kea
Version:         1.3.0
Extended version:1.3.0 (tarball)
OS Family:       Linux
Using GNU sed:   yes
Premium package: no

C++ Compiler:
CXX:             g++ --std=c++11
CXX_VERSION:     g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16)
CXX_STANDARD:    201103
DEFS:            -DHAVE_CONFIG_H
CPPFLAGS:         -DOS_LINUX  -I$(top_srcdir)/ext/coroutine -DBOOST_ASIO_HEADER_ONLY -DBOOST_ASIO_DISABLE_THREADS=1
CXXFLAGS:        -g -O2
LDFLAGS:          -lpthread
KEA_CXXFLAGS:     -Wall -Wextra -Wnon-virtual-dtor -Wwrite-strings -Woverloaded-virtual -Wno-sign-compare -pthread -Wno-missing-field-initializers -fPIC

Python:
PYTHON_VERSION:  not needed (because kea-shell is disabled)

Boost:
BOOST_VERSION:   1.53
BOOST_INCLUDES:  
BOOST_LIBS:       -lboost_system

OpenSSL:
CRYPTO_VERSION:  OpenSSL 1.0.2k  26 Jan 2017
CRYPTO_CFLAGS:   
CRYPTO_INCLUDES: 
CRYPTO_LDFLAGS:  
CRYPTO_LIBS:     -lcrypto

Botan: no

Log4cplus:
LOG4CPLUS_VERSION: 1.1.3

./configureに成功したのでmakeする。そしてmake installする。

インストールしたソースは/usr/local/src/に置いておく。

自動起動ファイルの作成

自動起動ファイル(kea-dhcp4.service)を作成する。ファイルは/usr/lib/systemd/system/kea-dhcp4.serviceに配置しておく。

[Unit]
Description=Kea DHCPv4 Server
Wants=network-online.target
After=network-online.target mariadb.service postgresql.service

[Service]
Type=simple
ExecStart=/opt/kea/sbin/kea-dhcp4 -c /opt/kea/etc/kea/kea-dhcp4.conf
ExecStop=/opt/kea/sbin/keactrl stop -s dhcp4
Restart=always

[Install]
WantedBy=multi-user.target

How do I cat multi-line content to a file in a systemd unit file? - Stack Overflow

配置後はsystemctl enable kea-dhcp4を入力して自動起動を有効化する。また、systemctl status kea-dhcp4で自動起動が有効になっているか確認する。

mysqlの設定

自動起動を有効化、その確認、mysqlを起動。

$ systemctl enable mariadb
$ systemctl status
$ systemctl start

初期設定を行う。

$ mysql_secure_installation

DBへ接続をする。

$ mysql -u root -p
MariaDB [(none)]> create database dhcpdb;
MariaDB [(none)]> grant all privileges on dhcpdb.* to 'kea'@'localhost' identified by 'PASSWORD';
MariaDB [(none)]> quit

テーブルを作成する。予め用意されているテーブルを流し込む。

$ mysql -u kea -p dhcpdb < /opt/kea/share/kea/scripts/mysql/dhcpdb_create.mysql^C
$ mysql -u kea -p dhcpdb

MariaDB [dhcpdb]> show tables;
+----------------------+
| Tables_in_dhcpdb     |
+----------------------+
| dhcp4_options        |
| dhcp6_options        |
| dhcp_option_scope    |
| host_identifier_type |
| hosts                |
| ipv6_reservations    |
| lease4               |
| lease6               |
| lease6_types         |
| lease_hwaddr_source  |
| lease_state          |
| schema_version       |
+----------------------+
12 rows in set (0.00 sec)
MariaDB [dhcpdb]> quit

keaの使い方

$ keactrl [COMMAND] [-c config-file] [-s dhcp4|dhcp6|ddns]
[COMMAND] -> start|stop|reload|status

使い方の例:

$ keactrl start

すべてのサーバ(dhcp4|dhcp6)を起動した。(デフォルトでddnsは無効)

$ keactrl stop

すべてのサーバを停止する。

$ keactrl start -s dhcp4

DHCPv4だけ起動する。

DBにログを記録

//

TFTPサーバの構築

$ yum install syslinux xinetd tftp-server
$ mkdir /var/lib/tftpboot/pxelinux.cfg/
$ cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/

$ vi /etc/xinetd.d/tftp
disable = no
# yes => no

sambaのインストール

公式サイトからソースコードを落とす。

$ wget https://download.samba.org/pub/samba/samba-latest.tar.gz

展開してcdする。

$ tar xvfz samba-latest.tar.gz
$ cd $_

コンパイルオプションを調べる

$ ./configure --help

オプションのspotlightはtracker-sparqlが無いぞと怒られるので

$ yum install tracker-devel

あとはインストールする

$ ./configure --enable-spotlight --with-systemd --download --prefix=/opt/samba
$ make
$ make install

systemdでの設定

サービスへの登録をする。samba.serviceは以下を参考にした。

Managing the Samba AD DC Service Using Systemd - SambaWiki

オプション-sで任意のコンフィグファイルを読み込めるのでExecStart=/opt/samba/sbin/samba -D -s /opt/samba/etc/smb.confと設定した。

設定ファイルsamba.service/usr/lib/systemd/system/へ配置する。

設置後には、systemctl daemon-reloadを実行する。

サービスを起動しようとしたらエラーが出た。

12月 29 18:09:42 mysrv systemd[1]: Started Samba Active Directory Domain Controller.
12月 29 18:09:42 mysrv samba[32567]: [2017/12/29 18:09:42.567104,  0] ../source4/smbd/server.c:602(binary_smbd_main)
12月 29 18:09:42 mysrv samba[32567]:   At this time the 'samba' binary should only be used for either:
12月 29 18:09:42 mysrv samba[32567]:   'server role = active directory domain controller' or to access the ntvfs file server with 'server services = +smb' or the rpc proxy with 'dcerpc endpoint servers = remote'
12月 29 18:09:42 mysrv samba[32567]:   You should start smbd/nmbd/winbindd instead for domain member and standalone file server tasks
12月 29 18:09:42 mysrv samba[32567]: [2017/12/29 18:09:42.567287,  0] ../lib/util/become_daemon.c:111(exit_daemon)
12月 29 18:09:42 mysrv samba[32567]:   STATUS=daemon failed to start: Samba detected misconfigured 'server role' and exited. Check logs for details, error code 22
12月 29 18:09:42 mysrv systemd[1]: samba.service: main process exited, code=exited, status=1/FAILURE
12月 29 18:09:42 mysrv systemd[1]: Unit samba.service entered failed state.
12月 29 18:09:42 mysrv systemd[1]: samba.service failed.

調べてみた所、同様のエラーが出ているケースを発見。

samba 4.3.0でsambaサーバの起動に失敗してた - φ(・・*)ゞ ウーン カーネルとか弄ったりのメモ

どうやらcacheディレクトリのパーミッションを変更してみると良いらしい。cacheのパスを忘れたのでコンパイルオプションからパスを取得する。

Samba のビルドオプションを確認する方法 - Samba-JP

$ samba -b
Samba version: 4.7.4
Build environment:
   Build host:  Linux hotate 3.10.0-693.11.1.el7.x86_64 #1 SMP Fri Dec 1 09:09:59 CST 2017 x86_64 x86_64 x86_64 GNU/Linux
Paths:
   BINDIR: /opt/samba/bin
   SBINDIR: /opt/samba/sbin
   CONFIGFILE: /opt/samba/etc/smb.conf
   NCALRPCDIR: /opt/samba/var/run/ncalrpc
   LOGFILEBASE: /opt/samba/var
   LMHOSTSFILE: /opt/samba/etc/lmhosts
   DATADIR: /opt/samba/share
   MODULESDIR: /opt/samba/lib
   LOCKDIR: /opt/samba/var/lock
   STATEDIR: /opt/samba/var/locks
   CACHEDIR: /opt/samba/var/cache
   PIDDIR: /opt/samba/var/run
   PRIVATE_DIR: /opt/samba/private
   CODEPAGEDIR: /opt/samba/share/codepages
   SETUPDIR: /opt/samba/share/setup
   WINBINDD_SOCKET_DIR: /opt/samba/var/run/winbindd
   NTP_SIGND_SOCKET_DIR: /opt/samba/var/lib/ntp_signd

CACHEDIRから/opt/samba/var/cacheにあることが分かった。そこでパーミッションをchmod 755 /opt/samba/var/cacheで変更してsystemctl restart sambaをしたが失敗。

もうすこし、調べてみたところ根本的な原因が分かった。

再起動後にSambaがうまく動いていない時の対処法(Ubuntu 14.04 LTS) - 以下省略!

smbdとsambaは別物でActive Directoryモードの場合はsambaだが、従来からの使い方はsmbdであるとのこと。つまり、smbdnmbd用にsystemd用のserviceファイルを作成すればいい。ファイルの作成方法は以下を参照。

9.6. システムのユニットファイルの作成および変更 - Red Hat Customer Portal

RHEL 7 における systemd の概要 - Red Hat Customer Portal

smb.confの作成

設定を書いてtestparmを実行したらdisplay charsetが存在しないパラメータと怒られた。どうやら廃止された模様。

SambaでWindowsファイル共有を行う正しいやり方 - ももいろテクノロジー

spotlightの設定

設定は結構めんどいっぽい。

Netatalk and Samba

セットアップは以下に従えばよい。

Spotlight - SambaWiki

dbus-launchが見つからないと怒られたのでdbus-x11をインストールする

Debian -- パッケージ内容検索結果 -- dbus-launch

Netatalkの設定

Netatalk 3.1.11 SRPM for Fedora and CentOS - Netatalk Wiki

rpmbuildやる時はユーザ追加してやれよって書いてあった。

いまさら聞けないrpmbuildことはじめ - hack in 3 minutes

$ yum install dconf perl perl-IO-Socket-INET6
$

マウントオプションを変更

user_xattr,aclをつける

細かい設定とか参考になった一撃スクリプト

Install Time Machine service on CentOS 7

afpの設定

$ vim /etc/netatalk/afpd.conf

バージョン古めだけど/etc/netatalk/afpd.confについて:

afpd.conf

バーション古めだけど/etc/netatalk/AppleVolumes.defaultについて:

AppleVolumes.default

avahi(bonjour)の設定

$ yum install avahi avahi-tools
$ cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services/avahi.xml
$ vim /etc/firewalld/services/avahi.xml

tomoyk/originalConfs

$ firewall-cmd --add-service=avahi --permanent
$ firewall-cmd --reload

Security

tcpwrapper, selinux, firewalld, clamav, fail2ban, tripwire

セキュリティ関係を含めた設定: Top 20 OpenSSH Server Best Security Practices - nixCraft