iptables小案例,nat表应用

iptables小案例:

需求1: 只针对filter表,预设INPUT链DROP,其他两个链ACCEPT,然后针对192.18.133.0/24开通22端口,对所有网段开放80端口,对所有网段开放21端口。

vim /usr/local/sbin/iptables.sh //加入以下内容

#! /bin/bash

ipt="/usr/sbin/iptables"

$ipt -F

$ipt -P INPUT DROP

$ipt -P OUTPUT ACCEPT

$ipt -P FORWARD ACCEPT

$ipt -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT 

$ipt -A INPUT -s 192.168.133.0/24 -p tcp --dport 22 -j ACCEPT

$ipt -A INPUT -p tcp --dport 80 -j ACCEPT

$ipt -A INPUT -p tcp --dport 21 -j ACCEPT 

 

完成编写后执行sh /usr/sbin/iptables.sh即可

-m state --state RELATED,ESTABLISHED 表示对进来的包的状态进行检测。已经建立tcp连接的包以及该连接相关的包允许通过!为了后面的22 80 21等端口更顺畅的通信。

 

icmp示例:

iptables -I INPUT -p icmp --icmp-type 8 -j DROP

这里--icmp-type选项要跟-p icmp一起使用,后面指定类型编号。这个8指的是能在本机ping通其他机器,而其他机器不能ping通本机。

 

nat表应用:

• A机器两块网卡ens33(192.168.133.130)、ens37(192.168.100.1),ens33可以上外网,ens37仅仅是内部网络,B机器只有ens37(192.168.100.100),和A机器ens37可以通信互联。

• 需求1:可以让B机器连接外网

• A机器上打开路由转发 echo "1">/proc/sys/net/ipv4/ip_forward

• A上执行 iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE

• B上设置网关为192.168.100.1

 

• 需求2:C机器只能和A通信,让C机器可以直接连通B机器的22端口

• A上打开路由转发echo "1">/ proc/sys/net/ipv4/ip_forward

•  A上执行iptables -t nat -A PREROUTING -d 192.168.133.130 -p tcp --dport 1122 -j DNAT --to 192.168.100.100:22

• A上执行iptables -t nat -A POSTROUTING -s 192.168.100.100 -j SNAT --to 192.168.133.130

• B上设置网关为192.168.100.1

 

netfilter保存和备份:

service iptables save 会把规则保存到/etc/sysconfig/iptables

 

把iptables规则备份到my.ipt文件中:

iptables-save > my.ipt

恢复刚才备份到规则:

iptables-restore < my.ipt

 

Linux防火墙-firewalld:

firewalld 9个zone

打开firewalld:

systemctl disable iptables

systemctl stop iptables

systemctl enable firewalld

systemctl start firewalld

 

firewalld默认有9个zone

默认zone为public

 

firewall-cmd --get-zones //查看所有zone

[root@gavin-123 ~]# firewall-cmd --get-zones

block dmz drop external home internal public trusted work

 

• firewall-cmd --get-default-zone//查看默认zone

[root@learnlinux ~]# firewall-cmd --get-default-zone

public

 

 

firewalld关于zone的操作:

firewall-cmd --set-default-zone=work //设定默认zone

[root@learnlinux ~]# firewall-cmd --set-default-zone=work

success

 

firewall-cmd --get-zone-of-interface=ens33 //查指定网卡

[root@learnlinux ~]# firewall-cmd --get-zone-of-interface=eth0

no zone

 

firewall-cmd --zone=public --add-interface=lo //给指定网卡设置zone

[root@gavin-123 ~]# firewall-cmd --zone=public --add-interface=lo

success

 

firewall-cmd --zone=dmz --change-interface=lo //针对网卡更改zone

[root@gavin-123 ~]# firewall-cmd --zone=work --change-interface=enp0s3

success

 

firewall-cmd --zone=dmz  --remove-interface=lo  //针对网卡删除zone

[root@gavin-123 ~]# firewall-cmd --zone=public --remove-interface=lo

success

[root@gavin-123 ~]# firewall-cmd --get-zone-of-interface=lo

no zone

 

firewall-cmd --get-active-zones  //查看系统所有网卡所在的zone

[root@gavin-123 ~]# firewall-cmd --get-active-zones

work

interfaces: enp0s3

public

interfaces: lo

 

firewalld关于service的操作:

firewall-cmd --get-services  查看所有的servies

[root@gavin-123 ~]# firewall-cmd --get-services

RH-Satellite-6 amanda-client bacula bacula-client dhcp dhcpv6 dhcpv6-client dns ftp high-availability http https imaps ipp ipp-client ipsec kerberos kpasswd ldap ldaps libvirt libvirt-tls mdns mountd ms-wbt mysql nfs ntp openvpn pmcd pmproxy pmwebapi pmwebapis pop3s postgresql proxy-dhcp radius rpc-bind samba samba-client smtp ssh telnet tftp tftp-client transmission-client vnc-server wbem-https

 

firewall-cmd --list-services  //查看当前zone下有哪些service

[root@gavin-123 ~]# firewall-cmd --list-services

dhcpv6-client http ssh

 

firewall-cmd --zone=work --list-services  //查看指定zone下有哪些service

[root@gavin-123 ~]# firewall-cmd --zone=work --list-services

dhcpv6-client ftp ipp-client ssh

 

firewall-cmd --zone=public --add-service=http //把http增加到public zone下面

[root@gavin-123 ~]# firewall-cmd --zone=public --add-service=ftp

success

 

firewall-cmd --zone=public --remove-service=http

[root@gavin-123 ~]# firewall-cmd --zone=public --remove-service=http

success

 

ls /usr/lib/firewalld/zones/ //zone的配置文件模板

[root@gavin-123 ~]# ls /usr/lib/firewalld/zones/

block.xml drop.xml home.xml public.xml work.xml

dmz.xml external.xml internal.xml trusted.xml

 

firewall-cmd --zone=public --add-service=http --permanent //更改配置文件,之后会在/etc/firewalld/zones目录下面生成配置文件

[root@gavin-123 ~]# firewall-cmd --zone=public --add-service=ftp --permanent

success

[root@gavin-123 ~]# cat /etc/firewalld/zones/public.xml

<?xml version="1.0" encoding="utf-8"?>

<zone>

<short>Public</short>

<description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>

<service name="ftp"/>

<service name="dhcpv6-client"/>

<service name="http"/>

<service name="ssh"/>

</zone>

 

需求:ftp服务自定义端口1121,需要在work zone下面放行ftp

cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services

 

vi /etc/firewalld/services/ftp.xml //把21改为1121

 

cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/

 

vi /etc/firewalld/zones/work.xml //增加一行

<service name="ftp"/>

 

firewall-cmd --reload //重新加载

 

firewall-cmd --zone=work --list-services

 

 

 

转载于:https://my.oschina.net/u/3803404/blog/1809324

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值