Linux网络相关、firewalld和netfilter、netfilter5表5链介绍、iptables语法

目录

一、Linux网络相关
二、firewalld和netfilter
三、netfilter5表5链介绍
四、iptables语法

一、Linux网络相关

  • ifconfig 命令查看网卡 IP
[root@minglinux-01 ~]# ifconfig
ens33: flags=4163  mtu 1500
        inet 192.168.89.130  netmask 255.255.255.0  broadcast 192.168.89.255
        inet6 fe80::20c:29ff:fe46:125  prefixlen 64  scopeid 0x20
        ether 00:0c:29:46:01:25  txqueuelen 1000  (Ethernet)
        RX packets 38830  bytes 36052844 (34.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 13961  bytes 2437860 (2.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (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
  • 修改配置文件/etc/sysconfig/network-scripts/ifcfg-ens33设置IP

  • ifdown ens33; ifup ens33
    ifdown即停用网卡,ifup即启动网卡。远程连接服务器时停用网卡后会导致断连,所以重启网卡尽量使用命令systemctl restart network

  • 添加虚拟网卡
    在Linux系统中,网卡是可以设定多重IP的,设置的过程如下:

[root@minglinux-01 ~]# cd /etc/sysconfig/network-scripts/
[root@minglinux-01 ~]# cd /etc/sysconfig/network-scripts/

编辑ifcfg-ens33:1这个配置文件。修改一下NAME、DEVICE、IPADDR,设置完毕重启网卡,如下所示:

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=c0f01c3c-028a-4219-86db-bc442037da9b
DEVICE=ens33:
ONBOOT=yes
IPADDR=192.168.89.139
GATEWAY=192.168.89.2
NETMASK=255.255.255.0
BROADCAST=192.168.89.255
DNS1=119.29.29.29

重启网卡后再查看网卡IP

systemctl restart network
[root@minglinux-01 network-scripts]# ifconfig 
ens33: flags=4163  mtu 1500
        inet 192.168.89.130  netmask 255.255.255.0  broadcast 192.168.89.255
        inet6 fe80::20c:29ff:fe46:125  prefixlen 64  scopeid 0x20
        ether 00:0c:29:46:01:25  txqueuelen 1000  (Ethernet)
        RX packets 40070  bytes 36144711 (34.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 14378  bytes 2487404 (2.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:: flags=4163  mtu 1500
        inet 192.168.89.139  netmask 255.255.255.0  broadcast 192.168.89.255
        ether 00:0c:29:46:01:25  txqueuelen 1000  (Ethernet)

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (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
  • 查看网卡连接状态
  1. mii-tool ens33命令
[root@minglinux-01 ~]# mii-tool ens33
ens33: negotiated 1000baseT-FD flow-control, link ok

这里显示link ok,就说明网卡为连接状态。如果显示no link,说明网卡坏了或者没有连接网线。

  1. ethtool ens33命令
[root@minglinux-01 ~]# 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
    Supported FEC modes: Not reported
    Advertised link modes:  10baseT/Half 10baseT/Full 
                            100baseT/Half 100baseT/Full 
                            1000baseT/Full 
    Advertised pause frame use: No
    Advertised auto-negotiation: Yes
    Advertised FEC modes: Not reported
    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。

  • 更改主机名
    使用hostname命令可以查看主机名:
[root@minglinux-01 network-scripts]# hostname
minglinux-01

使用hostname命令可以更改主机名,不过这样的修改保存在内存中,如果重启,主机名还会变成改动之前的名称。

使用hostnamectl set-hostname aminglinux-0更改主机名的同时还需要更改相关
的配置文件/etc/hostname,这样可以永久修改,重启不影响,实力命令如下:

[root@minglinux-01 network-scripts]# hostnamectl set-hostname minglinux-0
[root@minglinux-01 network-scripts]# hostname
minglinux-0
[root@minglinux-01 network-scripts]# cat /etc/hostname 
minglinux-0
  • 设置 DNS
    在Linux下设置DNS非常简单,只要把DNS地址写到配置文件/etc/resolv.conf中即可。
[root@minglinux-01 network-scripts]# cat /etc/resolv.conf

nameserver 119.29.29.29

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

文件/etc/hosts也能解析域名,我们可以修改该文件自定义域名,示例命令如下:

[root@minglinux-01 network-scripts]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.43.66  www.taobao.com
[root@minglinux-01 network-scripts]# ping -c 2 www.taobao.com
PING www.taobao.com (192.168.43.66) 56(84) bytes of data.
64 bytes from www.taobao.com (192.168.43.66): icmp_seq=1 ttl=128 time=1.28 ms
64 bytes from www.taobao.com (192.168.43.66): icmp_seq=2 ttl=128 time=0.693 ms

--- www.taobao.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.693/0.986/1.280/0.295 ms

二、firewalld和netfilter

  • SELinux

临时关闭SELinux使用setenforce 0

永久关闭需要更改配置文件/etc/selinux/config,把SELINUX= enforcing
改成SELINUX=disabled。

更改完该配置文件后,重启系统方可生效。可以使用getenforce命令获得当前SELinux的状态:

[root@minglinux-01 network-scripts]# getenforce
Permissive
[root@minglinux-01 ~]# getenforce     //修改配置文件后
Disabled
  • netfilter
    在之前的CentOS版本(比如5和6)的防火墙为netfilter,CentOS7的防火墙为firewalld。

首先,把firewalld关闭,然后开启之前版本的iptables,示例命令如下:

[root@minglinux-01 ~]# systemctl stop firewalld // 关闭firewalld服务
[root@minglinux-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@minglinux-01 ~]# yum install -y iptables-services // 安装iptables-services,这样就可以使用之前版本的iptables了
[root@minglinux-01 ~]# systemctl enable iptables // 让它开机启动
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to
/usr/lib/systemd/system/iptables.service.
[root@minglinux-01 ~]# systemctl start iptables // 启动iptables服务

三、netfilter5表5链介绍

  1. netfilter的5个表
  • filter表主要用于过滤包,是系统预设的表,这个表也是阿铭用得最多的表。该表内建3个链:INPUT、OUTPUT以及FORWARD。INPUT链作用于进入本机的包,OUTPUT链作用于本机送出的包,FORWARD链
    作用于那些跟本机无关的包。
  • nat表主要用于网络地址转换,它也有3个链。PREROUTING链的作用是在包刚刚到达防火墙时改变它的目的地址(如果需要的话),OUTPUT链的作用是改变本地产生的包的目的地址,POSTROUTING链的作用是在包即将离开防火墙时改变其源地址。
  • mangle表主要用于给数据包做标记,然后根据标记去操作相应的包。
  • raw表可以实现不追踪某些数据包,默认系统的数据包都会被追踪,但追踪势必消耗一定的资源,所以可以用raw表来指定某些端口的包不被追踪。
  • security表在CentOS 6中是没有的,它用于强制访问控制(MAC)的网络规则。
  1. netfilter的5个链

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

四、iptables语法

介绍几种常用的语法。

  • 查看iptables规则:iptables -nvL
    默认查看的是filter表
    规则保存在/etc/sysconfig/iptables文件中
[root@minglinux-01 ~]# systemctl enable iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptato /usr/lib/systemd/system/iptables.service.
[root@minglinux-01 ~]#  systemctl start iptables
[root@minglinux-01 ~]# 
[root@minglinux-01 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   13  1048 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 10 packets, 956 bytes)
 pkts bytes target     prot opt in     out     source               destination  

-nvL表示查看该表的规则,其中-n表示不针对IP反解析主机名,-L表示列出,-v表示列出的信息更加详细。

加-t选项可以指定要查看的表,如打印nat表的相关信息:

[root@minglinux-01 ~]# 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      
  • iptables -F 清除规则
    清除规则的两个命令:
    iptables -F
    iptables -Z

这里-F表示把所有规则全部删除,如果不加-t指定表,默认只清除filter表的规则。-Z表示把包以及流量计数器置零。

  • service iptables save 保存规则
  • 增加/删除一条规则
    iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
    规则中选项的作用:

 -A/-D:表示增加/删除一条规则。
 -I:表示插入一条规则,其实效果跟-A一样。
 -p:表示指定协议,可以是tcp、udp或者icmp。
 --dport:跟-p一起使用,表示指定目标端口。
 --sport:跟-p一起使用,表示指定源端口。
 -s:表示指定源IP(可以是一个IP段)。

 -d:表示指定目的IP(可以是一个IP段)。
 -j:后面跟动作,其中ACCEPT表示允许包,DROP表示丢掉包,REJECT表示拒绝包。
 -i:表示指定网卡。

  • 插入一条规则,把来自1.1.1.1的所有数据包丢掉:
    iptables -I INPUT -s 1.1.1.1 -j DROP
  • 删除刚插入的规则:
    iptables -D INPUT -s 1.1.1.1 -j DROP
  • 把来自2.2.2.2并且是TCP协议到本机80端口的数据包丢掉:
    iptables -I INPUT -s 2.2.2.2 -p tcp --dport 80 -j DROP
    --dport/--sport必须和-p选项一起使用

  • 把发送到10.0.1.14的22端口的数据包丢掉:
    iptables -I OUTPUT -p tcp --dport 22 -d 10.0.1.14 -j DROP

  • 把来自192.168.1.0/24这个网段且作用在eth0上的包放行:
    iptables -A INPUT -s 192.168.1.0/24 -i eth0 -j ACCEPT

  • 按编号列出规则:iptables -nvL --line-numbers

[root@minglinux-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  --  *      *       2.2.2.2              0.0.0.0/0            tcp dpt:80
2       80  5776 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        2   458 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
7        0     0 ACCEPT     all  --  eth0   *       192.168.1.0/24       0.0.0.0/0           

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, 356 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 DROP       tcp  --  *      *       0.0.0.0/0            10.0.1.14            tcp dpt:22
  • 按编号删除某一规则:
    iptables -D INPUT 1
    这里-D后面依次跟链名、规则num。这个num就是查看iptables规则时第1列的值。

  • 预设某一链的策略:
    iptables -P INPUT DROP
    即-P(大写)选项,它表示预设策略。策略内容或为DROP,或为ACCEPT,默认是ACCEPT。这个策略一旦设定后,只有使用命令iptables -P INPUT ACCEPT才能恢复成原始状态。

扩展(selinux了解即可)
  1. selinux教程 http://os.51cto.com/art/201209/355490.htm
    2.selinux pdf电子书 http://pan.baidu.com/s/1jGGdExK

你可能感兴趣的:(Linux网络相关、firewalld和netfilter、netfilter5表5链介绍、iptables语法)