linux基础(day 33)

时间:2022-04-27
本文章向大家介绍linux基础(day 33),主要内容包括10.11 Linux网络相关、10.12 firewalld和netfilter、10.13 netfilter5表5链介绍、10.14 iptables语法、扩展(selinux了解即可)、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

10.11 Linux网络相关

linux网络相关

  • ifconfig命令,可以查看虚拟网卡,而ip addr命令则不能查看
  • ifconfig查看网卡ip(yum install net-tools)
  • ifup ens33/ifdown ens33
  • 设定虚拟网卡ens33:1
  • mii-tool ens33 查看网卡是否连接
  • ethtool ens33 也可以查看网卡是否连接
  • 更改主机名 hostnamectl set-hostname aminglinux
  • DNS配置文件/etc/resolv.conf
  • /etc/hosts文件

ifconfig命令

  • 在centos7中,只能只用默认的 ip addr命令查看IP
  • ifconfig命令,查看网卡IP
    • 在centos6中是默认就存在的
    • 在centos7中则需要安装——>yum install net-tools
[root@localhost ~]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.202.130  netmask 255.255.255.0  broadcast 192.168.202.255
        inet6 fe80::20c:29ff:feff:458f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ff:45:8f  txqueuelen 1000  (Ethernet)
        RX packets 3131  bytes 295354 (288.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 345  bytes 37930 (37.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

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 0  (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

[root@localhost ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:ff:45:8f brd ff:ff:ff:ff:ff:ff
    inet 192.168.202.130/24 brd 192.168.202.255 scope global eno16777736
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:feff:458f/64 scope link 
       valid_lft forever preferred_lft forever
[root@localhost ~]# 
  • ifconfig命令和ip addr命令,相互比较
    • 两者显示的效果是相同的
      • ifconfig命令,显示的更加 清爽
      • ip add命令,看起来比较乱

ifconfig命令 参数 -a

  • ifconfig -a 表示当你的网卡dang掉的时候(没有ip的时候,就不会显示网卡),但在加上-a参数后,就可查看到

ifup/ifdown命,启动/关闭网卡

  • ifup eno16777736 启动网卡
  • ifdown eno16777736 关闭网卡
  • 若是在终端设置网卡,则需要两个命令同时进行(否则,在终端关闭网卡后,就需要在本机上重启网卡了)
    • ifdown eno16777736 && ifup eno16777736
[root@localhost ~]# ifdown eno16777736 && ifup eno16777736  //关闭网卡后并重新启动网卡
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)
[root@localhost ~]# 

增加虚拟的网卡,然后给虚拟网卡设定IP

  • 需求
    • 增加虚拟的网卡,然后给虚拟网卡设定IP
  • 做法:
  1. 首先到网卡配置文件里拷贝下
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ls
ifcfg-eno16777736  ifdown-isdn      ifdown-tunnel  ifup-isdn    ifup-Team
ifcfg-lo           ifdown-post      ifup           ifup-plip    ifup-TeamPort
ifdown             ifdown-ppp       ifup-aliases   ifup-plusb   ifup-tunnel
ifdown-bnep        ifdown-routes    ifup-bnep      ifup-post    ifup-wireless
ifdown-eth         ifdown-sit       ifup-eth       ifup-ppp     init.ipv6-global
ifdown-ippp        ifdown-Team      ifup-ippp      ifup-routes  network-functions
ifdown-ipv6        ifdown-TeamPort  ifup-ipv6      ifup-sit     network-functions-ipv6
[root@localhost network-scripts]# cp ifcfg-eno16777736 ifcfg-eno16777736:0    //增加反斜杠就为了脱义冒号
  1. 然后进入到编辑文件
[root@localhost network-scripts]# vi !$
vi ifcfg-eno16777736:0

更改其中的网卡名称和IP、DNS1和网关GATEWAY都可删除(可删除)
将NAME=eno16777736名称更改为NAME=eno16777736:0——>配置文件中的 冒号 ,就不需要脱义了
添加DEVICE=eno16777736:0——>这一步若不添加,我的虚拟网卡实现不了
将IP地址IPADDR=192.168.202.130更改为192.168.202.150(这里面的ip可随意更改)
DNS1和网关GATEWAY都可删除(可删除)——>因为已经有了DNS和网关GATEWAY了

然后退出保存
  1. 这时关闭,重启网卡,然后在查看,就会看到虚拟网卡的存在
[root@localhost network-scripts]# ifdown eno16777736 && ifup eno16777736
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/Act
[root@localhost network-scripts]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.202.130  netmask 255.255.255.0  broadcast 192.168.202.255
        inet6 fe80::20c:29ff:feff:458f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ff:45:8f  txqueuelen 1000  (Ethernet)
        RX packets 4180  bytes 393171 (383.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 982  bytes 138704 (135.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eno16777736:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.202.150  netmask 255.255.255.0  broadcast 192.168.202.255
        ether 00:0c:29:ff:45:8f  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 0  (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

[root@localhost network-scripts]# 
  1. 这时在自己的window物理机上去ping 192.168.202.150,会发现可以连接虚拟网卡的IP
  • 在lvs和keepalived的时候,会使用到虚拟网卡

mii-tool命令查看网卡是否连接网线

  • 一台服务器,连接了网线,现在我要知道这个网卡有没有插着网线,我自己本身不确定
    • 在机房中,可以查看网卡灯是否亮着
    • 但在系统中,可以使用mii-tool eno16777736 命令查看是否是link ok
  • 格式:mii-tool 网卡名称 查看网卡是否连接网线
[root@localhost ~]# mii-tool eno16777736
eno16777736: negotiated 1000baseT-FD flow-control, link ok
[root@localhost ~]# 
  • 这里若显示为 link ok 或者 no link ——>另外会有些机器不支持,提示not support
    • 这时候就可以使用另外一个命令查看ethtool eno16777736
      • 查看最底部 Link detected 是否为 yes

hostnamectl命令更改主机名

  • hostnamectl命令,更改主机名
    • centos6中,不支持该命令
[root@localhost ~]# hostnamectl set-hostname hanfeng-001
[root@localhost ~]# hostname    //在当前终端下,使用hostname命令,查看主机名
hanfeng-001
[root@localhost ~]# 

但会发主机名并没有立即生效,需要退出重新登录终端,或者进入一个子shell(输入一个bash即可)
[root@localhost ~]# bash
[root@hanfeng-001 ~]#         //就会看到主机名变更了

主机名配置文件存放位置

  • 主机名的配置文件存放位置 /etc/hostname
[root@hanfeng-001 ~]# cat /etc/hostname
hanfeng-001
[root@hanfeng-001 ~]# 

DNS配置文件存放位置

  • DNS配置文件存放位置 /etc/resolv.conf
[root@hanfeng-001 ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 119.29.29.29
[root@hanfeng-001 ~]# 
  • 谷歌的DNS为8.8.8.8
  • 我们在更改DNS的时候,只需要去更改网卡配置文件即可/etc/sysconfig/network-scripts/ifcfg-eno16777736
    • 当然也可以临时的编辑 /etc/resolv.conf 这个文件。但是更改/etc/resolv.conf 文件之后 ,在我们重启网卡后,它依旧会被网卡里面的配置文件 DNS 所覆盖

/etc/hosts文件

  • /etc/hosts这个文件是linux和window中都有的一个文件
  • 格式:左边是IP ,右边是域名
    • 支持一个IP,配多个域名(用 格 分开即可)
    • 若是一个域名,有多个IP,则会 以后面的生效——>若是前面有,后面也有,则以后面的为主
[root@hanfeng-001 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[[root@hanfeng-001 ~]# ping www.qq123.com
PING www.qq123.com (202.91.250.93) 56(84) bytes of data.
64 bytes from 202.91.250.93: icmp_seq=1 ttl=128 time=11.6 ms
64 bytes from 202.91.250.93: icmp_seq=2 ttl=128 time=11.3 ms
64 bytes from 202.91.250.93: icmp_seq=3 ttl=128 time=11.7 ms
64 bytes from 202.91.250.93: icmp_seq=4 ttl=128 time=11.8 ms
^C
--- www.qq123.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 11.363/11.652/11.811/0.172 ms
[root@hanfeng-001 ~]# 
  • 其中的202.91.250.93是一个公网IP,我想在我本机不需要访问到这个IP
    • 编辑/etc/hosts文件
[root@hanfeng-001 ~]# vim /etc/hosts

在/etc/hosts文件添加    192.168.202.150 www.qq123.com

[root@hanfeng-001 ~]# ping www.qq123.com
PING www.qq123.com (192.168.202.150) 56(84) bytes of data.
64 bytes from www.qq123.com (192.168.202.150): icmp_seq=1 ttl=64 time=0.100 ms
64 bytes from www.qq123.com (192.168.202.150): icmp_seq=2 ttl=64 time=0.045 ms
64 bytes from www.qq123.com (192.168.202.150): icmp_seq=3 ttl=64 time=0.042 ms
^C
--- www.qq123.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.042/0.062/0.100/0.027 ms
[root@hanfeng-001 ~]# 
  • 这里会发现直接解析到192.168.202.150这个IP这个IP上——>只在本机上生效
    • 在window上是无效的

10.12 firewalld和netfilter

Linux防火墙-netfilter

  • selinux临时关闭 setenforce 0
  • selinux永久关闭 vi /etc/selinux/config
  • centos7之前使用netfilter防火墙
  • centos7开始使用firewalld防火墙
  • 关闭firewalld开启netfilter方法
  • systemctl stop firewalld
  • systemctl disable firewalled
  • yum install -y iptables-services
  • systemctl enable iptables
  • systemctl start iptables

selinux防火墙

  • setenforce 0 临时关闭 selinux
  • vi /etc/selinux/config 永久关闭 selinux
  • selinux一般都是关闭的,因为开启selinux会增大运维管理成本,因为很多服务受限于selinux
    • 在关闭selinux后,也不会存在太大的安全问题
[root@hanfeng-001 ~]# vi /etc/selinux/config

将SELINUX=enforcing更改为SELINUX=disabled
然后在重启系统,就会永久关闭selinux

(若是将SELINUXTYPE=targeted 这里更改了,就会无法开启系统!!!千万注意)

Enforcing和Permissive区别

[root@hanfeng-001 ~]# getenforce
Enforcing   
[root@hanfeng-001 ~]# setenforce 0    //临时关闭
[root@hanfeng-001 ~]# getenforce
Permissive    
[root@hanfeng-001 ~]# 
  • 区别:
  • Permissive是selinux开启了,但是仅仅是遇到这种需要发生阻断的时候,他不需要真正的去阻断,仅仅是一个提醒

netfilter防火墙

  • netfilter防火墙是centos7之前的叫法
  • 在centos7的时候,叫做firewalld
  • 这 netfilter 和firewalld 两个防火墙机制不太一样,但内部的工具(iptables)用法是一样的
    • 可以通过iptables工具,去添加一些规则(比如,开放80端口,开放22端口,关闭8080端口)
  • 在centos7中,默认使用的是firewalld,而netfilter防火墙是没有开启的
    • 在centos7中,关闭firewalld,去使用netfilte防火墙也是没有问题的
    • 关闭firewalld,开启netfilter
      • 先systemctl disable firewalld 停掉firewalld,就是限制开机启动
      • 然后systemctl stop firewalld 关闭firewalld服务
      • 开启netfilter之前安装 iptables-services 包
        • 在安装完成后,就会产生一个iptables服务
      • 再systemctl enable iptables 设置开启激动——>(一定要关闭selinux,否则这里操作不了)
      • 再systemctl stat iptables 开启iptables服务
关闭firewalld
[root@hf-01 ~]# systemctl disable firewalld    //停掉firewalld,就是限制开机启动
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
[root@hf-01 ~]# systemctl stop firewalld    //关闭firewalld服务
开启netfilter
在开启之前,需要先安装一个iptables-services包
[root@hf-01 ~]# yum install -y iptables-services

[root@hf-01 ~]# systemctl enable iptables    //设置开启激动
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@hf-01 ~]# systemctl start iptables    //开启iptables服务
[root@hf-01 ~]# 
  • 在iptables服务启动之后,用 iptables -nvL 命令查自带的规则
  • iptables -nvL 查看默认规则
[root@hf-01 ~]# iptables -nvL    //查看默认规则
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   35  2436 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 22 packets, 3152 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@hf-01 ~]# 
  • iptables 仅仅是netfilter防火墙的一个工具

10.13 netfilter5表5链介绍

Linux防火墙—netfilter

  • netfilter的5个表
  • filter表用于过滤包,最常用的表,有INPUT、FORWARD、OUTPUT三个链
  • nat表用于网络地址转换,有PREROUTING、POSTROUTING三个链
  • managle表用于给数据包做标记,几乎用不到
  • raw表可以实现不追踪某些数据包
  • security表在centos6中并没有,用于强制访问控制(MAC)的网络规则
  • 参考文章

netfilter的五个表

  • 在centos中只有四个表,并没有security表
[root@hf-01 ~]# man iptables

查看五个表
              filter:
                  This is the default table (if no -t option is passed). It  contains
                  the  built-in chains INPUT (for packets destined to local sockets),
                  FORWARD (for packets being routed through the box), and OUTPUT (for
                  locally-generated packets).

              nat:
                  This table is consulted when a packet that creates a new connection
                  is encountered.  It consists of three  built-ins:  PREROUTING  (for
                  altering  packets  as  soon  as they come in), OUTPUT (for altering
                  locally-generated packets before  routing),  and  POSTROUTING  (for
                  altering packets as they are about to go out).  IPv6 NAT support is
                  available since kernel 3.7.

              mangle:
                  This table is used for specialized packet alteration.  Until kernel
                  2.4.17  it had two built-in chains: PREROUTING (for altering incom‐
                  ing packets before routing) and OUTPUT (for altering locally-gener‐
                  ated  packets  before  routing).   Since kernel 2.4.18, three other
                  built-in chains are also supported: INPUT (for packets coming  into
                  the box itself), FORWARD (for altering packets being routed through
                  the box), and POSTROUTING (for altering packets as they  are  about
                  to go out).

              raw:
                  This  table  is used mainly for configuring exemptions from connec‐
                  tion tracking in combination with the NOTRACK target.  It registers
                  at  the  netfilter  hooks  with  higher priority and is thus called
                  before ip_conntrack, or any other IP tables.  It provides the  fol‐
                  lowing  built-in  chains:  PREROUTING (for packets arriving via any
                  network interface) OUTPUT (for  packets  generated  by  local  pro‐
                  cesses)

              security:
                  This  table  is  used for Mandatory Access Control (MAC) networking
                  rules, such as those enabled by the SECMARK  and  CONNSECMARK  tar‐
                  gets.   Mandatory  Access  Control is implemented by Linux Security
                  Modules such as SELinux.  The security table is  called  after  the
                  filter table, allowing any Discretionary Access Control (DAC) rules
                  in the filter table to take effect before MAC  rules.   This  table
                  provides  the  following built-in chains: INPUT (for packets coming
                  into the box itself), OUTPUT (for altering locally-generated  pack‐
                  ets before routing), and FORWARD (for altering packets being routed
                  through the box).
  • 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表几乎用不到

参考文章

  • 参考文章
  • iptables传输数据包的过程
    • ① 当一个数据包进入网卡时,它首先进入PREROUTING链,内核根据数据包目的IP判断是否需要转送出去。
    • ② 如果数据包就是进入本机的,它就会沿着图向下移动,到达INPUT链。数据包到了INPUT链后,任何进程都会收到它。本机上运行的程序可以发送数据包,这些数据包会经过OUTPUT链,然后到达POSTROUTING链输出。
    • ③ 如果数据包是要转发出去的,且内核允许转发,数据包就会如图所示向右移动,经过FORWARD链,然后到达POSTROUTING链输出。
  • 总结:
  1. 如果是本机的,则会经过PREROUTING链--->INPUT链--->OUTPUT链--->POSTROUTING链
  2. 如果不是本机的,则会经过PREROUTING链--->FORWARD链--->POSTROUTING链

10.14 iptables语法

linux防火墙-netfilter

  • 查看iptables规则:iptables -nvL
  • iptables -F 清空规则
  • service iptables save 保存规则
  • iptables -t nat 参数-t 指定表
  • iptables -Z 可以把计数器清零
  • iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
  • iptables -I/-A/-D INPUT -s 1.1.1.1 -j DROP
  • iptables -I INPUT -s 192.168.1.0/24 -i eth0 -j ACCEPT
  • iptables -nvL --line-numbers
  • iptables -D INPUT 1
  • iptables -P INPUT DROP

iptables命令

  • iptables -nvL 查看iptables默认规则
[root@hf-01 ~]# iptables -nvL    //查看iptables规则
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  357 28956 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           
    2   184 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
   18  1404 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 251 packets, 57368 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@hf-01 ~]# 
  • service iptables restart 重启iptables规则
[root@hf-01 ~]# service iptables restart
Redirecting to /bin/systemctl restart iptables.service
[root@hf-01 ~]#
  • 存放默认规则的位置
    • /etc/sysconfig/iptables
[root@hf-01 ~]# 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@hf-01 ~]# 

iptables -F清空规则

  • iptables -F清空规则
    • 在清空规则后,再去查看,会发现没有规则了(但是在文件中依旧保存这规则)
[root@hf-01 ~]# iptables -F
[root@hf-01 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 8 packets, 576 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 5 packets, 636 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@hf-01 ~]# 
  • service iptables save 保存规则
    • 若是在清空规则后,去执行service iptables save保存规则,那存放规则的文件也会变成所保存的规则
  • 在iptables -F清空规则后,重启service restart iptables.service(重启服务器或者iptables规则),都会加载配置文件里面的规则
    • 在重启规则后,会看到原先被清空的规则重新加载了
[root@hf-01 ~]# service iptables restart
Redirecting to /bin/systemctl restart iptables.service
[root@hf-01 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   12   872 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 8 packets, 2048 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@hf-01 ~]# 
  • iptables -t nat -nvL 查看nat表中的规则
    • 在不指定表的时候,默认就是 filter 表

iptables -Z 把计数器清零

  • iptables -Z 把计数器清零
    • 在查看filter表的时候,会看到第一列和第二列都是有数据的
      • 第一列,是有多少个包
      • 第二列,是数据量,数据大小(单位:bytes字节)
[root@hf-01 ~]# 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@hf-01 ~]# 
  • 这里会看到数字都清零了,但过一会再来查看,会看到数字又出现了(因为在每时每刻都在通信)
[root@hf-01 ~]# iptables -t filter -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   52  3592 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 28 packets, 5152 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@hf-01 ~]# 

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@hf-01 ~]# iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP         //新增规则
[root@hf-01 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   97  7172 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
   27  2106 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 22 packets, 3768 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@hf-01 ~]# 

iptables命令 参数 -I

  • iptables -I INPUT -p tcp --dport 80 -j DROP
    • 这里是简写,不指定来源IP,和目标IP,只写目标的端口——>但一定要指定tcp/ip
    • 若是使用了 dport 或 sport ,那么前面必须 -p 指定它的协议
[root@hf-01 ~]# iptables -I INPUT -p tcp --dport 80 -j DROP
[root@hf-01 ~]# 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
  238 18252 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
   27  2106 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 8 packets, 2288 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@hf-01 ~]# 
  • -I和-A的区别:
    • -I,表示插入
    • -A,表示增加
  • 若是规则添加到前面,则是优先过滤最前面的规则,然后再去往下一条条的执行
    • 若是数据包匹配了第一条规则(同时满足两条规则),就会先匹配第一条规则。一旦匹配了第一条规则,那么数据包就会被抓取掉了,就不会再往下执行规则了
    • 一旦匹配规则,立即执行

iptables命令参数-d 删除规则

  • iptables -D INPUT -s 1.1.1.1 -j DROP 删除规则
[root@hf-01 ~]# iptables -D INPUT -p tcp --dport 80 -j DROP    //删除规则
[root@hf-01 ~]# iptables -D INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP    //删除规则
[root@hf-01 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  401 33844 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
   27  2106 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 4 packets, 592 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@hf-01 ~]# 

根据编号删除规则

  • 删除规则的另一种方法
  • iptables -nvL --line-numbers 打印出规则的序列号
    • 第一列就是number
[root@hf-01 ~]# iptables -nvL --line-numbers
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      605 53404 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       27  2106 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 47 packets, 7652 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
[root@hf-01 ~]# iptables -D INPUT 7    //删除序列7的规则
[root@hf-01 ~]# iptables -nvL --line-numbers
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      662 57360 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       27  2106 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 4 packets, 464 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
[root@hf-01 ~]# 

iptables命令 参数 -P

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

扩展(selinux了解即可)