How to configure a firewall in CentOS?

To configure the firewall in CentOS, you can use either iptables or firewalld. Below are the steps for configuring the firewall using firewalld.

  1. Install firewalld (if not already installed):
sudo yum install firewalld
  1. Start the firewalld service and set it to start at boot.
sudo systemctl start firewalld
sudo systemctl enable firewalld
  1. View the default firewall rules.
sudo firewall-cmd --list-all
  1. Add rules for allowing or denying access.
sudo firewall-cmd --zone=public --add-port=80/tcp --permanent  # 允许访问80端口
sudo firewall-cmd --zone=public --add-service=http --permanent   # 允许HTTP服务
sudo firewall-cmd --zone=public --remove-port=22/tcp --permanent  # 拒绝访问22端口
sudo firewall-cmd --reload  # 重新加载防火墙规则
  1. View the rules that have been added:
sudo firewall-cmd --zone=public --list-all
  1. Restart the firewalld service to apply the settings.
sudo systemctl restart firewalld

This completes the process of configuring the firewall in CentOS. You can add more rules as needed to protect your system.

Leave a Reply 0

Your email address will not be published. Required fields are marked *