Linux端口转发,端口映射,Linux本地端口转发Centos 6.9
系统:Centos 6.9/Centos 7
摘要:端口转发也称端口映射,基本使用的有两种方式,本机端口转发
分析:tomcat的默认端口是8080,所有用户都可以启动,但是除内部系统外,一般我们会选择使用80作为端口,可是普通用户没有对80的权限。怎么办,这个时候可以提前把80端口映射到8080端口,当tomcat以8080端口启动后就可以使用80端口访问。
具体配置:
[root@localhost sbin]# iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080
[root@localhost sbin]# service iptables save
[root@localhost sbin]# service iptables restart
会返回如下信息:
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
把上面文件添加到自动启动文件
/etc/rc.d/rc.local
启动加权限:chmod +x /etc/rc.d/rc.local
这说明修改了iptables文件了,可以放心使用。清楚的知道规则报错到了/etc/sysconfig/iptables文件。
或者编辑文件
/etc/sysconfig/iptables
-A PREROUTING -p tcp -m tcp --dport 访问输出端口 -j REDIRECT --to-ports 输入目标端口
-A PREROUTING -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8080
配置完成后,记着重启防火墙
# 关闭防火墙
service iptables stop
[root@localhost sbin]# service iptables save 开启
[root@localhost sbin]# service iptables restart 重启
[root@localhost sbin]# service iptables stop 停止
然后就可以使用了。
查端口的情况。
命令为:netstat -ntlp
这就是Linux系统的端口转发
遇到的问题
端口映射设置好是成功的,可是过一会就失效。发现了一个问题
系统的iptables服务没有启动,比如可以使用如下命令查看:
# chkconfig
找到第6行,发现iptables对应的level开关都是off,这个时候要把off改成on即可。
# chkconfig --help
chkconfig version 1.3.49.3 - Copyright (C) 1997-2000 Red Hat, Inc.
This may be freely redistributed under the terms of the GNU Public License.
usage: chkconfig [--list] [--type ] [name]
chkconfig --add
chkconfig --del
chkconfig --override
chkconfig [--level ] [--type ]
使用命令:
# chkconfig --level 012345 iptables on
重新查看:
# chkconfig
这个时候就解决问题了。