iptables基础实战练习

(1) 放行ssh (端口:22)

1 iptables -A INPUT -d 192.168.42.153 -p tcp --dport 22 -j ACCEPT
2 iptables -A  OUTPUT -s  192.168.42.153  -p tcp  --sport  22 -j ACCEPT

(2)修改默认规则链(关闭所有端口)

1 iptables -P INPUT DROP
2 iptables -P OUTPUT DROP
3 iptables -P FORWARD DROP

(3)放行web(80)端口 httpd nginx

1 iptables -I INPUT -d 192.168.42.153 -p tcp --dport 80 -j ACCEPT
2 iptables -I OUTPUT -s 192.168.42.153 -p tcp --sport 80 -j ACCEPT

(4)修改默认规则链后,我们发现ping不通自己,也ping不通别的主机

1 iptables -t filter -I INPUT -s 127.0.0.1 -d 127.0.0.1 -i lo  -j ACCEPT 
2 iptables -t filter -I OUTPUT -s 127.0.0.1 -d 127.0.0.1 -o lo  -j ACCEPT

(5)允许自己ping别的主机

1 iptables -t filter -I OUTPUT -s 192.168.42.153 -d 0/0  -p icmp --icmp-type 8 -j ACCEPT
2 iptables -t filter -I INPUT -s 0/0 -d 192.168.42.153 -p icmp --icmp-type 0 -j ACCEPT

(6)允许任何人来ping本机

1 iptables -t filter -I INPUT -s 0/0 -d 192.168.42.153 -p icmp --icmp-type 8 -j ACCEPT
2 iptables -t filter -I OUTPUT -s 192.168.42.153 -d 0/0  -p icmp --icmp-type 0 -j ACCEPT

(7)同时开发多个端口(多端口匹配)

1 iptables -I INPUT -s 0/0 -d 192.168.42.153 -p tcp -m multiport --dports 22,80,3306 -j ACCEPT
2 iptables -I INPUT -d 0/0 -s 192.168.42.153 -p tcp -m multiport --sports 22,80,3306 -j ACCEPT

(8)iptables -vnL –line-numbers #显示数字

iptables  -vnL INPUT  --line-numbers 
Chain INPUT (policy DROP 1 packets, 229 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        8   576 ACCEPT     icmp --  *      *       0.0.0.0/0            192.168.42.153       icmptype 8
2       12  1008 ACCEPT     icmp --  *      *       0.0.0.0/0            192.168.42.153       icmptype 0
3       16  1226 ACCEPT     all  --  lo     *       127.0.0.1            127.0.0.1           
4       88  7565 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.42.153       tcp dpt:80
5     2135  163K ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.42.153       tcp dpt:22

(9) 源地址,目的地址范围匹配

1 iptables -I INPUT -d 192.168.42.153 -p tcp --dport 23 -m iprange --src-range 192.168.42.150-192.168.42.158 -j ACCEPT
2 iptables -I OUTPUT -s 192.168.42.153 -p tcp --dport 23 -m iprange --dst-range  192.168.42.150-192.168.42.158 -j ACCEPT

(10)禁止包含”old”字符的页面出来

1 iptables -I OUTPUT -s 192.168.42.153 -d 0/0 -p tcp --sport 80 -m string --algo bm --string "old" -j DROP

(11)基于时间限定,9点到19点,禁止访问80端口

1 iptables -I INPUT -s 0/0  -d 192.168.42.153 -p tcp --dport 80  -m time --timestart 09:00:00 --timestop 19:00:00 --kerneltz  -j DROP

(12)周一到周五9点到19点禁止访问80端口

1 iptables -I INPUT  -d 192.168.42.153 -p tcp --dport 80  -m time --timestart 09:00:00 --timestop 19:00:00 --kerneltz --weekdays 1,2,3,4,5  -j DROP

(13)端口大于2个并发连接(禁止)

1 iptables -I INPUT -s 0/0 -d 192.168.42.153 -p tcp  --dport 22 -m connlimit --connlimit-above 2 -j DROP

(14)端口同一个客户端小于3个并发连接

1 iptables -I INPUT -s 0/0 -d 192.168.42.153 -p tcp  --dport 22 -m connlimit ! --connlimit-above 3 -j DROP

 

(15)目标地址和端口转换示例(对22端口的转换)

1 iptables -t nat -A PREROUTING -d 10.1.249.125 -p tcp --dport 22022 -j DNAT --to-destination 192.168.2.4:22

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/86859

赞 (2)
sunhaosunhao
上一篇 2017-09-10 20:49
下一篇 2017-09-10 22:51

相关推荐

  • rpm与yum的简单命令

                                  …

    Linux干货 2017-04-18
  • N22-第十三周作业

    1、建立samba共享,共享目录为/data,要求:(描述完整的过程)  1)共享名为shared,工作组为magedu;  2)添加组develop,添加用户gentoo,centos和ubuntu,其中gentoo和centos以develop为附加组,ubuntu不属于develop组;密码均为用户名;  3)添加samb…

    Linux干货 2016-11-14
  • 第三周作业

    1、列出当前系统上所有已登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 ~]# who | cut -d ' ' -f 1 | sort -u 2、取出最后登录到当前系统的用户的相关信息。 ~]# la…

    Linux干货 2016-12-19
  • find查找命令及压缩

    文件查找     在文件系统上查找符合条件的文件;     文件查找: locate, find         非实时查找(数据库查找): locate    &nbs…

    Linux干货 2016-08-18
  • 8月3日作业

    文件权限相关的课堂练习: 1)当用户xiaoming对/testdir 目录无执行权限时,意味着无法做哪些操作?     用户小明不能进入/testdir,不能删除,不能创建。使用ll能看到有哪些文件,但是看不到具体信息 2)当用户xiaoqiang对/testdir 目录无读权限时,意味着无法做哪些操作? 不能查看文…

    Linux干货 2016-08-07
  • 第十六周

    1、源码编译安装LNMP架构环境;  安装必要的包组和依赖的包 yum groupinstall “Development Tools” “Development Libraries”  yum install wget openssl-devel ncurses-devel cmake p…

    Linux干货 2017-08-15