Basic iptables settings in Linux

Source: Internet
Author: User
Tags ftp connection

Iptable is a built-in firewall for Linux. Many network service providers need to open the corresponding ports for VPN, web, and FTP connection. For example, to install lnmp firewall in centos, port 80 must be enabled.
In Linux.

Basic iptables knowledge

1. view the settings of iptables on the local machine.
[Root @ TP ~] # Iptables-l-N
Chain input (Policy accept)
Target prot opt source destination
Chain forward (Policy accept)
Target prot opt source destination
Chain output (Policy accept)
Target prot opt source destination
Chain RH-Firewall-1-INPUT (0 Records)
Target prot opt source destination
Accept all-0.0.0.0/0 0.0.0.0/0
Accept ICMP-0.0.0.0/0 0.0.0.0/0 ICMP Type 255
Accept ESP-0.0.0.0/0 0.0.0.0/0
Accept ah-0.0.0.0/0 0.0.0.0/0
Accept UDP-0.0.0.0/0 224.0.0.20.udp DPT: 5353
Accept UDP-0.0.0.0/0 0.0.0.0/0 uddpt: 631
Accept all-0.0.0.0/0 0.0.0.0/0 state related, established
Accept TCP-0.0.0.0/0 0.0.0.0/0 state New tcp dpt: 22
Accept TCP-0.0.0.0/0 0.0.0.0/0 state New tcp dpt: 80
Accept TCP-0.0.0.0/0 0.0.0.0/0 state New tcp dpt: 25
Reject all-0.0.0.0/0 0.0.0.0/0 reject-with ICMP-host-prohibited
We can see that when I installed Linux, I chose to have a firewall and opened ports 22, 80, and 25.
If you do not choose to start the firewall when installing Linux
[Root @ TP ~] # Iptables-l-N
Chain input (Policy accept)
Target prot opt source destination
Chain forward (Policy accept)
Target prot opt source destination
Chain output (Policy accept)
Target prot opt source destination
There are no rules.

2. Clear the original rules.
Whether or not you have enabled the firewall when installing Linux, if you want to configure your own firewall, clear all the filter rules.
[Root @ TP ~] # Iptables-F clear the rules of all rule chains in the filter of the preset table
[Root @ TP ~] # Iptables-x clear the rules in the User-Defined chain in the filter of the preset table
Let's take a look.
[Root @ TP ~] # Iptables-l-N
Chain input (Policy accept)
Target prot opt source destination
Chain forward (Policy accept)
Target prot opt source destination
Chain output (Policy accept)
Target prot opt source destination
Nothing, just like we didn't start the firewall when installing Linux. (say in advance, these configurations are the same as configuring IP addresses with commands, so restarting them will lose effect.) How to save them.
[Root @ TP ~] #/Etc/rc. d/init. d/iptables save
In this way, you can write it to the/etc/sysconfig/iptables file. Remember to repeat the firewall after writing it to make it take effect.
[Root @ TP ~] # Service iptables restart
Now, no configuration is available in the iptables configuration table. Let's start with our configuration.
3. Set preset rules
[Root @ TP ~] # Iptables-P input drop
[Root @ TP ~] # Iptables-P output accept
[Root @ TP ~] # Iptables-P forward drop
The above means that when two chain rules (input and forward) in the filter table in iptables are exceeded, how can we process data packets not in these two rules, that is, drop (discard ). it should be said that the configuration is safe. we want to control inbound data packets
For the output chain, that is, the outgoing package, we do not need to impose too many restrictions, but adopt accept. That is to say, what should we do if the package is not in a rule.
We can see what packets are allowed to pass through the input and forward chains, and what packets are not allowed to pass through the output chain.
This setting is quite reasonable. Of course you can also drop all three links, but I don't think it is necessary to do so, and the rules to be written will increase. but if you only want a limited number of rules, for example, only web servers. we recommend that all three links be drop.
Note: If you log on remotely through SSH, you should drop it when you enter the first command and press enter because you have not set any rules.
What should I do? Go to the local machine to operate it!

4. Add Rules.
First, add the input chain. The default rule of the input chain is drop, so we will write the chain that requires accetp ().
To enable remote SSH Login, We need to enable port 22.
[Root @ TP ~] # Iptables-A input-p tcp-dport 22-J accept
[Root @ TP ~] # Iptables-A output-p tcp-sport 22-J accept
(Note: If you set the output to drop, you need to write this rule. Many people are eager to write this rule, so they will never be able to ssh. Check it remotely.
The same applies to other ports. If the Web server is enabled and the output is set to drop, a chain should also be added:
[Root @ TP ~] # Iptables-A output-p tcp-sport 80-J accept .)
If the Web server is configured, enable port 80.
[Root @ TP ~] # Iptables-A input-p tcp-dport 80-J accept
If the email server is configured, enable port 25,110.
[Root @ TP ~] # Iptables-A input-p tcp-dport 110-J accept
[Root @ TP ~] # Iptables-A input-p tcp-dport 25-J accept
If the FTP server is configured, enable port 21.
[Root @ TP ~] # Iptables-A input-p tcp-dport 21-J accept
[Root @ TP ~] # Iptables-A input-p tcp-dport 20-J accept
If the DNS server is configured, enable port 53.
[Root @ TP ~] # Iptables-A input-p tcp-dport 53-J accept
If you have another server, you just need to open the port and write it.
The above mainly writes the input chain, and all the rules that are not in the above drop
Allow ICMP packets to pass, that is, allow Ping,
[Root @ TP ~] # Iptables-A output-p icmp-J accept (if output is set to drop)
[Root @ TP ~] # Iptables-A input-p icmp-J accept (if input is set to drop)
Allow loopback! (Otherwise, DNS may fail to be shut down normally)
Iptables-A input-I lo-P all-J accept (if it is input drop)
Iptables-A output-O lo-P all-J accept (if it is output drop)
The output chain is written below. The default rule of the output chain is accept, so we will write the chain that needs to be dropped (abandoned.
Reduce insecure port connections
[Root @ TP ~] # Iptables-A output-p tcp-sport 31337-J Drop
[Root @ TP ~] # Iptables-A output-p tcp-dport 31337-J Drop
Some Trojans scan services from ports 31337 to 31340 (elite ports in hacking languages. Since legal services do not use these non-standard ports for communication, blocking these ports can effectively reduce the chances of independent communication between machines that may be infected on your network and their remote master servers.
The same applies to other ports, such as 31335, 27444, 27665, 20034, 9704, 137-139 (SMB), and 2049 (NFS, I have not written all of them here. If you are interested, check the relevant information.
Of course, you can set the output chain to drop for more secure access, so you can add more rules, just like adding
Allow SSH Login. Just write it.
The more detailed rules are as follows:
For example, we only allow SSH connections to machines 192.168.0.3.
[Root @ TP ~] # Iptables-A input-s 192.168.0.3-p tcp-dport 22-J accept
If you want to allow or restrict the availability of a certain IP address, 192.168.0.0/24 indicates all IP addresses of 192.168.0.1-255.
24 indicates the number of subnet masks. Remember to delete this line in/etc/sysconfig/iptables.
-A input-p tcp-m tcp-dport 22-J accept because it indicates that all addresses can log on.
Or use the following command:
[Root @ TP ~] # Iptables-D input-p tcp-dport 22-J accept
Save it. I'll talk about it again. Instead, it uses the command method and only takes effect at that time. If you want to restart it, save it. write to the/etc/sysconfig/iptables file.
[Root @ TP ~] #/Etc/rc. d/init. d/iptables save
Write it like this! 192.168.0.3 indicates IP address other than 192.168.0.3
The same is true for other rule connections.
The following is the forward chain. The default rule of the forward chain is drop, so we will write the chain that requires accetp (VIA) to monitor the ongoing forwarding chain.
Enable the forwarding function (required when the default forward rule is drop when performing Nat)
[Root @ TP ~] # Iptables-a forward-I eth0-O eth1-M State-State related, established-J accept
[Root @ TP ~] # Iptables-a forward-I eth1-O eh0-J accept
Discard bad TCP Packets
[Root @ TP ~] # Iptables-a forward-p tcp! -Syn-M State-State new-J Drop
Number of IP fragments processed to prevent attacks. Up to 100 IP fragments are allowed per second.
[Root @ TP ~] # Iptables-a forward-F-m limit-limit 100/S-limit-burst 100-J accept
Set ICMP packet filtering to allow 1 packet per second. The trigger condition is 10 packets.
[Root @ TP ~] # Iptables-a forward-p icmp-m limit-limit 1/S-limit-burst 10-J accept
I only allow ICMP packets to pass in the front, because I have restrictions here.

Configure a NAT table
1. View local Nat settings
[Root @ TP rc. d] # iptables-T nat-l
Chain prerouting (Policy accept)
Target prot opt source destination
Chain postrouting (Policy accept)
Target prot opt source destination
SNAT all-192.168.0.0/24 anywhere to: 211.101.46.235
Chain output (Policy accept)
Target prot opt source destination
My Nat has been configured (only the simplest proxy Internet access function is provided, and no firewall rules have been added). For how to configure Nat, refer to my other article.
Of course, if you have not configured Nat, you do not need to clear the rules, because Nat does not have anything by default.
If you want to clear, the command is
[Root @ TP ~] # Iptables-F-T Nat
[Root @ TP ~] # Iptables-X-T Nat
[Root @ TP ~] # Iptables-z-T Nat
2. Add Rules
Add basic Nat address translation (see my other article on how to configure Nat ),
To add rules, we only add drop links. Because the default links are all accept.
Prevent Internet spoofing using Intranet IP addresses
[Root @ TP sysconfig] # iptables-T Nat-A prerouting-I eth0-s 10.0.0.0/8-J Drop
[Root @ TP sysconfig] # iptables-T Nat-A prerouting-I eth0-s 172.16.0.0/12-J Drop
[Root @ TP sysconfig] # iptables-T Nat-A prerouting-I eth0-s 192.168.0.0/16-J Drop
If we want to, for example, block MSN, QQ, BT, etc., we need to find the port or IP address they use (I think it is not necessary)
Example:
Disable all connections to 211.101.46.253
[Root @ TP ~] # Iptables-T Nat-A prerouting-D 211.101.46.253-J Drop
Disable ftp (21) Port
[Root @ TP ~] # Iptables-T Nat-A prerouting-p tcp-dport 21-J Drop
In this way, the write range is too large, so we can define it more accurately.
[Root @ TP ~] # Iptables-T Nat-A prerouting-p tcp-dport 21-D 211.101.46.253-J Drop
In this way, only the FTP connection of the 211.101.46.253 address is disabled. Other connections can also be. For example, Web (port 80) connections.
According to what I wrote, you only need to find the IP addresses, ports, and protocols of other software such as QQ and MSN.
Finally:
Drop illegal connection
[Root @ TP ~] # Iptables-A input-M State-State invalid-J Drop
[Root @ TP ~] # Iptables-A output-M State-State invalid-J Drop
[Root @ TP ~] # Iptables-a forward-M State-State invalid-J Drop
Allow all established and related connections
[Root @ TP ~] # Iptables-A input-M State-State established, related-J accept
[Root @ TP ~] # Iptables-A output-M State-State established, related-J accept
[Root @ TP ~] #/Etc/rc. d/init. d/iptables save
In this way, you can write it to the/etc/sysconfig/iptables file. Remember to repeat the firewall after writing it to make it take effect.
[Root @ TP ~] # Service iptables restart
Don't forget to save it. If it doesn't work, write a record and save it once. You can save and experiment to see if your requirements are met,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.