ファイアウォールの設定で良く使用するコマンドをまとめました。
環境はCentOSです。
CentOS6ではファイアウォールとしてiptablesを使用していましたが、
CentOS7からはfirewalldがデフォルトで実装されました。
ただ、CentOS7でもiptablesを使用することはできます。
それに、iptablesがデフォルトで有効になっているVPSサーバなどもあるので、
今回は、firewalldとiptablesを分けてまとめていきます。
コンテンツ
firewalldを利用する場合
firewalldの起動・停止
[起動] # systemctl start firewalld
[停止] # systemctl stop firewalld
よく使用するポートの開放コマンド
[HTTP(80)] # firewall-cmd --permanent --zone=public --add-service=http
[HTTPS(443)] # firewall-cmd --permanent --zone=public --add-service=https
[FTP(21)] # firewall-cmd --permanent --zone=public --add-service=ftp
[SMTP(25)] # firewall-cmd --permanent --zone=public --add-service=smtp
[POP3(110)] # firewall-cmd --permanent --zone=public --add-service=pop3
[IMAP(143)] # firewall-cmd --permanent --zone=public --add-service=imap
[SSH(22)] # firewall-cmd --permanent --zone=public --add-service=ssh
[MySQL(3306)] # firewall-cmd --permanent --zone=public --add-service=mysql
設定を削除するコマンド
[HTTP(80)] # firewall-cmd --permanent --zone=public --remove-service=http
※「http」の部分は、必要に応じて変更して下さい。
設定の反映コマンド
設定の変更後は、必ず下記のコマンドを実行して、設定内容を反映させて下さい。
firewall-cmd --reload
iptablesを利用する場合
iptablesの起動・停止・再起動
[起動] # service iptables start
または
[起動] # systemctl start firewalld
[停止] # service iptables stop
または
[停止] # systemctl stop firewalld
[再起動] # service iptables restart
または
[再起動] # systemctl restart firewalld
よく使用するポートの開放コマンド
[HTTP(80)] # iptables -A INPUT -p tcp --dport 80 -j ACCEPT
[HTTPS(443)] # iptables -A INPUT -p tcp --dport 443 -j ACCEPT
[FTP(21)] # iptables -A INPUT -p tcp --dport 21 -j ACCEPT
[SMTP(25)] # iptables -A INPUT -p tcp --dport 25 -j ACCEPT
[POP3(110)] # iptables -A INPUT -p tcp --dport 110 -j ACCEPT
[IMAP(143)] # iptables -A INPUT -p tcp --dport 143 -j ACCEPT
[SSH(22)] # iptables -A INPUT -p tcp --dport 22 -j ACCEPT
[MySQL(3306)] # iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
設定を削除する(ポートを閉じる)コマンド
[HTTP(80)] # iptables -A INPUT -p tcp --dport 80 -j DROP
※ポート番号の部分は、必要に応じて変更して下さい。
設定内容を保存
[MySQL(3306)] # iptables-save > /etc/sysconfig/iptables
設定の変更後は、必ず内容を保存して、再起動(restart)して下さい。
この記事へのコメントはありません。