初识iptables规则

iptables出入是双向的,设置iptables规则需要双向的。

服务端:172.16.16.102 IDC-16-102

客户端:172.16.16.104 IDC104

 

1、保存对防火墙的设置

[root@IDC-16-102 ~]# serivce iptables save 写入永久文件

执行该命令,临时iptables规则将写入/etc/sysconfig/iptables文件,重启之后永久生效,需要添加iptables开机启动项

[root@IDC-16-102 ~]# chkconfig iptables on 加入开机启动项

 

2、查看iptables规则及编号

[root@IDC-16-102 ~]# iptables -nvL --line-nu 查看规则

[root@IDC-16-102 ~]# iptables -D INPUT 3 删除规则

[root@IDC-16-102 ~]# iptables -D OUTPUT 3 删除规则

 

3、关闭所有的INPUT FORWARD(转发) OUTPUT的所有端口

[root@IDC-16-102 ~]# iptables -P INPUT DROP

[root@IDC-16-102 ~]# iptables -P FORWARD DROP

[root@IDC-16-102 ~]# iptables -P OUTPUT DROP

关闭后所有的ip都不可以连接进来,只能本地登录,外面ping不进来,本地也ping不出去,ssh出入也不正常

 

4、打开22端口ssh连接,需要在本机服务端设置

允许172.16.16.104连接102服务器

[root@IDC-16-102 ~]# iptables -A INPUT -p tcp -s 172.16.16.104 --dport 22 -j ACCEPT

[root@IDC-16-102 ~]# iptables -A OUTPUT -p tcp --sport 22 -d 172.16.16.104 -j ACCEPT

允许所有远程ip访问102服务端

[root@IDC-16-102 ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT

[root@IDC-16-102 ~]# iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT

 

 

5、打开ping机制

双向ping通:允许客户端172.16.16.104ping102

[root@IDC-16-102 ~]# iptables -A INPUT -s 172.16.16.104 -d 172.16.16.102 -j ACCEPT

[root@IDC-16-102 ~]# iptables -A OUTPUT -s 172.16.16.102 -d 172.16.16.104 -j ACCEPT

 

单项ping通:(102ping104,104ping不通102)

 

6、开启上网,优先清空规则

以上操作会导致服务器102不能下载,建议清空规则开始下载

[root@IDC-16-102 ~]# iptables -A INPUT -d 172.16.16.102 -j ACCEPT

[root@IDC-16-102 ~]# iptables -A OUTPUT -s 172.16.16.102 -j ACCEPT

[root@IDC-16-102 ~]# ping 8.8.8.8

64 bytes from 8.8.8.8: icmp_seq=1 ttl=44 time=19.4 ms

[root@IDC-16-102 ~]# ping www.baidu.com

64 bytes from 111.13.100.92: icmp_seq=1 ttl=51 time=57.0 ms

 

7、开启80端口访问

目前只有这一种办法,其他添加两条规则的经测试均不适用。

[root@IDC-16-102 ~]# egrep -v "#|^$" /etc/sysconfig/iptables 添加80规则,21端口也适用

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

[root@IDC-16-102 ~]# /etc/init.d/iptables restart 重启防火墙

[root@IDC104 ~]# nmap 172.16.16.102 开始嗅探,端口是否关闭

Nmap scan report for 172.16.16.102

PORT STATE SERVICE

22/tcp open ssh

80/tcp open http

 

posted @ 2017-05-14 22:32  wang_wei123  阅读(333)  评论(0编辑  收藏  举报