1. 安装iptables-services :
yum install iptables-services
2. 修改iptables文件:
vi /etc/sysconfig/iptables
linux系统的端口设置在/etc/sysconfig/iptables文件中配置。使用编辑器打开该文件。内容如下:
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
如果我们需要对外开放8080端口,则上面文件中添加如下code
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
同时还需要注意的是,这段代码需要加入到
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
之后,否则端口也不能打开。最后的配如下:
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
3. 保存上面的文件后,在终端运行如下命令:更新防火墙配置
重启:
service iptables restart
4. 查看到开放的端口
/sbin/iptables -L -n
附:
开启:
service iptables start
关闭:
service iptables stop