Time4VPSのStorage VPSを使うためにLinuxでサーバーを立てることになりました。FreeBSD 12.0の初期設定 フォーマットに従って、Debian 9.11とCentOS 7.6の設定内容をまとめます。
OSのメンテナンスとパッケージシステム LinuxはFreeBSDと違ってOS部分もディストリビューターによって違うものを寄せ集めているので、OSのアップデートもパッケージシステムが担当しているっぽい。Debian系のパッケージマネージャはapt、Red Hat系のCentOSのパッケージマネージャはyumだそうです。
というわけでまずはパッケージシステムの更新。
Debian
ちなみにDebianのバージョンは/etc/debian_versionファイルに書いてあります。
1 2 $ cat /etc/debian_version 9.11
あっさりしてる。
CentOS
CentOSバージョンは/etc/redhat-release。
1 2 $ cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core)
ファイアウォールの設定 Debian Debian9のファイアウォールはufwというツールを使うらしい。そしてデーモンの操作はserviceではなくてsystemctlコマンドを使います。
まずはufwのインストール。Debianはパッケージをインストールするだけでデーモンが自動起動されるようになります。ただしufwが起動しているだけなので改めて有効化する必要があります。
1 2 apt install ufw ufw enable
以降、Qiitaのiptablesが難しいためufwでWEBサーバーのファイアウォール設定 に従って操作しました。
1 2 3 4 5 ufw default DENY ufw limit 20022 ufw allow 80 ufw allow 443 ufw allow 25
SSH,HTTP,HTTPS,MAILあたりのポートを開いておきます。ちなみにSSHだけlimitなのはブルートフォースアタック対策です。
ちゃんと設定できたかどうかをstatusで確認しておきます。
1 2 3 4 5 6 7 8 9 10 11 12 13 # ufw status Status: active To Action From -- ------ ---- 20022 LIMIT Anywhere 80 ALLOW Anywhere 443 ALLOW Anywhere 25 ALLOW Anywhere 20022 (v6) LIMIT Anywhere (v6) 80 (v6) ALLOW Anywhere (v6) 443 (v6) ALLOW Anywhere (v6) 25 (v6) ALLOW Anywhere (v6)
CentOS 一方CentOS7のファイアウォールはfirewalldというサービスを使います。デーモンの操作にsytemctlを使うのは同じです。
startではサービスが立ち上がるだけで、再起動後も自動的に動かすためにはenableしなきゃいけない。FreeBSDと違って/etc/rc.conf設定が不要なのは楽なのかな。
1 2 systemctl start firewalld.service systemctl enable firewalld.service
動作状態はstatusで確認できます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 # systemctl status firewalld.service ● firewalld.service – firewalld – dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2018-12-13 20:29:21 JST; 10h ago Docs: man:firewalld(1) Main PID: 179 (firewalld) CGroup: /system.slice/firewalld.service └─179 /usr/bin/python -Es /usr/sbin/firewalld –nofork –nopid Dec 13 20:10:06 1sae.f.time4vps.cloud firewalld[790]: WARNING: ebtables not usable, disabling ethernet bridge firewall. Dec 13 20:17:20 1sae.f.time4vps.cloud systemd[1]: Stopping firewalld – dynamic firewall daemon… Dec 13 20:17:21 1sae.f.time4vps.cloud systemd[1]: Stopped firewalld – dynamic firewall daemon. Dec 13 20:29:20 1sae.f.time4vps.cloud systemd[1]: Starting firewalld – dynamic firewall daemon… Dec 13 20:29:21 1sae.f.time4vps.cloud systemd[1]: Started firewalld – dynamic firewall daemon. Dec 13 20:29:21 1sae.f.time4vps.cloud firewalld[179]: WARNING: ipset not usable, disabling ipset usage in firewall. Dec 13 20:29:21 1sae.f.time4vps.cloud firewalld[179]: ERROR: Failed to read file "/proc/sys/net/netfilter/nf_conn…per' Dec 13 20:29:21 1sae.f.time4vps.cloud firewalld[179]: WARNING: Failed to get and parse nf_conntrack_helper setting Dec 13 20:29:21 1sae.f.time4vps.cloud firewalld[179]: WARNING: ebtables not usable, disabling ethernet bridge firewall. Dec 13 20:29:22 1sae.f.time4vps.cloud firewalld[179]: WARNING: /etc/sysconfig/network-scripts/ifcfg-venet0: Dupli…no"' Hint: Some lines were ellipsized, use -l to show in full.
設定はfirewall-cmdというコマンドを使います。
最初の–add-portはsshの接続ポート指定。続く3つの–add-serviceは許可するサービスの追加設定。いずれも–permanentを付けないと恒久設定になりません。さらに–reloadしないと設定が読み込まれません。
1 2 3 4 5 firewall-cmd --permanent --add-port=20022/tcp firewall-cmd --permanent --add-service=http firewall-cmd --permanent --add-service=https firewall-cmd --permanent --add-service=dns firewall-cmd --reload
ちゃんと設定できたかどうか、–list-allで確かめておきましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 # firewall-cmd –list-all public target: default icmp-block-inversion: no interfaces: sources: services: ssh dhcpv6-client http https dns ports: 20022/tcp protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
SSHの設定 こちらはFreeBSDと同じ。ssh接続用ユーザーを作ったら 、~/.ssh/authorized_keyファイルに公開鍵を書き込んで、/etc/ssh/sshd_configを設定します。
1 2 3 4 5 Port 20022 # 接続ポートを標準外にすることで攻撃されにくくします PermitRootLogin no # rootでのログインを拒否します PasswordAuthentication no # パスワード認証を拒否します PermitEmptyPasswords no # パスワードなしの接続を拒否します AllowUsers general_user # ログインユーザーを限定します
以前はProtocol 2
の設定をしていましたが、OpenSSH 7.4以降SSH v1接続は廃止された ので、もはや設定不要です。
ファイアウォールの設定で開いておいたポートと間違いないことを確認したら、sshdを再起動します。
1 # systemctl restart sshd.service
ntpの設定 Time4VPSのStorage VPSはOpenVZ上の仮想環境。カーネルがホストOSと共通なので、NTPサービスの設定は不要です。なのでufwの設定でもntpポートは開けませんでした。
不要な一時ファイルの削除 Debian9やCentOS7では/tmpおよび/var/tmpディレクトリ以下の一時ファイルをsystemd-tmpfiles-cleanというサービスが自動削除してくれます。
1 2 3 4 5 6 7 8 # systemctl status systemd-tmpfiles-clean.timer ● systemd-tmpfiles-clean.timer – Daily Cleanup of Temporary Directories Loaded: loaded (/usr/lib/systemd/system/systemd-tmpfiles-clean.timer; static; vendor preset: disabled) Active: active (waiting) since Sat 2018-11-24 10:42:49 JST; 4h 23min ago Docs: man:tmpfiles.d(5) man:systemd-tmpfiles(8) Nov 24 10:42:49 81014.s.time4vps.cloud systemd[1]: Started Daily Cleanup of Temporary Directories. Nov 24 10:42:49 81014.s.time4vps.cloud systemd[1]: Starting Daily Cleanup of Temporary Directories.
設定は/usr/lib/tmpfiles.d/tmp.confファイル。削除対象はvで始まるディレクトリ、除外対象はxまたはXで始まるディレクトリやファイルなどです。CentOS7では/tmpは10日、/var/tmpは30日で削除されるようになっていますね。
1 2 3 4 5 6 7 8 # Clear tmp directories separately, to make them easier to override v /tmp 1777 root root 10d v /var/tmp 1777 root root 30d # Exclude namespace mountpoints created with PrivateTmp=yes x /tmp/systemd-private-%b-* X /tmp/systemd-private-%b-*/tmp x /var/tmp/systemd-private-%b-* X /var/tmp/systemd-private-%b-*/tmp
スワップファイルの設定 Time4VPSのStorage VPSにもスワップパーティションはありません。以前はメインメモリの半分ぐらいのSwapファイルが設定されていたのですが、2019年7月17日以降なくなってしまったようです。
OpenVZではホストから割り当てられているっぽいので、後から/etc/fstabで追加することもできません。