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 is intended for use on the Ubuntu operating system. For Debian, a simple command is used.
CentOS [Fedora]
Setting
After installing the utility, we will proceed to its detailed configuration.
Arguments
-A - add a rule to the section.
-C - check all the rules.
-D - Delete the rule.
-I - insert the rule with the required number.
-L - print all the rules in the current section.
-S - output all rules.
-F - clear all rules.
-N - Create a partition.
-X - Remove the partition.
-P - set the default action.
-p - install the protocol.
-s - specify the address of the sender.
-d - specify the recipient address.
-i is the input network interface.
-o is the outgoing network interface.
-j - follow the rule.
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.
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.
Opening port(s)
First, let’s check our list of rules:
![](https://wiki.spacecore.pro/~gitbook/image?url=https%3A%2F%2F287241268-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FyXvdNIxNLIOcexv7AfU2%252Fuploads%252F59PomOErUUFQTq8ttTlN%252Fimage.png%3Falt%3Dmedia%26token%3Dc0ceef8c-ae67-4c26-a13d-78c4b8281198&width=768&dpr=4&quality=100&sign=de9995ce&sv=1)
Let’s try to open oneTCP-порт 80 for входящих соединений:
Let’s check the list again...
![](https://wiki.spacecore.pro/~gitbook/image?url=https%3A%2F%2F287241268-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FyXvdNIxNLIOcexv7AfU2%252Fuploads%252Ff3O4tN3fkEquxHpUE29L%252Fimage.png%3Falt%3Dmedia%26token%3D984cec57-d19b-45e8-bd59-e8ee3788e1fb&width=768&dpr=4&quality=100&sign=6523688a&sv=1)
Now let’s try to open the UDP port range from 25565 to 25570 for outgoing connections:
Let’s check the result.
![](https://wiki.spacecore.pro/~gitbook/image?url=https%3A%2F%2F287241268-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FyXvdNIxNLIOcexv7AfU2%252Fuploads%252F9ftGzkzYEto5CYTZTgWa%252Fimage.png%3Falt%3Dmedia%26token%3D8301983b-1068-4f6b-952f-02ee436fb9c6&width=768&dpr=4&quality=100&sign=1a1c7b91&sv=1)
Want to close all inbound connections for TCP 250? No problem.
![](https://wiki.spacecore.pro/~gitbook/image?url=https%3A%2F%2F287241268-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FyXvdNIxNLIOcexv7AfU2%252Fuploads%252FpeOx3IB0WpNMbnua8cOX%252Fimage.png%3Falt%3Dmedia%26token%3D5d2cd8a1-abaf-414d-99e8-44d16b0bbbb3&width=768&dpr=4&quality=100&sign=a0c947f9&sv=1)
Rule removal
Now try to remove the rule that allows inbound connections for TCP 80:
![](https://wiki.spacecore.pro/~gitbook/image?url=https%3A%2F%2F287241268-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FyXvdNIxNLIOcexv7AfU2%252Fuploads%252FJF7QoF80yUq5cfx1d7WI%252Fimage.png%3Falt%3Dmedia%26token%3D1537c4af-0c09-4e85-9ef2-84405d7e11d5&width=768&dpr=4&quality=100&sign=c445c7df&sv=1)
Deletion of all rules
To do this, use the command
![](https://wiki.spacecore.pro/~gitbook/image?url=https%3A%2F%2F287241268-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FyXvdNIxNLIOcexv7AfU2%252Fuploads%252F6BYNFIPS5KH47ojqV86e%252Fimage.png%3Falt%3Dmedia%26token%3Deb44d2c5-e1e1-4957-9fbc-176ad32c00ff&width=768&dpr=4&quality=100&sign=2fbd36fd&sv=1)
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.
![](https://wiki.spacecore.pro/~gitbook/image?url=https%3A%2F%2F287241268-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FyXvdNIxNLIOcexv7AfU2%252Fuploads%252FIcCRdTYIXWbkshqxFafz%252Fimage.png%3Falt%3Dmedia%26token%3Dad80fc3c-926d-4637-b907-627fc2ea1c12&width=768&dpr=4&quality=100&sign=6c8cbd21&sv=1)
It worked. The rules are saved and will be active even after restarting our server!
Last updated