七周三次课

七周三次课html

10.11 Linux网络相关linux

10.12 firewalld和netfiltervim

10.13 netfilter5表5链介绍windows

10.14 iptables语法centos

10.11 Linux网络相关安全

ifconfig命令bash

查看网卡IP服务器

若是系统没有该命令可使用yum安装:网络

[root@tianqi-01 ~]# yum install -y net-toolsless

查看网卡IP还可使用命令:ip add,这个显示有点乱!

ifconfig -a

显示全部网卡信息(包括down掉的或者没有IP地址的网卡)

启动/关闭网卡

ifup/ifdown [网卡名]

应用环境:更改单个指定网卡配置后须要重启才能生效,为了不关闭或重启全部网卡,能够对单个网卡执行该命令。
注: 若是网卡正在使用中,不要单独使用ifdown命令!!!解决办法: “# ifdown ens33 && ifup ens33”,使两个命令一块儿执行!

为虚拟机添加网卡

在linux系统中,网卡是能够设定多重IP的。

如今想增长一个IP,就须要增长一个虚拟网卡。

[root@tianqi-01 ~]# cd /etc/sysconfig/network-scripts
[root@tianqi-01 network-scripts]# cp ifcfg-ens33 ifcfg-ens33\:0

[root@tianqi-01 network-scripts]# ls
ifcfg-ens33    ifdown-bnep  ifdown-isdn    ifdown-sit       ifup          ifup-ippp  ifup-plusb   ifup-sit       ifup-wireless
ifcfg-ens33:0  ifdown-eth   ifdown-post    ifdown-Team      ifup-aliases  ifup-ipv6  ifup-post    ifup-Team      init.ipv6-global
ifcfg-lo       ifdown-ippp  ifdown-ppp     ifdown-TeamPort  ifup-bnep     ifup-isdn  ifup-ppp     ifup-TeamPort  network-functions
ifdown         ifdown-ipv6  ifdown-routes  ifdown-tunnel    ifup-eth      ifup-plip  ifup-routes  ifup-tunnel    network-functions-ipv6

#之因此加\,是为了脱义:,否则在linux命令行下面没法识别。而后编辑ifcfg-ens33\:0,必定要把DEVICE改为ens33\:0.

[root@tianqi-01 network-scripts]# vim ifcfg-ens33:0

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33:0
UUID=bd1e1864-e75b-4f5f-bc63-9b1b0d64feff
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.11.139
NETMASK=255.255.255.0
GATEWAY=192.168.11.2

[root@tianqi-01 network-scripts]# ifdown ens33 && ifup ens33
Device 'ens33' successfully disconnected.
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2)

[root@tianqi-01 network-scripts]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.11.136  netmask 255.255.255.0  broadcast 192.168.11.255
        inet6 fe80::1eb9:8f9e:264a:7159  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:08:64:43  txqueuelen 1000  (Ethernet)
        RX packets 1503  bytes 133236 (130.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1210  bytes 115291 (112.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.11.139  netmask 255.255.255.0  broadcast 192.168.11.255
        ether 00:0c:29:08:64:43  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

上面就多了一个网卡,看看windows能不能ping通192.168.11.139,发现是能够ping通的,这就证实没问题。

• mii-tool ens33 查看网卡是否链接

查看服务器是否连着网线,在机房的话,能够看网卡的灯是否是亮着,在系统里面的话,使用上面的命令。

[root@tianqi-01 network-scripts]# mii-tool ens33
ens33: negotiated 1000baseT-FD flow-control, link ok

这里显示link ok,说明网卡为链接状态。若是显示no link,就说明网卡坏了或者没有链接网线。若是显示not support,可使用下面一个命令。

• ethtool ens33 也能够查看网卡是否链接

[root@tianqi-01 network-scripts]# ethtool ens33
Settings for ens33:
    Supported ports: [ TP ]
    Supported link modes:   10baseT/Half 10baseT/Full 
                            100baseT/Half 100baseT/Full 
                            1000baseT/Full 
    Supported pause frame use: No
    Supports auto-negotiation: Yes
    Advertised link modes:  10baseT/Half 10baseT/Full 
                            100baseT/Half 100baseT/Full 
                            1000baseT/Full 
    Advertised pause frame use: No
    Advertised auto-negotiation: Yes
    Speed: 1000Mb/s
    Duplex: Full
    Port: Twisted Pair
    PHYAD: 0
    Transceiver: internal
    Auto-negotiation: on
    MDI-X: off (auto)
    Supports Wake-on: d
    Wake-on: d
    Current message level: 0x00000007 (7)
                   drv probe link
    Link detected: yes

若是网卡没有链接,最下面一行Link detected显示为no。

• 更改主机名 hostnamectl set-hostname aminglinux

[root@tianqi-01 network-scripts]# hostname
tianqi-01
[root@tianqi-01 network-scripts]# hostnamectl set-hostname tianqi
[root@tianqi-01 network-scripts]# bash
#这里root@tianqi-01没有发生变化,能够退出从新登录或者bash命令进入子目录。

[root@tianqi network-scripts]# cat /etc/hostname
tianqi

• DNS配置文件/etc/resolv.conf

DNS是用来解析域名的,平时咱们访问网站都是直接输入一个网址,而DNS把这个网址解析到一个IP。

在linux下设置DNS很是简单,只要把DNS地址写到配置文件/etc/resolv.conf中便可。

[root@tianqi-01 ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 119.29.29.29

第一行是个注释,说明这个配置文件中的DNS IP地址是由NetworkManager服务生成的。NetworkManager是一个集成的linux网络管理器,不经常使用。resolv.conf有它固定的格式,必定要写成

nameserver IP的格式。能够多写几个nameserver,系统默认会用第一个nameserver去解析域名,当第一个解析不成功时会使用第二个。

若是只是临时修改DNS IP地址,就直接修改/etc/resolv.conf;若是是永久生效的话,仍是要修改网卡的配置文件。

[root@tianqi-01 network-scripts]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=bd1e1864-e75b-4f5f-bc63-9b1b0d64feff
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.11.136
NETMASK=255.255.255.0
GATEWAY=192.168.11.2
DNS1=119.29.29.29
DNS2=8.8.8.8        #这个是谷歌DNS

[root@tianqi-01 network-scripts]# ifdown ens33 && ifup ens33
Device 'ens33' successfully disconnected.
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)

[root@tianqi-01 ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 119.29.29.29
nameserver 8.8.8.8

/etc/hosts文件是Linux、Windows都有的,用于指定域名访问的IP地址:

在linux下还有一个特殊文件也能解析域名,不过须要咱们在里面手动添加IP地址和域名。它的做用是临时解析某个域名,很是有用。

[root@tianqi-01 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

如今ping一下qq,如今发现是公网IP。

[root@tianqi-01 network-scripts]# ping www.qq123.com
PING www.qq123.com (211.155.235.108) 56(84) bytes of data.
64 bytes from 211.155.235.108 (211.155.235.108): icmp_seq=1 ttl=128 time=33.8 ms
64 bytes from 211.155.235.108 (211.155.235.108): icmp_seq=2 ttl=128 time=30.0 ms
64 bytes from 211.155.235.108 (211.155.235.108): icmp_seq=3 ttl=128 time=30.1 ms
64 bytes from 211.155.235.108 (211.155.235.108): icmp_seq=4 ttl=128 time=29.9 ms
64 bytes from 211.155.235.108 (211.155.235.108): icmp_seq=5 ttl=128 time=38.9 ms
^C
--- www.qq123.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4030ms
rtt min/avg/max/mdev = 29.984/32.593/38.929/3.493 ms
例如我修改hosts文件指定一个域名的ip:

[root@tianqi-01 network-scripts]# vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 www.test.com

[root@tianqi-01 network-scripts]# ping www.test.com
PING www.test.com (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.070 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.085 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.130 ms
^C
--- www.test.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2020ms
rtt min/avg/max/mdev = 0.070/0.095/0.130/0.025 ms

ping这个域名时就会访问127.0.0.1 IP地址

一个IP能够指定多个域名:

[root@tianqi-01 network-scripts]# vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 www.test.com www.123.com www.0000000.com www.8888.com

[root@tianqi-01 network-scripts]# ping www.8888.com
PING www.test.com (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.058 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.057 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.057 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.058 ms
^C
--- www.test.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3018ms
rtt min/avg/max/mdev = 0.057/0.057/0.058/0.007 ms

若是有两个一样的域名指定了不一样的IP,以第一个的那个IP为准:

[root@tianqi-01 network-scripts]# vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 www.test.com www.123.com www.0000000.com www.8888.com
192.168.11.136 www.123.com

[root@tianqi-01 network-scripts]# ping www.123.com
PING www.123.com (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.067 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.186 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.057 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.057 ms
^C
--- www.123.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3000ms
rtt min/avg/max/mdev = 0.057/0.091/0.186/0.055 ms
[root@tianqi-01 network-scripts]# 

10.12 firewalld和netfilter

selinux是linux系统特有的安全机制,安装完系统后,通常要把selinux关闭,以避免引发没必要要的麻烦。

•selinux临时关闭 setenforce 0

这仅仅是临时的,要想永久关闭须要更改配置文件,须要把SELINUX=enforcing改为

SELINUX=disabled。

更改完配置文件之后,重启系统方可生效。可使用getenforce命令得到当前selinux的状态。

[root@tianqi-01 network-scripts]# vim /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

[root@tianqi-01 network-scripts]# getenforce
Enforcing
[root@tianqi-01 network-scripts]# setenforce 0
[root@tianqi-01 network-scripts]# getenforce

Permissive

• centos7以前使用netfilter防火墙,centos7的防火墙是firewalld,iptables是其中的工具。工具用法是同样的,用这个工具能够添加一些规则,好比关闭和开启一些端口。

在centos7上使用centos6的防火墙机制也是没有问题的。

一、关闭firewalld服务

[root@tianqi-01 network-scripts]# systemctl disable firewalld    //停掉firewalld,就是限制开机启动
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

[root@tianqi-02 ~]# systemctl stop firewalld    //关闭firewalld服务

2.安装iptables-services包:

开启netfilter

在开启以前,须要先安装一个iptables-services包

[root@tianqi-01 network-scripts]# yum install -y iptables-services
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile

这样就可使用以前的iptables了

安装完以后就会有一个iptables服务:

[root@tianqi-01 network-scripts]# systemctl enable iptables    //设置开机启动
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.

3.开启iptables服务:

[root@tianqi-01 ~]# systemctl start iptables

4.使用iptables -nvL命令查看iptables服务自带的一些规则:

[root@tianqi-01 network-scripts]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   27  1808 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

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

10.13 netfilter5表5链介绍

• netfilter的5个表

• filter表用于过滤包,最经常使用的表,有INPUT、FORWARD、OUTPUT三个链

• nat表用于网络地址转换,有PREROUTING、OUTPUT、POSTROUTING三个链

• managle表用于给数据包作标记,几乎用不到

• raw表能够实现不追踪某些数据包,阿铭历来不用

• security表在centos6中并无,用于强制访问控制(MAC)的网络规则,阿铭没用过

• 参考文章 http://www.cnblogs.com/metoy/p/4320813.html

netfilter的五个表 

  • filter表,就是默认的一个表,包含了三个内置的链:INPUT、FORWARD、OUTPUT
    • INPUT链,表示数据进来的包进来要通过的一个链,进入到本机
      • 好比,进入到本机后,将80端口进来的数据包,访问80端口的数据包检查下它的原IP是什么,发现可疑的IP须要禁掉
    • FORWARD链,这个数据包到了机器,并不会进入内核里,由于这个数据包不是给你处理的,而是给另一台机器处理的,因此这时候须要判断下你的目标地址是否为本机,若是不是本机,则须要通过FORWARD这个链
      • 在通过 FORWARD链的时候,也会作一些操做,把目标地址作一些更改,或者作一个转发
    • OUTPUT链,是在本机产生的一些包,在出去以前作的一些操做
      • 好比,这个包是发给某一个IP的,这个IP我要禁掉,不让这个包过去(已加入到黑名单),只要是到那个IP的,都给禁掉。
  • nat表,也有三个链PREROUTING 、OUTPUT、POSTROUTING
    • PREROUTING链,这个链用来更改这个数据包——>在进来的那一刻就去更改
    • OUTPUT链,它和上面filter表中的OUTPUT链是同样的
    • POSTROUTING链,这个链也是更改数据包——>在出去的那一刻更改
  • nat表,使用案列
    • 路由器的实现的共享上网就是nat实现的
    • 端口映射
  • mangle表和raw表和security表几乎用不到

netfilter的五个链 
PREROUTING:数据包进入路由表以前 
INPUT:经过路由表后目的地为本机 
FORWARDING:经过路由表后,目的地不为本机 
OUTPUT:由本机产生,向外转发 
POSTROUTING:发送到网卡接口以前

iptables传输数据包的过程 

① 当一个数据包进入网卡时,它首先进入PREROUTING链,内核根据数据包目的IP判断是否须要转送出去。 
② 若是数据包就是进入本机的,它就会沿着图向下移动,到达INPUT链。数据包到了INPUT链后,任何进程都会收到它。本机上运行的程序能够发送数据包,这些数据包会通过OUTPUT链,而后到达POSTROUTING链输出。 
③ 若是数据包是要转发出去的,且内核容许转发,数据包就会如图所示向右移动,通过FORWARD链,而后到达POSTROUTING链输出。 

这里写图片描述

这里写图片描述

  • 总结:
  1. 若是是本机的,则会通过PREROUTING链--->INPUT链--->OUTPUT链--->POSTROUTING链
  2. 若是不是本机的,则会通过PREROUTING链--->FORWARD链--->POSTROUTING链

学习扩展连接文章,能够帮助咱们更好的学习iptables的表和链。

10.14 iptables语法

•数据包流向与netfilter的5个链

• PREROUTING:数据包进入路由表以前

• INPUT:经过路由表后目的地为本机

• FORWARD:经过路由表后,目的地不为本机

• OUTPUT:由本机产生,向外发出

• POSTROUTING:发送到网卡接口以前

iptables的默认规则在/etc/sysconfig/iptables文件里保存着:

[root@tianqi-01 network-scripts]# cat /etc/sysconfig/iptables
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

清除规则使用iptables -F命令:

这样虽然清空了规则,可是默认规则还会保存在/etc/sysconfig/iptables文件里。

想要当前的规则保存到/etc/sysconfig/iptables文件里就要使用service iptables save命令:

若是没有保存到/etc/sysconfig/iptables文件里的话,重启服务后就会从新加载/etc/sysconfig/iptables文件里的规则:

[root@tianqi-01 network-scripts]# iptables -nvL    //查看iptables默认规则
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  107  7496 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

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

  • service iptables restart 重启iptables规则

[root@tianqi-02 ~]# service iptables restart
Redirecting to /bin/systemctl restart  iptables.service
[root@tianqi-02 ~]# 

存放默认规则的位置

  • /etc/sysconfig/iptables

[root@tianqi-02 ~]# cat /etc/sysconfig/iptables
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
[root@tianqi-02 ~]# 

iptables -F清空规则

  • iptables -F清空规则
    • 在清空规则后,再去查看,会发现没有规则了(可是在文件中依旧保存这规则)

[root@tianqi-02 ~]# iptables -F
[root@tianqi-02 ~]# iptables -nvL

Chain INPUT (policy ACCEPT 28 packets, 1848 bytes)
 pkts bytes target     prot opt in     out     source               destination         

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

Chain OUTPUT (policy ACCEPT 15 packets, 1412 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@tianqi-02 ~]# 

  • service iptables save 保存规则

    • 如果在清空规则后,去执行service iptables save保存规则,那存放规则的文件也会变成所保存的规则
  • 在iptables -F清空规则后,重启service restart iptables.service(重启服务器或者iptables规则),都会加载配置文件里面的规则

    • 在重启规则后,会看到原先被清空的规则从新加载了

[root@tianqi-02 ~]# service iptables restart
Redirecting to /bin/systemctl restart  iptables.service
[root@tianqi-02 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   28  1848 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 15 packets, 1412 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@tianqi-02 ~]# 

目前咱们作的这些操做都是针对的filter表,这是默认的表。

查看nat表的规则:

[root@tianqi-01 network-scripts]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain INPUT (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         

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@tianqi-01 network-scripts]# 

使用iptables -nvL命令显示的规则里,能够看到第一行是有数字的,加上-Z选项能够将计数器清零:

iptables -Z 把计数器清零

  • iptables -Z 把计数器清零
    • 在查看filter表的时候,会看到第一列和第二列都是有数据的
      • 第一列,是有多少个包
      • 第二列,是数据量,数据大小(单位:bytes字节)

[root@tianqi-01 network-scripts]# iptables -Z;iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@tianqi-01 network-scripts]# 

  • 这里会看到数字都清零了,但过一会再来查看,会看到数字又出现了(由于在每时每刻都在通讯)

[root@tianqi-02 ~]# iptables -t filter -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   49  3244 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
    1   229 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 27 packets, 3484 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@tianqi-02 ~]# 

清零是为了在某些需求下,能够计算某个时间段某个ip传送过来的数据包数量。

iptables新增规则 -A

  • 在iptables命令中,没有-t 指定表的时候,默认就是filter表
  • iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
    • -A,就是增长一条规则(这里针对的是INPUT链)
      • 新增的规则会在规则的最后面
    • -s ,指定来源IP
    • -p,指定它的协议,是TCP,仍是UDP,或者是ICMP协议
    • -sport,来源的端口
    • -d,指目标的IP
    • -dport,指目标的端口
    • -j,操做
    • DROP,扔掉
    • REJECT,拒绝
  • DROP扔掉和REJECT拒绝,最终实现的效果是同样的,都是为了让数据包过不来,至关于把IP给封掉
  • DROP和REJECT区别:
    • DROP,在这个数据包来了以后,看都不看直接扔掉
    • REJECT,在这个数据包来了以后,先看一看,看完以后,在拒绝

添加规则示例:

[root@tianqi-01 network-scripts]#  iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
[root@tianqi-01 network-scripts]# iptables -nvL

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   10   760 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
    0     0 DROP       tcp  --  *      *       192.168.188.1        192.168.188.128      tcp spt:1234 dpt:80

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 3 packets, 356 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@tianqi-01 network-scripts]# 

iptables命令 参数 -I

  • iptables -I INPUT -p tcp --dport 80 -j DROP
    • 这里是简写,不指定来源IP,和目标IP,只写目标的端口——>但必定要指定tcp/ip
    • 如果使用了 dport 或 sport ,那么前面必须 -p 指定它的协议

[root@tianqi-01 network-scripts]#  iptables -I INPUT -p tcp --dport 80 -j DROP
[root@tianqi-01 network-scripts]#  iptables -nvL

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80
   23  1696 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
    0     0 DROP       tcp  --  *      *       192.168.188.1        192.168.188.128      tcp spt:1234 dpt:80

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

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

插入规则会在最前面,添加的规则会在后面。

  • -I和-A的区别:
    • -I,表示插入
    • -A,表示增长
  • 如果规则添加到前面,则是优先过滤最前面的规则,而后再去往下一条条的执行
    • 如果数据包匹配了第一条规则(同时知足两条规则),就会先匹配第一条规则。一旦匹配了第一条规则,那么数据包就会被抓取掉了,就不会再往下执行规则了
    • 一旦匹配规则,当即执行

iptables命令参数-D 删除规则

  • iptables -D INPUT -s 1.1.1.1 -j DROP 删除规则

[root@tianqi-01 network-scripts]# iptables -D INPUT -p tcp --dport 80 -j DROP
[root@tianqi-01 network-scripts]# iptables -nvL

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   34  2484 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
    1   229 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
    0     0 DROP       tcp  --  *      *       192.168.188.1        192.168.188.128      tcp spt:1234 dpt:80

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

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

根据编号删除规则

  • 删除规则的另外一种方法
  • iptables -nvL --line-numbers 打印出规则的序列号
    • 第一列就是number

[root@tianqi-01 network-scripts]# iptables -I INPUT -p tcp --dport 80 -j DROP
[root@tianqi-01 network-scripts]# iptables -nvL

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80
   44  3196 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
    1   229 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
    0     0 DROP       tcp  --  *      *       192.168.188.1        192.168.188.128      tcp spt:1234 dpt:80

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 3 packets, 356 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@tianqi-01 network-scripts]# 

[root@tianqi-01 network-scripts]# iptables -nvL --line-number    #加上这个选项后会显示行号
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 DROP       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80
2       51  3684 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
3        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
4        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
5        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
6        1   229 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
7        0     0 DROP       tcp  --  *      *       192.168.188.1        192.168.188.128      tcp spt:1234 dpt:80

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

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

删除第7条规则

[root@tianqi-01 network-scripts]# iptables -D INPUT 7
[root@tianqi-01 network-scripts]# iptables -nvL --line-number

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 DROP       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80
2       67  4776 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
3        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
4        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
5        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
6        1   229 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

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

iptables命令 参数 -P

  • iptables -P OUTPUT DROP 默认的规则

    • 链中,有一个默认的策略policy,policy ACCEPT表示这个链不加这些规则的话,那OUTPUT没有任何的规则,因此对于OUTPUT链的数据包来说,policy ACCEPT就是由默认的策略来决定的
      • 默认的策略是由ACCEPT来决定,全部的数据包只要是没有具体的规则来匹配,那么它就走默认的策略
  • 默认的规则最好不要去改变!!!

注意: 尽可能不要随意更改该配置,尤为是在进行远程登陆时,一旦执行该命令后将会断开链接。这个策略设定后只能用命令:‘iptables -P OUTPUT ACCEPT’来恢复成原始状态,不能使用-F参数。

友情连接:阿铭Linux

相关文章
相关标签/搜索