您的位置:首页 > 理论基础 > 计算机网络

linux网络相关、firewalld和netfilter、netfilter5表5链介绍、ipta

2017-11-29 23:16 831 查看

Linux网络相关

ifconfig查看网卡ip (yum install -y net-tools)

ifconfig -a 当网卡没有ip或down掉时候,ifconfig是不显示的,加上-a参数之后这些网卡也会显示。

ifdown/ifup 网卡名(关闭/启动网卡)

单独针对一个网卡进行更改,对网卡配置文件修改后重启:ifdown ens33 && ifup ens33 。

service network restart和systemctl restart network.service是对所有的网卡进行重启。

增加虚拟网卡

先复制一个之前的ens33网卡配置文件,然后将虚拟网卡名字改为ens33:0(ens33:n)。

然后修改下ip地址。

重启ens33网卡。(ifdown ens33 && ifup ens33)

[root@localhost network-scripts]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.159.128 netmask 255.255.255.0 broadcast 192.168.159.255
inet6 fe80::cfb6:b956:82de:6f77 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:43:65:0b txqueuelen 1000 (Ethernet)
RX packets 5138 bytes 445724 (435.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3140 bytes 439420 (429.1 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.159.130 netmask 255.255.255.0 broadcast 192.168.159.255
ether 00:0c:29:43:65:0b txqueuelen 1000 (Ethernet)

执行ifconfig查看网卡发现ens33:0这个虚拟网卡,可以ping通。lvs和keepalived会用到虚拟网卡。

查看网卡是否连接网线

mii-tool 网卡名

[root@localhost ~]# mii-tool ens33
ens33: negotiated 1000baseT-FD flow-control, link ok

最后那里显示link ok就是连接正常

ethtool 网卡名

[root@localhost ~]# ethtool ens33
Settings for ens33:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
......
Current message level: 0x00000007 (7)
drv probe link
Link detected: yes

最后link detected:yes 说明连接正常,连接不正常为no。

更改主机名

更改主机名hostnamectl set-hostname abc(将主机名更改为abc。该命令只适用于centos7。)修改完成之后需要重新登录才生效。

主机名配置文件为/etc/hostname

hostname abc 只是零时将主机名改成abc。

DNS配置文件

DNS的配置文件为/etc/resolv.conf

[root@localhost ~]# cat /etc/resolv.conf

Generated by NetworkManager

nameserver 119.29.29.29

DNS配置文件信息是从网卡配置文件中获取的。更改了DNS配置文件之后,DNS会临时生效。重启网卡,DNS配置文件的内容会和网卡配置文件同步。

/etc/hosts 文件

这个文件是linux和windows都有的。访问自定义域名时候用到。仅限在本机上使用。

[root@localhost ~]# ping www.baidu.com
PING www.a.shifen.com (115.239.211.112) 56(84) bytes of data.
......
[root@localhost ~]# ping www.qq.com
PING www.qq.com (14.17.42.40) 56(84) bytes of data.
......

ping百度和qq时候都能获取到一个地址。分别为115.239.211.112和14.17.42.40。

修改/etc/hosts文件,自定义IP所对应的域名

[root@localhost ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.159.2    www.baidu.com www.qq.com
[root@localhost ~]# ping www.baidu.com
PING www.baidu.com (192.168.159.2) 56(84) bytes of data.
[root@localhost ~]# ping www.qq.com
PING www.baidu.com (192.168.159.2) 56(84) bytes of data.


在/etc/hosts配置文件中自定义IP对应的域名后,ping域名会ping自定的IP。一个ip可以对应多个域名,用空格隔开,当一个域名对应多个IP时,以配置文件中靠前的配置为准。

selinux

selinux 是一种安全子系统(防火墙)

查看selinux状态

使用命令getenforce可以查看selinux的状态。Enforcing为开启。permissive为关闭。

临时开启和关闭selinux

getenforce 1 使selinux的状态变成Enforcing(开启)
getenforce 0 使selinux的状态变成permissive(关闭)

该操作只能临时更改selinux的状态。

永久关闭selinux

selinux配置文件/etc/selinux/config

永久关闭selinux需要修改配置文件中的SELINUX,将SELINUX=enforcing改成SELINUX=disable。重启系统就生效了。

netfilter防火墙

iptables和firewalld都是管理netfilter防火墙的工具,centos7默认使用firewalld来管理netfilter防火墙。

关闭firewalld

[root@localhost ~]# systemctl disable firewalld #不让它开机启动
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
[root@localhost ~]# systemctl stop firewalld #把这个服务停掉

开启iptablees

[root@localhost ~]# yum install -y iptables -services #安装iptables包
[root@localhost ~]# systemctl enable iptables #开机启动iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@localhost ~]# systemctl start iptables #启动iptables服务

查看iptables默认规则

使用命令iptables -nvL可以查看iptables的默认规则。

[root@localhost ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target     prot opt in     out     source               destination
52  3472 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 33 packets, 3096 bytes)
pkts bytes target     prot opt in     out     source               destination

netfilter 5表及链的介绍

表名

filter:对包进行过滤

nat:进行地址转换

mangleL:对数据包进行修改

raw:一般是为了不再让iptables对数据包进行跟踪,提高性能

security:这个表用于强制访问控制(MAC)网络规则

链名

filter的三种链

INPUT链:作用于输入本机的数据包

OUTPUT链:作用与本机输出的数据包

FORWARD链:作用于与本机无关的包

nat的三种链:

PRERPUTING链:作用是在包刚刚到达防火墙时改变它的目的地址,如果需要的话。

OUTPUT链:改变本地产生的包的目的地址。

POSTROUTING链:在包离开防火墙之前改变其源地址。

iptables传输数据包的过程

当一个数据包进入网卡时,它首先进入PREROUTING链,内核根据数据包目的IP判断是否需要转送出去。

如果数据包就是进入本机的,它就会沿着图向下移动,到达INPUT链。数据包到了INPUT链后,任何进程都会收到它。本机上运行的程序可以发送数据包,这些数据包会经过OUTPUT链,然后到达POSTROUTING链输出。

如果数据包是要转发出去的,且内核允许转发,数据包就会如图所示向右转移,经过FORWARD链,然后到达POSTROUTING链输出。



iptables的规则表和链:

表(tables)提供特定的功能,iptables内置了5个表,即filter表、nat表、mangle表、raw表和security表,分别用于实现包过滤,网络地址转换、包重构(修改)、据跟踪处理和强制访问控制(MAC)网络规则。

链(chains)是数据包传播的路径,每一条链其实就是众多规则中的一个检查清单,每一条链中可以有一 条或数条规则。当一个数据包到达一个链时,iptables就会从链中第一条规则开始检查,看该数据包是否满足规则所定义的条件。如果满足,系统就会根据 该条规则所定义的方法处理该数据包;否则iptables将继续检查下一条规则,如果该数据包不符合链中任一条规则,iptables就会根据该链预先定 义的默认策略来处理数据包。

iptables语法

基本语法
iptables [-t表][操作命令][链][规则匹配器][-j目标动作]



filter表:对包进行过滤

nat表:进行地址转换

mangle表:对数据包进行修改

raw表:一般是为了不再让iptables对数据包进行跟踪,提高性能

security表:这个表用于强制访问控制(MAC)网络规则

常用操作命令

-A:在制定链尾部添加规则

-D:删除匹配的规则

-R:替换匹配规则

-I:在指定位置插入规则,例如:iptables -I INPUT 1 --dport80 -j ACCEPT(将规则插入到filter表INPUT链中的第一位上)

-L/S::列出指定链或所有链的规则

-F:删除指定链或所有链的规则

-N:创建用户自定义链,例如:iptables -N allowed

-X:删除指定的用户自定义链

-P:为指定链设置默认规则策略,对自定义链不起作用

-Z:将指定链或所有链的计数器清零

-E:更改自定义链的名称,例:iptables -E allowed disallowed

-n:ip地址和端口号以数字的方式显示,例:iptables -Ln

-v: 查看规则表详细的信息

常用的规则匹配器

-P tcp|udp|icmp|all 匹配协议,all会匹配所有协议

-s addr[/mask] 匹配源地址

-d addr[/mask] 匹配目标地址

--sport port1[:port2] 匹配源端口(可指定连续的端口)

--dport port1[:port2]匹配目的端口(可指定连续的端口)

-o interface 匹配出口网卡,只适用FORWARD、POSTROUTING、OUTPUT。例:iptables -A FORWARD -o etho

--icmp-type 匹配icmp类型(使用iptables -p icmp -h可查看可用的ICMP类型)

--tcp-flags mask comp
匹配TCP标记,mask表示检查范围,comp表示匹配mask中的哪些标记。
例:iptables -A FORWARD -p tcp --tcp-flags ALL SYN,ACK -j ACCEPT(表示匹配SYN和ACK标记的数据包)

目标动作

ACCEPT 允许数据包通过

DROP 丢弃数据包

REJECT 丢弃数据包,并且将拒绝信息发送给发送方

SNAT 源地址转换(在nat表上),例:iptables -t nat -A POSTROUTING -d 192.168.0.102 -j SNAT --to 192.168.0.1

DNAT 目标地址转换(在nat表上),例:iptables -t nat -A PREROUTING -d 202.202.202.2 -j DNAT --to-destination 192.168.0.102

REDIRECT 目标端口转换(在nat表上),例:iptables -t nat -D PREROUTING -p tcp --dport 8080 -i eth2.2 -j REDIRECT --to 80

MARK 将数据包打上标记,例:iptables -t mangle -A PREROUTING -s 192.168.1.3 -j MARK --set-mark 60

注意

目标地址转换一般在PREROUTING链上操作

源地址转换一般在POSTROUTING链上操作

iptables一般操作

iptable规则配置文件:/etc/sysconfig/iptables

iptable -nvL查看规则,默认是查看filter表的规则。

iptable -F 清空链规则

[root@centos-01 ~]# iptables -F #清空规则
[root@centos-01 ~]# iptables -nvL #查看规则
Chain INPUT (policy ACCEPT 6 packets, 428 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 4 packets, 448 bytes)
pkts bytes target prot opt in out source destination

iptable -F只是临时清空所有链规则,配置文件中还保存着之前的规则。

想要把当前规则保存到配置文件中需要执行以下命令

[root@centos-01 ~]# service iptables save

使用iptable -F清空规则之后。重启服务,之前的规则还会恢复。

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

重启服务或重启服系统之后,还会加载配置文件中的规则。

之前这些操作都是对filter表进行的。若要对其他表进行,需要加-t参数

[root@centos-01 ~]# iptables -nvL -t nat


查看nat表的规则

iptables -Z 清零计数器

[root@centos-01 ~]# iptables -nvL #查看规则
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
100 7168 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
[root@centos-01 ~]# iptables -Z #清零计数器
[root@centos-01 ~]# iptables -nvL #查看规则
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
6 428 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED

清零计数器是把规则表中的pkts(数据包)和bytes(数据量)两列的数字清零。

iptables -A/I 增加一条规则

[root@centos-01 ~]# iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP

不加-t默认是在filter表中增加规则。
-A INPUT 在INPUT链中添加规则。-p tcp 指定协议类型tcp。-s 192.168.188.1 指定源地址为192.168.188.1。--sport 1234 指定源端口为1234。-d 192.168.188.128 指定目标地址为192.168.188.128。--dport 80 指定目标端口为80。-j DROP 指定目标动作为丢弃数据包。

[root@centos-01 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
348 25560 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
3 234 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

最后一条就为刚才添加的规则。

[root@centos-01 ~]# iptables -I INPUT -p tcp --dport 80 -j DROP
[root@centos-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
484 34780 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED

-A 添加的规则在最后一行,-I 添加的规则在第一行。

优先过滤最前面的规则,加入数据包匹配了第一条规则,那么它就会被DROP掉,不会继续往下匹配。

iptables -D删除规则

[root@centos-01 ~]# iptables -nvL --line-numbers
[root@centos-01 ~]# iptables -D INPUT 6

显示出各个规则的行号,然后根据行号删除规则。-D选项后需要说明是哪条链的第几条规则。

更改链的默认策略

规则表中policy后跟的就是默认策略,默认策略为ACCEPT,通过-P命令可以修个各个链的默认策略。

[root@centos-01 ~]# iptables -P OUTPUT DROP

注意 : 尽量不要随意更改该配置,尤其是在进行远程登录时,一旦执行该命令后将会断开连接。这个策略设定后只能用命令:‘iptables -P OUTPUT ACCEPT’来恢复成原始状态,不能使用-F参数。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iptables 网络相关
相关文章推荐