How do you configure firewall rules in Linux?
In Linux systems, you can use the iptables command to configure firewall rules. Here are some common examples of iptables commands:
- Viewing the current firewall rules:
iptables -L
- Grant access to a specific IP address to access a certain port.
iptables -A INPUT -s <IP地址> -p tcp --dport <端口号> -j ACCEPT
- Allow all IP addresses to access a specific port.
iptables -A INPUT -p tcp --dport <端口号> -j ACCEPT
- Block access to a specific IP address on a certain port.
iptables -A INPUT -s <IP地址> -p tcp --dport <端口号> -j DROP
- Allow a certain IP address range to access a specific port.
iptables -A INPUT -s <起始IP地址/结束IP地址> -p tcp --dport <端口号> -j ACCEPT
- Allow packets to pass through a certain port without logging.
iptables -A INPUT -p tcp --dport <端口号> -j ACCEPT -m limit --limit 0/min
- Clear all firewall rules.
iptables -F
- Block all traffic.
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP
Please make sure to be cautious when configuring firewall rules to avoid any errors that may cause the system to lose network access. It is recommended to backup the current rules before making any changes, so that they can be restored in case of any issues.