Use iptables port forwarding achieved under CentOS and Ubuntu16.04

Had previously been used in CentOS iptables environment or Nginx implement port forwarding, took over these two days is a system of servers Ubuntu16.04 afternoon the teacher asked to run a project on this server port number to hide away, still as configured CentOS iptables to configure port forwarding. Hao Shi Hao Shi is found, but used iptables friends should also know that the iptables rules are set up with immediate effect, after the restart the machine, the configuration information in iptables will be cleared, so we generally preserved these configurations, so iptables loaded automatically at system startup, save each had to re-enter. But the problem lies in the set on restart to take effect, read a few blog know, there are many different places on Ubuntu and CentOS / RedHat configuration, iptables is a. Then deliberately write articles about the record, when used back to see later.
 

Under CentOS using iptables port forwarding

 
Here is the first to note that, CentOS7 and up use firewalld replaced iptables, if you want to use iptables need to manually stop firewalld, and the need to re-install iptables.

The first two steps are the steps to stop firewalld.service and re-install the iptables, CentOS7 version below can jump directly to the third step.

1. Stop firewalld.service (Note: The two commands have to perform, and requires root privileges)

systemctl stop firewalld.service
systemctl disable firewalld.service

2. Reinstall iptables

yum install iptables

3. Next, iptables is implemented using a port forwarding (here for the guide request for port 80 as an example 8080)

iptables -t -nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

4. Save the settings after

service iptables save

Not finished, in order to restart or after the entry into force, we have to execute the following two commands

systemctl restart iptables .service
systemctl enable iptables .service

 

Under Ubuntu using iptables port forwarding

 

First, the transfer request command with the same CentOS (here 80 to request for port 8080 is an example of the guide)

iptables -t -nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

Then, save the configuration information CentOS just not the same, here we need to save the firewall rules to /etc/iptables.up.rules file

iptables-save > /etc/iptables.up.rules

Then, modify the script / etc / network / interfaces, add a line at the end, application firewall rule upon network startup:

pre-up iptables-restore < /etc/iptables.up.rules

Guess you like

Origin blog.csdn.net/u013568373/article/details/91590938