Home > Operation and Maintenance > Linux Operation and Maintenance > How to configure a firewall in Linux

How to configure a firewall in Linux

WBOY
Release: 2023-06-18 21:37:38
Original
6097 people have browsed it

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.

  1. Check the firewall status
    Before starting the configuration, please check the firewall status in the system. You can check if iptables is enabled using the following command:
sudo systemctl status iptables
Copy after login

If iptables is not enabled, please enable it using the following command:

sudo systemctl start iptables
Copy after login
  1. Configure iptables firewall
    iptables is A rule-based firewall that filters and forwards packets to protect the system. You can set iptables rules in Linux using the following command:
sudo iptables -A INPUT -p tcp --dport <端口号> -j ACCEPT
Copy after login

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
Copy after login

This command will block all packets from the specified IP address.

  1. Configuring firewalld firewall
    firewalld is an advanced firewall designed to protect modern Linux systems. It enables dynamic rules to adapt to changes in the network environment. To configure firewalld, use the following command:
sudo systemctl start firewalld
Copy after login

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
Copy after login

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
Copy after login

This command will block all packets from the specified IP address.

  1. Saving and Applying Rules
    After setting up iptables or firewalld rules, you need to save and apply them to make them effective. To save iptables rules, use the following command:
sudo service iptables save
Copy after login

For firewalld rules, use the following command:

sudo firewall-cmd --reload
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template