linux基礎(day 33)

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以前的叫法html

  • 在centos7的時候,叫作firewalldlinux

  • 這 netfilter 和firewalld 兩個防火牆機制不太同樣,但內部的工具(iptables)用法是同樣的shell

    • 能夠經過iptables工具,去添加一些規則(好比,開放80端口,開放22端口,關閉8080端口)
  • 在centos7中,默認使用的是firewalld,而netfilter防火牆是沒有開啓的vim

    • 在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 保存規則centos

    • 如果在清空規則後,去執行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 默認的規則bash

    • 鏈中,有一個默認的策略policy,policy ACCEPT表示這個鏈不加這些規則的話,那OUTPUT沒有任何的規則,因此對於OUTPUT鏈的數據包來說,policy ACCEPT就是由默認的策略來決定的
      • 默認的策略是由ACCEPT來決定,全部的數據包只要是沒有具體的規則來匹配,那麼它就走默認的策略
  • 默認的規則最好不要去改變!!!服務器

擴展(selinux瞭解便可)

相關文章
相關標籤/搜索