linux- network management -6

Hub Hub physical layer device
multiport repeater, the MAC address is not learned

Ethernet bridge OSI layer two data link layer

  1. Extended network bandwidth
  2. Dividing the collision domain network, so that network conflicts is limited to the minimum range
  3. Switch as more intelligent switching equipment that provides more functionality required by the user: Priority, virtual network, remote detection

• MAC address learning and memory, port forwarding,
• When the bridge to the broadcast time frame, it immediately forwarded to all other ports except the receiving port
• For unknown MAC address, the bridge will be forwarded to the port in addition to receiving the frame all ports other than
• when a bridge to a data frame, if the purpose of the frame is located on the receive port of the network segment, the data frame is filtered off it; if the destination MAC address is located in a different port, the bridge will the frame is forwarded to the port

tcp properties

Work in the transport layer
connection-oriented protocol
full-duplex protocol
half off
error check
data is packaged into segments, ordering
confirmation mechanism
data recovery, the retransmission
flow control, sliding window
congestion control, slow start and congestion avoidance algorithms

Three-way handshake with the four waving
state machine

LNT SS
/ proc / SYS / NET / IPv4 / tcp_max_syn_backlog complete the connection queue size is not recommended to adjust the size of 1024 or more
/ proc / sys / net / core / somaxconn complete the connection queue size is recommended to adjust the size of more than 1024

UDP

  1. Work in the transport layer
  2. Do not provide reliable network access
  3. Connectionless protocol
  4. Limited error checking
  5. High transmission performance
  6. No data recovery characteristics

View the current number of connections for each ip system

netstat -ant|grep 'tcp\b'|tr -s " " ":"|cut -d":" -f4|sort|uniq -c

tcpdump

1.1, filtering host
• crawl all through eth1, source or destination address is 192.168.1.1 network data
tcpdump -i eth1 Host 192.168.1.1
• specify the source address
tcpdump -i eth1 192.168.1.1 src Host
• specify the destination address
tcpdump -i eth1 dst host 192.168.1.1

1.2, filtered ports
• fetch all through eth1, source or destination network data port 25 of
the tcpdump -i eth1 Port 25
• source port
the tcpdump -i eth1 the src Port 25
• Specify destination port
tcpdump -i eth1 dst port 25

1.3, the network filtering
the tcpdump -i eth1 192.168 NET
the tcpdump -i eth1 the src 192.168 NET
the tcpdump -i eth1 DST NET 192.168

1.4、协议过滤
tcpdump -i eth1 arp
tcpdump -i eth1 ip
tcpdump -i eth1 tcp
tcpdump -i eth1 udp
tcpdump -i eth1 icmp

https://www.cnblogs.com/ggjucheng/archive/2012/01/14/2322659.html

iptables

报文流向
1. 到本机某进程的报文: prerouting--> input
2. 由本机转发的报文:prerouting-->forward-->postrouting
3. 由本机某进程发出的报文:output-->postrouting 
4. 
表,所有的规则都是以下四种分类的规则,所有的表都存于这四个表中
1. filter 表,负责过滤功能,防火墙,内核模块: iptables_filter
2. nat表,network address translation,网络地址转换功能,内核模块iptables_nat
3. mangle表,拆解报文,做出修改,并重新封装的功能:iptables_mangle
4.  raw表,关闭nat表上启用的连接追踪机制;iptables_raw


动作

ACCEPT: 允许
DROP: 直接丢弃,不给回应信息,客户端过了超时时间才会有反应
REJECT: 拒绝数据包通过,会有回应信息,客户端刚请求就会收到拒绝的信息
SNAT: 源地址转换,解决内网用户用同一个公网地址上网的问题
MASQUERADE: 是SNAT的一种特殊开工,适用于动态的,临时会变的IP上
DNAT: 目标地址转换
REDIRECT: 在本机做端口映射
LOG:在/var/log/message文件中记录日志信息,然后将数据包传给下一条规则,只做记录,不做其它任何操作,仍然让下一条规则去匹配

IPTABLES命令参数说明:

#iptables
-t 指定要操作的表【raw,mangle,nat,filter -- 如果没有使用-t选项指定表,默认为filter表】
-L 列出规则
--line-number 可以查看的时候显示序号【--line (centos中可缩写)】
-n 不对IP地址进行名称反解,直接显示IP地址【anywhere,0.0.0.0/0】
-v 显示更详细的信息【报文个数,对应匹配的报文大小总和,等】
-x 显示精确匹配到计数值
-A 表示在需要添加的规则表中进行添加
-I [=大写的 i ] 表示表需要添加的规则表中进行插入到指定的位置,默认为开始【-I INPUT 2 指定位置】
-D 删除规则
-R 修改规则【必须明确指定规则对应原本的匹配条件,建议-D先删除,再来添加】
-s 报文的源地址
-d 报文的目标地址
-p 协议类型
【-p tcp --dport 22】--dport 报文的目的端口
【-p tcp --sport 22】--sport 报文的源端口
【-p tcp -m multiport --dports 25,110,143,80】使用multiport指定多个端口
-i 指定网卡接口
-m 指定的扩展模块
-N 创建自定义链
【iptables -t filter -N IN_WEB】




添加规则
【iptables -F INPUT清空filter表INPUT链中的规则.】
屏蔽某个IP【拒绝192.168.1.11上所有的报文访问当前机器,可省略-t filter】
iptables -t filter -I INPUT -s 192.168.1.11 -j DROP
【-I为插入规则,-s为IP,-j DROP为屏蔽动作】
iptables -t filter -I INPUT 2 -s 192.168.1.11 -j DROP
删除规则
查看规则【根据需要来查看,以下为查看filter表中的INPUT链中的规则】
iptables --line -vnL INPUT
删除上面查看出来的第三条规则【删除filter表中的INPUT链中的第三条规则】
iptables -t filter -D INPUT 3
根据具体的匹配条件与动作来删除规则【需要知道添加的规则,可以使用iptables -nvL进行查看】
iptables -D INPUT -s 192.168.1.11 -j DROP
保存防火墙规则,如果你误操作了,并且没有保存,那么重启防火墙将会恢复到配置文件中的模样
service iptables save
iptables-save > /etc/sysconfig/iptables【iptables-save是将当前的规则以保存后的格式输入到屏幕上,再利用重定向保存到配置文件中】
Centos 6系列会保存在 /etc/sysconfig/iptables 文件中
重新载入规则【重载规则,现有的规则会被覆盖】
iptables-restore < /etc/sysconfig/iptables
 
参数-s格式【-d也可以参考下面格式】
单IP
-s 192.168.1.11
多IP
-s 192.168.1.11,192,168.4.2
指定 IP段
-s 10.6.0.0/16
取反【不满足源IP为192.168.1.11的报文进行INPUT链默认操作,并不表示拒绝*注意,与默认规则有关】
! -s 192.168.1.11
【iptables -t filter -A INPUT ! -s 192.168.1.11 -j ACCEPT】
 
-p格式【不使用-p的时候表示所有协议】
Centos 6
tcp,udp,udplite,icmp,esp,ah,sctp
Centos 7
tcp,udp,udplite,icmp,icmpv6,esp,ah,sctp,mh
指定端口
【-p tcp --dport 22】--dport 报文的目的端口
【-p tcp --sport 22】--sport 报文的源端口
取反
-p tcp ! --dport 22 【表示目标端口不是22端口则会被匹配到】
取一个范围
-p tcp --dport 22:25【表示22到25端口之间所有的端口,22,23,24,25】
-p tcp --dport :22 【表示0到22端口】
-p tcp --dport 80: 【表示80到65535端口】
【-m multiport 扩展模块只可以用于tcp协议与udp协议,即配合-p tcp或者-p udp使用,后面接的--dport变为了--dports】
-p tcp -m multiport --dports 25,110,143,80 【表示匹配多个离散的目标端口,注意其中使用-m multiport模块】
-p tcp -m multiport --dports 22,80:88 【可以指定连续端口范围并且可以添加离散端口号】
【完整例】
iptables -t filter -I INPUT -s 192.168.1.11 -p tcp -m multiport --dports 25,110,143,80 -j DROP
 
-m格式
-m表示指定的模块
iptables -t filter -I INPUT -s 192.168.1.11 -p tcp -m tcp --dport 22 -j REJECT
但是与前面-p选项对应的协议名称相同的模块,所以可以忽略【如果是-m udp不则,就不可以忽略了】
iptables -t filter -I INPUT -s 192.168.1.11 -p tcp --dport 22 -j REJECT
 
扩展模块
【-m multiport】
看上面的-p选项,已经说明了
 
【-m iprange】指定一段连续的IP地址范围
【-m iprange --src-range或-m iprange --dst-range】
支持使用"!"取反
屏蔽源IP地址192.168.1.100到192.168.1.200之间的来源报文
iptables -t filter -I INPUT -m iprange --src-range 192.168.1.100-192.168.1.200 -j DROP
 
【string扩展模块】
报文中包含对应的字符串,如果某个网页的内容是OOXX
#curl 192.168.1.146/index.html
OOXX
#iptables -t filter -I INPUT -m string --algo bm --string "OOXX" -j REJECT
--algo:指定匹配算法,可选算法:bm与kmp此选项为必须选项
--string:用于指定需要匹配的字符串
 
【time扩展模块】
指定每天9点至18点不可以看网页<访问80与443端口>
#iptables -t filter -I OUTPUT -p tcp --dport 80 -m time --timestart 09:00:00 --timestop 18:00:00 -j REJECT
#iptables -t filter -I OUTPUT -p tcp --dport 443 -m time --timestart 09:00:00 --timestop 18:00:00 -j REJECT
--timestart起始时间
--timestop结束时间
-m time --weekdays 6,7【限定周六日,可以使用数字,也可以使用Mon,Tue,Wed,Thu,Fri,Sat,Sun】
【weekdays与timestart、timestop结合】---限定周六日的9点至18点不可以访问80端口
#iptables -t filter -I OUTPUT -p tcp --dport 80 -m time --timestart 09:00:00 --timestop 18:00:00 --weekdays 6,7 -j REJECT
--monthdays选项,可以指定每个月的哪 一天
每个月的第4个星期5
-m time --weekdays 5 --monthdays 22,23,24,25,26,27,28
使用--datestart选项与--datestop选项,指定具体的日期范围
-m time --datestart 2018-06-22 --datestop 2018-06-30
--monthdays与--weekdays可以使用“!"取反,其它的不可以
 
【connlimit扩展模块】
限制每个IP地址同时链接到server端的链接数量,不用指定Ip,默认就是针对每个客户端IP
限制每个Ip地址最多只能占用两个SSH连接到server端
#iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 2 -j REJECT
------connlimit-above 2表示限制每个IP链接数量上限为2,可以利用“!"取反
--connlimit-mask 24限定某类网段
结合例:<24网段最多有两个ssh客户端连接到当前服务器>
#iptables -t filter -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 2 --connlimit-mask 24 -j REJECT
 
【limit扩展模块】
limit模块是对”报文到达速率“进行限制的,<限制单独时间内流入包的数量>
例:先清除规则,避免其它的规则影响,再设置速度,再做包过滤
#iptables -F
#iptables -t filter -I INPUT -p icmp -m limit --limit 10/minute -j ACCEPT
#iptables -t filter -A INPUT -p icmp -j REJECT
--limit-burst <令牌桶的概念>,令牌桶中最多存放3个令牌,每分钟生成10个令牌(第6秒生成一个令牌)
时间单位可以使用/second 秒 /minute 分 /hour 时 /day 天
#iptables -t filter -I INPUT -p icmp -m limit --limit-burst 3 --limit 10/minute -j ACCEPT
#iptables -t filter -A INPUT -p icmp -j REJECT
 
【--tcp-flags】
详解看如下朱双印老师博客的说明:
http://www.zsythink.net/archives/1578
用于匹配报文的tcp头的标志位
例:
#iptables -t filter -I INPUT -p tcp -m tcp --dport 22 --tcp-flags SYN,ACK,FIN,RST,URG,PSH SYN -j REJECT
#前面省略 --tcp-flags SYN,ACK,FIN,RST,URG,PSH SYN,ACK -j REJECT
#前面省略 --tcp-flags ALL SYN -j REJECT
#前面省略 --tcp-flags ALL SYN,ACK -j REJECT
--syn 用于匹配tcp新建连接的请求报文 ,相当于使用”--tcp-flags SYN,RST,ACK,FIN SYN"
#iptables -t filter -I INPUT -p tcp -m tcp --dport 22 --syn -j REJECT
 
【-icmp】
详细看下面链接中朱双印老师的讲解
http://www.zsythink.net/archives/1588
禁止所有icmp类型的报文进入本机<无法发送ping,也无法接收ping>
#iptables -t filter -I INPUT -p icmp -j REJECT
可以ping通别人,但是别人无法ping通我们
#iptables -t filter -I INPUT -p icmp -m icmp --icmp-type 8/0 -j REJECT
也可以使用报文描述去匹配<空格需要替换为->,效果同上面
#iptables -t filter -I INPUT -p icmp -m icmp --icmp-type "echo-request" -j REJECT
 
【state模块】
链接报文状态
有五种状态:
NEW: 连接的第一包
ESTABLISHED:NEW状态包后面的包状态理解,表示连接已建立
RELATED:数据进程连接--数据连接<如FTP中的数据连接>
INVALID:包没有办法被识别,或者空上包没有任何状态,可以主动屏蔽INVALID的报文
UNTRACKED:未被追踪的报文 ,当报文的状态为Untracked时通常表示无法找到相关的连接
####
如何判断报文是否是为了回应之前发出的报文,如果不是拒绝
#iptables -t filter -I INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
#iptables -t filter -I INPUT -j REJECT
《上面这句是表示不是的报文作何处理》
 
 
【-N 自定义链】
创建自定义链
#iptables -t filter -N IN_WEB
将规则添加至自定义链中
#iptables -t filter -I IN_WEB -s 192.168.1.123 -j REJECT
#iptables -I IN_WEB -s 192.168.1.188 -j REJECT
查看自定义链规则
#iptables -t filter --line -nvL IN_WEB
将访问本机的80端口匹配到这条规则
#iptables -I INPUT -p tcp --dport 80 -j IN_WEB
重命名自定义链
#iptables -E IN_WEB WEB
删除自定义链,要先删除引用,清除自定义链规则
#iptables -D INPUT 1
#iptables -t filter -F WEB
#iptables -t filter -X WEB
 
 
【LOG动作】
将所有发往22端口的报文相关信息记录在日志中
<如果使用此日志功能,建议尽可能的将条件匹配得精确一些>
#iptables -I INPUT -p tcp --dport 22 -j LOG
【修改日志路径】
#vim /etc/rsyslog.conf
添加如下信息:
#kern.warning /var/log/iptables.log
重启rsyslog服务(或者syslogd)
#service rsyslog restart
--log-level 日志级别:emerg, alert,crit,error,warning,notice,info,debug
--log-prefix 可以给记录到日志的相关信息添加“标签”之类的信息,(*注意:不可超过29个字符)
例:<将所有主动连接22端口的报文信息均记录到日志中(其中有使用到state模块,相关的看上面的这模块说明)>
#iptables -I INPUT -p tcp --dport 22 -m state --state NEW -j LOG --log-prefix "want-in-from-port-22"
 
【网络防火墙】
添加路由,访问某网段,指定走某路由
#route add -net 10.1.0.0/16 gw 192.168.1.146
设置转发报文
(临时办法有下面两种)
#cat /proc/sys/net/ipv4/ip_forward <内容为0表示不转发>
#echo 1>/proc/sys/net/ipv4/ip_forward
或者如下办法:
#sysctl -w net.ipv4.ip_forward=1
(永久生效办法)
#/etc/sysctl.conf (centos7为/usr/lib/sysctl.d/00-system.conf文件) 添加 net.ipv4.ip_forward=1即可
在FORWARD链的末端添加一条默认拒绝所有转发的规则
#iptables -A FORWARD -j REJECT
添加一条允许10.1/16段的所有的IP均可正常的访问外部主机的web请求(注意进出均要放行)
#iptables -I FORWARD -s 10.1.0.0/16 -p tcp --dport 80 -j ACCEPT
上面的是访问报文的规则,下面的是响应报文的规则,但是响应的这样相对麻烦
#iptables -I FORWARD -s 10.1.0.0/16 -p tcp --sport 80 -j ACCEPT
可以按下面的办法,添加一条规则,将所有的响应的报文均放行,这样就不用重复的添加了<此条规则为所有的响应报文均放行,疑问的话,参考上面的state规则>
#iptables -I FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
如:在上面的基础上,需要再开放转发访问22端口的话。
#iptables -I FORWARD -s 10.1.0.0/16 -p tcp --dport 22 -j ACCEPT
 
【SNAT--也称源地址转换】
内网机器设置网关为iptables机器的中转IP
1. 内网机器网关设置<gateway>
2. 开启服务器的转发设置<echo 1>/proc/sys/net/ipv4/ip_forward>
<10为内网服务器网段,146为服务器的公网IP地址,就是将内网服务器访问过来的报文修改为192.168.1.146,然后再发送出去>
#iptables -t nat -A POSTROUTING -s 10.1.0.0/16 -j SNAT --to-source 192.168.1.146
【DNAT】
先清空表数据, 146为公司的公网地址<也可以理解为服务器的公网地址>,目标端口为3389,进行目标地址转换
将访问公网146的3389端口,会被映射到10.1.0.6的3389端口上(注意:设置之前,可能需要设置一下SNAT)
#iptables -t nat -F
#iptables -t nat -I PREROUTING -d 192.168.1.146 -p tcp --dport 3389 -j DNAT --to-destination 10.1.0.6:3389
#iptables -t nat -I PREROUTING -d 192.168.1.146 -p tcp --dport 801 -j DNAT --to-destination 10.1.0.1:80
【MASQUERADE --动态IP的SNAT】
SNAT相同的动作,但是是将IP修改为eth1网卡上可用的IP地址
#iptables -t nat -I POSTROUTING -s 10.1.0.0/16 -o eth1 -j MASQUERADE
【REDIRECT】
将本机的80端口映射到本机的8080端口上
#iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080

Network Management Command

NetworkManager 默认联网守护进程
nmtui NetworkManager 的使用光标的简单文本用户界面(TUI)

在 Red Hat Enterprise Linux 7 中,首先启动 NetworkManager,此时 /etc/init.d/network 会使用NetworkManager 检查,以避免破坏 NetworkManager 的连接。NetworkManager 主要在使用 sysconfig 配置文件的主要应用程序中使用,而 /etc/init.d/network 主要是作为备用程序在此要程序中使用。

编辑ifcfg 文件后,命令会读取所有连接配置文件
~]# nmcli connection reload

或者 只重新载入那些有变化的文件ifcfg-ifname
~]# nmcli con load /etc/sysconfig/network-scripts/ifcfg-ifname

ifup 脚本是一个通用脚本,可完成一些任务,并调用具体接口脚本,比如 ifup-ethX、ifupwireless、ifup-ppp 等等。用户手动运行 ifup eth0 后:
1. ifup 会查找名为 /etc/sysconfig/network-scripts/ifcfg-eth0 的文件;
2. 如果存在 ifcfg 文件,ifup 会在那个文件中查找 TYPE 密钥,以确定要调用的脚本类型;
3. ifup 根据 TYPE 调用 ifup-wireless 或 ifup-eth 或 ifup-XXX
4. 具体类型脚本执行具体类型设置;
5. 然后具体类型脚本让通用功能执行与 IP 相关的任务,比如 DHCP 或静态设置。

引导时,/etc/init.d/network 会读取所有 ifcfg 文件,并检查每个包含 ONBOOT=yes 的文件,确定是否已在 ifcfg 列出的设备中启动 NetworkManager。如果 NetworkManager 正在启动或已经启动那个设备,则不需要对那个文件进行任何操作,然后检查下一个包含 ONBOOT=yes 的文件。如果 NetworkManager尚未启动那个设备,则 initscripts 会继续采用传统方式运行,并为那个 ifcfg 文件调用 ifup。

不要在保存目前使用的 ifcfg 文件的同一位置保存其备份文件。该脚本会运行 ifcfg-*,扩展名.old、.orig、.rpmnew、.rpmorig 和 .rpmsave 除外。最好是不要将备份文件保存在 /etc/ 目录下

使用 nmcli 启动和停止接口
nmcli con up id bond0n
mcli con up id port0
nmcli dev disconnect iface bond0
nmcli dev disconnect iface ens3

建议使用 nmcli dev disconnect iface iface-name 命令

添加以太网连接意味着生成一个配置文件
添加静态以太网连接
nmcli connection add type ethernet con-name connection-name ifnameinterface-name

NetworkManager 会将内部参数 connection.autoconnect 设定为 yes。NetworkManager 还会将设置保存到 /etc/sysconfig/network-scripts/ifcfg-my-office 文件中,在该文件中会将 ONBOOT 指令设定为 yes。

添加静态以太网连接
~]$ nmcli con add type ethernet con-name test-lab ifname ens9 ip410.10.10.10/24  gw4 10.10.10.254

NetworkManager 会将其内部参数 ipv4.method 设定为 manual,将 connection.autoconnect 设定为yes。NetworkManager 还会将设置写入 /etc/sysconfig/network-scripts/ifcfg-my-office 文件,其中会将对应 BOOTPROTO 设定为 none,并将 ONBOOT 设定为 yes

添加额外dns
~]$ nmcli con mod test-lab +ipv4.dns "8.8.8.8 8.8.4.4"
激活新连接
~]$ nmcli con up test-lab ifname ens9
使用 nmcli 配置静态路由
 nmcli connection modify eth0 +ipv4.routes "192.168.122.0/24  10.10.10.1"
 会将 192.168.122.0/24 子网的流量指向位于 10.10.10.1 的网关
 
 配置 DHCP 客户端
 
 ~]# ip address add 10.0.0.3/24 dev eth0
 系统重启后会丢失命令行中给出的 ip 命令。
 
 使用命令行配置静态路由
 ip route add 192.0.2.1 via 10.0.0.1 [dev ifname]
 192.0.2.1 是用点分隔的十进制符号中的 IP 地址,10.0.0.1 是下一个跃点,ifname 是进入下一个跃点的退出接口。
 
 ip route add 192.0.2.0/24 via 10.0.0.1 [dev ifname]
 
 
 配置主机名
 hostname 有三种类型:static、pretty 和 transient
 ~]$ nmtui   配置
 ~]# hostnamectl set-hostname name
 
 配置网络绑定
 
 ~]$ nmcli con add type bond con-name mybond0 ifname mybond0 mode active-backup
 ~]$ nmcli con add type bond-slave ifname ens7 master mybond0
 ~]$ nmcli con add type bond-slave ifname ens3 master mybond0
 
 要启动绑定,则必须首先启动从属接口,
 ~]$ nmcli con up bond-slave-ens7
 ~]$ nmcli con up bond-slave-ens3
 ~]$ nmcli con up bond-mybond0
 
 
 网络成组
 联合或合并网络连接,以提供具有较高吞吐量的本地连接或冗余的方式可称为“频道绑定”、“以太网绑定”、“端口聚合”、“频道成组”、“NIC 成组”、“链接合并” 等等
 
 使用 NetworkManager 守护进程控制成组的端口接口时,特别是发现错误时,请记住以下要点:
 1. 启动主接口不会自动启动端口接口。
 2. 启动端口接口总是会启动主接口。
 3. 停止主接口总是会停止端口接口。
 4. 没有端口的主机可启动静态 IP 连接。
 5. 没有端口的主机在启动 DHCP 连接时会等待端口。
 6. 添加附带载波的端口后,使用 DHCP 连接的主机会等待端口完成连接。
 7. 添加不附带载波的端口后,使用 DHCP 连接的主机会让端口继续等待
 
 使用命令行配置网络成组
 
 ~]$ nmcli connection add type team ifname team-ServerA
 ~]$ nmcli con add type team-slave con-name Team0-port1 ifname eth0 masterTeam0
 

Guess you like

Origin www.cnblogs.com/g2thend/p/11621051.html