<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://serverrental.store/index.php?action=history&amp;feed=atom&amp;title=Setting_Up_iptables_Firewall</id>
	<title>Setting Up iptables Firewall - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://serverrental.store/index.php?action=history&amp;feed=atom&amp;title=Setting_Up_iptables_Firewall"/>
	<link rel="alternate" type="text/html" href="https://serverrental.store/index.php?title=Setting_Up_iptables_Firewall&amp;action=history"/>
	<updated>2026-04-14T21:48:24Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.36.1</generator>
	<entry>
		<id>https://serverrental.store/index.php?title=Setting_Up_iptables_Firewall&amp;diff=5792&amp;oldid=prev</id>
		<title>Admin: New server guide</title>
		<link rel="alternate" type="text/html" href="https://serverrental.store/index.php?title=Setting_Up_iptables_Firewall&amp;diff=5792&amp;oldid=prev"/>
		<updated>2026-04-13T10:00:58Z</updated>

		<summary type="html">&lt;p&gt;New server guide&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Setting Up iptables Firewall ==&lt;br /&gt;
&lt;br /&gt;
This guide provides a comprehensive overview of setting up and managing the iptables firewall on Linux systems. iptables is a powerful command-line utility that allows you to configure the Linux kernel firewall. It enables you to define rules for packet filtering, network address translation (NAT), and more, acting as a crucial layer of security for your server.&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
&lt;br /&gt;
Before you begin, ensure you have the following:&lt;br /&gt;
&lt;br /&gt;
*   A Linux server (e.g., Ubuntu, Debian, CentOS).&lt;br /&gt;
*   Root or sudo privileges on the server.&lt;br /&gt;
*   Basic understanding of networking concepts (IP addresses, ports, protocols).&lt;br /&gt;
*   SSH access to your server.&lt;br /&gt;
&lt;br /&gt;
== Understanding iptables Basics ==&lt;br /&gt;
&lt;br /&gt;
iptables operates using a set of ''tables'', each containing ''chains'' of ''rules''.&lt;br /&gt;
&lt;br /&gt;
*   '''Tables''': The most common tables are:&lt;br /&gt;
** '''filter''': Used for packet filtering (allowing or denying traffic). This is the default table.&lt;br /&gt;
** '''nat''': Used for Network Address Translation (e.g., port forwarding, masquerading).&lt;br /&gt;
** '''mangle''': Used for altering packet headers (e.g., Quality of Service).&lt;br /&gt;
*   '''Chains''': These are sequences of rules that packets traverse. Common built-in chains are:&lt;br /&gt;
** '''INPUT''': For packets destined for the local system.&lt;br /&gt;
** '''OUTPUT''': For packets originating from the local system.&lt;br /&gt;
** '''FORWARD''': For packets that are routed through the system (not destined for it).&lt;br /&gt;
*   '''Rules''': Each rule specifies criteria for matching packets and an action to take (e.g., ACCEPT, DROP, REJECT).&lt;br /&gt;
&lt;br /&gt;
== Basic Firewall Configuration (Filter Table) ==&lt;br /&gt;
&lt;br /&gt;
The ''filter'' table is where you'll define most of your security policies.&lt;br /&gt;
&lt;br /&gt;
=== Default Policies ===&lt;br /&gt;
&lt;br /&gt;
It's good practice to set default policies that are restrictive, then explicitly allow what you need.&lt;br /&gt;
&lt;br /&gt;
# Set the default policy for the INPUT chain to DROP (block all incoming traffic by default).&lt;br /&gt;
&amp;lt;pre&amp;gt;sudo iptables -P INPUT DROP&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Set the default policy for the FORWARD chain to DROP (if your server acts as a router, otherwise DROP is fine).&lt;br /&gt;
&amp;lt;pre&amp;gt;sudo iptables -P FORWARD DROP&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Set the default policy for the OUTPUT chain to ACCEPT (allow all outgoing traffic by default).&lt;br /&gt;
&amp;lt;pre&amp;gt;sudo iptables -P OUTPUT ACCEPT&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Allowing Essential Traffic ===&lt;br /&gt;
&lt;br /&gt;
Now, we need to allow specific traffic that is essential for your server to function.&lt;br /&gt;
&lt;br /&gt;
# Allow traffic on the loopback interface (localhost). This is crucial for many local services.&lt;br /&gt;
&amp;lt;pre&amp;gt;sudo iptables -A INPUT -i lo -j ACCEPT&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Allow established and related connections. This is vital for return traffic from outgoing connections.&lt;br /&gt;
&amp;lt;pre&amp;gt;sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Allow SSH (port 22) to enable remote access. Replace 22 with your SSH port if you've changed it.&lt;br /&gt;
&amp;lt;pre&amp;gt;sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Allow HTTP (port 80) for web servers.&lt;br /&gt;
&amp;lt;pre&amp;gt;sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Allow HTTPS (port 443) for secure web servers.&lt;br /&gt;
&amp;lt;pre&amp;gt;sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Viewing iptables Rules ===&lt;br /&gt;
&lt;br /&gt;
To see the rules you've added, use the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;sudo iptables -L -v -n&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*   `-L`: List rules.&lt;br /&gt;
*   `-v`: Verbose output (shows interface, packet/byte counts).&lt;br /&gt;
*   `-n`: Numeric output (shows IP addresses and port numbers instead of resolving them).&lt;br /&gt;
&lt;br /&gt;
== Network Address Translation (NAT) ==&lt;br /&gt;
&lt;br /&gt;
NAT is used to modify network address information in packet headers. A common use case is masquerading, where your server acts as a gateway for a private network, giving it internet access.&lt;br /&gt;
&lt;br /&gt;
=== Enabling IP Forwarding ===&lt;br /&gt;
&lt;br /&gt;
For NAT to work, you need to enable IP forwarding in the kernel.&lt;br /&gt;
&lt;br /&gt;
1.  Edit the sysctl configuration file:&lt;br /&gt;
    &amp;lt;pre&amp;gt;sudo nano /etc/sysctl.conf&amp;lt;/pre&amp;gt;&lt;br /&gt;
2.  Uncomment or add the following line:&lt;br /&gt;
    &amp;lt;pre&amp;gt;net.ipv4.ip_forward=1&amp;lt;/pre&amp;gt;&lt;br /&gt;
3.  Save and close the file.&lt;br /&gt;
4.  Apply the changes immediately:&lt;br /&gt;
    &amp;lt;pre&amp;gt;sudo sysctl -p&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Setting Up Masquerading ===&lt;br /&gt;
&lt;br /&gt;
This rule allows traffic from a private network (e.g., 192.168.1.0/24) to access the internet through your server's public IP address. Replace `eth0` with your server's public network interface.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This command tells iptables to masquerade (hide) the source IP address of packets leaving the `eth0` interface with the IP address of `eth0` itself.&lt;br /&gt;
&lt;br /&gt;
== Port Forwarding ==&lt;br /&gt;
&lt;br /&gt;
Port forwarding allows you to direct incoming traffic on a specific port on your server to a different IP address and/or port on your internal network.&lt;br /&gt;
&lt;br /&gt;
For example, to forward traffic from your server's port 8080 to an internal web server at 192.168.1.100 on port 80:&lt;br /&gt;
&lt;br /&gt;
1.  First, ensure that the FORWARD chain is configured to accept the traffic. If your default FORWARD policy is DROP, you'll need to allow it:&lt;br /&gt;
    &amp;lt;pre&amp;gt;sudo iptables -A FORWARD -p tcp --dport 80 -d 192.168.1.100 -j ACCEPT&amp;lt;/pre&amp;gt;&lt;br /&gt;
    (Adjust the destination IP and port as needed).&lt;br /&gt;
&lt;br /&gt;
2.  Then, add the NAT rule to redirect the traffic.&lt;br /&gt;
    &amp;lt;pre&amp;gt;sudo iptables -t nat -A PREROUTING -p tcp --dport 8080 -j DNAT --to-destination 192.168.1.100:80&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This rule tells iptables: &amp;quot;When a TCP packet arrives destined for port 8080 on this server, change its destination to 192.168.1.100 on port 80.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Saving iptables Rules ==&lt;br /&gt;
&lt;br /&gt;
iptables rules are volatile and will be lost upon reboot unless saved. The method for saving rules varies by distribution.&lt;br /&gt;
&lt;br /&gt;
=== For Debian/Ubuntu ===&lt;br /&gt;
&lt;br /&gt;
1.  Install the `iptables-persistent` package:&lt;br /&gt;
    &amp;lt;pre&amp;gt;sudo apt-get update&amp;lt;/pre&amp;gt;&lt;br /&gt;
    &amp;lt;pre&amp;gt;sudo apt-get install iptables-persistent&amp;lt;/pre&amp;gt;&lt;br /&gt;
    During installation, you'll be prompted to save the current IPv4 and IPv6 rules.&lt;br /&gt;
&lt;br /&gt;
2.  To save rules manually after making changes:&lt;br /&gt;
    &amp;lt;pre&amp;gt;sudo netfilter-persistent save&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== For CentOS/RHEL (using `iptables-services`) ===&lt;br /&gt;
&lt;br /&gt;
1.  Install `iptables-services`:&lt;br /&gt;
    &amp;lt;pre&amp;gt;sudo yum install iptables-services&amp;lt;/pre&amp;gt;&lt;br /&gt;
    Or for newer systems:&lt;br /&gt;
    &amp;lt;pre&amp;gt;sudo dnf install iptables-services&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
2.  Enable and start the service:&lt;br /&gt;
    &amp;lt;pre&amp;gt;sudo systemctl enable iptables&amp;lt;/pre&amp;gt;&lt;br /&gt;
    &amp;lt;pre&amp;gt;sudo systemctl start iptables&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
3.  Save the current rules:&lt;br /&gt;
    &amp;lt;pre&amp;gt;sudo service iptables save&amp;lt;/pre&amp;gt;&lt;br /&gt;
    Or:&lt;br /&gt;
    &amp;lt;pre&amp;gt;sudo iptables-save &amp;gt; /etc/sysconfig/iptables&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Advanced Techniques and Considerations ==&lt;br /&gt;
&lt;br /&gt;
*   '''Rate Limiting''': Protect against brute-force attacks by limiting the rate of incoming connections.&lt;br /&gt;
    &amp;lt;pre&amp;gt;sudo iptables -A INPUT -p tcp --syn --dport 22 -m connlimit --connlimit-above 5 -j DROP&amp;lt;/pre&amp;gt;&lt;br /&gt;
    This example drops connections if more than 5 new connections per minute are attempted to port 22.&lt;br /&gt;
&lt;br /&gt;
*   '''Stateful Firewall''': The `conntrack` module (used in `ESTABLISHED,RELATED`) makes iptables stateful, meaning it tracks the state of network connections.&lt;br /&gt;
&lt;br /&gt;
*   '''IPv6 Firewall''': Remember to configure `ip6tables` for IPv6 traffic if your server uses it. The syntax is very similar to `iptables`.&lt;br /&gt;
&lt;br /&gt;
*   '''GPU Servers''': For demanding applications like machine learning inference or training, consider dedicated GPU servers. You can find cost-effective options at [https://en.immers.cloud/signup/r/20241007-8310688-334/ Immers Cloud], with pricing starting from $0.23/hr for inference to $4.74/hr for H200 GPUs.&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
*   '''Can't connect to SSH''':&lt;br /&gt;
    *   Double-check that the SSH port (default 22) is explicitly allowed in your INPUT chain.&lt;br /&gt;
    *   Ensure your default INPUT policy is not DROP or that you have an ACCEPT rule for your IP address.&lt;br /&gt;
    *   Verify that `iptables-persistent` or `iptables-services` is configured to load rules on boot.&lt;br /&gt;
&lt;br /&gt;
*   '''Website not accessible''':&lt;br /&gt;
    *   Confirm that ports 80 and 443 are open in the INPUT chain.&lt;br /&gt;
    *   If using NAT/port forwarding, ensure the FORWARD chain is configured correctly and the NAT rule is present and correct.&lt;br /&gt;
&lt;br /&gt;
*   '''iptables commands not found''':&lt;br /&gt;
    *   Ensure the `iptables` package is installed on your system.&lt;br /&gt;
    *   For persistent saving, ensure `iptables-persistent` (Debian/Ubuntu) or `iptables-services` (CentOS/RHEL) is installed and configured.&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
*   [[Firewall Configuration]]&lt;br /&gt;
*   [[SSH Security Best Practices]]&lt;br /&gt;
*   [[Basic Linux Networking]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Security]]&lt;br /&gt;
[[Category:Networking]]&lt;br /&gt;
[[Category:Firewall]]&lt;/div&gt;</summary>
		<author><name>Admin</name></author>
	</entry>
</feed>