Iptables settings and nat service settings

Source: Internet
Author: User
I. Overview of firewalls (I). Overview of firewalls refers to the combination of components set between different networks or network security domains, which can enhance the security of internal networks. It uses the access control mechanism to determine which internal services allow external access and which external requests are allowed to access internal services. It can be determined based on the type of network transmission. 1. Overview of firewall
(1) Introduction to the firewall
A firewall is a combination of components set between different networks or network security domains. it enhances the security of the internal network of an organization. It uses the access control mechanism to determine which internal services allow external access and which external requests are allowed to access internal services. It determines whether an IP packet can be transmitted to or from the Intranet based on the network transmission type.
The firewall checks every data packet that passes through the review to determine whether it has matched filtering rules, and compares them one by one based on the rule sequence until one of the rules is met, then, the corresponding action is made based on the control mechanism. If none of the above conditions are met, data packets are discarded to protect network security.
You can use the firewall to implement the following functions:

It can protect vulnerable services;
Control access to network systems between intranet and internet;
Centrally manages intranet security to reduce management costs;
Improve the confidentiality and privacy of the network;
Record the network usage status to provide a basis for security planning and network maintenance.
(2) classification of firewalls
Firewall technology is divided into many types based on different defense methods and focuses, but generally it can be divided into two types: packet filtering firewall and proxy server.
(3) working principles of the firewall
1. how packet filtering firewall works
① After a packet is transmitted from the Internet to the firewall, the firewall forwards the packet to the packet inspection module for processing before the IP layer sends the packet to the TCP layer.
② First, compare it with the first filter rule.

③ If it is the same as the first module, it will be reviewed to determine whether to forward the data packet. if the review result is a data packet, the data packet will be sent to the TCP layer for processing, otherwise, it will be discarded.
④ If it is different from the first filter rule, then compare it with the second rule. if it is the same, review it. The process is the same as ③.
⑤ If it is different from the second filter rule, compare it with the next filter rule until it is compared with all the filter rules. If none of the filtering rules are met, the data packet is discarded.
2. working principle of proxy service firewall
Proxy service firewall implements the firewall function at the application layer. It provides transmission-related statuses, application-related statuses, and transmission-related information. It can also process and manage information.
For details about how it works, see section 11.1.2.
II. IptablesIntroduction
Netfilter/iptables (iptables) is a packet filtering firewall on the Linux platform. like most Linux software, this packet filtering firewall is free of charge, it can replace expensive commercial firewall solutions to implement functions such as packet filtering, Packet redirection, and network address translation NAT. The Iptables/netfilter packet filtering firewall consists of two components: netfilter and iptables.
III. iptables basics
(1). rules (rules)
A rule (rules) is actually a predefined condition of the network administrator. The rule is generally defined as "if the data packet header meets this condition, it will process this packet ". Information Packet filtering table in the kernel space where rules are stored
The rules specify the source address, destination address, Transmission Protocol (such as TCP, UDP, ICMP), and service type (such as HTTP, FTP, and SMTP. When a packet matches a rule, iptables processes the packet according to the rule-defined method, such as accept, reject, and drop. The main task of configuring a firewall is to add, modify, and delete these rules.
(2). chain)
A chain is the path for packet propagation. each chain is actually a check list among many rules. each chain can have one or several rules. When a packet arrives at a chain, iptables checks the first rule in the chain to check whether the packet meets the conditions defined by the rule. If yes, the system will process the packet according to the method defined by the rule; otherwise, iptables will continue to check the next rule. if the packet does not comply with any rule in the chain, iptables processes data packets according to the predefined default policy of the chain.
(3). table (tables)
Table (tables) provides specific functions. iptables has three built-in tables: filter table, nat table, and mangle table, which are used for packet filtering respectively, network address translation and packet reconstruction.
1. filter table
2. nat table
3. mangle table
(4) iptables data packet transmission process
① When a packet enters the NIC, it first enters the PREROUTING chain, and the kernel determines whether to forward the packet based on the destination IP address.
② If the data packet enters the local machine, it will move down the graph and reach the INPUT chain. After the packet arrives at the INPUT chain, any process will receive it. Programs running on the local machine can send data packets that pass through the OUTPUT chain and then reach the POSTROUTING chain OUTPUT.
③ If the packet is to be forwarded and the kernel allows Forwarding, the packet will move to the right, as shown in-4, after the FORWARD chain, and then reach the POSTROUTING chain output.
4. disable the system firewall
(1). iptables command format
Run the "setup" command to start the text mode configuration utility, select "firewall configuration" in "select a tool", and then select the "run tool" button. The firewall configuration page appears. set "security level" to "disabled" and select "OK. The command format of iptables is complex. the general format is as follows:
Iptables [-t table]-command matching operation
1. table options
The table option specifies the iptables built-in table to which the command applies. iptables includes the filter table, nat table, and mangle table.
2. command options

3. matching options

4. action options

(2) use of iptables
1. define the default policy
When a packet does not comply with any rule in the chain, iptables processes the packet based on the predefined default policy of the chain. the format of the default policy is as follows.
Iptables [-t table name] <-P> <链名> <动作>
The parameters are described as follows.
[-T table name]: the table to which the default policy applies. filter, nat, and mangle can be used. If no table is specified, iptables uses the filter table by default.
<-P>: defines the default policy.
<链名> : The chain to which the default policy applies. INPUT, OUTPUT, FORWARD, PREROUTING, OUTPUT, and POSTROUTING can be used.
<动作> : Action to process data packets. you can use ACCEPT (ACCEPT data packets) and DROP (discard data packets ).
2. View iptables rules
The command format for viewing iptables rules is:
Iptables [-t table name] <-L> [chain name]
The parameters are described as follows.
[-T table name]: lists the rules of a table. you can use filter, nat, and mangle to list the rules. If no table is specified, iptables displays the rule list of the filter table by default.
<-L>: view the list of rules for the specified table and chain.
[Chain name]: it refers to the list of chain rules in a specified table. INPUT, OUTPUT, FORWARD, PREROUTING, OUTPUT, and POSTROUTING can be used. If no chain is specified, the rule list of all links in a table is displayed.
3. add, insert, delete, and replace rules
The rules are defined in the following format:
Iptables [-t table name] <-A | I | D | R> chain name [rule number] [-I | o Nic name] [-p protocol type] [-s source IP address | source subnet] [-- sport source port number] [-d destination IP address | destination subnet] [-- dport destination port number] <-j action> parameters are described as follows.
[-T table name]: defines the table to which the default policy applies. filter, nat, and mangle can be used. If no table is specified, iptables uses the filter table by default.
-A: A new rule is added to the last row of the rule list. rule numbers cannot be used for this parameter.
-I: insert a rule. rules at the specified position will be moved in sequence. If no rule number is specified, insert the rule before the first rule.
-D: Delete a rule from the rule list. you can enter a complete rule or specify a rule number to delete it.
-R: If a rule is replaced, the sequence is not changed. you must specify the number of the rule to be replaced.
<链名> : Specifies the chain rule list in the specified table. INPUT, OUTPUT, FORWARD, PREROUTING, OUTPUT, and POSTROUTING can be used.
[Rule number]: The rule number is used to insert, delete, and replace rules. The number is arranged in the order of the rule list. the number of the first rule in the rule list is 1.
[-I | o Nic name]: I indicates the NIC from which the data packet enters, and o indicates the NIC from which the data packet is output. You can use ppp0, eth0, and eth1 Nic names.
[-P protocol type]: you can specify the protocol applied by the rule, including TCP, UDP, and ICMP.
[-S source IP address | source subnet]: IP address or subnet address of the source host.
[-- Sport source port number]: source port number of the IP address of the data packet.
[-D Target IP address | target subnet]: IP address or subnet address of the target host.
[-- Dport destination port number]: Destination port number of the IP address of the data packet.
<-J action>: Action of processing data packets. for details about each action, see Table 10-3.
4. clear rules and counters
When creating a rule, you often need to clear the original and old rules so that they do not affect the new rules. If there are many rules, it will be very troublesome to delete them one by one. in this case, you can use the clearing rule parameters provided by iptables to quickly delete all rules. The format of the defined parameters is:
Iptables [-t table name] <-F | Z>
The parameters are described as follows.
[-T table name]: specifies the table to which the default policy applies. filter, nat, and mangle can be used. If no table is specified, iptables uses the filter table by default.
-F: delete all rules in the specified table.
-Z: returns the data packet counter and traffic counter in the specified table to zero.
5. NAT service
(1). What is a private address?
Privateaddress is a non-registered address, which is used internally by the organization. Private IP addresses cannot be directly connected to the Internet, but can be used on Intranet IP addresses within the company.
(2) What is NAT
NAT is a standard method for ing an address domain (such as a private Intranet) to another address domain (such as the Internet. It is an IETF standard developed according to RFC1631 that allows an IP address domain to appear on the Internet with a public IP address. NAT can convert the addresses of all nodes in the internal network into one IP address, and vice versa. It can also be applied to firewall technology to hide individual IP addresses from external discovery, so that external access is not allowed.
Ask the internal network device.
(3) How NAT works
1. static network address translation
① Create a static NAT ing table on the NAT server.
② When an intranet host (IP address 192.168.16.10) needs to establish a session connection to the Internet, the request is first sent to the NAT server. After receiving the request, the NAT server checks the NAT ing table based on the received request packets.
③ If static address translation has been configured for this address, the NAT server uses the corresponding internal public IP address and forwards data packets. Otherwise, the NAT server will not convert the address and directly discard the data packets. The NAT server uses 202.96.128.2 to replace the internal private IP address (192.168.16.10), as shown in. 13.
④ The host on the Internet responds after receiving the data packet (in this case, the host receives the 202.96.128.2 request ).
⑤ Check the NAT ing table when the NAT server receives packets from a host on the Internet. If the NAT ing table has matched ing items, replace the destination IP address of the data packet with the internal private IP address and forward the data packet to the internal host. If no matching ING exists, the data packet is discarded.
2. Dynamic Network address translation
① When an intranet host (IP address 192.168.16.10) needs to establish a session connection to the Internet, the request is first sent to the NAT server. After receiving the request, the NAT server checks the NAT ing table based on the received request packets.
② If no address translation ing item has been set up for the internal host, the NAT server will decide to convert the address (create a ing item of 192.168.16.10: 2320 255.→ 202.96.128.2: 2320, and record the session status ). If this ing item already exists, the NAT server uses this record to convert the address and record the session status. The NAT server then sends data packets to the Internet host using the converted address.
③ After receiving the information, the Internet host responds and returns the response information to the NAT server.
④ When the NAT server receives the response, check the NAT ing table. If the NAT ing table has matched ing items, replace the destination IP address of the data packet with the internal public IP address and forward the data packet to the internal host. If no matching ING exists, the data packet is discarded.
3. network address and port conversion
① When an internal host (with the IP address 192.168.16.10 and Port 1235) needs to establish a connection with a host on the Internet (with the IP address 202.18.4.6 and Port 2350, first, send the request to the NAPT server. After the NAPT server receives the request, it checks the NAPT ing table based on the received request packet.
② If no address translation ing item has been set up for the internal host, the NAPT server will create a Session for the transmission and allocate a port 3200 to the Session, then change the source port of the packet to 3200. Therefore, the original 192.168.16.10: 1235 → 202.18.4.6: 2350 packet is converted to 202.96.128.2: 3200 → 202.18.4.6: 2350.
③ After receiving the information, the Internet host responds and sends the response information back to the NAPT server.
④ When the NAPT server receives the response information, check the NAPT ing table. If the NAPT ing table has matched ing items, replace the destination IP address of the data packet with the internal public IP address and forward the data packet to the internal host. If no matching ING exists, the data packet is discarded.
6. use iptables to implement NAT services
(1). configure the network environment
(1) configure the Nic eth0
DEVICE = eth0 # Nic device alias
BOOTPROTO = static # The IP address of the NIC is specified statically.
BROADCAST = 192.168.16.255 # network address of the NIC
HWADDR = 00: 0C: 29: FD: D3: 29 # Nic MAC address
IPADDR = 192.168.16.1 # Nic ip address
NETMASK = 255.255.255.0 # subnet mask of the network adapter
NETWORK = 192.168.16.0 # NETWORK address of the NIC
ONBOOT = yes # Activate the Nic when the system starts
TYPE = Ethernet # The Network card type is Ethernet.
(2) configure the network adapter eth1
DEVICE = eth1 # Nic device alias
BOOTPROTO = static # The IP address of the NIC is specified statically.
BROADCAST = 172.16.1.255 # network address of the NIC
HWADDR = 00: 0C: 29: FD: D3: 33 # Nic MAC address
IPADDR = 172.16.1.1 # Nic ip address
NETMASK = 255.255.255.0 # subnet mask of the network adapter
NETWORK = 172.16.1.0 # Network address of the NIC
ONBOOT = yes # Activate the Nic when the system starts
TYPE = Ethernet # The Network card type is Ethernet.
(3) specify a DNS server for the system
Nameserver 61.144.56.101
Nameserver 202.96.128.68
(4) make the network configuration take effect
Restart Linux or run the "/etc/init. d/network restart" command to make the above configuration take effect.
(2) establish an ADSL connection
1. rp-pppoe installation
By default, Red Hat EnterpriseLinux installer installs the rp-pppoe software on the system, run the following command to check whether the system has installed the rp-pppoe software or to check the version installed.
Rpm-q rp-pppoe
If the system has not installed the rp-pppoe software, you can add 1st Red Hat Enterprise Linux5 installation disks to the optical drive, after loading the optical drive, find the RPM installation package file rp-pppoe-3.5-32.1.i386.rpm of the rp-pppoe software under the Server directory of the disc, and install the rp-pppoe software using the following command.
Rpm-ivh/mnt/Server/rp-pppoe-3.5-32.1.i386.rpm
2. set ADSL connection parameters
(3) rp-pppoe control script
1. ADSL dialing
Adsl-start
2. view the status of the current connection. to view the status of the current ADSL connection, run the "adsl-status" command.
3. Disconnect
To disconnect the ADSL connection, run the "adsl-stop" command.
(4). use iptables for NAT
To use iptables to implement NAT, follow these steps.
① Enable the kernel routing function. To implement the NAT function, you must first set the file/proc/sys/net/ipv4/ip_forward to 1 (the default value is 0) to enable the kernel routing function. The specific command is as follows.
Echo "1">/proc/sys/net/ipv4/ip_forward
② Implement IP camouflage. Add a rule to the POSTROUTING chain in the nat table. the content of this rule is that all packets sent by the ppp0 interface will be masked (MASQUERADE), so that you can use iptables to implement NAT commands. The specific command is as follows ..
Iptables-t nat-a postrouting-o ppp0-j MASQUERADE
(5) NAT client configuration
1. configure the NAT client in Linux
(1) set the IP address of the default gateway
Edit the/etc/sysconfig/network file and use the GATEWAY option to specify the IP address of the default GATEWAY.
② Set the IP address of the DNS server
In Linux, it is easy to configure the DNS client. you can directly edit the file/etc/resolv. conf and use the nameserver option to specify the IP address of the DNS server.
2. configure the NAT client in Windows 2000/XP/2003
(6) automatic dialing and NAT server configuration at startup
To enable the Linux server to automatically dial and configure the NAT server after it is started, add the following command to/etc/rc. d/rc. the end of the local file (you can also add iptables to this file later ).
/Sbin/adsl-start
Echo "1">/proc/sys/net/ipv4/ip_forward
/Sbin/iptables-t nat-a postrouting-o ppp0-j MASQUERADE
VII. iptables tips
(1) prohibit clients from accessing unhealthy websites
[Example 1] add an iptables rule to prohibit users from accessing the website with the domain name www.playboy.com, and then view the list of forwarding chain rules in the filter table.
Iptables-I FORWARD-d www.playboy.com-j DROP
Iptables-t filter-L FORWARD
[Example 2] add the iptables rule to prohibit users from accessing the website with the IP address 202.17.61.4, and then view the list of forwarding chain rules in the filter table.
Iptables-I FORWARD-d 202.17.61.4-j DROP
Iptables-t filter-L FORWARD
(2) prohibit some clients from accessing the Internet.
[Example 1] add an iptables rule to prohibit clients with IP addresses 192.168.1.200 from accessing the Internet, and then view the list of forwarding rules in the filter table.
Iptables-I FORWARD-s 192.168.1.200-j DROP
Iptables-t filter-L FORWARD
[Example 2] add an iptables rule to disable all clients in the 192.168.2.0 subnet from accessing the Internet, and then view the list of forwarding rules in the filter table.
Iptables-I FORWARD-s 192.168.2.0/24-j DROP
Iptables-t filter-L FORWARD
(3) prohibit clients from accessing certain services
[Example 1] prohibit all clients in the 192.168.1.0 subnet from downloading over FTP (that is, closing Port 21 of TCP), and then view the list of forwarding rules in the filter table.
Iptables-I FORWARD-s 192.168.1.0/24-p tcp -- dport 21-j DROP
Iptables-t filter-L FORWARD
[Example 2] disable all clients in the 192.168.1.0 subnet from using Telnet to connect to a remote computer (that is, port 23 of the TCP protocol is closed), and then view the list of forwarding rules in the filter table.
Iptables-I FORWARD-s 192.168.1.0/24-p tcp -- dport 23-j DROP
Iptables-t filter-L FORWARD
(4). force access to the specified site
[Example] force all clients to access the Web server 210.21.118.68 and view the PREROUTING link rule list in the nat table.
Iptables-t nat-I prerouting-I eth0-p tcp -- dport 80-j DNAT -- to 210.21.118.68: 80
Iptables-t nat-L PREROUTING
(5) prohibit clients from using QQ
Iptables-I FORWARD-p tcp -- dport 8000-j DROP
Iptables-I FORWARD-p udp -- dport 8000-j DROP
Iptables-I FORWARD-d tcpconn.tencent.com-j DROP
Rpm-ivh/mnt/Server/rp-pppoe-3.5-32.1.i386.rpm
2. set ADSL connection parameters
(3) rp-pppoe control script
1. ADSL dialing
Adsl-start
2. view the status of the current connection. to view the status of the current ADSL connection, run the "adsl-status" command.
3. Disconnect
To disconnect the ADSL connection, run the "adsl-stop" command.
(4). use iptables for NAT
To use iptables to implement NAT, follow these steps.
① Enable the kernel routing function. To implement the NAT function, you must first set the file/proc/sys/net/ipv4/ip_forward to 1 (the default value is 0) to enable the kernel routing function. The specific command is as follows.
Echo "1">/proc/sys/net/ipv4/ip_forward
② Implement IP camouflage. Add a rule to the POSTROUTING chain in the nat table. the content of this rule is that all packets sent by the ppp0 interface will be masked (MASQUERADE), so that you can use iptables to implement NAT commands. The specific command is as follows ..
Iptables-t nat-a postrouting-o ppp0-j MASQUERADE

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.