# Configure IPTables

This is a subsystem for working with network packages, which passes through its filter all connections on the server. Let’s take a closer look at the IPTables configuration.

## General information

**IPTables** is already built into the main **Linux** kernel by default, but the tools for working with it in many distributions are not available by default, so let’s use the command to install the utility.

### Debian / Ubuntu

```
[sudo] apt install iptables
```

{% hint style="info" %}
**Sudo** is intended for use on the **Ubuntu** operating system. For **Debian**, a simple command is used.
{% endhint %}

### CentOS \[Fedora]

```
sudo yum install iptables
```

## Setting

After installing the utility, we will proceed to its detailed configuration.

## Arguments

{% hint style="info" %}
**-A** - add a rule to the section.&#x20;

**-C** - check all the rules.&#x20;

**-D** - Delete the rule.&#x20;

**-I** - insert the rule with the required number.&#x20;

**-L** - print all the rules in the current section.&#x20;

**-S** - output all rules.&#x20;

**-F** - clear all rules.&#x20;

**-N** - Create a partition.&#x20;

**-X** - Remove the partition.&#x20;

**-P** - set the default action.&#x20;

**-p** - install the protocol.&#x20;

**-s** - specify the address of the sender.

**-d** - specify the recipient address.&#x20;

**-i** is the input network interface.&#x20;

**-o** is the outgoing network interface.&#x20;

**-j** - follow the rule.
{% endhint %}

{% hint style="info" %}
**INPUT** —is responsible for handling incoming packets and connections.

**FORWARD** —is used for passing connections. This is where the corresponding packets come in, which are sent to your server, but do not define it as the purpose of delivery.

**OUTPUT** — completely opposite to the first. Used for outgoing packets and connections.
{% endhint %}

{% hint style="info" %}
**ACCEPT** — skip package.

**DROP** —remove package.

**REJECT** — reject the packet.

**LOG** — make a log file of the appropriate package.

**QUEUE** — send the packet to the user’s application.
{% endhint %}

## Opening port(s)

First, let’s check our list of rules:

```
iptables -L
```

<figure><img src="https://287241268-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyXvdNIxNLIOcexv7AfU2%2Fuploads%2F59PomOErUUFQTq8ttTlN%2Fimage.png?alt=media&#x26;token=c0ceef8c-ae67-4c26-a13d-78c4b8281198" alt=""><figcaption></figcaption></figure>

Let’s try to open one**TCP-порт** **80** for **входящих соединений**:

```
iptables -t filter -A INPUT -p tcp --dport 80 -j ACCEPT
```

Let’s check the list again...

<figure><img src="https://287241268-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyXvdNIxNLIOcexv7AfU2%2Fuploads%2Ff3O4tN3fkEquxHpUE29L%2Fimage.png?alt=media&#x26;token=984cec57-d19b-45e8-bd59-e8ee3788e1fb" alt=""><figcaption></figcaption></figure>

Now let’s try to open the **UDP** port range from **25565 to 25570** for outgoing connections:

```
iptables -t filter -A OUTPUT -p udp --dport 25565:25570 -j ACCEPT
```

Let’s check the result.

<figure><img src="https://287241268-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyXvdNIxNLIOcexv7AfU2%2Fuploads%2F9ftGzkzYEto5CYTZTgWa%2Fimage.png?alt=media&#x26;token=8301983b-1068-4f6b-952f-02ee436fb9c6" alt=""><figcaption></figcaption></figure>

Want to close **all inbound connections for TCP 250**? No problem.

```
iptables -t filter -A INPUT -p tcp --dport 250 -m state --state ESTABLISHED -j DROP
```

<figure><img src="https://287241268-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyXvdNIxNLIOcexv7AfU2%2Fuploads%2FpeOx3IB0WpNMbnua8cOX%2Fimage.png?alt=media&#x26;token=5d2cd8a1-abaf-414d-99e8-44d16b0bbbb3" alt=""><figcaption></figcaption></figure>

## Rule removal

Now try to remove the rule that allows inbound connections for **TCP 80**:

```
iptables -t filter -D INPUT -p tcp --dport 80 -j ACCEPT
```

<figure><img src="https://287241268-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyXvdNIxNLIOcexv7AfU2%2Fuploads%2FJF7QoF80yUq5cfx1d7WI%2Fimage.png?alt=media&#x26;token=1537c4af-0c09-4e85-9ef2-84405d7e11d5" alt=""><figcaption></figcaption></figure>

## Deletion of all rules

To do this, use the command

```
iptables -F
```

<figure><img src="https://287241268-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyXvdNIxNLIOcexv7AfU2%2Fuploads%2F6BYNFIPS5KH47ojqV86e%2Fimage.png?alt=media&#x26;token=eb44d2c5-e1e1-4957-9fbc-176ad32c00ff" alt=""><figcaption></figcaption></figure>

## Preservation of established rules

By default, all the rules that have been created are applied until the next reboot and will be deleted during it. To avoid this, let’s save the **IPTables** rules that we created. To do this, use the appropriate command.

```
iptables-save
```

<figure><img src="https://287241268-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyXvdNIxNLIOcexv7AfU2%2Fuploads%2FIcCRdTYIXWbkshqxFafz%2Fimage.png?alt=media&#x26;token=ad80fc3c-926d-4637-b907-627fc2ea1c12" alt=""><figcaption></figcaption></figure>

It worked. The rules are saved and will be active even after restarting our server!
