Using iptables for static IP ing using iptables for static IP ing is similar to using port ing. The specific operations are as follows: in fact, static ing and port ing are similar, however, static IP addresses are used to redirect servers on the Intranet to the internet (or external networks), while port ing only maps some service ports on the Intranet to a port on the internet server. Maybe some brothers wonder why static ip ing is required? Is it enough to directly set it to an Internet IP address? In fact, the reason is very simple. In order to facilitate unified management and security policy formulation. Assume that your server needs to be relocated or changed. If there is only one server, you can just change the IP address on this server or change the new server address to the old server address, what if there are several servers? You have to deal with longer network disconnection times ...... Static IP ing makes it easier for you to manage multiple servers and increase security (equivalent to creating a firewall for the server ). If you want to replace the old server with a new server, you can allocate another Internet IP address to the new server or use port ing (similar to static IP ing, I personally think it is not as convenient as static IP ing). After the test function is fully implemented, you can directly modify the firewall configuration. In just a moment, the website can be switched, and then the old server can be processed after the switch is completed. During the relocation, you do not have to change the IP address and subnet mask of each server. Instead, you do not have to connect all servers to the firewall and change the IP address of the firewall once, you do not need to change the IP address of the Real Server (all are private IP addresses ). This is all about the benefits. In fact, the configuration is still very simple. Assume that you have two web servers with ip addresses 192.168.1.1 and 192.168.1.2, one vswitch and one linux dual Nic server (used for firewall and static ip ing), and one external line, several available public IP addresses (for example, 1.1.1.1 ~ 1.1.1.6. Connect the external network to a network card of the linux Server (such as eth0), set the network card address to 1.1.1.1, connect another network card (such as eth1) to the switch, and set the IP address to 192.168.1.254. The two web servers are connected to the vswitch respectively, and the gateway is set to 192.168.1.254 (that is, the Intranet address of the linux Firewall ). The line has been set up. Now let's talk about how to set up a linux server. Don't you need to install it? It is recommended that you use a newer version (the new version of iptables is more powerful and supports higher hardware, so you don't need to spend a lot of energy on device debugging ), after the system is installed, set the IP address ...... How to Set it? Previously implemented by modifying the ifcfg-eth0 and ifcfg-eth1 files under the/etc/sysconfig/network-scripts/directory. Now you can use the ifconfig or ip address add command. (these settings are provided that your Nic has been detected and can work properly. If you have any problem installing the NIC, discuss it later, the version I used is redhat as4 ). For example, if you set eth0, you can use ifconfig eth0 1.1.1.1/30 up (I mentioned that your available ip address range is generally 1.1 ~ 1.7, which means that the subnet mask is 255.255.255.248, Which is/30.) To set an ip address for eth0, you can also use the command ip address add 1.1.1.1/30 brd + dev eth0. To add a gateway, run the "ip route add default via gateway IP dev eth0" command. I don't need to talk about eth1, do I? Note that in linux, the default gateway (default) of the entire system can only have one (of course, you can add a gateway by adding a route or a policy route, but the default gateway can only have one ), therefore, do not use the following command to add the default gateway to eth1. Otherwise, no one will be able to access the network. If all the above commands are successfully executed, congratulations, you have prepared all the basic items. Otherwise, your network card is not installed when your system is installed ...... Now let's take a look at the iptables service. You can run iptables-V to see if iptables has been installed in the system (usually installed). You can also display your version number-my version is 1.3.6. OK. Edit the script. Enter vi FWstart. sh to enter the editing page (the text body is shown below )#! /Bin/sh # The following two parameters are the parameters for starting the linux forwarding function and anti-syn Attack. # Data packet forwarding in linux cannot be implemented without the first entry # This means that the file/proc/sys/net/ipv4/ip_forward is set to 1 # I don't know much about the second entry, but add it. Echo "1">/proc/sys/net/ipv4/ip_forward echo "1">/proc/sys/net/ipv4/tcp_syncookies # Load various modules required for iptables startup, it is said that many versions do not need to be added, because the specific parameter references have actually been called # They are used, but there is nothing to consider compatibility. If you have removed these lines, if the problem occurs, add it. Modprobe ip_tables modprobe ip_nat_ftp modprobe ip_nat_irc modprobe ip_conntrack modprobe ip_conntrack_ftp modprobe tables # Clear the original iptables table (mainly filter table and nat table) and chain/sbin/iptables-F/sbin/iptables-X/sbin/iptables-Z/sbin/iptables-F-t nat/sbin/iptables-X-t nat In the table /sbin/iptables-Z-t nat # Set the default rules for the three chains in the filter table, the inbound (INPUT) outbound (OUTPUT) allows ACCEPT # FORWARD (FORWARD) to discard the DROP, so as to play a role in the firewall. # You can also set more detailed INPUT rules. In fact, the INPUT chain is designed to protect the firewall itself. I will not talk about it much. # My focus is on the FORWARD chain, it filters forwarding packets. /Sbin/iptables-p input accept/sbin/iptables-p output accept/sbin/iptables-p forward drop # Add an IP address to the network adapter. A network adapter can have many IP addresses, in this way, when accessing 1.1.1.2 through the Internet, # can be redirected to 192.168.1.1 on the Intranet. Ip address add 1.1.1.2/30 brd + dev eth0 ip address add 1.1.1.3/30 brd + dev eth0 # set to allow access to the server 192.168.1.1 and 192.168.1.2, otherwise, the Internet # cannot send data to the two servers. Of course, you can set more rules. This is actually the firewall for the two # servers. For example, if you want to set only allow access to port 80 on 1.1, change #/sbin/iptables-a forward-d 192.168.1.1-j ACCEPT to #/sbin/iptables-a forward-d 192.168.1.1 -- dport 80-j ACCEPT, # Do not change anything else unless you understand the meaning of the change. However, I would like to remind you that according to this setting, your two # servers are basically the same as accessing the internet separately, but only performing address conversion, # A little more protection. We recommend that you learn more about iptables firewall. /Sbin/iptables-a forward-s 192.168.1.1-j ACCEPT/sbin/iptables-a forward-d 192.168.1.1-j ACCEPT/sbin/iptables-a forward-s 192.168.1.2-j ACCEPT/ sbin/iptables-a forward-d 192.168.1.2-j ACCEPT # below is the nat table, this should never be changed unless you fully understand the functions of the filter tables. # It means that the three links of the nat subject will receive any data transmission by default. /Sbin/iptables-t nat-p prerouting accept/sbin/iptables-t nat-p postrouting accept/sbin/iptables-t nat-p output accept # This truly implements address translation statement, meaning: # In the PREROUTING chain (that is, the data packet that just enters the linux Server), modify the data packet whose destination address is 1.1.1.2 and change its destination address to 192.168.1.1; # In the POSTROUTING chain (the packet sent from the linux server will be sent immediately), change the source address of data with the source address 192.168.1.1 # change the source address of the package to 1.1.1.2; /sbin/iptables-t nat-a prerouting-d 1.1.1.2-j DNAT -- to-destination 192.168.1.1/sbin/iptables-t Nat-a postrouting-s 192.168.1.1-j SNAT -- to-source 1.1.1.2/sbin/iptables-t nat-a prerouting-d 1.1.1.3-j DNAT -- to-destination 192.168.1.2/sbin/iptables -t nat-a postrouting-s 192.168.1.2-j SNAT -- to-source 1.1.1.3 # The script ends here. Your Intranet servers 192.168.1.1 and 192.168.1.2 should already be accessible through IP addresses 1.1.1.2 and 1.1.1.3.