ウエブサイトを公開する前にiptablesはインストールされてることを確認して「ufw,fail2ban,sshguard」のインストール及び設定を済ませてたので安心してたが、iptables -Lとfail2ban.logのチェックで何時までも変化が無いので調べるとナナナナんと設定ファイルの作成を忘れていたと言うお粗末な事態に😞!即、iptablesのファイルを作成、又、自動起動用ファイルを作成した。
Xubuntuの自宅サーバでは基本設定用のファイルがあったと思ったけど、VMware PlayerのUbuntuでは設定ファイルを作成する必要があり、又、自動的に起動するための簡単なスクリプトを書く必要があった。
## iptablesのインストール
1 2 3 |
$ sudo apt update $ sudo apt -y install iptables $ sudo /sbin/iptables -L ←確認 |
## iptablesに読み込ませる設定ファイルの作成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
$ sudo vim /etc/iptables.up.rules *filter # Allows all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0 -A INPUT -i lo -j ACCEPT -A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT # Accepts all established inbound connections -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # Allows all outbound traffic # You can modify this to only allow certain traffic -A OUTPUT -j ACCEPT # Allows HTTP and HTTPS connections from anywhere (the normal ports for websites) -A INPUT -p tcp --dport 80 -j ACCEPT -A INPUT -p tcp --dport 443 -j ACCEPT # Allows SSH connections # THE -dport NUMBER IS THE SAME ONE YOU SET UP IN THE SSHD_CONFIG FILE -A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT # Allow ping -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT # log iptables denied calls -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7 # Reject all other inbound - default deny unless explicitly allowed policy -A INPUT -j REJECT -A FORWARD -j REJECT COMMIT * SSHのポート番号「?dport 22」は、使用しているポート番号を記入。 |
## 設定ファイルをiptablesに取り組むコマンド
1 2 3 4 5 6 7 |
$ sudo /sbin/iptables-restore < /etc/iptables.up.rules # iptable -Lで確認 Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT all -- anywhere anywhere REJECT all -- anywhere 127.0.0.0/8 reject-with icmp-port-unreachable --- 省略 --- |
## 自動的に実行の設定
if-pre-up.d(ディレクトリ)にiptablesと言うスクリプトを作成
1 2 3 4 5 |
$ sudo vim /etc/network/if-pre-up.d/iptables #!/bin/sh /sbin/iptables-restore < /etc/iptables.up.rules * このスクリプトを実行出来るように権限を与える。 $ sudo chmod +x /etc/network/if-pre-up.d/iptables |
基本的な設定が完了、後は、FTP、Mail等のポートを書き込むことになる。