Configure Linux Firewall Whitelist
One way to implement a whitelist function in a Linux system is by configuring firewall rules.
- Firstly, make sure that a firewall software such as iptables or firewalld is installed. If not, you can install using the following command:
- Regarding iptables:
- Please install iptables using the sudo apt-get command.
- For firewalld:
- Please use the command “sudo apt-get install firewalld”
- Next, create a new firewall rule file, such as whitelist.rules. You can use the following command to create and open the file:
- Open the file whitelist.rules in the directory /etc/ using the text editor nano with superuser privileges.
- In the whitelist.rules file, include the IP addresses or IP address ranges that need to be allowed through the firewall. Each IP address or range should be on a separate line and follow the syntax:
- Allow incoming traffic from a specific IP address or range of IP addresses.
- For example, to allow a host with an IP address of 192.168.1.100 to pass through the firewall, you can add the following rule:
- Allow traffic from source IP address 192.168.1.100.
- To allow hosts with IP address range 192.168.1.0/24 to pass through the firewall, you can add the following rule:
- Allow input from 192.168.1.0/24 to pass through
- After adding all the necessary IP addresses or IP address ranges that need to be allowed, save and close the file.
- Next, apply the rules from the whitelist.rules file to the firewall. For iptables, you can use the following command:
- Please reload the whitelist rules from the specified file using the command sudo iptables-restore.
- For firewalld, the following commands can be used:
- Please reload the firewall using sudo firewall-cmd.
- After completing the steps above, the firewall will only allow IP addresses or ranges of IP addresses listed in the whitelist to pass through. All other IP addresses will be blocked.
It is important to note that the above methods are only applicable to common firewall software such as iptables and firewalld. Different configuration methods may be required for other specific firewall software.