Firewall is an important part of protecting network security. It can filter out data packets from untrusted sources and malicious attacks in the network to protect the system. In Linux, common firewalls are iptables and firewalld. In this article, we will cover the steps on how to configure a firewall in Linux.
sudo systemctl status iptables
If iptables is not enabled, please enable it using the following command:
sudo systemctl start iptables
sudo iptables -A INPUT -p tcp --dport <端口号> -j ACCEPT
This command will allow TCP connections through the specified port number. You can also set up iptables with other rules, for example:
sudo iptables -A INPUT -s <IP地址> -j DROP
This command will block all packets from the specified IP address.
sudo systemctl start firewalld
You can then configure firewalld rules using the following command:
sudo firewall-cmd --zone=public --add-port=<端口号>/tcp --permanent sudo firewall-cmd --zone=public --remove-port=<端口号>/tcp --permanent
This will allow or disallow TCP connections through the specified port number. firewalld also supports other rules, such as:
sudo firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="<IP地址>" reject' --permanent
This command will block all packets from the specified IP address.
sudo service iptables save
For firewalld rules, use the following command:
sudo firewall-cmd --reload
This command will reload the firewalld configuration, and apply the new rules.
Summary
This article introduces how to configure a firewall in a Linux system. You can use iptables or firewalld to filter network traffic and protect your system. After configuring the rules, save and apply them to take effect.
The above is the detailed content of How to configure a firewall in Linux. For more information, please follow other related articles on the PHP Chinese website!