ubuntu iptables firewall guide

This article is automatically synchronized by Tencent Cloud + Community, the original address is https://stackoverflow.club/article/iptables_in_ubuntu/

Introduction to iptables#

Netfilter/iptables is integrated with the kernel, and there is no such thing as start/stop or disable. You can use the iptables command to create filter rules. (Now the newer kernel is integrated by default, no need to install separately)

And ufw is a tool for simplifying iptables configuration on ubuntu. It defines a series of rules and adds them to iptables. So when ufw is enabled, you can see a series of ufw type words in iptables rules. These specific rules defined by ufw are under /etc/ufw/*.rules.

The following is suitable for Ubuntu 16.04.5 LTS

Basic commands#

View help:

iptables -h

View filter rules##

sudo iptables -vnL --line-numbers

The same chain is matched from top to bottom, so num is very important;

Create a new iptables rule without affecting existing connections;

Save the created rules to file##

iptables-save >/etc/iptables.up.rules

Recovering rules from files##

/sbin/iptables-restore </etc/iptables.up.rules

You can also add the last command to /etc/rc.local to restore the rules when the system restarts

Full command rules#

iptables [-t table] command chain [match][-j target]

The following is an explanation of the main parameters of each command.

- t table, table has four options, the default is filter:

command, defines how to write rules:

chain, netfilter can filter in five positions:

match: Matching rules. The commonly used rules are as follows, and multiple matching rules can be used in parallel:

target: the operation/response performed, the following are common:

Practical example#

Open a tcp port##

sudo iptables -A INPUT -p tcp --dport 22-j ACCEPT

Delete a rule##

Rule 1 is deleted here

sudo iptables -t filter -D FORWARD 1

Add a nat record##

sudo iptables -t nat -A POSTROUTING -s 192.168.255.0/24-j MASQUERADE

Save and restore iptables

sudo iptables-save >./iptables.bak
sudo iptables-restore <./iptables.bak

References#

Well-written blog, suitable for getting started

Suitable for in-depth after getting started

Recommended Posts

ubuntu iptables firewall guide
Ubuntu install guide
ubuntu beautification guide
Use iptables under ubuntu
CentOS7 install iptables firewall
ufw of ubuntu firewall rules
Ubuntu 20.04 best configuration guide for engineers
Ubuntu20.04 firewall setting simple tutorial (Xiaobai)
Ubuntu18.04 installation Anaconda3 and VSCode guide
Ubuntu 20.04 best configuration guide (required for newbies)