This tutorial uses the s0.d1.small server to demonstrate basic firewall configuration. The example focuses on installing iptables and managing firewall rules. These tasks do not require substantial compute resources, so the smallest instance is sufficient.
Prerequisites
A deployed s0.d1.small BMC instance running Ubuntu. Connect to your server via SSH, or use the remote console, and have a terminal open and ready.
Step 1: Install iptables
The iptables utility manages packet-filtering rules on Linux servers.
1. Install iptables with:
sudo apt install iptablesCode language: Bash (bash)
2. Install the persistent package to keep iptables firewall rules after rebooting:
sudo apt install iptables-persistentCode language: Bash (bash)
Follow any prompts displayed during installation.
Step 2: Review the Existing Firewall Rules
Inspect the current set of rules on the server:
sudo iptables -L -vCode language: Bash (bash)
Review the chains, default policies, and existing rules before making changes. Do not assume that all chains accept traffic by default.
Warning
Incorrect firewall rules can block SSH access or other required network traffic. Before changing rules, ensure you have an alternative way to access the server (such as the remote console).
Step 3: Add Firewall Rules
The syntax for appending a new rule to a chain is:
sudo iptables -A [CHAIN] [options] -j [TARGET]Code language: Bash (bash)
Common options are:
| Option | Description |
|---|---|
-A | Appends a rule to the specified chain. |
-i | Matches traffic arriving through a specified network interface, such as eth0 or lo. |
-p | Matches a protocol, such as tcp, udp, or icmp. |
-s | Matches a source IP address or network. |
--dport | Matches the destination port, such as 22 for SSH or 443 for HTTPS. |
-j | Specifies the target action, such as ACCEPT, DROP, or RETURN. |
Allow SSH Traffic
To accept SSH connections on TCP port 22:
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPTCode language: Bash (bash)
Ensure this rule is compatible with the chain's existing rules and default policy.
Allow Traffic from a Specific IP Address
To accept connections from a specified IP address:
sudo iptables -A INPUT -s [IP_ADDRESS] -j ACCEPTCode language: Bash (bash)
Replace [IP_ADDRESS] with the address you want to allow.
Verify the Rules
List the rules:
sudo iptables -L -vCode language: Bash (bash)
Confirm the new rules appear in the expected chains.
Step 4: Save the Firewall Rules
To persist the current rules across reboots, use the netfilter-persistent utility:
sudo netfilter-persistent saveCode language: Bash (bash)
The command saves the current rules using the iptables-persistent package utility.
Step 6: Delete Firewall Rules
You can remove rules when they are no longer needed.
Flush a Chain
To remove all rules from all chains in the current table, run:
sudo iptables -FCode language: Bash (bash)
The command removes rules not it does not reset a chain's default policies. Review the effects before running it on a production server.
Delete a Specific Rule
1. List rules with their line numbers:
sudo iptables -L --line-numbersCode language: Bash (bash)
2. Delete a rule:
sudo iptables -D INPUT [RULE_LINE_NUMBER]Code language: Bash (bash)
After deleting rules, review the configuration and save the updated rules to persist changes across reboots.