Basic configuration of iptables firewall

Source: Internet
Author: User
(I) basic actions list existing iptables policies iptables-L insert a policy iptables-IINPUT3-ptcp -- dport22-jACCEPT notes: all traffic that uses the TCP protocol and the target Port is 22 all allow to pass the order of 3 policy to delete a policy iptables-DINPUT3 to delete all policies (temporarily effective) iptab
(I) Basic operations
List existing IptablesPolicy
Iptables-L
Insert a policy
Iptables-I INPUT 3-p tcp -- dport 22-jACCEPT
Note: all traffic that uses the TCP protocol and the target Port is 22 is allowed to pass in the order of 3
Delete a policy
Iptables-d input 3
Delete all policies (temporarily effective)
Iptables-F
Table:


(Ii) matching parameters
Based on IP address
-S 192.168.1.1 (source address)
-D 192.168.1.0/24 (target address)
Interface-based
-I eth0 (traffic received from eth0)
-O eth1 (traffic sent from eth1)
Excluded parameters
-S '! '1970. 168.1.0/24 (all network segments except 192.168.1.0/24)
Based on protocols and ports
-P tcp -- dport 23 (target port)
-P udp -- sport 53 (source port)
-P lcmp
(Iii) example
① Control the network traffic to the local machine
Iptables-a input-s 192.168.1.100-jDROP
Iptables-a input-p tcp -- dport 80-jDROP
Iptables-a input-s 192.168.1.0/24-p tcp -- dport 22-j DROP
Iptables-a input-I eth0-j ACCEPT
② When using linux as a routing (data forwarding) device, you can set a forwarding control policy at the filter point FORWARD.
For example, disable all traffic from 192.168.1.0/24 to 10.1.1.0/24.
Iptables-a forward-s192.168.1.0/24-d 10.1.1.0/24-j DROP
③ NAT (network address translation) is used to modify the IP address of a data packet. NAT is divided into two types:
● SNAT: source address conversion, usually used to disguise internal addresses
● DNAT: Destination address translation, usually used for jump ( Server load balancerOr disguise protection)
In iptables, NAT tables are implemented.
DNAT can only be used on PROROUTING.
SNAT can only be used on the POSTROUTING filter point.
★Jump through NAT:
Iptables-t nat-APREROUTING-p tcp -- dport 80-j DNAT -- to-dest 192.168.1.10
★Forward Data through NAT:
Iptables-t nat-AOUTPUT-p tcp -- dport 80-j DNAT -- to-dest 192.168.1.100: 8080
★Mask data streams through NAT:
Iptables-t nat-APOSTROUTING-o eth0-j MASQUERADE
★Hide source IP addresses through NAT
Iptables-t nat-APOSTROUTING-j SNAT -- to-source 1.3.1.4
(Iv) configuration file
Policies added using iptables are not permanently saved.
If you need to permanently save the policy, you need to keep this record in the configuration file
/Etc/sysconfig/iptables
Run the following command to write all the iptables policies in the memory to the configuration file:
Service iptables save
(V) summary
To remotely manage a linux host and modify the iptables policy, you must first allow ssh traffic from the client host to ensure this is the first iptables policy,
Otherwise, you may lock yourself out due to misconfiguration.

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.