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.
- Install firewalld (if not already installed):
sudo yum install firewalld
- Start the firewalld service and set it to start at boot.
sudo systemctl start firewalld
sudo systemctl enable firewalld
- View the default firewall rules.
sudo firewall-cmd --list-all
- 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 # 重新加载防火墙规则
- View the rules that have been added:
sudo firewall-cmd --zone=public --list-all
- 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.