iptables命令bash
-t |
指定表名称 |
DROP |
丢弃 |
-n |
不作解析 |
-N |
增长链 |
-L |
列出指定表的策略 |
-E |
修改链名称 |
-A |
增长策略 |
-X |
删除链 |
--dport |
端口 |
-D |
删除指定策略 |
-s |
数据来源 |
-I |
插入 |
-j |
动做 |
-R |
修改策略 |
ACCEPT |
容许 |
-P(大写) |
修改默认策略 |
REJECT |
拒绝 |
-p(小写) |
端口 |
例如:以下基本操做命令网络
iptables -t filter -nL #查看filter表中的策略 iptable -F #刷掉filter表中的全部策略,当没有用-t指定表名称时默认时filter service iptables save #保存当前策略 iptables -A INPUT -i lo -j ACCEPT #容许lo iptables -A INPUT -p tcp --dport 22 -j ACCEPT ##容许访问22端口 iptables -A INPUT -s 172.25.254.231 -j ACCEPT ##容许250主机访问本机全部端口 iptables -A INPUT -j REJECT ##拒绝全部主机的数据来源 iptables -N redhat ##增长链redhat iptables -E redhat westos ##改变链名称 iptables -X westos ##删除westos链 iptable -D INPUT 2 ##删除INPUT链中的第二条策略 iptables -I INPUT -p tcp --dport 80 -j REJECT ##插入策略到INPUT中的第一条 iptables -R INPUT 1 -p tcp --dport 80 -j ACCEPT ##修改第一条策略 iptable -P INPUT DROP ##把INPUT表中的默认策略改成drop
2.iptables火墙策略ssh
2.1 加快数据传输速度tcp
也就是当第一次数据传输被server接收以后,之后的数据若是是相同类型的,则就为RELATED和ESTABLISHED两种类型的,下面是此种火墙策略的简单例子。ide
RELATED:第二次工具
ESTABLISHED:正在创建链接的测试
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -A INPUT -i lo -m state --state NEW -j ACCEPT iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j ACCEPT iptables -A INPUT -p tcp --dport 80 -m state --state NEW -j ACCEPT iptables -A INPUT -p tcp --dport 443 -m state --state NEW -j ACCEPT iptables -A INPUT -p tcp --dport 53 -m state --state NEW -j ACCEPT iptables -A INPUT -j REJECT
2.2 SANT 火墙策略ui
SANT至关于一个漏由的功能,他就是将server做为一个网桥。至关与内网(client)和外网不一样,这时有一个漏由(server)能够链接外网(就是下面中的172.25.254的网段)
this
在server上做以下配置spa
配置server的网卡有两块,一块为私有网段,分别为: eth0:172.25.254.231 eth1:172.25.31.231 [root@server ~]# sysctl -a | grep forward net.ipv4.ip_forward = 0 #这个是主机的漏由功能,将下面的语句写入/etc/sysctl.conf,至关于打开主机的漏由功能 [root@server ~]# echo "net.ipv4.ip_forward = 1" >>/etc/sysctl.conf [root@server ~]# sysctl -p [root@server ~]# iptables -t nat -A POSTROUTING -o eth1 -j SNAT --to-source 172.25.254.231
在client 上做以下配置
配client的网卡为私有网段为172.25.31.131 添加网关:172.25.31.231
测试:当你没有做这个火墙策略,你ping不通172.25.254这个网段。除了server的IP。
作完SNAT策略以后你能够ping 172.25.254网段的ip的网段,你能够看到他是从172.25.254.231出去的。
用你client链接172.25.254.31时,会显示你是用172.25.254.231链接的,而不是你的client
2.3 DNAT 火墙策略
DNAT就是当有人链接你的主机的时候,你若是不想让他链接,你能够直接链接到其余的主机,就是将这个请求的目标地址转换成其余的目标地址
iptables -t nat -A PREROUTING -i eth0 -j DNAT --to-dest 172.25.31.131 #也就是有人链接我server上边eth0的时候我让他链接个人客户端。
用172.25.254.31链接你的server,他会链接到172.25.31.131上,就是你的client
2.firewall火墙策略
2.1 firewall的zone的分类
drop | 丢弃全部进入的包,而不给出任何响应 |
block |
拒绝全部外部发起的链接,容许内部发起的链接 |
public |
容许指定的进入链接 |
external |
出去的ipv4网络链接经过此区域假装和转发,仅接受ssh服务链接诶 |
dmz |
仅接受ssh服务链接 |
work |
通常用于工做区域,仅接受ssh ipp-client samba-client dhcpv6-client |
home |
同上,相似 用于家庭网络 |
internal |
同上,相似,用于内部网络 |
trusted |
信任全部链接 |
2.2 文件添加基本的火墙策略
在文件中/etc/firewalld/zones/public.xml 中就是你火墙开启的服务,以下举例
[root@client ~]# firewall-cmd --list-all public (default, active) interfaces: eth0 sources: services: dhcpv6-client ssh ports: masquerade: no forward-ports: icmp-blocks: rich rules: #这时咱们查看/etc/firewalld/zones/public.xml这个文件的内容 [root@client ~]# 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="dhcpv6-client"/> <service name="ssh"/> #这里就只容许ssh服务,若是咱们添加一个服务,http就直接将这个写入此文件中 <service name="http"/> </zone>
若是咱们打开http服务,这时火墙就会加载这个目录下的文件爱。
在 /usr/lib/firewalld/services/目录下有不少服务的配置文件,这里就不列出了,以http为例 [root@client ~]# ll /usr/lib/firewalld/services/ |grep http -rw-r-----. 1 root root 448 Feb 28 2014 https.xml -rw-r-----. 1 root root 353 Feb 28 2014 http.xml -rw-r-----. 1 root root 310 Feb 28 2014 wbem-https.xml [root@client ~]# cat /usr/lib/firewalld/services/http.xml <?xml version="1.0" encoding="utf-8"?> <service> <short>WWW (HTTP)</short> <description>HTTP is the protocol used to serve Web pages. If you plan to make your Web server publicly available, enable this option. This option is not required for viewing pages locally or developing Web pages.</description> <port protocol="tcp" port="80"/> </service> #这里面指明了http的端口和所使用的通讯协议。
2.3 用命令添加一些基本的火墙策略
[root@client ~]# firewall-cmd --get-zones ROL block dmz drop external home internal public trusted work #查看firewall的zone [root@client ~]# firewall-cmd --set-default-zone=trusted success #修改默认的firewall的zone [root@client ~]# firewall-cmd --list-all trusted (default, active) interfaces: eth0 sources: services: ports: masquerade: no forward-ports: icmp-blocks: rich rules: #查看firewall的火墙策略 [root@client ~]# firewall-cmd --reload success #从新加载firewall的火墙策略 [root@client ~]# firewall-cmd --complete-reload success #他也是从新加载火墙策略,就是他是即时生效。 [root@client ~]# firewall-cmd --permanent --add-port=8080/tcp success #将tcp协议的8080端口永久的加入到火墙策略中 #--permanent是永久修改的意思 [root@client ~]# firewall-cmd --permanent --add-source=172.25.254.231 --zone=trusted success #接受来自172.25.254.231的全部请求 [root@client ~]# firewall-cmd --permanent --add-interface=eth0 --zone=public success #将eth0网卡的zone类型永久修改为public类型的 [root@client ~]# firewall-cmd --permanent --remove-rich-rule="rule family=ipv4 source address=172.25.254.31 forward-port port=22 protocol=tcp to-port=22 to-addr=172.25.254.131" #永久删除一条rich rule策略
2.4 firewall Direct Rules工具添加火墙策略
[root@client ~]# firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 ! -s 172.25.254.231 -p tcp --dport 22 -j ACCEPT success #接受全部来自22端口的tcp协议请求,除了来自172.25.254.231这个主机的,可是也没有拒绝这台主机, #这条策略和他没有关系
2.4 firewall 的Rich Rules(就是iptables的SNAT和DNAT这里就再也不多讲)
[root@client ~]# firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=172.25.254.231 masquerade' success #让全部进入client的网从172.25.254.231出去 [root@client ~]# firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=172.25.254.31 forward-port port=22 protocol=tcp to-port=22 to-addr=172.25.254.131" success #从172.25.254.31来的22端口的tcp协议请求,将这个请求转发到172.25.254.131的22端口的tcp。