Webinoly 配置 iptables 防火牆

·

iptables

Unsplash+

Webinoly 在 VPS 上安裝完後,也建置好網站,最後有一個重要的工作,在 VPS 上配置防火牆。

Webinoly 或 EasyEngine 安裝完是沒有開啟任何防火牆的,也沒有配置文件,為了服務器的安全,建議大家啟用防火牆設置,所以就需要手動來配置 iptables.rules 文件。

如果您是使用 Vultr、GCP 主機,可以從管理後台開啟防火牆功能,就不需要在主機上再配置,可以省略這篇文章。

查看系統是否安裝 iptables

whereis iptables

回覆

iptables: /sbin/iptables /usr/share/iptables /usr/share/man/man8/iptables.8.gz # 表示已經安裝 iptables
apt-get install iptables #如果默認沒有安裝,請執行此命令安裝

iptables -L #查看防火牆配置信息,顯示如下:

Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination

開始配置 iptables.rules 文件

因為 VPS 主機是安裝 EasyEngine 或 Webinoly 程序,所以下面的 port 要打開:

  • 22 / TCP (Inbound / Outbound) : Standard SSH port
  • 80 / TCP (Inbound / Outbound) : Standard HTTP port
  • 443 / TCP(Inbound / Outbound) : Standard HTTPS port
  • 22222 / TCP (Inbound) : To access EasyEngine admin tools
  • 11371 / TCP (Outbound) : To connect to GPG Key Server

創建 iptables.rules 文件:

vi /etc/iptables.rules

添加以下內容:

  • 22:SSH 遠程管理端口
  • 80 / 443:web 服務器端口
  • 3306:MySQL 數據庫鏈接端口 (有這需求在自行打開)
  • 465 / 587:SMTP 寄信端口
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 465 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 587 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22222 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 11371 -j ACCEPT
-A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
-A INPUT -p icmp -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

配置文件裡有兩條規則,作用是每秒鐘只允許 100 個碎片,用來防止小型 DDoS 攻擊。

-A INPUT -p icmp -m limit --limit 1/sec --limit-burst 10 -j ACCEPT
-A INPUT -f -m limit --limit 100/sec --limit-burst 100 -j ACCEPT

使防火牆規則生效:

iptables-restore < /etc/iptables.rules

CentOS 上可以執行:service iptables save 保存規則。

但是在 Debian / Ubuntu 系統上, iptables 是不會保存規則的。需要按如下步驟進行,讓網卡關閉保存 iptables 規則,啟動時加載 iptables 規則。

創建 /etc/network/if-post-down.d/iptables 文件,添加如下內容:

vi /etc/network/if-post-down.d/iptables
#!/bin/bash
iptables-save > /etc/iptables.rules

添加執行權限:

chmod +x /etc/network/if-post-down.d/iptables

創建 /etc/network/if-pre-up.d/iptables 文件,添加如下內容:

vi /etc/network/if-pre-up.d/iptables
#!/bin/bash
iptables-restore < /etc/iptables.rules

添加執行權限:

chmod +x /etc/network/if-pre-up.d/iptables

最後查看 iptables 規則是否生效:

iptables -L -n

iptables 的規則其實還有很多,這邊只有列出基本常用到的規則,更多的規則說明就要請各位自行上網查詢了。

2018/07/15 補充:

安裝 Ubuntu 18.04 版本的系統,在設置關機存檔 iptables 文件時會有所不同,更改如下:

vim /etc/network/if-down.d/iptables
#!/bin/bash
iptables-save > /etc/iptables.rules

添加執行權限:

chmod +x /etc/network/if-down.d/iptables

開機啟動的設置是一樣的。

參考文件:

https://help.ubuntu.com/community/IptablesHowTo/

hosted by kinsta

Comments

在〈Webinoly 配置 iptables 防火牆〉中有 1 則留言

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

Hey, 想嘗試 Kinsta 主機嗎?

18748

Kinsta 高效能主機

wPowered Starter 方案

馬上訂購

18749
Your Mastodon Instance