• 35648

    文章

  • 23

    评论

  • 20

    友链

  • 最近新加了很多技术文章,大家多来逛逛吧~~~~
  • 喜欢这个网站的朋友可以加一下QQ群,我们一起交流技术。

31.网络相关 firewalld、netfilter 5表5链 iptables语法 原

欢迎来到阿八个人博客网站。本 阿八个人博客 网站提供最新的站长新闻,各种互联网资讯。 喜欢本站的朋友可以收藏本站,或者加QQ:我们大家一起来交流技术! URL链接:https://www.abboke.com/jsh/2019/0814/105689.html

10.11 Linux网络相关

10.12 firewalld和netfilter

10.13 netfilter5表5链介绍

10.14 iptables语法

(拓展:末尾有端口映射,jpg)

'

 

 

10.11 Linux网络相关:

~1. ifconfig 查看网卡ip (yum install net-tools)

centos7默认没有ifconfig这个命令,需要安装。ifconfig查看起来更加清爽

但是可以用ip addr 来查看

ifconfig -a 当你的网卡down掉的时候,或者没有ip的时候,ifconfig是不显示的。加上-a就可以显示

~2.ifup ens33/ifdown ens33

ifdown ens33 ens33这个网卡断掉(也叫down掉)

ifup ens33 down掉之后,ifup重新连接

用在增加一个网卡或者更改一个网关,但不想重启全部的网络服务,就可以用ifdown ifup指定一个来用。远程连接的时候不要ifdown掉,但是可以这样用ifdown ens33 && ifup ens33

~3. 设定虚拟网卡ens33:1

后面lvs keepalived的时候,会用到这个虚拟网卡

操作步骤详见实例3.

~4.mii-tool ens33 查看网卡是否连接

我们连接了网线,不在机房(可看灯有没有亮),远程连接的时候,检查是否网卡有没有插入网线

mii-tool ens33 结果是不是link ok

~5. ethtool ens33 也可以查看网卡是否连接

如果没有mii-tool可以用 ethtool ens33 ,检查最后一行是不是Link detected: yes

~6.更改主机名 hostnamectl set-hostname aminglinux

centos7使用的

更改之后,他的配置文件在 /etc/hostname

~7. DNS配置文件 /etc/resolv.conf

如果我们更改DNS,就更改网卡的配置文件就可以了。因为/etc/resolv.conf也是网卡的配置文件(/etc/sysconfig/network-scripts/ens33)定义的。如果在/etc/resolv.conf更改DNS,当然也可以vim,但是重启之后会被网卡的配置文件所覆盖掉,适合临时更改。这个需要注意

~8. /etc/hosts文件

这个文件是linux和windows都有的。后面的lamp做实验,访问一个自定义的域名的时候就用到这个文件,但是在windows上更改的

假如,我们ping www.qq.123.com,他是一个公网IP。但是我想让他在我们的本机不要访问那个公网IP,要让他访问到192.168..30.134来,可以直接vim /etc/hosts,在最下面一行直接写 192.168.30.134 www.qq123.com。我们在ping就会到我们设定这个IP来,立即生效,但只在本机生效。可支持一个IP多个域名。同一个域名设置了多个IP,以最后一次设置的IP为准。以行为单位,左边IP,右边域名,以空格分割

 

 

 

实例:

3.

[root@axinlinux-01 ~]# cd /etc/sysconfig/network-scripts/ 我们先cd进网卡的配置文件

[root@axinlinux-01 network-scripts]# cp ifcfg-ens33 ifcfg-ens33\:1 cp ens33重命名为ens:1。要脱意一下冒号

[root@axinlinux-01 network-scripts]# vim ifcfg-ens33:1 修改一下ens:1的配置

vim ifcfg-ens33\:0

 

TYPE=Ethernet

BOOTPROTO=static

DEFROUTE=yes

NAME=ens33:0 找到NAME这一行名字改为ens:1(配置文件里不需要脱意)

UUID=d116f724-9bfb-4573-9f5b-7dace5640272

DEVICE=ens33 找到DEVICE这一行名字改为ens:1(配置文件里不需要脱意)

ONBOOT=yes:o

IPADDR=192.168.30.137

NETMASK=255.255.255.0

GATEWAY=192.168.30.2 将网关删掉

DNS33=14.114.114.114 将DNS33删掉,因为已经有一个DNS33了

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:1
UUID=8ea80c7d-64b5-42aa-9ece-7b07528f4373
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.30.134
NETMASK=255.255.255.0

~                   

[root@axinlinux-01 network-scripts]# ifdown ens33:1 && ifup ens33:1 指定重启ens33:1这个网卡

[root@axinlinux-01 network-scripts]# ifconfig 我们在ifconfig一下

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.30.134  netmask 255.255.255.0  broadcast 192.168.30.255
        inet6 fe80::181d:8196:fb2c:cb62  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:a0:e1:ed  txqueuelen 1000  (Ethernet)
        RX packets 10299  bytes 1703052 (1.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7133  bytes 572196 (558.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.30.137  netmask 255.255.255.0  broadcast 192.168.30.255
        ether 00:0c:29:a0:e1:ed  txqueuelen 1000  (Ethernet)

ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.30.130  netmask 255.255.255.0  broadcast 192.168.30.255
        inet6 fe80::7398:c3c4:f2a6:1ede  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:a0:e1:f7  txqueuelen 1000  (Ethernet)
        RX packets 78488  bytes 5688301 (5.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 25428  bytes 1684172 (1.6 MiB)
        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 1000  (Local Loopback)
        RX packets 8019  bytes 736515 (719.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8019  bytes 736515 (719.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 

4.5.

[root@axinlinux-01 network-scripts]# mii-tool ens33

ens33: negotiated 1000baseT-FD flow-control, link ok

[root@axinlinux-01 network-scripts]# ethtool ens33

ens33: negotiated 1000baseT-FD flow-control, link ok
[root@afeilinux-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

 

6.

[root@axinlinux-01 network-scripts]# cat /etc/hostname

axinlinux-01

 

8.

[root@axinlinux-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.30.130 axinlinux-01

192.168.30.131 axinlinux-02
192.168.30.134 www.wangxin.com axin.com

[root@axinlinux-01 network-scripts]# vim !$

vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.30.130 axinlinux-01

192.168.30.131 axinlinux-02
192.168.30.134 www.wangxin.com axin.com

 

[root@axinlinux-01 network-scripts]# ping www.wangxin.com

PING www.wangxin.com (192.168.30.134) 56(84) bytes of data.
64 bytes from www.wangxin.com (192.168.30.134): icmp_seq=1 ttl=64 time=0.079 ms
64 bytes from www.wangxin.com (192.168.30.134): icmp_seq=2 ttl=64 time=0.049 ms
64 bytes from www.wangxin.com (192.168.30.134): icmp_seq=3 ttl=64 time=0.083 ms
64 bytes from www.wangxin.com (192.168.30.134): icmp_seq=4 ttl=64 time=0.083 ms
64 bytes from www.wangxin.com (192.168.30.134): icmp_seq=5 ttl=64 time=0.041 ms
^C
--- www.wangxin.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4002ms
rtt min/avg/max/mdev = 0.041/0.067/0.083/0.018 ms

 

 

 

 

----------------------------------------------------------------------------------------------------------------------------------------------------

 

10.12 firewalld和netfilter:

 

linux防火墙-netfilter

~1. selinux临时关闭 setenforce o

我们之前密钥认证的时候就要临时关闭

~2. selinux永久关闭 vi /etc/selinux/config

vi之后,将 SELINUX=enforcing 改为 disabled。getenforce一下显示enforcing。这个时候再setenforce o,再去getenforce一下,发现已经为permissive

permissive表示,虽然selinux已经开启了。但是仅仅是,遇到需要发生阻断的时候,他不需要真正的去阻断,仅仅是有一个提醒。我们不会从屏幕上看到,但是有个地方会去记录

~3. centos7之前使用netfilter防火墙

都是可以用iptables来配置端口的

~4. centos7开始使用firewalld防火墙

都是可以用iptables来配置端口的

我们可以在7上将firewalld关闭,将netfiter打开。是可以在7上使用netfilter的。方法如下:

先执行~6.(把firewalld停掉,不让他开机启动) > 在执行~5.(把这个服务关掉) > 然后再 执行~7. 把netfilter开启(实际上就是安装一下) > 再执行~8. (产生这个服务) > 最后执行~9. (开始这个服务)

iptables -nvL可以查看他的规则

iptables是netfilter的工具,而名字是叫netfilter

~5. systemctl stop firewalld

~6. systemctl disable firewallded

~7. yum install -y iptables-services

~8. systemctl enable iptables

~9. systemctl start iptables

 

 

实例:

2.

[root@axinlinux-01 network-scripts]# 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
# 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@axinlinux-01 network-scripts]# getenforce

Enforcing

[root@axinlinux-01 network-scripts]# setenforce 0

[root@axinlinux-01 network-scripts]# getenforce

Permissive

 

4.(将firewalld关闭,开启netfilter)

[root@axinlinux-01 network-scripts]# systemctl disable firewallded

Failed to execute operation: No such file or directory

[root@axinlinux-01 network-scripts]# systemctl stop firewalld

[root@axinlinux-01 network-scripts]# yum install -y iptables-services

[root@axinlinux-01 network-scripts]# systemctl enable iptables

Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.

[root@axinlinux-01 network-scripts]# systemctl start iptables

 

 

----------------------------------------------------------------------------------------------------------------------------------------------------

 

 

10.13 netfilter5表5链介绍:

 

linux防火墙—netfilter

 

~1. netfilter的5个表

~2. filter表用于过滤包,最常用的表,有INPUT、PORWARD、OUTPUT三个链

~3. nat表用于网络地址转换,有PREROUTING、OUTPUT、POSTROUTING三个链

~4. managle表用于给数据包表标记,几乎用不到

~5. raw表可以实现不追踪某些数据包,阿明从来不用

~6.security表在centos6中并没有,用于强制访问控制(MAC)的网络规则,阿铭没用过

~7. 参考文章 http:www.cnblogs.com/metoy/4320813.html

 

 

 

linux防火墙-netfilter

~1. 数据包流向与netfilter的5个链

~2. PREROUTING:数据包进入路由表之前

~3. INPUT:通过路由表后目的地为本机

~4. FORWARD:通过路由表后,目的地不为本机

~5. OUTPUT:由本机产生,向外发出

~6. POSTROUTING:发送到网卡接口之前

 

 

 

----------------------------------------------------------------------------------------------------------------------------------------------------

 

 

10.14 iptables语法:

 

linux防火墙—netfilter

 

iptables相关的命令与用法:

~1. 查看iptables规则:iptables -nvL

service iptables restart 重启iptables

/etc/sysconfig/iptables 规则保存位置

详见实例1.

~2. iptables -F 清空规则

感觉规则不合适的时候可全部清除。但需注意即是清除了规则,他的文件里(/etc/sysconfig/iptables)还是有的

~3. service iptables save 保存规则

把当前的规则保存到文件里面去

但是我们刚刚已经删除了,清空规则之后,我们在保存规则保存的仅仅是空规则。我们可以重启他的服务,让他重新加载回来(service iptables restart),

我们写完的规则仅仅只是在内存中生效的,想让他重启后依然生效,就要service iptables save

也就是说,重启服务器或者是重启iptables,他都会重新加载配置文件里的规则

 

~4. iptables -t nat -t 指定表(不加-t就是默认filter表)

以上1 2 3 针对的都是filter表

写法为 iptables -t nat -nvL

当然net表里面没有规则

~5. iptables -Z 可以把计数器清零

iptables -nvL后,第二行是有数字的。pkts表示的是有多少包,byts表示的是数据量,数据大小(字节)

-Z之后数据清零,再次查看时,又会产生一些数据,是因为每时每刻都是在产生的

~6. iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP

增加DROP的规则,来源IP是某某某IP的,访问我们某一个端口的。或者来源端口是什么的。我们给他DROP(扔掉

)掉

没有加-t,就是默认的filter表

-A 增加一条规则。后面的INPUT就是增加的链是INPUT链。

-s指定一个来源IP

-p指定协议。是tcp还是udp或者icmp

sport 1234 来源的端口是1234

-d目标的IP

dport 80 目标的端口是80

最后操作 -j DROP ,DROP就是把数据直接给扔掉。还有一个 REJECT拒绝(来时会告诉他被拒绝了,比较有礼貌。DORP是看都不看直接扔掉)DORP用的比较多。扔掉和拒绝的效果都是一样的,都是让这个数据过不来。相当于把这个IP给封掉了

~7. iptables -I/-A/-D INPUT -s 1.1.1.1 -j DROP

iptables -I INPUT -p tcp --dport 80 -j DORP (在用-dport或-sport的时候,前面要指定哪个协议)

根据~6. -A为增加一条规则,即排在后面。-I为插入一条规则,即为排在最前面(置顶)。排在最前面,我们在匹配的时候回优先匹配最前面的规则,然后才会一条条的往下执行。如果你的这个规则,这个数据包已经匹配了第一条规则,比如我们访问80端口的并且满足下面的一条规则,这两条规则同时满足。那么同时满足他会匹配第一条,一旦匹配了第一条这个数包就DORP掉了,就不会往下走了。一旦匹配直接执行

-D 删除一条规则,比如iptables -D INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP(删除~6.那条规则)

~8. iptables -IINPUT -s 192.168.1.0/24 -i eth0 -j ACCEPT

指定(-i)网卡为etho的

~9. iptables -nvL --line-numbers

假如,我们很久之前写了一条很长的规则,就像~6.。我们想把它删了,但是又想不起来这条规则是怎么写的。我们可以执行这条命令,在前面加上序号,用序号把他删掉。写法为 iptables -D INPUT 7

~10. iptables -D INPUT 1

利用序号,把规则删掉

~11. iptables -P INPUT DROP

-P指定链的默认的规则。像OUTPUT没有任何的规则。那么他就有默认的策略(ACCEPT)来决定。默认的策略就是ACCEPT。所有的数据包,只要是没有具体的规则来匹配,那么就走默认的策略。那么这个默认的策略也可以改,也可以改成DORP。写法为:iptables -P OUTPUT DROP。但是不要这么改默认的规则。一旦这么改了,就会连不上服务器,因为DORP掉了,数据回传不过来,xshell只能掉了。就只能回到服务器在改回来 iptables -P OUTPUT ACCEPT

默认的规则不要改,保持默认就可以了

 

实例:

1.

[root@axinlinux-01 ~]# iptables -nvL 红色部分是他的规则

Chain INPUT (policy ACCEPT 0 packets, 0 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 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

--

-

 

2.

[root@axinlinux-01 ~]# iptables -F 清除规则

[root@axinlinux-01 ~]# iptables -nvL 会发现就没有规则了

Chain INPUT (policy ACCEPT 23 packets, 1924 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 15 packets, 1608 bytes)

pkts bytes target prot opt in out source destination

 

3.

[root@axinlinux-01 ~]# service iptables restart 重启iptables 在查看规则

Redirecting to /bin/systemctl restart iptables.service 发现已加载回来

[root@axinlinux-01 ~]# iptables -nvL

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination

22 1872 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

 

4.

[root@axinlinux-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

 

6.

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

[root@axinlinux-01 ~]# iptables -nvL

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination

197 16288 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

2 458 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

以上-选项相对应的。 即为新增加的DORP规则

 

 

扩展:

图为 端口映射:

 

相关文章

暂住......别动,不想说点什么吗?
  • 全部评论(0
    还没有评论,快来抢沙发吧!