您的位置:首页 > 其它

firewalld、netfilter、 netfilter5表5链、iptables介绍

2017-11-29 20:48 441 查看

1. Linux网络相关

ifconfig查看网卡ip(如果没有这个命令请使用yum install net-tools安装)

ifdown ens33 //关闭网卡ens33(在使用远程终端连接时不可使用这条命令,否则会断掉网络)

ifup ens33 //打开网卡ens33,用这种方式可以单独重启指定的网卡(有几张网卡的情况下),而不是重启整个网络服务。
设定虚拟网卡ens33:1

ifdown ens33 && ifup ens33 //在远程终端可以把两条命令相结合使用,这样重启的效果更好。
示例如下:
[root@aminglinux-01 ~]# ifdown ens33 && ifup ens33
成功断开设备 'ens33'。
成功激活的连接(D-Bus 激活路径:/org/freedesktop/NetworkManager/ActiveConnection/2)


给一个网卡设定多个IP
操作示例如下:
[root@aminglinux-01 ~]# cd /etc/sysconfig/network-scripts/
[root@aminglinux-01 network-scripts]# cp ifcfg-ens33  ifcfg-ens33\:0  //复制网卡配置文件,并取一个新的网卡名,这里加反斜杠(\)是因为要把:转义,不然linux命令无法识别。
[root@aminglinux-01 network-scripts]# vi ifcfg-ens33\:0 //更改完成后保存退出
[root@aminglinux-01 network-scripts]# cat ifcfg-ens33\:0
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33:0  //这个地方要更改成前面设置的ens33:0
UUID=3c08189a-cb3f-4879-92de-ee0691b1d4cc
DEVICE=ens33:0 //这个地方要更改成前面设置的ens33:0
ONBOOT=yse
IPADDR=192.168.1.190 //更改一个没有使用的IP
NETMASK=255.255.0.0
GATEWAY=192.168.1.1
DNS1=119.29.29.29
DNS2=8.8.8.8
[root@aminglinux-01 network-scripts]# ifdown ens33 && ifup ens33 //重启网卡
成功断开设备 'ens33'。
成功激活的连接(D-Bus 激活路径:/org/freedesktop/NetworkManager/ActiveConnection/3)
[root@aminglinux-01 network-scripts]# ifconfig  //重启后可以看到多了一个网卡ens33:0
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
inet 192.168.1.185  netmask 255.255.0.0  broadcast 192.168.255.255
inet6 fe80::1ffb:cde1:5f3e:5778  prefixlen 64  scopeid 0x20<link>
ether 00:0c:29:09:e5:58  txqueuelen 1000  (Ethernet)
RX packets 3561287  bytes 361515464 (344.7 MiB)
RX errors 0  dropped 1092  overruns 0  frame 0
TX packets 113641  bytes 22059136 (21.0 MiB)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.190 netmask 255.255.0.0 broadcast 192.168.255.255
ether 00:0c:29:09:e5:58 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 28 bytes 2380 (2.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 28 bytes 2380 (2.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

6. 查看网卡是否连接
示例如下:

[root@aminglinux-01 ~]# mii-tool ens33 //link ok表示连接正常,如果显示no link表示网卡坏了或没有连接网线
ens33: negotiated 1000baseT-FD flow-control, link ok
[root@aminglinux-01 ~]# ethtool ens33 //最后一行显示link detected:yes说明网卡正常,如果显示为no,说明网卡坏或没有连接网线
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

7. 更改主机名

示例如下:

[root@aminglinux-01 ~]# hostname //查看主机名
aminglinux-01
[root@aminglinux-01 ~]# hostname Gary-tao //更改主机名,但是这仅仅只是保存在内存中,重启后失效。
[root@aminglinux-01 ~]# hostname //更改完成,重启后失效。
Gary-tao
[root@aminglinux-01 ~]# vim /etc/hostname //如果想要重启生效,需要更改配置文件。
[root@aminglinux-01 ~]# hostnamectl set-hostname Gary-tao //这个命令可以直接更改/ect/hostname文件 ,只适用centos7
[root@aminglinux-01 ~]# hostname
gary-tao
[root@aminglinux-01 ~]# cat /etc/hostname //显示文件已更改生效
gary-tao

8. 更改DNS

- 在网卡配置文件里更改DNS是永久更改, 更改命令vi /etc/sysconfig/network-scripts/ifcfg-ens33(网卡名称),

- 但是在/etc/resolv.conf中也可以更改DNS,这里的更改是临时的。一般可以在此设置多个DNS,以做备用,如果只是临时修改DNS地址,就直接修改DNS配置文件,如果需要永久生效的话,还是要修改网卡的配置文件。
示例如下:

[root@aminglinux-01 ~]# cat /etc/resolv.conf //查看网卡已有的DNS

Generated by NetworkManager

nameserver 119.29.29.29
nameserver 8.8.8.8
[root@aminglinux-01 ~]# vim /etc/resolv.conf //临时更改

Generated by NetworkManager

nameserver 119.29.29.29
nameserver 8.8.8.8

- /etc/hosts文件

在linux下有一个特殊文件/etc/hosts可能解析域名,不过需要我们在里面手动添加ip与域名,它的作用是临时解析某个域名,非常有用。

示例如下:

[root@aminglinux-01 ~]# cat /etc/hosts //查看文件内容
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@aminglinux-01 ~]# ping www.qq123.com
PING www.qq123.com (202.91.250.93) 56(84) bytes of data.
64 bytes from 202.91.250.93 (202.91.250.93): icmp_seq=1 ttl=231 time=30.3 ms
64 bytes from 202.91.250.93 (202.91.250.93): icmp_seq=2 ttl=231 time=30.2 ms
64 bytes from 202.91.250.93 (202.91.250.93): icmp_seq=3 ttl=231 time=30.2 ms
^C
--- www.qq123.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 30.227/30.284/30.395/0.162 ms
[root@aminglinux-01 ~]# vim /etc/hosts //编辑文件把上面这个域名指向的ip更改成192.168.1.190 ,保存退出
[root@aminglinux-01 ~]# ping www.qq123.com //验证域名指定IP
PING www.qq123.com (192.168.1.190) 56(84) bytes of data.
64 bytes from www.qq123.com (192.168.1.190): icmp_seq=1 ttl=64 time=0.152 ms
64 bytes from www.qq123.com (192.168.1.190): icmp_seq=2 ttl=64 time=0.086 ms
64 bytes from www.qq123.com (192.168.1.190): icmp_seq=3 ttl=64 time=0.060 ms
^C
--- www.qq123.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 0.060/0.099/0.152/0.039 ms

注意:
1. 一个IP后面可以跟多个域名,可以是几十个甚至上百个。
2. 每一行只能有一个IP,也就是一个域名不能对应多个IP。
3. 如果有多行中出现相同的域名(对应的IP不一样),会按最前面出现的记录来解析。

----------

## 2. firewalld和netfilter

1. SElinux

selinux是Linux系统特有的安全机制,因为这种机制的限制太多,配置也麻烦,所以几乎没有人真正的应用它。安装完系统后我们一般会选择关闭selinux。

示例如下:

[root@gary-tao ~]# getenforce //查看当前SElinux的状态
Enforcing
[root@gary-tao ~]# setenforce 0 //临时关闭
[root@gary-tao ~]# getenforce
Permissive
[root@gary-tao ~]# cat /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=enforcing

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@gary-tao ~]# vi /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 //把enforcing改成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

2. netfilter和firewalld

- 在centos版本5和6上用的防火墙是netfiler,centos7则用的是firewalld防火墙,很多人把Linux的防火墙叫作iptables,其实这不是,iptables仅仅是一个工具。目前现在很多企业依然在使用centos6,但firewalld是向下兼容netfiler的,同样也支持之前版本的命令用法。

示例如下:

[root@gary-tao ~]# 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@gary-tao ~]# systemctl stop firewalld //关闭firewalld服务
[root@gary-tao ~]# yum install -y iptables-services //安装iptables-services,这样就可以使用之前版本的iptables了。
[root@gary-tao ~]# systemctl enable iptables //让它开机启动
[root@gary-tao ~]# systemctl start iptables //启动iptables服务

----------

## 3. netfilter5表5链介绍

- netfilter的5个表

1. filter表用于过滤包,是系统预设的表,也是最常用的表,该表有内建三个链INPUT、OUTPUT、FORWARD。
- INPUT链作用于进入本机的包。
- OUTPUT链作用于本机送机的包。
- FORWARD链作用于那些跟本机无关的包。

2. nat表用于网络地址转换,有PREROUTING、OUTPUT、POSTROUTING三个链。
- PREROUTINC链的作用是在包刚刚到达防火墙时改变它的目的地址(如果需要的话)
- OUTPUT链的作用是改变本地产生的包的目的地址。
- POSTROUTINC链的作用是在包即将离开防火墙时改变其源地址。

3. 数据包流向与netfilter的5个链
- PREROUTING:数据包进入路由表之前。
- INPUT:通过路由表后目的地为本机。
- FORWARD:通过路由表后,目的地不为本机。
- OUTPUT:由本机产生,向外发出。
- POSTROUTING:发送到网卡接口之前。

4. 具体的数据包流向,可以参考下图。

![](https://i.imgur.com/SnGqpwR.png)

参考文章 http://www.cnblogs.com/metoy/p/4320813.html 
## 4. iptables语法

1. iptables已经有默认规则,我们可以用以下命令查看。但是我们在设置自己的规则之前,建议先将其清除。-nvL选项表示查看规则,-F选项表示清除当前规则,但清除只是临时的,重启还是会加载已经保存的规则,所以需要使用service iptables save保存一下规则。通过下面这个命令输出,我们也可以看到防火墙规则保存在/etc/sysconfig/iptables里,可以查看下这个文件。

示例如下:

[root@gary-tao ~]# iptables -nvL //查看iptables默认规则
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
56 4076 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
1235 94818 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 40 packets, 3788 bytes)
pkts bytes target prot opt in out source destination
[root@gary-tao ~]# service iptables restart //重启规则
[root@gary-tao ~]# cat /etc/sysconfig/iptables //查看规则文件
Redirecting to /bin/systemctl restart iptables.service
[root@gary-tao ~]# iptables -F //清空所有规则
[root@gary-tao ~]# service iptables save //保存后规则里就什么规则都没有了
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ 确定 ]

2. -t后面跟表名,指定filter表

[root@gary-tao ~]# iptables -t filter -nvl

3. iptables -Z   //-Z把包和流量计数器清零
4. 增加/删除一条规则,其用法如下:
5. iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP  //增加一条规则

[root@gary-tao ~]# iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
[root@gary-tao ~]# iptables -nvL
Chain INPUT (policy ACCEPT 847 packets, 64330 bytes)
pkts bytes target prot opt in out source destination
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

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

6. iptables -I INPUT -s 1.1.1.1 -j DROP //表示插入一条规则,丢掉所有来自1.1.1.1的数据包。
7. iptables -A INPUT -s 1.1.1.1 -j DROP //表示增加一条规则
8. iptables -D INPUT -s 1.1.1.1 -j DROP //注意删除一条规则时,必须和插入的规则一致。
9. iptables -I INPUT -s 192.168.1.0/24 -i eth0 -j ACCEPT //表示把来自192.168.1.0/24这个网段且作用在etho上的包放行。
10. 有时候服务器上的iptables过多了,你想删除一某一条规则,但又不知道之前创建时的规则,你可先查看iptables规则,命令如下:

[root@gary-tao ~]# iptables -nvL --line-numbers
Chain INPUT (policy ACCEPT 9235 packets, 708K bytes)
num pkts bytes target prot opt in out source destination
1 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

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

然后删除一条规则,使用如下命令:

[root@gary-tao ~]# iptables -D INPUT 1

11. iptables还有一个规则经常用支,即-P(大写)选项,它表示预设策略,用法如下:

[root@gary-tao ~]# iptables -P INPUT DROP

说明:-P后面跟链名,策略内容或为DROP,或为ACCEPT,默认是ACCEPT。注意:如果你在连接远程服务器,千万不要随便执行这个命令,因为一旦输入命令并回车,远程连接会被断开。这个策略一旦设定成功后,只有使用命令iptables -P INPUT ACCEPT才能恢复成原始状态。

解释:
- -A:增加一条规则。
- -D:删除一条规则。
- -I:插入一条规则 insert,其效果-A一样。
- -p:指定协议,可以是tcp、udp或者icmp。
- --dport:和-p一起使用,指定目标端口destination port。
- --sport:和-p一起使用,指定源端口source port。
- -s:指定源ip(可以是一个ip段)。
- -d:指定目的ip(可以是一个ip段)。
- -j:后面跟动作,ACCEPT表示允许包,REJECT表示拒绝包,DROP表示丢掉包。
- -i:指定网卡interface(不常用,但偶尔能用到)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐