If you’re a Linux user, then chances are your system is already using iptables to control your server’s firewall. For those who are new to the term, iptables is a user-space utility program that gives administrators the ability to modify the IP filtering rules used by the Linux kernel firewall. In other words, it’s a system of rules that tells your server what it can and can’t access online. By editing your system’s iptable rules, you can control incoming and outgoing information by allowing or limiting access to specific ports or IP addresses through your firewall.
In this guide, we’ll cover the basics of how to edit your Linux server’s iptable rules using the command line.
*Note: iptables requires users to have admin access. When modifying iptables rules, be sure to execute your commands as the root user.
Modifying Iptables Rules
Before you begin making changes to your iptables rules, you’ll first need to know the commands to stop and restart iptables. The commands are as follows:
To stop iptables, use the command:
service iptables stop
To start iptables, use the command:
service iptables start
Now that you know how to start and stop iptables as needed, you can begin modifying your system’s rules to your specific needs.
Allowing/Denying Specific IP Addresses in Iptables
To modify which IP addresses are able to connect to your server, just follow these steps:
- First, you’ll need to stop iptables using the service iptables stop command.
- Next, to allow a specific IP address, use the command:
iptables -A INPUT -s 22.214.171.124 -j ACCEPT
*Note: you will need to replace the “126.96.36.199” listed in these examples with the specific IP address you are attempting to allow/deny access to.
- If you are attempting to allow an IP address that is on the list of banned IPs, you can remove the rule banning the IP address by using the command:
iptables -D INPUT -s 188.8.131.52 -j DROP
By using “-D” instead of “-A”, you will delete the rule, allowing access to this specific IP address.
- Alternatively, if you are trying to deny a specific IP address from accessing your server, use the command:
iptables -A INPUT -s 184.108.40.206 -j DROP
- Once you’ve added or denied all necessary IPs, save your changes using the following command:
service iptables save
- Lastly, you must restart iptables using the service iptables start command.
Allowing/Denying Specific Ports within Iptables
In addition to determining which IP addresses can access or be accessed by your server, you can also use iptables rules to determine which ports your server is accessible on.
*Note: remember to use the stop, save, and start commands before and after to ensure that your changes are accepted and saved.
For example, if you’d like to be able to brows the web and access websites that communicate via port 80, you can append the following rules to allow access to port 80 on your server.
iptables -A INPUT -p tcp -m tcp –sport 80 -j ACCEPT
iptables -A OUTPUT -p tcp -m tcp –dport 80 -j ACCEPT
If you’d also like to allow access to secure websites, those listed as HTTPS, you must open port 443 in addition to port 80. The command to do so looks like this:
iptables -A INPUT -p tcp -m tcp –sport 443 -j ACCEPT
iptables -A OUTPUT -p tcp -m tcp –dport 443 -j ACCEPT
*Note: If you wanted to deny access to either of these ports, you can repeat this same command and change the final term from ACCEPT to DROP.
Another trick that might prove useful to you, depending on your needs, is the ability to allow remote SSH access by appending the rules to port 22. This can be done using the following rules:
iptables -A INPUT -p tcp –dport 22 -j ACCEPT
iptables -A OUTPUT -p tcp –sport 22 -j ACCEPT
And there you have! You now know several basic iptables commands and can allow or deny ports and IP addresses as needed. By keeping your iptables up to date, you can strengthen your server’s firewall and keep it secure from unwanted access.
Need More Personalized Help?
If you have any further issues, questions, or would like some assistance checking on this or anything else, please reach out to us from your my.hivelocity.net account -> Support and provide your server credentials within the encrypted field for the best possible security and support.
If you are unable to reach your my.hivelocity.net account or if you are on the go, please reach out from your valid my.hivelocity.net account email to us here at: firstname.lastname@example.org. We are also available to you through our phone and live chat system 24/7/365.