iptables的简单用法和iptables实现外网连接内网

iptables的几种简单用法

1、拒绝所有主机ping当前的主机。

拒绝之前,先把自己允许了,
[ root@centos8-1 ~# iptables -AINPUT -s 10.0.0.1 -j REJECT
这就拒绝了所有主机ping我
[ root@centos8-1 ~# iptables -AINPUT -j  REJECT
查看定义的规则,就看到刚定义的规则了,注意顺序不能改变
[ root@centos8-1 ~# iptables -vnL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  133 11022 ACCEPT     all  --  *      *       10.0.0.1             0.0.0.0/0           
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination   

2、本机能够访问别的机器的HTTP服务,但是别的机器无法访问本机。

拒绝了所有网段的主机访问我的tcp80端口
[ root@centos8-1 /var/www/html# iptables -AINPUT  -p tcp --dport 80 -j REJECT

查看在防火墙上定义的规则,第二条是拒绝了所有主机访问我的http服务(http服务就是tcp 80端口)
[ root@centos8-1 /var/www/html# iptables -vnL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  789 58572 ACCEPT     all  --  *      *       10.0.0.1             0.0.0.0/0           
    0     0 REJECT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80 reject-with icmp-port-unreachable

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination       

3、当我们发现有 ip 恶意攻击我们得时候,我们可以通过对防火墙设定规则来进行控制。所以我们可以添加connlimit模块来实现对最大并发得控制。

10.0.0.18为本机,当任何主机通过INPUT并发连接我,连接的数量大于2,就拒绝
[ root@centos8-1 /var/www/html# iptables -AINPUT -d 10.0.0.18 -m connlimit --connlimit-above 2 -j REJECT

用iptables实现外网通过ssh连接内网

4、实践题

实验前提需求

 

 现在我在外地出差使用A7互联网主机,但是现在由于公司有业务需要我 ssh 链接到内网、这时候

我就联系我们公司同事在防火墙上配置相关规则让我链接进公司内网
A7:把网卡改为仅主机,ip地址改为192.168.1.128  网关指向192.168.1.129
A8:需要两块网卡一块仅主机ip设置为 192.168.1.129 ;另一块是nat,ip设置为10.0.0.8
B8:把网卡设置为nat ip为10.0.0.18
====================================================
A8:echo 1 > /proc/sys/net/ipv4/ip_forward
然后依次执行这三条命令
iptables -AFORWARD -j REJECT iptables -IFORWARD -s 192.168.1.128 -p tcp --dport 22 -j ACCEPT iptables -IFORWARD -d 192.168.1.128 -p tcp --sport 22 -j ACCEPT

 

posted @ 2020-09-19 15:51  天-王-盖-地-虎  阅读(850)  评论(0编辑  收藏  举报