Practical Linux commands, not the most comprehensive but practical --- iptables command practice

Source: Internet
Author: User
Tags ftp connection
Reprinted from: http://jiujiang.blog.sohu.com/97911176.html If you do not know the basic knowledge about iptables, we recommend that you first check it out. Start Configuration Let's configure a filter table firewall. (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 references)
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, this is the case [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 has 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 at [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 has nothing to do with. It is the same as 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 to write it to the/etc/sysconfig/iptables file. after writing, remember to repeat the firewall to make it work. [root @ TP ~] # Service iptables restart: No configuration is available in the iptables configuration table. Let's start 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 and operate it! (4) Add a rule. 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. 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 on 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 infected machines on your network and their remote master servers. There are also other ports, such: ports 31335, 27444, 27665, 20034 NetBus, 9704, 137-139 (SMB), and 2049 (NFS) should also be disabled. I have not written all of them here, if you are interested, check related 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, you can also restrict the number of subnet masks for a certain IP address. The value 192.168.0.0/24 indicates all IP addresses in the range of 192.168.0.1-255. but 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 command: [root @ TP ~] # Iptables-D input-p tcp -- dport 22-J accept and save it. I'll talk about it again. Instead, it uses the command method and takes effect only at the time. If you want to restart it, it will also take effect, save it. write to the/etc/sysconfig/iptables file. [root @ TP ~] #/Etc/rc. d/init. d/iptables save! 192.168.0.3 indicates that other rule connections except the IP address 192.168.0.3 are also set. 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, this is because I have restrictions here. 2. 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.235chain 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 more information about how to configure Nat, see 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 DropDisable ftp (21) Port[Root @ TP ~] # Iptables-T Nat-A prerouting-p tcp -- dport 21-J DropIn 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 you can't save it, write it once. You can save it while doing experiments to see if it meets your requirements,

I have tried all the rules above and there is no problem. It took me nearly a month to write this article. I searched for the materials and did my experiments on my own. I hope it will be helpful to everyone. If there are incomplete and incomplete information, please submit it. This article focuses on configuration. I will upload the basic knowledge about iptables and instructions on commands and commands as soon as possible. Of course, you can search for them online.
Related Article

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.